Refactor user storage and update

Rename accounts to users to be consistent with the new naming scheme
where account only referes to the logged in user of the session and
implement live updates of users via a user store which listens for
updates from the event stream.
This commit is contained in:
Hornwitser 2025-06-23 00:17:22 +02:00
parent 6336ccdb96
commit 3be7f8be05
24 changed files with 331 additions and 182 deletions

View file

@ -1,4 +1,4 @@
import { readAccounts } from "~/server/database";
import { readUsers } from "~/server/database";
import { canSeeCrew } from "./utils/schedule";
import type { ApiAccount, ApiEvent } from "~/shared/types/api";
@ -66,16 +66,16 @@ export function cancelSessionStreams(sessionId: number) {
}
const encodeEventCache = new WeakMap<ApiEvent, Map<ApiAccount["type"] | undefined, string>>();
function encodeEvent(event: ApiEvent, accountType: ApiAccount["type"] | undefined) {
function encodeEvent(event: ApiEvent, userType: ApiAccount["type"] | undefined) {
const cache = encodeEventCache.get(event);
const cacheEntry = cache?.get(accountType);
const cacheEntry = cache?.get(userType);
if (cacheEntry) {
return cacheEntry;
}
let data: string;
if (event.type === "schedule-update") {
if (!canSeeCrew(accountType)) {
if (!canSeeCrew(userType)) {
event = {
type: event.type,
updatedFrom: event.updatedFrom,
@ -83,14 +83,29 @@ function encodeEvent(event: ApiEvent, accountType: ApiAccount["type"] | undefine
};
}
data = JSON.stringify(event);
} else if (event.type === "user-update") {
if (
!canSeeCrew(userType)
|| !event.data.deleted && event.data.type === "anonymous" && !canSeeAnonymous(userType)
) {
event = {
type: event.type,
data: {
id: event.data.id,
updatedAt: event.data.updatedAt,
deleted: true,
}
}
}
data = JSON.stringify(event);
} else {
throw Error(`encodeEvent cannot encode ${event.type} event`);
}
if (cache) {
cache.set(accountType, data);
cache.set(userType, data);
} else {
encodeEventCache.set(event, new Map([[accountType, data]]));
encodeEventCache.set(event, new Map([[userType, data]]));
}
return data;
}
@ -101,20 +116,20 @@ export async function broadcastEvent(event: ApiEvent) {
if (!streams.size) {
return;
}
const accounts = await readAccounts();
const users = await readUsers();
for (const [stream, streamData] of streams) {
// Account events are specially handled and only sent to the account they belong to.
// Account events are specially handled and only sent to the user they belong to.
if (event.type === "account-update") {
if (streamData.accountId === event.data.id) {
sendMessage(stream, `id: ${id}\nevent: update\ndata: ${JSON.stringify(event)}\n\n`);
}
} else {
let accountType: ApiAccount["type"] | undefined;
let userType: ApiAccount["type"] | undefined;
if (streamData.accountId !== undefined) {
accountType = accounts.find(a => a.id === streamData.accountId)?.type
userType = users.find(a => a.id === streamData.accountId)?.type
}
const data = encodeEvent(event, accountType)
const data = encodeEvent(event, userType)
sendMessage(stream, `id: ${id}\nevent: update\ndata: ${data}\n\n`);
}
}