fix(realtime): redact browser event details
This commit is contained in:
@@ -4,8 +4,8 @@ import { MESSAGE_MAILBOX_NATIONAL_BASE, MESSAGE_MAILBOX_PUBLIC, type MessageType
|
||||
import {
|
||||
applyReadModelDelta,
|
||||
cloneReadModelJson,
|
||||
type RealtimeEvent,
|
||||
type RealtimeReadModelChanges,
|
||||
type PublicRealtimeEvent,
|
||||
type RealtimeReadModelInvalidation,
|
||||
} from '@sammo-ts/common';
|
||||
import { trpc } from '../utils/trpc';
|
||||
import { useMapViewerStore } from './mapViewer';
|
||||
@@ -13,7 +13,7 @@ import { useSessionStore } from './session';
|
||||
import { createLatestRefreshQueue } from '../utils/latestRefreshQueue';
|
||||
import { createRateLimitedRefreshQueue } from '../utils/rateLimitedRefreshQueue';
|
||||
import { structurallyShare } from '../utils/structuralShare';
|
||||
import { createMergedReadModelRefreshQueue, resolveDashboardRefreshPlan } from '../utils/dashboardReadModel';
|
||||
import { createMergedReadModelRefreshQueue } from '../utils/dashboardReadModel';
|
||||
import { createBroadcastTabCoordinator, type BroadcastTabCoordinator } from '../utils/broadcastTabCoordinator';
|
||||
import { resolveWithReadModelSnapshotFallback } from '../utils/readModelDeltaRecovery';
|
||||
|
||||
@@ -612,16 +612,11 @@ export const useMainDashboardStore = defineStore('mainDashboard', () => {
|
||||
}
|
||||
);
|
||||
|
||||
const refreshChangedReadModels = async (changes: RealtimeReadModelChanges) => {
|
||||
const refreshChangedReadModels = async (plan: RealtimeReadModelInvalidation) => {
|
||||
const id = generalId.value;
|
||||
if (!id) {
|
||||
return;
|
||||
}
|
||||
const plan = resolveDashboardRefreshPlan(changes, {
|
||||
generalId: id,
|
||||
cityId: city.value?.id ?? null,
|
||||
nationId: nation.value?.id ?? null,
|
||||
});
|
||||
if (!Object.values(plan).some(Boolean)) {
|
||||
return;
|
||||
}
|
||||
@@ -944,12 +939,12 @@ export const useMainDashboardStore = defineStore('mainDashboard', () => {
|
||||
return url.toString();
|
||||
};
|
||||
|
||||
const parseRealtimePayload = (raw: MessageEvent): RealtimeEvent | null => {
|
||||
const parseRealtimePayload = (raw: MessageEvent): PublicRealtimeEvent | null => {
|
||||
if (!raw.data || typeof raw.data !== 'string') {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
const parsed = JSON.parse(raw.data) as RealtimeEvent;
|
||||
const parsed = JSON.parse(raw.data) as PublicRealtimeEvent;
|
||||
if (!parsed || typeof parsed !== 'object') {
|
||||
return null;
|
||||
}
|
||||
@@ -962,21 +957,6 @@ export const useMainDashboardStore = defineStore('mainDashboard', () => {
|
||||
}
|
||||
};
|
||||
|
||||
const isMailboxRelevant = (mailbox: number): boolean => {
|
||||
if (mailbox === MESSAGE_MAILBOX_PUBLIC) {
|
||||
return true;
|
||||
}
|
||||
const currentGeneralId = generalId.value;
|
||||
if (currentGeneralId && mailbox === currentGeneralId) {
|
||||
return true;
|
||||
}
|
||||
const currentNationId = nationId.value;
|
||||
if (currentNationId && mailbox === MESSAGE_MAILBOX_NATIONAL_BASE + currentNationId) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
const closeRealtimeSource = () => {
|
||||
if (!realtimeSource) {
|
||||
return;
|
||||
@@ -1095,36 +1075,34 @@ export const useMainDashboardStore = defineStore('mainDashboard', () => {
|
||||
realtimeStatus.value = realtimeEnabled.value ? 'idle' : 'paused';
|
||||
realtimeCoordinator?.postFromLeader({ kind: 'status', status: 'idle' });
|
||||
});
|
||||
source.addEventListener('turnCompleted', (event) => {
|
||||
source.addEventListener('readModelInvalidated', (event) => {
|
||||
if (realtimeCoordinator !== null && !realtimeCoordinator.isLeader()) return;
|
||||
const payload = parseRealtimePayload(event);
|
||||
if (!payload || payload.type !== 'turnCompleted') {
|
||||
if (!payload || payload.type !== 'readModelInvalidated') {
|
||||
return;
|
||||
}
|
||||
if (!payload.changes) {
|
||||
// Rolling deployment fallback for an older daemon.
|
||||
readModelRefreshQueue.request(payload.invalidation);
|
||||
});
|
||||
source.addEventListener('messagesInvalidated', (event) => {
|
||||
if (realtimeCoordinator !== null && !realtimeCoordinator.isLeader()) return;
|
||||
const payload = parseRealtimePayload(event);
|
||||
if (!payload || payload.type !== 'messagesInvalidated') {
|
||||
return;
|
||||
}
|
||||
void refreshMessages();
|
||||
});
|
||||
|
||||
// Rolling deployment fallback: an older API may still expose internal
|
||||
// events. Do not inspect their payload; use the bounded full refresh.
|
||||
for (const legacyEventType of ['turnCompleted', 'readModelChanged'] as const) {
|
||||
source.addEventListener(legacyEventType, () => {
|
||||
if (realtimeCoordinator !== null && !realtimeCoordinator.isLeader()) return;
|
||||
realtimeRefreshQueue.request();
|
||||
return;
|
||||
}
|
||||
readModelRefreshQueue.request(payload.changes);
|
||||
});
|
||||
source.addEventListener('readModelChanged', (event) => {
|
||||
});
|
||||
}
|
||||
source.addEventListener('messageCreated', () => {
|
||||
if (realtimeCoordinator !== null && !realtimeCoordinator.isLeader()) return;
|
||||
const payload = parseRealtimePayload(event);
|
||||
if (!payload || payload.type !== 'readModelChanged') {
|
||||
return;
|
||||
}
|
||||
readModelRefreshQueue.request(payload.changes);
|
||||
});
|
||||
source.addEventListener('messageCreated', (event) => {
|
||||
if (realtimeCoordinator !== null && !realtimeCoordinator.isLeader()) return;
|
||||
const payload = parseRealtimePayload(event);
|
||||
if (!payload || payload.type !== 'messageCreated') {
|
||||
return;
|
||||
}
|
||||
if (isMailboxRelevant(payload.mailbox)) {
|
||||
void refreshMessages();
|
||||
}
|
||||
void refreshMessages();
|
||||
});
|
||||
source.addEventListener('ping', () => {
|
||||
if (realtimeEnabled.value) {
|
||||
|
||||
@@ -1,85 +1,29 @@
|
||||
import {
|
||||
createEmptyRealtimeReadModelChanges,
|
||||
mergeRealtimeReadModelChanges,
|
||||
createEmptyRealtimeReadModelInvalidation,
|
||||
mergeRealtimeReadModelInvalidations,
|
||||
resolveRealtimeReadModelInvalidation,
|
||||
type RealtimeReadModelChanges,
|
||||
type RealtimeReadModelInvalidation,
|
||||
type RealtimeViewerIdentity,
|
||||
} from '@sammo-ts/common';
|
||||
|
||||
export interface DashboardReadModelIdentity {
|
||||
generalId: number | null;
|
||||
cityId: number | null;
|
||||
nationId: number | null;
|
||||
}
|
||||
|
||||
export interface DashboardRefreshPlan {
|
||||
context: boolean;
|
||||
lobby: boolean;
|
||||
map: boolean;
|
||||
commands: boolean;
|
||||
contacts: boolean;
|
||||
boardAccess: boolean;
|
||||
reservedTurns: boolean;
|
||||
records: boolean;
|
||||
frontStatus: boolean;
|
||||
}
|
||||
|
||||
const contains = (ids: readonly number[], id: number | null): boolean => id !== null && ids.includes(id);
|
||||
export type DashboardReadModelIdentity = RealtimeViewerIdentity;
|
||||
export type DashboardRefreshPlan = RealtimeReadModelInvalidation;
|
||||
|
||||
export const resolveDashboardRefreshPlan = (
|
||||
changes: RealtimeReadModelChanges,
|
||||
identity: DashboardReadModelIdentity
|
||||
): DashboardRefreshPlan => {
|
||||
const ownGeneralChanged = contains(changes.generalIds, identity.generalId);
|
||||
const ownCityChanged = contains(changes.cityIds, identity.cityId);
|
||||
const ownNationChanged = contains(changes.nationIds, identity.nationId);
|
||||
const ownFrontStatusNationChanged = contains(
|
||||
changes.frontStatusNationIds ?? changes.nationIds,
|
||||
identity.nationId
|
||||
);
|
||||
const ownGeneralMapChanged = contains(changes.mapGeneralIds ?? changes.generalIds, identity.generalId);
|
||||
const frontStatusGeneralChanged =
|
||||
changes.frontStatusGeneralIds !== undefined
|
||||
? changes.frontStatusGeneralIds.length > 0
|
||||
: changes.contactsChanged;
|
||||
const ownFrontStatusActorChanged = contains(changes.frontStatusActorIds ?? [], identity.generalId);
|
||||
const ownLobbyGeneralChanged = contains(changes.lobbyGeneralIds ?? changes.generalIds, identity.generalId);
|
||||
const lobbyChanged = changes.lobbyChanged ?? changes.contactsChanged;
|
||||
const entityContextChanged = ownGeneralChanged || ownCityChanged || ownNationChanged;
|
||||
const mapEntitiesChanged =
|
||||
(changes.mapCityIds ?? changes.cityIds).length > 0 ||
|
||||
(changes.mapNationIds ?? changes.nationIds).length > 0;
|
||||
const commandEntitiesChanged = changes.cityIds.length > 0 || changes.nationIds.length > 0;
|
||||
|
||||
return {
|
||||
context: entityContextChanged,
|
||||
lobby: changes.worldChanged || lobbyChanged || ownLobbyGeneralChanged,
|
||||
map: changes.worldChanged || mapEntitiesChanged || ownGeneralMapChanged,
|
||||
commands: changes.worldChanged || commandEntitiesChanged || ownGeneralChanged,
|
||||
contacts: changes.contactsChanged,
|
||||
boardAccess: ownGeneralChanged || ownNationChanged,
|
||||
reservedTurns: contains(changes.reservedGeneralIds, identity.generalId),
|
||||
records:
|
||||
changes.globalRecordsChanged ||
|
||||
changes.worldHistoryChanged ||
|
||||
contains(changes.recordGeneralIds, identity.generalId),
|
||||
// lastTurnTime is intentionally excluded. This slice contains the
|
||||
// nation notice/vote/presence model and only follows related changes.
|
||||
frontStatus:
|
||||
Boolean(changes.frontStatusChanged) ||
|
||||
frontStatusGeneralChanged ||
|
||||
ownFrontStatusNationChanged ||
|
||||
ownFrontStatusActorChanged,
|
||||
};
|
||||
};
|
||||
): DashboardRefreshPlan => resolveRealtimeReadModelInvalidation(changes, identity);
|
||||
|
||||
type TimerHandle = ReturnType<typeof setTimeout>;
|
||||
|
||||
export interface MergedReadModelRefreshQueue {
|
||||
request(changes: RealtimeReadModelChanges): void;
|
||||
request(invalidation: RealtimeReadModelInvalidation): void;
|
||||
cancelPending(): void;
|
||||
}
|
||||
|
||||
export const createMergedReadModelRefreshQueue = (
|
||||
refresh: (changes: RealtimeReadModelChanges) => Promise<void>,
|
||||
refresh: (invalidation: RealtimeReadModelInvalidation) => Promise<void>,
|
||||
options: {
|
||||
minIntervalMs?: number;
|
||||
now?: () => number;
|
||||
@@ -91,7 +35,7 @@ export const createMergedReadModelRefreshQueue = (
|
||||
const now = options.now ?? Date.now;
|
||||
const setTimer = options.setTimer ?? ((callback, delayMs) => setTimeout(callback, delayMs));
|
||||
const clearTimer = options.clearTimer ?? ((handle) => clearTimeout(handle));
|
||||
let pending = createEmptyRealtimeReadModelChanges();
|
||||
let pending = createEmptyRealtimeReadModelInvalidation();
|
||||
let hasPending = false;
|
||||
let running = false;
|
||||
let timer: TimerHandle | null = null;
|
||||
@@ -108,7 +52,7 @@ export const createMergedReadModelRefreshQueue = (
|
||||
return;
|
||||
}
|
||||
const next = pending;
|
||||
pending = createEmptyRealtimeReadModelChanges();
|
||||
pending = createEmptyRealtimeReadModelInvalidation();
|
||||
hasPending = false;
|
||||
running = true;
|
||||
lastStartedAt = now();
|
||||
@@ -120,14 +64,14 @@ export const createMergedReadModelRefreshQueue = (
|
||||
};
|
||||
|
||||
return {
|
||||
request: (changes) => {
|
||||
pending = hasPending ? mergeRealtimeReadModelChanges(pending, changes) : changes;
|
||||
request: (invalidation) => {
|
||||
pending = hasPending ? mergeRealtimeReadModelInvalidations(pending, invalidation) : invalidation;
|
||||
hasPending = true;
|
||||
schedule();
|
||||
},
|
||||
cancelPending: () => {
|
||||
hasPending = false;
|
||||
pending = createEmptyRealtimeReadModelChanges();
|
||||
pending = createEmptyRealtimeReadModelInvalidation();
|
||||
if (timer !== null) {
|
||||
clearTimer(timer);
|
||||
timer = null;
|
||||
|
||||
Reference in New Issue
Block a user