From c6dee2da0919038b49398be4c3a60f9a36358036 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 5 Sep 2023 20:15:23 +0200 Subject: [PATCH] Purge deleted users from user lookup caches --- .../src/queue/processors/db/delete-account.ts | 3 +++ .../backend/src/server/api/stream/types.ts | 6 ++++++ packages/backend/src/services/user-cache.ts | 21 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/packages/backend/src/queue/processors/db/delete-account.ts b/packages/backend/src/queue/processors/db/delete-account.ts index adb93484b..cca7750cf 100644 --- a/packages/backend/src/queue/processors/db/delete-account.ts +++ b/packages/backend/src/queue/processors/db/delete-account.ts @@ -8,6 +8,7 @@ import { MoreThan } from "typeorm"; import { deleteFileSync } from "@/services/drive/delete-file.js"; import { sendEmail } from "@/services/send-email.js"; import meilisearch from "@/db/meilisearch.js"; +import { publishInternalEvent } from "@/services/stream.js"; const logger = queueLogger.createSubLogger("delete-account"); @@ -18,6 +19,7 @@ export async function deleteAccount( const user = await Users.findOneBy({ id: job.data.user.id }); if (!user) return; + const isLocal = Users.isLocalUser(user); { // Delete notes @@ -98,6 +100,7 @@ export async function deleteAccount( // nop } else { await Users.delete(job.data.user.id); + publishInternalEvent(isLocal ? "localUserDeleted" : "remoteUserDeleted", { id: user.id }); } return "Account deleted"; diff --git a/packages/backend/src/server/api/stream/types.ts b/packages/backend/src/server/api/stream/types.ts index 91095a46d..601c56f41 100644 --- a/packages/backend/src/server/api/stream/types.ts +++ b/packages/backend/src/server/api/stream/types.ts @@ -38,9 +38,15 @@ export interface InternalStreamTypes { localUserUpdated: { id: User["id"]; }; + localUserDeleted: { + id: User["id"]; + }; remoteUserUpdated: { id: User["id"]; }; + remoteUserDeleted: { + id: User["id"]; + }; webhookCreated: Webhook; webhookDeleted: Webhook; webhookUpdated: Webhook; diff --git a/packages/backend/src/services/user-cache.ts b/packages/backend/src/services/user-cache.ts index ed700185d..a5e12e34a 100644 --- a/packages/backend/src/services/user-cache.ts +++ b/packages/backend/src/services/user-cache.ts @@ -27,6 +27,19 @@ subscriber.on("message", async (_, data) => { if (obj.channel === "internal") { const { type, body } = obj.message; switch (type) { + case "localUserDeleted": { + await userByIdCache.delete(body.id); + await localUserByIdCache.delete(body.id); + const toDelete = Array.from(await localUserByNativeTokenCache.getAll()) + .filter((v) => v[1]?.id === body.id) + .map((v) => v[0]); + await localUserByNativeTokenCache.delete(...toDelete); + const uriCacheToDelete = Array.from(await uriPersonCache.getAll()) + .filter((v) => v[1]?.id === body.id) + .map((v) => v[0]); + await uriPersonCache.delete(...uriCacheToDelete); + break; + } case "localUserUpdated": { await userByIdCache.delete(body.id); await localUserByIdCache.delete(body.id); @@ -55,6 +68,14 @@ subscriber.on("message", async (_, data) => { } break; } + case "remoteUserDeleted": { + await userByIdCache.delete(body.id); + const toDelete = Array.from(await uriPersonCache.getAll()) + .filter((v) => v[1]?.id === body.id) + .map((v) => v[0]); + await uriPersonCache.delete(...toDelete); + break; + } case "userTokenRegenerated": { const user = (await Users.findOneByOrFail({ id: body.id,