owltide/server/api/auth/account.delete.ts

54 lines
1.5 KiB
TypeScript
Raw Normal View History

/*
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
SPDX-License-Identifier: AGPL-3.0-or-later
*/
2025-03-07 22:28:55 +01:00
import {
readUsers, readSessions, readSubscriptions,
writeUsers, writeSessions, writeSubscriptions,
} from "~/server/database";
import { broadcastEvent, cancelAccountStreams } from "~/server/streams";
2025-03-07 22:28:55 +01:00
export default defineEventHandler(async (event) => {
const serverSession = await requireServerSession(event);
let users = await readUsers();
2025-03-07 22:28:55 +01: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 => {
if (session.account.id === serverSession.account.id) {
2025-03-07 22:28:55 +01:00
removedSessionIds.add(session.id);
return false;
}
return true;
});
cancelAccountStreams(serverSession.account.id);
2025-03-07 22:28:55 +01:00
await writeSessions(sessions);
await deleteCookie(event, "session");
// 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);
// 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.
await updateScheduleInterestedCounts(users);
2025-03-07 22:28:55 +01:00
})