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,16 +1,18 @@
import { readSchedule, writeSchedule } from '~/server/database';
import { readSchedule, ServerUser, writeSchedule } from '~/server/database';
import { broadcastEvent } from '~/server/streams';
import type { ApiAccount, ApiSchedule } from '~/shared/types/api';
export async function updateScheduleInterestedCounts(accounts: ApiAccount[]) {
export async function updateScheduleInterestedCounts(users: ServerUser[]) {
const eventCounts = new Map<number, number>();
const eventSlotCounts = new Map<number, number>();
for (const account of accounts) {
if (account.interestedEventIds)
for (const id of account.interestedEventIds)
for (const user of users) {
if (user.deleted)
continue;
if (user.interestedEventIds)
for (const id of user.interestedEventIds)
eventCounts.set(id, (eventCounts.get(id) ?? 0) + 1);
if (account.interestedEventSlotIds)
for (const id of account.interestedEventSlotIds)
if (user.interestedEventSlotIds)
for (const id of user.interestedEventSlotIds)
eventSlotCounts.set(id, (eventSlotCounts.get(id) ?? 0) + 1);
}
@ -58,8 +60,12 @@ export async function updateScheduleInterestedCounts(accounts: ApiAccount[]) {
});
}
export function canSeeCrew(accountType: string | undefined) {
return accountType === "crew" || accountType === "admin";
export function canSeeAnonymous(userType: string | undefined) {
return userType === "admin";
}
export function canSeeCrew(userType: string | undefined) {
return userType === "crew" || userType === "admin";
}
/** Filters out crew visible only parts of schedule */

View file

@ -1,5 +1,5 @@
import type { H3Event } from "h3";
import { nextSessionId, readSessions, readSubscriptions, type ServerSession, writeSessions, writeSubscriptions } from "~/server/database";
import { nextSessionId, readSessions, readSubscriptions, type ServerSession, type ServerUser, writeSessions, writeSubscriptions } from "~/server/database";
const oneYearSeconds = 365 * 24 * 60 * 60;
@ -34,12 +34,12 @@ export async function clearServerSession(event: H3Event) {
deleteCookie(event, "session");
}
export async function setServerSession(event: H3Event, accountId: number) {
export async function setServerSession(event: H3Event, account: ServerUser) {
const sessions = await readSessions();
await clearServerSessionInternal(event, sessions);
const newSession: ServerSession = {
accountId,
account,
id: await nextSessionId(),
};