diff --git a/lib/accounts.js b/lib/accounts.js index 4447e23..50d679d 100644 --- a/lib/accounts.js +++ b/lib/accounts.js @@ -803,7 +803,10 @@ const storableUsernameProblem = username => { }; // gives the account a new username, and carries it into everyone's buddy and -// block lists. returns "invalid_username" or "username_taken", else null. +// block lists. The staff dashboard stores a few human-readable names beside +// account ids; keep those labels current too, so a rename does not split a +// player's history across two names. Returns "invalid_username" or +// "username_taken", else null. // the player must not be online: the game knows them by the old name. // with `anyName` (the admin panel) the sign-up rules about length and // characters do not apply; see storableUsernameProblem. @@ -825,6 +828,18 @@ const rename = (account, username, anyName = false) => { } db.prepare("UPDATE accounts SET username = ?, username_key = ? WHERE id = ?").run(username, newKey, account.id); console.log(`[accounts] renamed "${logText(account.username)}" to "${logText(username)}" (id ${logText(account.id)})`); + for (const [table, column] of [ + ["reports", "reporter"], ["reports", "target"], ["feedback", "username"], + ["chat_log", "username"], ["chat_log", "to_name"], ["appeals", "username"], + ["ban_log", "username"], ["mod_log", "by"], ["mod_log", "target"], + ]) { + // A few lean unit tests load the account store without every dashboard + // module. The tables are present in the actual server, but keep rename + // usable in that smaller setup too. + if (db.prepare("SELECT 1 FROM sqlite_master WHERE type = 'table' AND name = ?").get(table)) { + db.prepare(`UPDATE ${table} SET ${column} = ? WHERE ${column} = ? COLLATE NOCASE`).run(username, account.username); + } + } byKey.delete(oldKey); account.username = username; byKey.set(newKey, account); diff --git a/public/admin.html b/public/admin.html index 1f37874..b337ab4 100644 --- a/public/admin.html +++ b/public/admin.html @@ -215,9 +215,25 @@ .page-title h1 { font-size: 26px; } @media (max-width: 860px) { #panel { grid-template-columns: 1fr; } - #side { position: static; height: auto; flex-direction: column; } - nav { flex-direction: row; flex-wrap: wrap; } - nav button { width: auto; } + #side { position: static; height: auto; gap: 12px; padding: 12px; } + nav { flex-direction: row; flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden; padding-bottom: 2px; scrollbar-width: thin; } + nav button { flex: 0 0 auto; width: auto; } + #main { padding: 16px 12px 32px; } + section { padding: 15px; border-radius: 14px; } + .page-title { align-items: flex-start; flex-direction: column; gap: 3px; margin-bottom: 14px; } + .columns, .actions { grid-template-columns: minmax(0, 1fr); } + .toolbar input[type="search"] { flex-basis: 100%; min-width: 0; width: 100%; } + .table-wrap { max-width: 100%; } + } + @media (max-width: 520px) { + body { font-size: 14px; overflow-x: hidden; } + .brand { padding: 0; } + .tiles, .paintings { grid-template-columns: minmax(0, 1fr); } + .tile { padding: 14px; } + .tile .value { font-size: 26px; } + dl { grid-template-columns: 1fr; gap: 2px; } + dd { margin-bottom: 9px; } + .preview { padding: 8px; } } section { background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 20px; margin-bottom: 18px; box-shadow: var(--shadow); } @@ -1002,6 +1018,8 @@ let tab = "overview"; let lastLogId = 0; let timer = null; + // Older responses must never overwrite a newer account search. + let accountSearchRun = 0; const api = async (path, body) => { const response = await fetch(`/dashboard/api/admin/${path}`, body === undefined ? {} : { @@ -1735,6 +1753,7 @@ // the server searches, once typing stops for a moment. let addressWait = null; $("account-search").addEventListener("input", () => { + accountSearchRun++; clearTimeout(addressWait); addressWait = setTimeout($("account-search-by").value !== "name" ? searchByAddress : refresh, $("account-search-by").value !== "name" ? 400 : 250); }); @@ -1752,6 +1771,7 @@ } }); $("account-search-by").addEventListener("change", () => { + accountSearchRun++; const byAddress = $("account-search-by").value !== "name"; $("account-search").placeholder = {address: "203.0.113.7, or the start of one", device: "Hardware ID, or the start of one"}[$("account-search-by").value] || "Search"; if (byAddress) { @@ -1761,7 +1781,10 @@ refresh(); } }); - $("account-filter").addEventListener("change", () => refresh()); + $("account-filter").addEventListener("change", () => { + accountSearchRun++; + refresh(); + }); const act = async (username, action, extra, statusElement) => { const result = await api("account", {username, action, ...extra}); @@ -2041,7 +2064,7 @@ $("masterpieces").replaceChildren(...paintings.map(painting => { const box = el("div", undefined, "painting"); const picture = el("img"); - picture.src = `/admin/api/masterpiece?id=${encodeURIComponent(painting.id)}`; + picture.src = `/dashboard/api/admin/masterpiece?id=${encodeURIComponent(painting.id)}`; picture.alt = `Painting by ${painting.creator}`; picture.loading = "lazy"; picture.addEventListener("click", () => showPreview(picture.src)); @@ -2861,14 +2884,23 @@ } } else if (tab === "accounts") { - const query = $("account-search-by").value !== "name" ? "" : $("account-search").value.trim(); - result = await api(`accounts?q=${encodeURIComponent(query)}&filter=${$("account-filter").value}`); - if (result.ok) { + const searchBy = $("account-search-by").value; + const query = searchBy !== "name" ? "" : $("account-search").value.trim(); + const filter = $("account-filter").value; + const searchRun = ++accountSearchRun; + result = await api(`accounts?q=${encodeURIComponent(query)}&filter=${filter}`); + // A slower earlier search must not replace what the person just typed. + if (result.ok && searchRun === accountSearchRun && tab === "accounts" + && searchBy === $("account-search-by").value + && query === (searchBy !== "name" ? "" : $("account-search").value.trim()) + && filter === $("account-filter").value) { accounts = result.data.accounts; accountTotal = result.data.total; badges = result.data.badges || []; await loadCurrent(); - renderAccounts(); + if (searchRun === accountSearchRun && tab === "accounts") { + renderAccounts(); + } } } else if (tab === "stats") { diff --git a/public/mod.html b/public/mod.html index e2e0497..ed2d748 100644 --- a/public/mod.html +++ b/public/mod.html @@ -216,9 +216,25 @@ .page-title h1 { font-size: 26px; } @media (max-width: 860px) { #panel { grid-template-columns: 1fr; } - #side { position: static; height: auto; flex-direction: column; } - nav { flex-direction: row; flex-wrap: wrap; } - nav button { width: auto; } + #side { position: static; height: auto; gap: 12px; padding: 12px; } + nav { flex-direction: row; flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden; padding-bottom: 2px; scrollbar-width: thin; } + nav button { flex: 0 0 auto; width: auto; } + #main { padding: 16px 12px 32px; } + section { padding: 15px; border-radius: 14px; } + .page-title { align-items: flex-start; flex-direction: column; gap: 3px; margin-bottom: 14px; } + .columns, .actions { grid-template-columns: minmax(0, 1fr); } + .toolbar input[type="search"] { flex-basis: 100%; min-width: 0; width: 100%; } + .table-wrap { max-width: 100%; } + } + @media (max-width: 520px) { + body { font-size: 14px; overflow-x: hidden; } + .brand { padding: 0; } + .tiles, .paintings { grid-template-columns: minmax(0, 1fr); } + .tile { padding: 14px; } + .tile .value { font-size: 26px; } + dl { grid-template-columns: 1fr; gap: 2px; } + dd { margin-bottom: 9px; } + .preview { padding: 8px; } } section { background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 20px; margin-bottom: 18px; box-shadow: var(--shadow); } @@ -1020,6 +1036,8 @@ let tab = "home"; let selected = null; // the username of the player whose box is open let players = []; + // Older responses must never overwrite a newer player search. + let playerSearchRun = 0; let chatQuery = null; // what the Chat page shows: {username, room, text} let chatLines = []; let timer = null; @@ -1560,6 +1578,7 @@ $("player-only").addEventListener("click", event => { const chip = event.target.closest("button[data-only]"); if (chip) { + playerSearchRun++; setListOnly(chip.dataset.only); refresh(); } @@ -1567,6 +1586,7 @@ // from another page: that player's box, whether or not the search shows them. const openPlayer = username => { + playerSearchRun++; $("player-search").value = username; $("player-search-by").value = "name"; $("player-search").placeholder = "Search"; @@ -1582,6 +1602,7 @@ // every account that used a hardware ID: the player list, searched by it. const searchDevice = id => { + playerSearchRun++; $("player-search-by").value = "device"; $("player-search").placeholder = "Hardware ID, or the start of one"; $("player-search").value = id; @@ -1664,6 +1685,7 @@ // by address, asked once typing stops: each search is written down. let searchWait = null; $("player-search").addEventListener("input", () => { + playerSearchRun++; clearTimeout(searchWait); searchWait = setTimeout(refresh, $("player-search-by").value === "name" ? 250 : 700); }); @@ -1687,6 +1709,7 @@ } }); $("player-search-by").addEventListener("change", () => { + playerSearchRun++; $("player-search").placeholder = {address: "203.0.113.7, or the start of one", device: "Hardware ID, or the start of one"}[$("player-search-by").value] || "Search"; refresh(); }); @@ -1966,7 +1989,7 @@ let recentList = []; let paintingsShown = ""; let recentShown = ""; - const paintingSrc = painting => `/mod/api/masterpiece?id=${encodeURIComponent(painting.id)}`; + const paintingSrc = painting => `/dashboard/api/staff/masterpiece?id=${encodeURIComponent(painting.id)}`; const paintingWords = painting => (painting.status === "waiting" ? `Waiting · painted ${ago(painting.createdAt)}` : `Approved${painting.approvedAt ? ` ${ago(painting.approvedAt)}` : ""}${painting.approvedBy ? ` by ${painting.approvedBy === myName ? "you" : painting.approvedBy}` : ""}` + `${painting.likes ? ` · ${painting.likes} ${painting.likes === 1 ? "like" : "likes"}` : ""}`); @@ -2766,9 +2789,13 @@ const searchBy = $("player-search-by").value; const byAddress = searchBy !== "name"; const query = $("player-search").value.trim(); + const searchRun = ++playerSearchRun; $("address-hint").hidden = searchBy !== "address"; $("player-only").hidden = byAddress; if (byAddress && query === "") { + if (searchRun !== playerSearchRun || tab !== "players") { + return; + } players = []; renderPlayers(); $("players-more").hidden = true; @@ -2777,6 +2804,12 @@ else { const only = listOnly(); const result = await api(`players?q=${encodeURIComponent(query)}${byAddress ? `&by=${searchBy}` : only ? `&only=${only}` : ""}`); + if (searchRun !== playerSearchRun || tab !== "players" + || searchBy !== $("player-search-by").value + || query !== $("player-search").value.trim() + || (!byAddress && only !== listOnly())) { + return; + } players = result.ok ? result.data.players : []; renderPlayers(); const total = result.ok && result.data.total ? result.data.total : players.length; diff --git a/test/features.js b/test/features.js index ae9e721..ce61602 100644 --- a/test/features.js +++ b/test/features.js @@ -529,6 +529,7 @@ const run = async () => { a.xt("fr", own[4], "Ben", 2); assert.deepStrictEqual((await a.nextXt("fr")).slice(2), ["1"]); const waiting = (await panel("reports", undefined, admin)).data; + const reportId = waiting.reports[0].id; assert.strictEqual(waiting.open, 1); assert.strictEqual(waiting.reports.length, 1); assert.deepStrictEqual([waiting.reports[0].reporter, waiting.reports[0].target, waiting.reports[0].reason, waiting.reports[0].detail, waiting.reports[0].room], @@ -705,6 +706,12 @@ const run = async () => { await panel("debug", {action: "everythingOnSale", on: false}, admin); assert.ok(await dated(1030) > 0); + // Renames must carry the names shown on existing moderation records. + // The admin action disconnects Ben before making this change. + assert.strictEqual((await panel("account", {username: "Ben", action: "rename", newName: "Benny"}, admin)).status, 200); + const renamedReport = (await panel("reports?handled=1", undefined, admin)).data.reports.find(report => report.id === reportId); + assert.strictEqual(renamedReport.target, "Benny"); + a.socket.destroy(); b.socket.destroy(); };