From 941aed6a141d12f3abc33ce07d5f7930c3d50881 Mon Sep 17 00:00:00 2001 From: tamaina Date: Mon, 4 Mar 2024 00:05:48 +0000 Subject: [PATCH] refreshAndfindKey --- .../core/activitypub/ApDbResolverService.ts | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/packages/backend/src/core/activitypub/ApDbResolverService.ts b/packages/backend/src/core/activitypub/ApDbResolverService.ts index 1495ba7a95..61fcc6731d 100644 --- a/packages/backend/src/core/activitypub/ApDbResolverService.ts +++ b/packages/backend/src/core/activitypub/ApDbResolverService.ts @@ -114,6 +114,15 @@ export class ApDbResolverService implements OnApplicationShutdown { } } + @bindThis + private async refreshAndfindKey(userId: MiUser['id'], keyId: string): Promise { + this.refreshCacheByUserId(userId); + const keys = await this.getPublicKeyByUserId(userId); + if (keys == null || !Array.isArray(keys)) return null; + + return keys.find(x => x.keyId === keyId) ?? null; + } + /** * AP Actor id => Misskey User and Key * @param uri AP Actor id @@ -158,11 +167,7 @@ export class ApDbResolverService implements OnApplicationShutdown { // まずはキャッシュを更新して再取得 const cacheRaw = this.publicKeyByUserIdCache.cache.get(user.id); if (cacheRaw && cacheRaw.date > Date.now() - 1000 * 60 * 12) { - this.refreshCacheByUserId(user.id); - const keys = await this.getPublicKeyByUserId(user.id); - if (keys == null || !Array.isArray(keys)) return null; - - const exactKey = keys.find(x => x.keyId === keyId); + const exactKey = await this.refreshAndfindKey(user.id, keyId); if (exactKey) return { user, key: exactKey }; } @@ -171,12 +176,7 @@ export class ApDbResolverService implements OnApplicationShutdown { const renewed = await this.apPersonService.fetchPersonWithRenewal(uri, 0); if (renewed == null || renewed.isDeleted) return null; - this.refreshCacheByUserId(user.id); - const keys = await this.getPublicKeyByUserId(user.id); - if (keys == null || !Array.isArray(keys)) return null; - - const exactKey = keys.find(x => x.keyId === keyId); - if (exactKey) return { user, key: exactKey }; + return { user, key: await this.refreshAndfindKey(user.id, keyId) }; } return { user, key: null };