From 0b23a5adaaf26ca549bc2c3a034da8f2bf572eb9 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Wed, 20 Sep 2023 09:27:28 -0400 Subject: [PATCH] fix: properly update cache --- .../backend/src/server/api/authenticate.ts | 12 ++++----- .../api/endpoints/admin/moderators/add.ts | 8 ++++++ .../api/endpoints/admin/moderators/remove.ts | 8 ++++++ .../api/endpoints/admin/silence-user.ts | 8 ++++++ .../api/endpoints/admin/suspend-user.ts | 8 ++++++ .../api/endpoints/admin/unsilence-user.ts | 8 ++++++ .../api/endpoints/admin/unsuspend-user.ts | 8 ++++++ packages/backend/src/server/web/feed.ts | 25 +++++++++++++++---- 8 files changed, 74 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/server/api/authenticate.ts b/packages/backend/src/server/api/authenticate.ts index 460a0ce84b..e0dc01fe77 100644 --- a/packages/backend/src/server/api/authenticate.ts +++ b/packages/backend/src/server/api/authenticate.ts @@ -48,7 +48,10 @@ export default async ( if (isNativeToken(token)) { const user = await localUserByNativeTokenCache.fetch( token, - () => Users.findOneBy({ token }) as Promise, + () => + Users.findOneBy({ + token: token as string, + }) as Promise, true, ); @@ -83,14 +86,11 @@ export default async ( Users.findOneBy({ id: accessToken.userId, }) as Promise, - true, ); if (accessToken.appId) { - const app = await appCache.fetch( - accessToken.appId, - () => Apps.findOneByOrFail({ id: accessToken.appId! }), - true, + const app = await appCache.fetch(accessToken.appId, () => + Apps.findOneByOrFail({ id: accessToken.appId as string }), ); return [ diff --git a/packages/backend/src/server/api/endpoints/admin/moderators/add.ts b/packages/backend/src/server/api/endpoints/admin/moderators/add.ts index 478f2661b6..f9d230bd74 100644 --- a/packages/backend/src/server/api/endpoints/admin/moderators/add.ts +++ b/packages/backend/src/server/api/endpoints/admin/moderators/add.ts @@ -1,6 +1,11 @@ import define from "../../../define.js"; import { Users } from "@/models/index.js"; import { publishInternalEvent } from "@/services/stream.js"; +import { + localUserByIdCache, + userByIdCache, + userDenormalizedCache, +} from "@/services/user-cache.js"; export const meta = { tags: ["admin"], @@ -28,6 +33,9 @@ export default define(meta, paramDef, async (ps) => { throw new Error("cannot mark as moderator if admin user"); } + await userDenormalizedCache.delete(user.id); + await userByIdCache.delete(user.id); + await localUserByIdCache.delete(user.id); await Users.update(user.id, { isModerator: true, }); diff --git a/packages/backend/src/server/api/endpoints/admin/moderators/remove.ts b/packages/backend/src/server/api/endpoints/admin/moderators/remove.ts index a43cc0cbe1..cad6aaa1e6 100644 --- a/packages/backend/src/server/api/endpoints/admin/moderators/remove.ts +++ b/packages/backend/src/server/api/endpoints/admin/moderators/remove.ts @@ -1,6 +1,11 @@ import define from "../../../define.js"; import { Users } from "@/models/index.js"; import { publishInternalEvent } from "@/services/stream.js"; +import { + localUserByIdCache, + userByIdCache, + userDenormalizedCache, +} from "@/services/user-cache.js"; export const meta = { tags: ["admin"], @@ -24,6 +29,9 @@ export default define(meta, paramDef, async (ps) => { throw new Error("user not found"); } + await userDenormalizedCache.delete(user.id); + await userByIdCache.delete(user.id); + await localUserByIdCache.delete(user.id); await Users.update(user.id, { isModerator: false, }); diff --git a/packages/backend/src/server/api/endpoints/admin/silence-user.ts b/packages/backend/src/server/api/endpoints/admin/silence-user.ts index a61823297b..22b53806e3 100644 --- a/packages/backend/src/server/api/endpoints/admin/silence-user.ts +++ b/packages/backend/src/server/api/endpoints/admin/silence-user.ts @@ -2,6 +2,11 @@ import define from "../../define.js"; import { Users } from "@/models/index.js"; import { insertModerationLog } from "@/services/insert-moderation-log.js"; import { publishInternalEvent } from "@/services/stream.js"; +import { + localUserByIdCache, + userByIdCache, + userDenormalizedCache, +} from "@/services/user-cache.js"; export const meta = { tags: ["admin"], @@ -29,6 +34,9 @@ export default define(meta, paramDef, async (ps, me) => { throw new Error("cannot silence admin"); } + await userDenormalizedCache.delete(user.id); + await userByIdCache.delete(user.id); + await localUserByIdCache.delete(user.id); await Users.update(user.id, { isSilenced: true, }); diff --git a/packages/backend/src/server/api/endpoints/admin/suspend-user.ts b/packages/backend/src/server/api/endpoints/admin/suspend-user.ts index 681bd4a49b..87552fccf8 100644 --- a/packages/backend/src/server/api/endpoints/admin/suspend-user.ts +++ b/packages/backend/src/server/api/endpoints/admin/suspend-user.ts @@ -7,6 +7,11 @@ import { doPostSuspend } from "@/services/suspend-user.js"; import { publishUserEvent } from "@/services/stream.js"; import { scyllaClient } from "@/db/scylla.js"; import { SuspendedUsersCache } from "@/misc/cache.js"; +import { + localUserByIdCache, + userByIdCache, + userDenormalizedCache, +} from "@/services/user-cache.js"; export const meta = { tags: ["admin"], @@ -38,6 +43,8 @@ export default define(meta, paramDef, async (ps, me) => { throw new Error("cannot suspend moderator"); } + await userDenormalizedCache.delete(user.id); + await userByIdCache.delete(user.id); await SuspendedUsersCache.init().then((cache) => cache.add(user.id)); await Users.update(user.id, { isSuspended: true, @@ -49,6 +56,7 @@ export default define(meta, paramDef, async (ps, me) => { // Terminate streaming if (Users.isLocalUser(user)) { + await localUserByIdCache.delete(user.id); publishUserEvent(user.id, "terminate", {}); } diff --git a/packages/backend/src/server/api/endpoints/admin/unsilence-user.ts b/packages/backend/src/server/api/endpoints/admin/unsilence-user.ts index 6a01b8e8d3..a0410c60cf 100644 --- a/packages/backend/src/server/api/endpoints/admin/unsilence-user.ts +++ b/packages/backend/src/server/api/endpoints/admin/unsilence-user.ts @@ -2,6 +2,11 @@ import define from "../../define.js"; import { Users } from "@/models/index.js"; import { insertModerationLog } from "@/services/insert-moderation-log.js"; import { publishInternalEvent } from "@/services/stream.js"; +import { + localUserByIdCache, + userByIdCache, + userDenormalizedCache, +} from "@/services/user-cache.js"; export const meta = { tags: ["admin"], @@ -25,6 +30,9 @@ export default define(meta, paramDef, async (ps, me) => { throw new Error("user not found"); } + await userDenormalizedCache.delete(user.id); + await userByIdCache.delete(user.id); + await localUserByIdCache.delete(user.id); await Users.update(user.id, { isSilenced: false, }); diff --git a/packages/backend/src/server/api/endpoints/admin/unsuspend-user.ts b/packages/backend/src/server/api/endpoints/admin/unsuspend-user.ts index bc50d49a4f..cd8a8d9f0a 100644 --- a/packages/backend/src/server/api/endpoints/admin/unsuspend-user.ts +++ b/packages/backend/src/server/api/endpoints/admin/unsuspend-user.ts @@ -3,6 +3,11 @@ import define from "../../define.js"; import { Users } from "@/models/index.js"; import { insertModerationLog } from "@/services/insert-moderation-log.js"; import { doPostUnsuspend } from "@/services/unsuspend-user.js"; +import { + localUserByIdCache, + userByIdCache, + userDenormalizedCache, +} from "@/services/user-cache.js"; export const meta = { tags: ["admin"], @@ -26,6 +31,9 @@ export default define(meta, paramDef, async (ps, me) => { throw new Error("user not found"); } + await userDenormalizedCache.delete(user.id); + await userByIdCache.delete(user.id); + await localUserByIdCache.delete(user.id); await SuspendedUsersCache.init().then((cache) => cache.delete(user.id)); await Users.update(user.id, { isSuspended: false, diff --git a/packages/backend/src/server/web/feed.ts b/packages/backend/src/server/web/feed.ts index 2aa488b79a..aecdf028f6 100644 --- a/packages/backend/src/server/web/feed.ts +++ b/packages/backend/src/server/web/feed.ts @@ -35,11 +35,26 @@ export default async function ( searchCriteria.replyId = IsNull(); } - const notes = await Notes.find({ - where: searchCriteria, - order: { createdAt: -1 }, - take: history, - }); + let notes: Note[] = []; + if (scyllaClient) { + const query = [prepared.note.select.byUserId, "LIMIT ?"]; + notes = await scyllaClient + .execute( + query.join(" "), + [user.id, Math.min(history, config.scylla?.queryLimit ?? 100)], + { prepare: true }, + ) + .then((result) => result.rows.map(parseScyllaNote)); + notes = notes.filter( + (note) => !(!renotes && note.renoteId) && !(!replies && note.replyId), + ); + } else { + notes = await Notes.find({ + where: searchCriteria, + order: { createdAt: -1 }, + take: history, + }); + } const feed = new Feed({ id: author.link,