fix: renew cache ttl and purge with account deletion

This commit is contained in:
Namekuji 2023-08-07 18:06:25 -04:00
parent 9ff89d95cd
commit 680056cfd9
No known key found for this signature in database
GPG key ID: 1D62332C07FBA532
3 changed files with 24 additions and 15 deletions

View file

@ -384,7 +384,9 @@ export const UserRepository = db.getRepository(User).extend({
);
const userId = typeof src === "object" ? src.id : src;
const user = await userDenormalizedCache.fetch(userId, () =>
const user = await userDenormalizedCache.fetch(
userId,
() =>
this.findOneOrFail({
where: { id: userId },
relations: {
@ -392,6 +394,7 @@ export const UserRepository = db.getRepository(User).extend({
banner: true,
},
}),
true,
);
const meId = me ? me.id : null;

View file

@ -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";

View file

@ -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, () =>
const user = await userDenormalizedCache.fetch(
userId,
() =>
Users.findOneOrFail({
relations: { avatar: true, banner: true },
where: { id: userId },
}),
true,
);
ctx.redirect(Users.getAvatarUrlSync(user));
}