The two defects the phase 6 browser walk found and #6 described but did not carry. They were written, walked and left uncommitted; `edge` still has the shapes the walk condemned. **Every refusal sentence was invisible.** Core's request primitive reads one field — `(data && data.message) || res.statusText` — and this module has answered `{ error: … }` since phase 1. It got away with it because every failure until phase 6 landed in `ErrorState` on a page whose whole content was missing, where a generic sentence is honest. A form is different: the sentence IS the outcome, and the link page showed *Service Unavailable* for all four of the refusals phase 6 exists to write. All 23 bodies now answer in `message` — core's `Error` schema, which these routes' own `#swagger.responses` already referenced, so the annotations stop being a claim the handlers contradict. `test/errorShape.test.js` drives each outcome rather than grepping for the field, and asserts the half that is easy to leave behind: a body carrying BOTH fields renders correctly in a browser and keeps the wrong shape alive for the next route that copies it. **The player saw a stale name.** `/player/rust` showed the name recorded at link time while the admin panel showed the one the game last saw — the same person labelled two ways on one site, because a Rust name changes on a whim and only the admin read joined `rust_players`. A LEFT JOIN, because an account can be linked and never played on. 123 server tests, 39 client tests, `check:imports`, `check:bundle`, `check:swagger`, `check:externals` — all green. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PMH6bw1jXMgbyF3ZWGEzSM
160 lines
5.8 KiB
JavaScript
160 lines
5.8 KiB
JavaScript
// ── SQL, and nothing else ─────────────────────────────────────────────────
|
|
//
|
|
// The `.db.js` half of the pair (see `servers.db.js` for why the split earns its
|
|
// keep). Raw parameterised SQL through `core.query`, placeholders always.
|
|
|
|
const core = require('../../core')
|
|
|
|
const LINKS = 'rust_account_links'
|
|
const PLAYERS = 'rust_players'
|
|
const STATS = 'rust_player_wipe_stats'
|
|
|
|
/**
|
|
* The link for one Steam id, or undefined.
|
|
*
|
|
* Joins core's `users` for the username, because every caller that asks "who
|
|
* owns this?" wants a name rather than an integer — and the one caller that
|
|
* refuses a re-link has to be able to say *whose* it is.
|
|
*/
|
|
async function getBySteamId(steamId) {
|
|
const rows = await core.query(
|
|
`SELECT l.steam_id AS steamId, l.user_id AS userId, l.name, l.server_id AS serverId,
|
|
l.linked_at AS linkedAt, u.username
|
|
FROM ${LINKS} l
|
|
JOIN users u ON u.id = l.user_id
|
|
WHERE l.steam_id = ?`,
|
|
[steamId],
|
|
)
|
|
return rows[0]
|
|
}
|
|
|
|
/**
|
|
* Every Steam account one website user holds, newest first.
|
|
*
|
|
* **It joins `rust_players` for the name the game last saw**, and that is not a
|
|
* convenience. The name on the LINK is what the player was called at the moment
|
|
* they linked, which is a Rust name and changes on a whim — so a player who has
|
|
* renamed since sees a name they no longer use, on the one page of the site that
|
|
* is about who they are. The admin panel already preferred the newer one; this
|
|
* is the same rule applied where the person themselves is reading.
|
|
*
|
|
* A LEFT JOIN, because a player can link an account and never play on it.
|
|
*/
|
|
async function listForUser(userId) {
|
|
return core.query(
|
|
`SELECT l.steam_id AS steamId, l.user_id AS userId, l.name, l.server_id AS serverId,
|
|
l.linked_at AS linkedAt, p.name AS playerName
|
|
FROM ${LINKS} l
|
|
LEFT JOIN ${PLAYERS} p ON p.steam_id = l.steam_id
|
|
WHERE l.user_id = ?
|
|
ORDER BY l.linked_at DESC`,
|
|
[userId],
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Record a link.
|
|
*
|
|
* **A plain INSERT, never an upsert**, and that is the whole of D23 expressed in
|
|
* SQL. `ON DUPLICATE KEY UPDATE` here would silently move a Steam id from one
|
|
* website account to another — which, once phase 7 makes a link a privilege path
|
|
* and phase 13 makes it an entitlement, is an account takeover performed by
|
|
* typing a six-character code. The duplicate-key error is the refusal, and the
|
|
* controller turns it into a sentence.
|
|
*/
|
|
async function insert({ steamId, userId, name, serverId }) {
|
|
await core.query(
|
|
`INSERT INTO ${LINKS} (steam_id, user_id, name, server_id)
|
|
VALUES (?, ?, ?, ?)`,
|
|
[steamId, userId, name || null, serverId || null],
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Remove a link the caller owns.
|
|
*
|
|
* Scoped by `user_id` in the statement rather than checked before it: a delete
|
|
* that reads, decides, then writes has a gap between the read and the write, and
|
|
* this way the ownership test and the deletion are the same operation. Answers
|
|
* how many rows went, so a caller can tell "removed" from "was not yours".
|
|
*/
|
|
async function removeOwned(steamId, userId) {
|
|
const result = await core.query(
|
|
`DELETE FROM ${LINKS} WHERE steam_id = ? AND user_id = ?`,
|
|
[steamId, userId],
|
|
)
|
|
return Number(result && result.affectedRows) || 0
|
|
}
|
|
|
|
/**
|
|
* Remove a link whoever holds it — the in-game `/unlink` path, and the staff
|
|
* unlink on the `admin.users.detail` panel (D25).
|
|
*
|
|
* Unscoped by user on purpose: neither caller is the link's owner and both have
|
|
* already established their authority another way. In game the authority is the
|
|
* Steam account itself — whoever is connected as it is who it is; on the admin
|
|
* panel it is the tier gate. Which is why the admin caller writes an
|
|
* `activity.log` entry naming the operator and this does not: it cannot tell the
|
|
* two apart, and a log line that guessed would be worse than none.
|
|
*/
|
|
async function removeBySteamId(steamId) {
|
|
const result = await core.query(`DELETE FROM ${LINKS} WHERE steam_id = ?`, [steamId])
|
|
return Number(result && result.affectedRows) || 0
|
|
}
|
|
|
|
/**
|
|
* Every link one user holds, enriched with what this module knows about that
|
|
* player — for the `admin.users.detail` panel.
|
|
*
|
|
* A LEFT JOIN, because a player can link an account and never play on it. An
|
|
* operator looking at that user should see the link, not an empty panel.
|
|
*/
|
|
async function listForUserWithPlayer(userId) {
|
|
return core.query(
|
|
`SELECT l.steam_id AS steamId, l.name, l.server_id AS serverId, l.linked_at AS linkedAt,
|
|
p.name AS playerName, p.first_seen AS firstSeen, p.last_seen AS lastSeen
|
|
FROM ${LINKS} l
|
|
LEFT JOIN ${PLAYERS} p ON p.steam_id = l.steam_id
|
|
WHERE l.user_id = ?
|
|
ORDER BY l.linked_at DESC`,
|
|
[userId],
|
|
)
|
|
}
|
|
|
|
/**
|
|
* Per-server all-time totals for one Steam id.
|
|
*
|
|
* The same rows the public leaderboard sums, grouped by server instead of
|
|
* filtered to one — so an operator sees a player across the fleet in one read.
|
|
* All-time, deliberately: an admin looking at a user wants their history, not
|
|
* this week's.
|
|
*/
|
|
async function statsForSteamId(steamId) {
|
|
return core.query(
|
|
`SELECT s.server_id AS serverId, srv.name AS serverName,
|
|
SUM(s.kills) AS kills,
|
|
SUM(s.deaths) AS deaths,
|
|
SUM(s.npc_kills) AS npcKills,
|
|
SUM(s.structures) AS structures,
|
|
SUM(s.playtime_sec) AS playtimeSec,
|
|
MAX(s.last_seen) AS lastSeen,
|
|
COUNT(DISTINCT s.wipe_id) AS wipes
|
|
FROM ${STATS} s
|
|
LEFT JOIN rust_servers srv ON srv.id = s.server_id
|
|
WHERE s.steam_id = ?
|
|
GROUP BY s.server_id, srv.name
|
|
ORDER BY SUM(s.playtime_sec) DESC`,
|
|
[steamId],
|
|
)
|
|
}
|
|
|
|
module.exports = {
|
|
getBySteamId,
|
|
listForUser,
|
|
listForUserWithPlayer,
|
|
insert,
|
|
removeOwned,
|
|
removeBySteamId,
|
|
statsForSteamId,
|
|
}
|