clean up and improve logic in ApPersonService.fetchPerson

This commit is contained in:
Hazelnoot 2025-09-15 18:26:13 -04:00
parent 4d09ccbcc8
commit 35eecf02b3

View file

@ -293,28 +293,38 @@ export class ApPersonService implements OnModuleInit {
* Misskeyに対象のPersonが登録されていればそれを返しnullを返します * Misskeyに対象のPersonが登録されていればそれを返しnullを返します
*/ */
@bindThis @bindThis
public async fetchPerson(uri: string): Promise<MiLocalUser | MiRemoteUser | null> { public async fetchPerson(uri: string, opts?: { withDeleted?: boolean, withSuspended?: boolean }): Promise<MiLocalUser | MiRemoteUser | null> {
const cached = await this.uriPersonCache.fetchMaybe(uri); const _opts = {
if (cached) return await this.cacheService.findOptionalUserById(cached) as MiRemoteUser | MiLocalUser | undefined ?? null; withDeleted: opts?.withDeleted ?? false,
withSuspended: opts?.withSuspended ?? true,
};
// URIがこのサーバーを指しているならデータベースからフェッチ let userId;
if (uri.startsWith(`${this.config.url}/`)) {
const id = uri.split('/').pop(); // Resolve URI -> User ID
const u = await this.usersRepository.findOneBy({ id }) as MiLocalUser | null; const parsed = this.utilityService.parseUri(uri);
if (u) await this.uriPersonCache.set(uri, u.id); if (parsed.local) {
return u; userId = parsed.type === 'users' ? parsed.id : null;
} else {
userId = await this.cacheService.uriPersonCache.fetch(uri);
} }
//#region このサーバーに既に登録されていたらそれを返す // No match
const exist = await this.usersRepository.findOneBy({ uri }) as MiLocalUser | MiRemoteUser | null; if (!userId) {
return null;
if (exist) {
await this.uriPersonCache.set(uri, exist.id);
return exist;
} }
//#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<Partial<Pick<MiRemoteUser, 'avatarId' | 'bannerId' | 'backgroundId' | 'avatarUrl' | 'bannerUrl' | 'backgroundUrl' | 'avatarBlurhash' | 'bannerBlurhash' | 'backgroundBlurhash'>>> { private async resolveAvatarAndBanner(user: MiRemoteUser, icon: any, image: any, bgimg: any): Promise<Partial<Pick<MiRemoteUser, 'avatarId' | 'bannerId' | 'backgroundId' | 'avatarUrl' | 'bannerUrl' | 'backgroundUrl' | 'avatarBlurhash' | 'bannerBlurhash' | 'backgroundBlurhash'>>> {
@ -851,7 +861,7 @@ export class ApPersonService implements OnModuleInit {
} }
//#region このサーバーに既に登録されていたらそれを返す //#region このサーバーに既に登録されていたらそれを返す
const exist = await this.fetchPerson(uri); const exist = await this.fetchPerson(uri, { withDeleted: true });
if (exist) return exist; if (exist) return exist;
//#endregion //#endregion