From 35f7bde1a41d9489c4a20143085ef37e9658f1d2 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Tue, 5 Sep 2023 22:56:35 +0200 Subject: [PATCH] Clear public key cache on user deletion --- .../src/remote/activitypub/db-resolver.ts | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/packages/backend/src/remote/activitypub/db-resolver.ts b/packages/backend/src/remote/activitypub/db-resolver.ts index a710b9f11..52626f399 100644 --- a/packages/backend/src/remote/activitypub/db-resolver.ts +++ b/packages/backend/src/remote/activitypub/db-resolver.ts @@ -18,6 +18,7 @@ import { uriPersonCache, userByIdCache } from "@/services/user-cache.js"; import type { IObject } from "./type.js"; import { getApId } from "./type.js"; import { resolvePerson } from "./models/person.js"; +import {redisClient, subscriber} from "@/db/redis.js"; const publicKeyCache = new Cache("publicKey", 60 * 30); const publicKeyByUserIdCache = new Cache( @@ -204,3 +205,28 @@ export default class DbResolver { }; } } + +subscriber.on("message", async (_, data) => { + const obj = JSON.parse(data); + + if (obj.channel === "internal") { + const { type, body } = obj.message; + switch (type) { + case "remoteUserDeleted": + case "localUserDeleted": { + const toDelete = Array.from(await publicKeyByUserIdCache.getAll()) + .filter((v) => v[1]?.userId === body.id) + .map((v) => v[0]); + const toDeleteKey = Array.from(await publicKeyCache.getAll()) + .filter((v) => v[1]?.userId === body.id) + .map((v) => v[0]); + await publicKeyByUserIdCache.delete(...toDelete); + await publicKeyCache.delete(...toDeleteKey); + break; + } + default: + break; + } + } +}); +