From 35eecf02b39dd86ea25889dee147f733c1a9868a Mon Sep 17 00:00:00 2001 From: Hazelnoot Date: Mon, 15 Sep 2025 18:26:13 -0400 Subject: [PATCH] clean up and improve logic in ApPersonService.fetchPerson --- .../activitypub/models/ApPersonService.ts | 46 +++++++++++-------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/packages/backend/src/core/activitypub/models/ApPersonService.ts b/packages/backend/src/core/activitypub/models/ApPersonService.ts index 25cd0ce8c0..927ce9e574 100644 --- a/packages/backend/src/core/activitypub/models/ApPersonService.ts +++ b/packages/backend/src/core/activitypub/models/ApPersonService.ts @@ -293,28 +293,38 @@ export class ApPersonService implements OnModuleInit { * Misskeyに対象のPersonが登録されていればそれを返し、登録がなければnullを返します。 */ @bindThis - public async fetchPerson(uri: string): Promise { - const cached = await this.uriPersonCache.fetchMaybe(uri); - if (cached) return await this.cacheService.findOptionalUserById(cached) as MiRemoteUser | MiLocalUser | undefined ?? null; + public async fetchPerson(uri: string, opts?: { withDeleted?: boolean, withSuspended?: boolean }): Promise { + const _opts = { + withDeleted: opts?.withDeleted ?? false, + withSuspended: opts?.withSuspended ?? true, + }; - // URIがこのサーバーを指しているならデータベースからフェッチ - if (uri.startsWith(`${this.config.url}/`)) { - const id = uri.split('/').pop(); - const u = await this.usersRepository.findOneBy({ id }) as MiLocalUser | null; - if (u) await this.uriPersonCache.set(uri, u.id); - return u; + let userId; + + // Resolve URI -> User ID + const parsed = this.utilityService.parseUri(uri); + if (parsed.local) { + userId = parsed.type === 'users' ? parsed.id : null; + } else { + userId = await this.cacheService.uriPersonCache.fetch(uri); } - //#region このサーバーに既に登録されていたらそれを返す - const exist = await this.usersRepository.findOneBy({ uri }) as MiLocalUser | MiRemoteUser | null; - - if (exist) { - await this.uriPersonCache.set(uri, exist.id); - return exist; + // No match + if (!userId) { + return null; } - //#endregion - return null; + const user = await this.cacheService.findUserById(userId) + .catch(() => null) as MiLocalUser | MiRemoteUser | null; + + if (user?.isDeleted && !_opts.withDeleted) { + return null; + } + if (user?.isSuspended && !_opts.withSuspended) { + return null; + } + + return user; } private async resolveAvatarAndBanner(user: MiRemoteUser, icon: any, image: any, bgimg: any): Promise>> { @@ -851,7 +861,7 @@ export class ApPersonService implements OnModuleInit { } //#region このサーバーに既に登録されていたらそれを返す - const exist = await this.fetchPerson(uri); + const exist = await this.fetchPerson(uri, { withDeleted: true }); if (exist) return exist; //#endregion