v1.3.5: 표시 이름 빈틈 — 이름 바꾼 뒤 도착한 범인 분석에도 예전 이름이 남지 않게

- 가린 예전 이름을 meta.hidden_names 에 기억, 분석 저장(Culprit.save) · 이름 다시 받기(setDisplayNames, 건너뛰기 없이 전체)에서 바꿈
- 회귀 테스트: 이름을 바꾼 뒤 예전 이름이 든 분석 저장 → DB 에 예전 이름 0건 (예전 코드에서 실패 확인)
- site/server/scripts/presence-e2e.mts: 실제 디스코드에서 봇 음성 입장/퇴장 → 상태 메시지 녹음 중/대기 중 전환 확인

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
EJClaw
2026-10-16 03:14:00 +09:00
parent 0dd46a5e16
commit 3f7bf1cb86
8 changed files with 98 additions and 11 deletions

View File

@@ -146,7 +146,8 @@ export class Culprit {
this.store.db.prepare('UPDATE culprit_analyses SET current=0 WHERE match_id=?').run(matchId);
this.store.db
.prepare('INSERT OR REPLACE INTO culprit_analyses(id,match_id,created_at,created_by,model,current,result,meta) VALUES(?,?,?,?,?,1,?,?)')
.run(id, matchId, a.createdAt, JSON.stringify(a.createdBy), a.model, JSON.stringify({ result: a.result, players: a.players }), JSON.stringify(a.meta));
// 표시 이름(실명 숨기기)을 정하기 전에 시작한 분석이 뒤늦게 와도 예전 이름이 남지 않게 저장할 때 바꾼다
.run(id, matchId, a.createdAt, JSON.stringify(this.store.scrubCreatedBy(a.createdBy)), a.model, this.store.scrubNames(JSON.stringify({ result: a.result, players: a.players })), JSON.stringify(a.meta));
});
tx();
return this.current(matchId)!;

View File

@@ -126,6 +126,7 @@ export class Store {
ON CONFLICT(id) DO UPDATE SET name=excluded.name, avatar=excluded.avatar, updated_at=excluded.updated_at`,
)
.run(u.id, this.displayNames()[u.id] ?? u.name, colorFor(u.id), u.avatar, now);
this.rememberHidden(u.id, u.name);
return this.getUser(u.id)!;
}
@@ -141,6 +142,38 @@ export class Store {
}
return this.names!;
}
/** 표시 이름으로 가린 예전 이름들 (아이디 → 디스코드에서 받은 이름들). 범인 분석 본문 등 글 속 이름을 바꿀 때 쓴다 */
private hiddenNames(): Record<string, string[]> {
try {
return JSON.parse(this.meta('hidden_names') || '{}');
} catch {
return {};
}
}
private rememberHidden(id: string, old: string): void {
const name = this.displayNames()[id];
if (!name || old === name || old.trim().length < 2) return;
const h = this.hiddenNames();
if (h[id]?.includes(old)) return;
(h[id] ||= []).push(old);
this.setMeta('hidden_names', JSON.stringify(h));
}
/** 글(JSON 문자열 포함) 속 예전 이름을 표시 이름으로 바꾼다. 긴 이름부터 (짧은 이름이 긴 이름의 일부일 수 있어서) */
scrubNames(text: string): string {
const names = this.displayNames();
const pairs: [string, string][] = [];
for (const [id, olds] of Object.entries(this.hiddenNames())) if (names[id]) for (const o of olds) pairs.push([o, names[id]]);
pairs.sort((x, y) => y[0].length - x[0].length);
const j = (x: string) => JSON.stringify(x).slice(1, -1);
for (const [o, n] of pairs) text = text.split(j(o)).join(j(n));
return text;
}
/** 분석을 만든 사람 { id, name }: 표시 이름이 있으면 그것으로 */
scrubCreatedBy<T extends { id?: string; name?: string }>(by: T): T {
const n = by?.id ? this.displayNames()[by.id] : undefined;
if (n && by.name && by.name !== n) this.rememberHidden(by.id!, by.name);
return n ? { ...by, name: n } : by;
}
/** 새 표시 이름을 저장하고, 이미 저장된 유저 이름 · 지난 범인 분석에 남은 예전 이름도 바꾼다 */
setDisplayNames(map: Record<string, string>): void {
const names: Record<string, string> = {};
@@ -148,23 +181,25 @@ export class Store {
this.names = names;
this.setMeta('display_names', JSON.stringify(names));
const culprit = !!this.db.prepare(`SELECT 1 FROM sqlite_master WHERE type='table' AND name='culprit_analyses'`).get();
const j = (x: string) => JSON.stringify(x).slice(1, -1);
this.db.transaction(() => {
for (const [id, name] of Object.entries(names)) {
const u = this.getUser(id);
if (!u || u.name === name) continue;
this.rememberHidden(id, u.name);
this.db.prepare('UPDATE users SET name=? WHERE id=?').run(name, id);
if (culprit && u.name.length >= 2) this.db.prepare('UPDATE culprit_analyses SET result=replace(result,?,?)').run(j(u.name), j(name));
}
if (!culprit) return;
for (const r of this.db.prepare('SELECT id, created_by FROM culprit_analyses').all() as { id: string; created_by: string }[]) {
// 건너뛰지 않고 매번 모든 분석을 다시 훑는다 (이름을 바꾸기 전에 시작한 분석이 뒤늦게 저장된 경우도 고친다)
for (const r of this.db.prepare('SELECT id, created_by, result FROM culprit_analyses').all() as { id: string; created_by: string; result: string }[]) {
let by: any;
try {
by = JSON.parse(r.created_by);
} catch {
continue;
by = null;
}
if (by?.id && names[by.id] && by.name !== names[by.id]) this.db.prepare('UPDATE culprit_analyses SET created_by=? WHERE id=?').run(JSON.stringify({ ...by, name: names[by.id] }), r.id);
const createdBy = by ? JSON.stringify(this.scrubCreatedBy(by)) : r.created_by;
const result = this.scrubNames(r.result);
if (createdBy !== r.created_by || result !== r.result) this.db.prepare('UPDATE culprit_analyses SET created_by=?, result=? WHERE id=?').run(createdBy, result, r.id);
}
})();
}