From 9d30fd8f923f157eee588c6207628db9e1ff7391 Mon Sep 17 00:00:00 2001 From: Namekuji Date: Mon, 7 Aug 2023 07:13:16 -0400 Subject: [PATCH] perf: pass muted instances as a param --- packages/backend/src/db/scylla.ts | 41 +++++++++++-------- .../server/api/endpoints/notes/timeline.ts | 10 ++++- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/packages/backend/src/db/scylla.ts b/packages/backend/src/db/scylla.ts index 1577dbc85c..0fc19c4eb6 100644 --- a/packages/backend/src/db/scylla.ts +++ b/packages/backend/src/db/scylla.ts @@ -18,6 +18,7 @@ import { getTimestamp } from "@/misc/gen-id.js"; import Logger from "@/services/logger.js"; import { UserProfiles } from "@/models/index.js"; import { getWordHardMute } from "@/misc/check-word-mute.js"; +import type { UserProfile } from "@/models/entities/user-profile.js"; function newClient(): Client | null { if (!config.scylla) { @@ -355,8 +356,9 @@ export async function filterVisibility( if (followingIds) { followings = followingIds; } else { - const cache = await LocalFollowingsCache.init(user.id); - followings = await cache.getAll(); + followings = await LocalFollowingsCache.init(user.id).then((cache) => + cache.getAll(), + ); } filtered = filtered.filter( @@ -389,8 +391,9 @@ export async function filterChannel( if (followingIds) { followings = followingIds; } else { - const cache = await ChannelFollowingsCache.init(user.id); - followings = await cache.getAll(); + followings = await ChannelFollowingsCache.init(user.id).then((cache) => + cache.getAll(), + ); } filtered = filtered.filter( (note) => !note.channelId || followings.includes(note.channelId), @@ -429,32 +432,38 @@ export async function filterMutedUser( notes: ScyllaNote[], user: { id: User["id"] }, mutedIds?: User["id"][], + mutedInstances?: UserProfile["mutedInstances"], exclude?: User, ): Promise { let ids: User["id"][]; + let instances: UserProfile["mutedInstances"]; if (mutedIds) { ids = mutedIds; } else { - const userCache = await UserMutingsCache.init(user.id); - ids = await userCache.getAll(); + ids = await UserMutingsCache.init(user.id).then((cache) => cache.getAll()); + } + + if (mutedInstances) { + instances = mutedInstances; + } else { + instances = await InstanceMutingsCache.init(user.id).then((cache) => + cache.getAll(), + ); } if (exclude) { ids = ids.filter((id) => id !== exclude.id); } - const instanceCache = await InstanceMutingsCache.init(user.id); - const mutedInstances = await instanceCache.getAll(); - return notes.filter( (note) => !ids.includes(note.userId) && !(note.replyUserId && ids.includes(note.replyUserId)) && !(note.renoteUserId && ids.includes(note.renoteUserId)) && - !(note.userHost && mutedInstances.includes(note.userHost)) && - !(note.replyUserHost && mutedInstances.includes(note.replyUserHost)) && - !(note.renoteUserHost && mutedInstances.includes(note.renoteUserHost)), + !(note.userHost && instances.includes(note.userHost)) && + !(note.replyUserHost && instances.includes(note.replyUserHost)) && + !(note.renoteUserHost && instances.includes(note.renoteUserHost)), ); } @@ -493,8 +502,7 @@ export async function filterBlockedUser( if (blockerIds) { ids = blockerIds; } else { - const cache = await UserBlockedCache.init(user.id); - ids = await cache.getAll(); + ids = await UserBlockedCache.init(user.id).then((cache) => cache.getAll()); } return notes.filter( @@ -515,8 +523,9 @@ export async function filterMutedRenotes( if (muteeIds) { ids = muteeIds; } else { - const cache = await RenoteMutingsCache.init(user.id); - ids = await cache.getAll(); + ids = await RenoteMutingsCache.init(user.id).then((cache) => + cache.getAll(), + ); } return notes.filter( diff --git a/packages/backend/src/server/api/endpoints/notes/timeline.ts b/packages/backend/src/server/api/endpoints/notes/timeline.ts index 8a201a8075..e2cb04f96b 100644 --- a/packages/backend/src/server/api/endpoints/notes/timeline.ts +++ b/packages/backend/src/server/api/endpoints/notes/timeline.ts @@ -25,6 +25,7 @@ import { } from "@/db/scylla.js"; import { ChannelFollowingsCache, + InstanceMutingsCache, LocalFollowingsCache, RenoteMutingsCache, UserBlockedCache, @@ -95,12 +96,14 @@ export default define(meta, paramDef, async (ps, user) => { followingChannelIds, followingUserIds, mutedUserIds, + mutedInstances, blockerIds, renoteMutedIds, ] = await Promise.all([ ChannelFollowingsCache.init(user.id).then((cache) => cache.getAll()), followingsCache.getAll(), UserMutingsCache.init(user.id).then((cache) => cache.getAll()), + InstanceMutingsCache.init(user.id).then((cache) => cache.getAll()), UserBlockedCache.init(user.id).then((cache) => cache.getAll()), RenoteMutingsCache.init(user.id).then((cache) => cache.getAll()), ]); @@ -120,7 +123,12 @@ export default define(meta, paramDef, async (ps, user) => { filtered = await filterChannel(filtered, user, followingChannelIds); filtered = await filterReply(filtered, ps.withReplies, user); filtered = await filterVisibility(filtered, user, followingUserIds); - filtered = await filterMutedUser(filtered, user, mutedUserIds); + filtered = await filterMutedUser( + filtered, + user, + mutedUserIds, + mutedInstances, + ); filtered = await filterMutedNote(filtered, user, mutedWords); filtered = await filterBlockedUser(filtered, user, blockerIds); filtered = await filterMutedRenotes(filtered, user, renoteMutedIds);