From 7e3582bac28554943d1df03eef69bfa699eeb2a4 Mon Sep 17 00:00:00 2001 From: lqvp <183242690+lqvp@users.noreply.github.com> Date: Wed, 18 Dec 2024 17:14:28 +0900 Subject: [PATCH] =?UTF-8?q?enhance(backend):=20=E3=82=AD=E3=83=A3=E3=83=83?= =?UTF-8?q?=E3=82=B7=E3=83=A5=E3=82=92=E4=BD=BF=E3=81=86=E3=82=88=E3=81=86?= =?UTF-8?q?=E3=81=AB=EF=BC=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../server/api/endpoints/following/history.ts | 36 +++++++++++++------ .../endpoints/following/requests/history.ts | 35 ++++++++++++------ 2 files changed, 49 insertions(+), 22 deletions(-) diff --git a/packages/backend/src/server/api/endpoints/following/history.ts b/packages/backend/src/server/api/endpoints/following/history.ts index c9698d7237cf..58b0b18ff4ea 100644 --- a/packages/backend/src/server/api/endpoints/following/history.ts +++ b/packages/backend/src/server/api/endpoints/following/history.ts @@ -10,6 +10,7 @@ import { QueryService } from '@/core/QueryService.js'; import type { FollowHistoryRepository } from '@/models/_.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { DI } from '@/di-symbols.js'; +import { CacheService } from '@/core/CacheService.js'; export const meta = { tags: ['following', 'account'], @@ -103,9 +104,11 @@ export default class extends Endpoint { private userEntityService: UserEntityService, private queryService: QueryService, + private cacheService: CacheService, ) { super(meta, paramDef, async (ps, me) => { if (ps.delete) { + await this.cacheService.userFollowingsCache.delete(me.id); await this.followHistoryRepository .createQueryBuilder() .delete() @@ -189,22 +192,33 @@ export default class extends Endpoint { break; } + const blockedUserIds = await this.cacheService.userBlockedCache.fetch(me.id); + if (blockedUserIds.size > 0) { + query.andWhere('fromUser.id NOT IN (:...blockedUserIds)', { blockedUserIds: Array.from(blockedUserIds) }); + query.andWhere('toUser.id NOT IN (:...blockedUserIds)', { blockedUserIds: Array.from(blockedUserIds) }); + } + const histories = await query .orderBy('history.timestamp', 'DESC') .take(ps.limit) .getMany(); - return await Promise.all(histories.map(async history => ({ - id: history.id, - type: history.type, - fromUser: history.fromUser - ? await this.userEntityService.pack(history.fromUser ?? history.fromUserId, me) - : { name: 'unknown user' }, - toUser: history.toUser - ? await this.userEntityService.pack(history.toUser ?? history.toUserId, me) - : { name: 'unknown user' }, - timestamp: history.timestamp.toISOString(), - }))); + return await Promise.all(histories.map(async history => { + const userFollowings = await this.cacheService.userFollowingsCache.fetch(me.id); + + return { + id: history.id, + type: history.type, + fromUser: history.fromUser + ? await this.userEntityService.pack(history.fromUser ?? history.fromUserId, me) + : { name: 'unknown user' }, + toUser: history.toUser + ? await this.userEntityService.pack(history.toUser ?? history.toUserId, me) + : { name: 'unknown user' }, + timestamp: history.timestamp.toISOString(), + isFollowing: Object.hasOwn(userFollowings, history.fromUserId), + }; + })); }); } } diff --git a/packages/backend/src/server/api/endpoints/following/requests/history.ts b/packages/backend/src/server/api/endpoints/following/requests/history.ts index f2a33e86c3c4..dfb08ee5fcb2 100644 --- a/packages/backend/src/server/api/endpoints/following/requests/history.ts +++ b/packages/backend/src/server/api/endpoints/following/requests/history.ts @@ -10,6 +10,7 @@ import { QueryService } from '@/core/QueryService.js'; import type { FollowRequestHistoryRepository } from '@/models/_.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { DI } from '@/di-symbols.js'; +import { CacheService } from '@/core/CacheService.js'; export const meta = { tags: ['following', 'account'], @@ -103,6 +104,7 @@ export default class extends Endpoint { private userEntityService: UserEntityService, private queryService: QueryService, + private cacheService: CacheService, ) { super(meta, paramDef, async (ps, me) => { // 削除モードの部分だけを修正(他のコードは同じ) @@ -190,22 +192,33 @@ export default class extends Endpoint { break; } + const blockedUserIds = await this.cacheService.userBlockedCache.fetch(me.id); + if (blockedUserIds.size > 0) { + query.andWhere('fromUser.id NOT IN (:...blockedUserIds)', { blockedUserIds: Array.from(blockedUserIds) }); + query.andWhere('toUser.id NOT IN (:...blockedUserIds)', { blockedUserIds: Array.from(blockedUserIds) }); + } + const histories = await query .orderBy('history.timestamp', 'DESC') .take(ps.limit) .getMany(); - return await Promise.all(histories.map(async history => ({ - id: history.id, - type: history.type, - fromUser: history.fromUser - ? await this.userEntityService.pack(history.fromUser ?? history.fromUserId, me) - : { name: 'unknown user' }, - toUser: history.toUser - ? await this.userEntityService.pack(history.toUser ?? history.toUserId, me) - : { name: 'unknown user' }, - timestamp: history.timestamp.toISOString(), - }))); + return await Promise.all(histories.map(async history => { + const userFollowings = await this.cacheService.userFollowingsCache.fetch(me.id); + + return { + id: history.id, + type: history.type, + fromUser: history.fromUser + ? await this.userEntityService.pack(history.fromUser ?? history.fromUserId, me) + : { name: 'unknown user' }, + toUser: history.toUser + ? await this.userEntityService.pack(history.toUser ?? history.toUserId, me) + : { name: 'unknown user' }, + timestamp: history.timestamp.toISOString(), + isFollowing: Object.hasOwn(userFollowings, history.fromUserId), + }; + })); }); } }