2025-06-30 18:58:24 +02:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
*/
|
2025-03-07 22:28:55 +01:00
|
|
|
import {
|
2025-06-23 00:17:22 +02:00
|
|
|
readUsers, readSessions, readSubscriptions,
|
|
|
|
writeUsers, writeSessions, writeSubscriptions,
|
2025-06-23 12:48:09 +02:00
|
|
|
} from "~/server/database";
|
2025-06-23 00:17:22 +02:00
|
|
|
import { broadcastEvent, cancelAccountStreams } from "~/server/streams";
|
2025-03-07 22:28:55 +01:00
|
|
|
|
|
|
|
export default defineEventHandler(async (event) => {
|
2025-07-07 22:42:49 +02:00
|
|
|
const serverSession = await requireServerSessionWithUser(event);
|
2025-06-23 00:17:22 +02:00
|
|
|
let users = await readUsers();
|
2025-03-07 22:28:55 +01:00
|
|
|
|
2025-07-08 15:43:14 +02:00
|
|
|
// Expire sessions for this user
|
|
|
|
const expiredSessionIds = new Set<number>();
|
2025-03-07 22:28:55 +01:00
|
|
|
let sessions = await readSessions();
|
2025-07-08 15:43:14 +02:00
|
|
|
const nowMs = Date.now();
|
|
|
|
for (const session of sessions) {
|
|
|
|
if (
|
|
|
|
!session.finished
|
|
|
|
&& session.successor !== undefined
|
|
|
|
&& session.expiresAtMs < nowMs
|
|
|
|
&& session.accountId === serverSession.accountId
|
|
|
|
) {
|
|
|
|
session.expiresAtMs = nowMs;
|
|
|
|
broadcastEvent({
|
|
|
|
type: "session-expired",
|
|
|
|
sessionId: session.id,
|
|
|
|
});
|
|
|
|
expiredSessionIds.add(session.id);
|
2025-03-07 22:28:55 +01:00
|
|
|
}
|
2025-07-08 15:43:14 +02:00
|
|
|
}
|
2025-07-07 22:42:49 +02:00
|
|
|
cancelAccountStreams(serverSession.accountId);
|
2025-03-07 22:28:55 +01:00
|
|
|
await writeSessions(sessions);
|
|
|
|
await deleteCookie(event, "session");
|
|
|
|
|
2025-06-23 00:17:22 +02:00
|
|
|
// Remove subscriptions for this user
|
2025-03-07 22:28:55 +01:00
|
|
|
let subscriptions = await readSubscriptions();
|
|
|
|
subscriptions = subscriptions.filter(
|
2025-07-08 15:43:14 +02:00
|
|
|
subscription => !expiredSessionIds.has(subscription.sessionId)
|
2025-03-07 22:28:55 +01:00
|
|
|
);
|
|
|
|
await writeSubscriptions(subscriptions);
|
|
|
|
|
2025-06-23 00:17:22 +02:00
|
|
|
// Remove the user
|
2025-07-07 22:42:49 +02:00
|
|
|
const account = users.find(user => user.id === serverSession.accountId)!;
|
2025-07-08 15:43:14 +02:00
|
|
|
const now = new Date(nowMs).toISOString();
|
2025-06-23 00:17:22 +02:00
|
|
|
account.deleted = true;
|
|
|
|
account.updatedAt = now;
|
|
|
|
await writeUsers(users);
|
|
|
|
await broadcastEvent({
|
|
|
|
type: "user-update",
|
|
|
|
data: {
|
|
|
|
id: account.id,
|
|
|
|
updatedAt: now,
|
|
|
|
deleted: true,
|
|
|
|
}
|
|
|
|
});
|
2025-03-07 22:28:55 +01:00
|
|
|
|
|
|
|
// Update Schedule counts.
|
2025-06-23 00:17:22 +02:00
|
|
|
await updateScheduleInterestedCounts(users);
|
2025-03-07 22:28:55 +01:00
|
|
|
})
|