Preserve conversation excerpts and deduplicate case evidence

This commit is contained in:
2026-10-17 19:35:52 -04:00
parent 83f1c91877
commit ef96a2c308
13 changed files with 1222 additions and 50 deletions
+9 -3
View File
@@ -123,9 +123,15 @@ export function serverChatTimeline(server: ServerRow, tail: TailResult): Context
export function matchChatTimeline(matchId: number): ContextualChatEntry[] {
return (
db()
.prepare('SELECT entry FROM match_chat WHERE match_id = ? ORDER BY rowid')
.all(matchId) as { entry: string }[]
).map((row) => JSON.parse(row.entry));
.prepare(
'SELECT message_key,occurrence,entry FROM match_chat WHERE match_id = ? ORDER BY rowid'
)
.all(matchId) as { message_key: string; occurrence: number; entry: string }[]
).map((row) => ({
...JSON.parse(row.entry),
matchId,
messageId: `${matchId}:${row.message_key}:${row.occurrence}`
}));
}
/** Save before filtering the live view so completed matches retain their messages. */
+6 -37
View File
@@ -1,5 +1,6 @@
import { randomUUID } from 'node:crypto';
import { db } from '../db/index.js';
import { archivedEvidenceSnapshots } from './evidence.js';
import { AuthError } from '../auth/index.js';
import { moderatorMaximumRestrictionMs, type AuthUser } from '../../auth.js';
import {
@@ -7,7 +8,6 @@ import {
isModerationCaseOpen,
moderationCaseOutcomes,
moderationCaseStatusLabels,
moderationGuid,
type ModerationCaseStatus,
type ModerationCasePriority,
type ModerationGame,
@@ -404,42 +404,10 @@ function archivedEvidence(
value: unknown,
now: string
): number {
const requested = value as { matchId: unknown; message: unknown };
if (!requested || !Number.isSafeInteger(requested.matchId))
throw new AuthError('Choose a recorded chat message.');
textField(requested.message, 'Chat message', 4096);
// Evidence must match the archived bytes, including intentional whitespace.
const message = requested.message as string;
const match = db()
.prepare('SELECT game,server,started_at FROM leaderboard_matches WHERE id=?')
.get(requested.matchId) as { game: string; server: string; started_at: string } | undefined;
if (!match || match.game !== target.game)
throw new AuthError('The source match is unavailable.', 404);
const entries = db()
.prepare('SELECT entry FROM match_chat WHERE match_id=?')
.all(requested.matchId) as { entry: string }[];
const found = entries
.filter((row) => {
const entry = JSON.parse(row.entry);
return (
entry.type === 'message' &&
moderationGuid(target.game, entry.message.guid) === target.guid &&
entry.message.message === message
);
})
.slice(0, 10);
if (!found.length)
throw new AuthError('The original chat message could not be verified in the archive.', 409);
for (const row of found)
evidence(
actor,
id,
message,
`Archived chat · ${match.server} · match ${requested.matchId}`,
JSON.stringify({ matchId: requested.matchId, match, entry: JSON.parse(row.entry) }),
now
);
return found.length;
const snapshots = archivedEvidenceSnapshots(id, target, value);
for (const snapshot of snapshots)
evidence(actor, id, snapshot.text, snapshot.source, snapshot.snapshot, now);
return snapshots.length;
}
function updateCase(actor: AuthUser, input: Record<string, unknown>, now: string) {
const row = caseRow(input.id);
@@ -548,6 +516,7 @@ export function mutateModeration(actorId: string, input: Record<string, unknown>
const row = caseRow(input.id);
checkCaseRevision(row, input.revision);
const count = archivedEvidence(actor, row.id, row, input.evidence, now);
if (!count) return { id: row.id };
touchCase(row.id, now);
moderationEvent(
actor,
+127
View File
@@ -0,0 +1,127 @@
import { db } from '../db/index.js';
import { AuthError } from '../auth/index.js';
import { matchChatTimeline } from '../fleet/chat.js';
import { moderationGuid, type ModerationGame } from '../../moderation.js';
import type { ContextualChatEntry } from '../../game-chat.js';
import { textField } from './core.js';
type Message = Extract<ContextualChatEntry, { type: 'message' }>;
/** Verify against the archive, then preserve independent excerpts across all participants. */
export function archivedEvidenceSnapshots(
caseId: string,
target: { game: ModerationGame; guid: string },
value: unknown
) {
const requested = value as Record<string, unknown> | null;
if (!requested || !Number.isSafeInteger(requested.matchId))
throw new AuthError('Choose a recorded chat message.');
const matchId = requested.matchId as number;
const match = db()
.prepare('SELECT game,server,started_at FROM leaderboard_matches WHERE id=?')
.get(matchId) as { game: string; server: string; started_at: string } | undefined;
if (!match || match.game !== target.game)
throw new AuthError('The source match is unavailable.', 404);
const entries = matchChatTimeline(matchId).filter(
(entry): entry is Message => entry.type === 'message'
);
const byId = new Map(entries.map((entry) => [entry.messageId!, entry]));
const isSubject = (entry: Message) =>
moderationGuid(target.game, entry.message.guid) === target.guid;
const legacy = requested.messageIds === undefined;
let selected: Message[];
if (legacy) {
textField(requested.message, 'Chat message', 4096);
selected = entries.filter(
(entry) => isSubject(entry) && entry.message.message === requested.message
);
} else {
if (
!Array.isArray(requested.messageIds) ||
!requested.messageIds.length ||
requested.messageIds.length > 100 ||
requested.messageIds.some((id) => typeof id !== 'string' || !byId.has(id))
)
throw new AuthError('Select between 1 and 100 archived message identifiers.');
const ids = new Set(requested.messageIds);
selected = entries.filter((entry) => ids.has(entry.messageId));
}
if (!selected.length || !selected.some(isSubject))
throw new AuthError(
'The original chat message could not be verified in the archive. Select a message from the case subject.',
409
);
const radius = (key: string) => {
const count = requested[key] ?? 3;
if (!Number.isSafeInteger(count) || (count as number) < 0 || (count as number) > 20)
throw new AuthError('Choose between 0 and 20 surrounding messages.');
return count as number;
};
const before = radius('contextBefore'),
after = radius('contextAfter');
const attached = new Set<string>();
const prior = db()
.prepare(
'SELECT snapshot FROM moderation_evidence WHERE case_id=? AND snapshot IS NOT NULL'
)
.all(caseId) as { snapshot: string }[];
for (const row of prior) {
try {
const snapshot = JSON.parse(row.snapshot);
if (snapshot.matchId !== matchId) continue;
if (Array.isArray(snapshot.selectedMessageIds)) {
for (const id of snapshot.selectedMessageIds)
if (typeof id === 'string') attached.add(id);
} else if (snapshot.entry?.type === 'message') {
// Older evidence predates IDs. Match each occurrence once without changing history.
const old = snapshot.entry.message;
const original = entries.find(
(entry) =>
!attached.has(entry.messageId!) &&
entry.message.guid === old.guid &&
entry.message.time === old.time &&
entry.message.player === old.player &&
entry.message.message === old.message
);
if (original) attached.add(original.messageId!);
}
} catch {
/* Keep malformed legacy snapshots readable without trusting them as identities. */
}
}
const fresh = selected.filter((entry) => !attached.has(entry.messageId!));
if (!fresh.length) return [];
// Keep the original selected subject as context when only a newly selected reply is fresh.
const groups = legacy ? fresh.map((entry) => [entry]) : [selected];
return groups.map((group) => {
const indexes = new Set<number>();
for (const entry of group) {
const index = entries.indexOf(entry);
for (
let i = Math.max(0, index - before);
i <= Math.min(entries.length - 1, index + after);
i++
)
indexes.add(i);
}
const primary = group.find(isSubject)!;
return {
text: group
.filter(isSubject)
.map((entry) => entry.message.message)
.join('\n'),
source: `Archived chat · ${match.server} · match ${matchId}`,
snapshot: JSON.stringify({
version: 2,
matchId,
match,
entry: primary,
entries: [...indexes].sort((a, b) => a - b).map((index) => entries[index]),
selectedMessageIds: group
.filter((entry) => !attached.has(entry.messageId!))
.map((entry) => entry.messageId),
highlightedMessageIds: group.filter(isSubject).map((entry) => entry.messageId)
})
};
});
}