From bd4d8694dda63dfe8093e605bcb9184231818cad Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Fri, 15 Dec 2023 11:24:13 +0900 Subject: [PATCH] perf: early return users/notes and users/featured-notes if me is blocked by requesting user (#12663) --- .../src/server/api/endpoints/users/featured-notes.ts | 11 ++++++++--- .../backend/src/server/api/endpoints/users/notes.ts | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/users/featured-notes.ts b/packages/backend/src/server/api/endpoints/users/featured-notes.ts index f84148d727..7243aa3b3e 100644 --- a/packages/backend/src/server/api/endpoints/users/featured-notes.ts +++ b/packages/backend/src/server/api/endpoints/users/featured-notes.ts @@ -51,6 +51,13 @@ export default class extends Endpoint { // eslint- private cacheService: CacheService, ) { super(meta, paramDef, async (ps, me) => { + const userIdsWhoBlockingMe = me ? await this.cacheService.userBlockedCache.fetch(me.id) : new Set(); + + // early return if me is blocked by requesting user + if (userIdsWhoBlockingMe.has(ps.userId)) { + return []; + } + let noteIds = await this.featuredService.getPerUserNotesRanking(ps.userId, 50); noteIds.sort((a, b) => a > b ? -1 : 1); @@ -65,11 +72,9 @@ export default class extends Endpoint { // eslint- const [ userIdsWhoMeMuting, - userIdsWhoBlockingMe, ] = me ? await Promise.all([ this.cacheService.userMutingsCache.fetch(me.id), - this.cacheService.userBlockedCache.fetch(me.id), - ]) : [new Set(), new Set()]; + ]) : [new Set()]; const query = this.notesRepository.createQueryBuilder('note') .where('note.id IN (:...noteIds)', { noteIds: noteIds }) diff --git a/packages/backend/src/server/api/endpoints/users/notes.ts b/packages/backend/src/server/api/endpoints/users/notes.ts index b32128a8aa..b485126ed8 100644 --- a/packages/backend/src/server/api/endpoints/users/notes.ts +++ b/packages/backend/src/server/api/endpoints/users/notes.ts @@ -86,6 +86,14 @@ export default class extends Endpoint { // eslint- if (ps.withReplies && ps.withFiles) throw new ApiError(meta.errors.bothWithRepliesAndWithFiles); + // early return if me is blocked by requesting user + if (me != null) { + const userIdsWhoBlockingMe = await this.cacheService.userBlockedCache.fetch(me.id); + if (userIdsWhoBlockingMe.has(ps.userId)) { + return []; + } + } + if (!serverSettings.enableFanoutTimeline) { const timeline = await this.getFromDb({ untilId,