diff --git a/packages/backend/src/queue/processors/inbox.ts b/packages/backend/src/queue/processors/inbox.ts index 145a32e34..4fbfdb234 100644 --- a/packages/backend/src/queue/processors/inbox.ts +++ b/packages/backend/src/queue/processors/inbox.ts @@ -49,7 +49,7 @@ export default async (job: Bull.Job): Promise => { // HTTP-Signature keyIdを元にDBから取得 let authUser: { user: CacheableRemoteUser; - key?: UserPublickey; + key: UserPublickey | null; } | null = await dbResolver.getAuthUserFromKeyId(signature.keyId); // keyIdでわからなければ、activity.actorを元にDBから取得 || activity.actorを元にリモートから取得 diff --git a/packages/backend/src/remote/activitypub/db-resolver.ts b/packages/backend/src/remote/activitypub/db-resolver.ts index 5c748bd61..67ee5663a 100644 --- a/packages/backend/src/remote/activitypub/db-resolver.ts +++ b/packages/backend/src/remote/activitypub/db-resolver.ts @@ -10,6 +10,7 @@ import { resolvePerson } from './models/person.js'; import { Cache } from '@/misc/cache.js'; const publicKeyCache = new Cache<(UserPublickey & { user: User }) | null>(Infinity); +const publicKeyByUserIdCache = new Cache(Infinity); export default class DbResolver { constructor() { @@ -107,14 +108,13 @@ export default class DbResolver { */ public async getAuthUserFromApId(uri: string): Promise<{ user: CacheableRemoteUser; - key?: UserPublickey; + key: UserPublickey | null; } | null> { const user = await resolvePerson(uri) as CacheableRemoteUser; if (user == null) return null; - // TODO: cache - const key = await UserPublickeys.findOne(user.id); + const key = await publicKeyByUserIdCache.fetch(user.id, () => UserPublickeys.findOne(user.id).then(x => x || null), v => v != null); // TODO: typeorm 3.0 にしたら.then(x => x || null)は消せる return { user,