From 680056cfd9ccac268b51bf6ae36587298da33d33 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Mon, 7 Aug 2023 18:06:25 -0400 Subject: [PATCH] fix: renew cache ttl and purge with account deletion --- .../backend/src/models/repositories/user.ts | 19 +++++++++++-------- .../src/queue/processors/db/delete-account.ts | 4 ++++ packages/backend/src/server/index.ts | 16 +++++++++------- 3 files changed, 24 insertions(+), 15 deletions(-) diff --git a/packages/backend/src/models/repositories/user.ts b/packages/backend/src/models/repositories/user.ts index cd21220b16..0fa8fdf189 100644 --- a/packages/backend/src/models/repositories/user.ts +++ b/packages/backend/src/models/repositories/user.ts @@ -384,14 +384,17 @@ export const UserRepository = db.getRepository(User).extend({ ); const userId = typeof src === "object" ? src.id : src; - const user = await userDenormalizedCache.fetch(userId, () => - this.findOneOrFail({ - where: { id: userId }, - relations: { - avatar: true, - banner: true, - }, - }), + const user = await userDenormalizedCache.fetch( + userId, + () => + this.findOneOrFail({ + where: { id: userId }, + relations: { + avatar: true, + banner: true, + }, + }), + true, ); const meId = me ? me.id : null; diff --git a/packages/backend/src/queue/processors/db/delete-account.ts b/packages/backend/src/queue/processors/db/delete-account.ts index 1cd7642ba5..1ab7e58023 100644 --- a/packages/backend/src/queue/processors/db/delete-account.ts +++ b/packages/backend/src/queue/processors/db/delete-account.ts @@ -8,6 +8,8 @@ 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 { acctToUserIdCache, userByIdCache, userDenormalizedCache } from "@/services/user-cache.js"; +import config from "@/config/index.js"; const logger = queueLogger.createSubLogger("delete-account"); @@ -98,6 +100,8 @@ export async function deleteAccount( // nop } else { await Users.delete(job.data.user.id); + await userDenormalizedCache.delete(job.data.user.id); + await userByIdCache.delete(job.data.user.id); } return "Account deleted"; diff --git a/packages/backend/src/server/index.ts b/packages/backend/src/server/index.ts index 341d63e529..841f0baa69 100644 --- a/packages/backend/src/server/index.ts +++ b/packages/backend/src/server/index.ts @@ -114,7 +114,7 @@ router.use(wellKnown.routes()); router.get("/avatar/@:acct", async (ctx) => { const { username, host } = Acct.parse(ctx.params.acct); const userId = await acctToUserIdCache.fetchMaybe( - `${username}@${host ?? config.host}`, + `${username.toLowerCase()}@${host ?? config.host}`, () => Users.findOne({ where: { @@ -123,17 +123,19 @@ router.get("/avatar/@:acct", async (ctx) => { isSuspended: false, }, }).then((user) => user?.id ?? undefined), - true, ); if (!userId) { ctx.redirect("/static-assets/user-unknown.png"); } else { - const user = await userDenormalizedCache.fetch(userId, () => - Users.findOneOrFail({ - relations: { avatar: true, banner: true }, - where: { id: userId }, - }), + const user = await userDenormalizedCache.fetch( + userId, + () => + Users.findOneOrFail({ + relations: { avatar: true, banner: true }, + where: { id: userId }, + }), + true, ); ctx.redirect(Users.getAvatarUrlSync(user)); }