Purge deleted users from user lookup caches

This commit is contained in:
Laura Hausmann 2023-09-05 20:15:23 +02:00
parent 9e5f96b9ad
commit c6dee2da09
No known key found for this signature in database
GPG Key ID: D044E84C5BE01605
3 changed files with 30 additions and 0 deletions

View File

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

View File

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

View File

@ -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,