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-06-09 16:51:05 +02:00
|
|
|
const serverSession = await requireServerSession(event);
|
2025-06-23 00:17:22 +02:00
|
|
|
let users = await readUsers();
|
2025-03-07 22:28:55 +01:00
|
|
|
|
2025-06-23 00:17:22 +02:00
|
|
|
// Remove sessions for this user
|
2025-03-07 22:28:55 +01:00
|
|
|
const removedSessionIds = new Set<number>();
|
|
|
|
let sessions = await readSessions();
|
|
|
|
sessions = sessions.filter(session => {
|
2025-06-23 00:17:22 +02:00
|
|
|
if (session.account.id === serverSession.account.id) {
|
2025-03-07 22:28:55 +01:00
|
|
|
removedSessionIds.add(session.id);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
});
|
2025-06-23 00:17:22 +02:00
|
|
|
cancelAccountStreams(serverSession.account.id);
|
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(
|
|
|
|
subscription => !removedSessionIds.has(subscription.sessionId)
|
|
|
|
);
|
|
|
|
await writeSubscriptions(subscriptions);
|
|
|
|
|
2025-06-23 00:17:22 +02:00
|
|
|
// Remove the user
|
|
|
|
const account = users.find(user => user.id === serverSession.account.id)!;
|
|
|
|
const now = new Date().toISOString();
|
|
|
|
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
|
|
|
})
|