From e00003edffc13e9d473c16c457901b35a910b13a Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 13 Jan 2023 08:56:06 +0900 Subject: [PATCH] refactor --- packages/backend/src/core/UserCacheService.ts | 7 ++++++- .../backend/src/core/activitypub/ApDbResolverService.ts | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/core/UserCacheService.ts b/packages/backend/src/core/UserCacheService.ts index 3f735c0c53..4d9ee7366d 100644 --- a/packages/backend/src/core/UserCacheService.ts +++ b/packages/backend/src/core/UserCacheService.ts @@ -2,7 +2,7 @@ import { Inject, Injectable } from '@nestjs/common'; import Redis from 'ioredis'; import type { UsersRepository } from '@/models/index.js'; import { Cache } from '@/misc/cache.js'; -import type { CacheableLocalUser, CacheableUser, ILocalUser } from '@/models/entities/User.js'; +import type { CacheableLocalUser, CacheableUser, ILocalUser, User } from '@/models/entities/User.js'; import { DI } from '@/di-symbols.js'; import { UserEntityService } from '@/core/entities/UserEntityService.js'; import { bindThis } from '@/decorators.js'; @@ -68,6 +68,11 @@ export class UserCacheService implements OnApplicationShutdown { } } + @bindThis + public findById(userId: User['id']) { + return this.userByIdCache.fetch(userId, () => this.usersRepository.findOneByOrFail({ id: userId })); + } + @bindThis public onApplicationShutdown(signal?: string | undefined) { this.redisSubscriber.off('message', this.onMessage); diff --git a/packages/backend/src/core/activitypub/ApDbResolverService.ts b/packages/backend/src/core/activitypub/ApDbResolverService.ts index 1f28fb3a07..1d0c2d5da4 100644 --- a/packages/backend/src/core/activitypub/ApDbResolverService.ts +++ b/packages/backend/src/core/activitypub/ApDbResolverService.ts @@ -159,7 +159,7 @@ export class ApDbResolverService { if (key == null) return null; return { - user: await this.userCacheService.userByIdCache.fetch(key.userId, () => this.usersRepository.findOneByOrFail({ id: key.userId })) as CacheableRemoteUser, + user: await this.userCacheService.findById(key.userId) as CacheableRemoteUser, key, }; }