From 10b3458cda4bb9fbd63a027c90034a9196d25664 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B4=87=E5=B3=B0=20=E6=9C=94=E8=8F=AF?= Date: Wed, 20 Nov 2024 21:38:00 +0900 Subject: [PATCH] =?UTF-8?q?Mod:=20=E3=81=A8=E3=82=8A=E3=81=82=E3=81=88?= =?UTF-8?q?=E3=81=9AproxyAccount=E3=82=92=E3=82=B7=E3=82=B9=E3=83=86?= =?UTF-8?q?=E3=83=A0=E3=82=A2=E3=82=AB=E3=82=A6=E3=83=B3=E3=83=88=E3=81=AA?= =?UTF-8?q?=E6=84=9F=E3=81=98=E3=81=AB=E3=81=97=E3=81=A6=E3=81=BF=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/src/core/ProxyAccountService.ts | 33 +++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/core/ProxyAccountService.ts b/packages/backend/src/core/ProxyAccountService.ts index c3ff2a68d3..f776d72405 100644 --- a/packages/backend/src/core/ProxyAccountService.ts +++ b/packages/backend/src/core/ProxyAccountService.ts @@ -4,25 +4,54 @@ */ import { Inject, Injectable } from '@nestjs/common'; +import { IsNull } from 'typeorm'; import type { MiMeta, UsersRepository } from '@/models/_.js'; import type { MiLocalUser } from '@/models/User.js'; import { DI } from '@/di-symbols.js'; import { bindThis } from '@/decorators.js'; +import { CreateSystemUserService } from '@/core/CreateSystemUserService.js'; +import { MemorySingleCache } from '@/misc/cache.js'; +import { MetaService } from '@/core/MetaService.js'; + +const ACTOR_USERNAME = 'proxy.actor' as const; @Injectable() export class ProxyAccountService { + private cache: MemorySingleCache; + constructor( @Inject(DI.meta) private meta: MiMeta, @Inject(DI.usersRepository) private usersRepository: UsersRepository, + + private createSystemUserService: CreateSystemUserService, + private metaService: MetaService, ) { + this.cache = new MemorySingleCache(Infinity); } @bindThis public async fetch(): Promise { - if (this.meta.proxyAccountId == null) return null; - return await this.usersRepository.findOneByOrFail({ id: this.meta.proxyAccountId }) as MiLocalUser; + const cached = this.cache.get(); + if (cached) return cached; + + const user = await this.usersRepository.findOneBy({ + host: IsNull(), + username: ACTOR_USERNAME, + }) as MiLocalUser | undefined; + + if (user) { + this.cache.set(user); + return user; + } else { + const created = await this.createSystemUserService.createSystemUser(ACTOR_USERNAME) as MiLocalUser; + this.cache.set(created); + const set = {} as Partial; + set.proxyAccountId = created.id; + await this.metaService.update(set); + return created; + } } }