diff --git a/packages/backend/src/services/instance-actor.ts b/packages/backend/src/services/instance-actor.ts index 279142b32..15bdc674f 100644 --- a/packages/backend/src/services/instance-actor.ts +++ b/packages/backend/src/services/instance-actor.ts @@ -1,7 +1,7 @@ import { IsNull } from 'typeorm'; import { ILocalUser } from '@/models/entities/user.js'; import { Users } from '@/models/index.js'; -import { createSystemUser } from './create-system-user.js'; +import { getSystemUser } from './system-user.js'; const ACTOR_USERNAME = 'instance.actor' as const; @@ -12,7 +12,7 @@ let instanceActor = await Users.findOneBy({ export async function getInstanceActor(): Promise { if (!instanceActor) { - instanceActor = await createSystemUser(ACTOR_USERNAME) as ILocalUser; + instanceActor = await getSystemUser(ACTOR_USERNAME) as ILocalUser; } return instanceActor; diff --git a/packages/backend/src/services/relay.ts b/packages/backend/src/services/relay.ts index 21b869a82..de7155ea0 100644 --- a/packages/backend/src/services/relay.ts +++ b/packages/backend/src/services/relay.ts @@ -9,7 +9,7 @@ import { genId } from '@/misc/gen-id.js'; import { Cache } from '@/misc/cache.js'; import { Relay } from '@/models/entities/relay.js'; import { MINUTE } from '@/const.js'; -import { createSystemUser } from './create-system-user.js'; +import { getSystemUser } from './system-user.js'; const ACTOR_USERNAME = 'relay.actor' as const; @@ -24,16 +24,8 @@ const relaysCache = new Cache( }), ); -export async function getRelayActor(): Promise { - const user = await Users.findOneBy({ - host: IsNull(), - username: ACTOR_USERNAME, - }); - - if (user) return user as ILocalUser; - - const created = await createSystemUser(ACTOR_USERNAME); - return created as ILocalUser; +async function getRelayActor(): Promise { + return await getSystemUser(ACTOR_USERNAME); } export async function addRelay(inbox: string): Promise { diff --git a/packages/backend/src/services/create-system-user.ts b/packages/backend/src/services/system-user.ts similarity index 86% rename from packages/backend/src/services/create-system-user.ts rename to packages/backend/src/services/system-user.ts index 8e39b00fb..2ad32a1c4 100644 --- a/packages/backend/src/services/create-system-user.ts +++ b/packages/backend/src/services/system-user.ts @@ -2,6 +2,7 @@ import { v4 as uuid } from 'uuid'; import { IsNull } from 'typeorm'; import { genRsaKeyPair } from '@/misc/gen-key-pair.js'; import { hashPassword } from '@/misc/password.js'; +import { Users } from '@/models/index.js'; import { User } from '@/models/entities/user.js'; import { UserProfile } from '@/models/entities/user-profile.js'; import { genId } from '@/misc/gen-id.js'; @@ -10,7 +11,14 @@ import { UsedUsername } from '@/models/entities/used-username.js'; import { db } from '@/db/postgre.js'; import generateNativeUserToken from '@/server/api/common/generate-native-user-token.js'; -export async function createSystemUser(username: string): Promise { +export async function getSystemUser(username: string): Promise { + const exist = await Users.findBy({ + usernameLower: username.toLowerCase(), + host: IsNull(), + }); + + if (exist) return exist; + const password = await hashPassword(uuid()); // Generate secret @@ -22,13 +30,6 @@ export async function createSystemUser(username: string): Promise { // Start transaction await db.transaction(async transactionalEntityManager => { - const exist = await transactionalEntityManager.countBy(User, { - usernameLower: username.toLowerCase(), - host: IsNull(), - }); - - if (exist) throw new Error('the user is already exists'); - account = await transactionalEntityManager.insert(User, { id: genId(), createdAt: new Date(),