server: dont fail if system user exists

closes FoundKeyGang/FoundKey#378

Changelog: Fixed
This commit is contained in:
Johann150 2023-04-20 22:05:26 +02:00
parent 3a73f2c3de
commit 688deda218
Signed by untrusted user: Johann150
GPG key ID: 9EE6577A2A06F8F1
3 changed files with 14 additions and 21 deletions

View file

@ -1,7 +1,7 @@
import { IsNull } from 'typeorm'; import { IsNull } from 'typeorm';
import { ILocalUser } from '@/models/entities/user.js'; import { ILocalUser } from '@/models/entities/user.js';
import { Users } from '@/models/index.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; const ACTOR_USERNAME = 'instance.actor' as const;
@ -12,7 +12,7 @@ let instanceActor = await Users.findOneBy({
export async function getInstanceActor(): Promise<ILocalUser> { export async function getInstanceActor(): Promise<ILocalUser> {
if (!instanceActor) { if (!instanceActor) {
instanceActor = await createSystemUser(ACTOR_USERNAME) as ILocalUser; instanceActor = await getSystemUser(ACTOR_USERNAME) as ILocalUser;
} }
return instanceActor; return instanceActor;

View file

@ -9,7 +9,7 @@ import { genId } from '@/misc/gen-id.js';
import { Cache } from '@/misc/cache.js'; import { Cache } from '@/misc/cache.js';
import { Relay } from '@/models/entities/relay.js'; import { Relay } from '@/models/entities/relay.js';
import { MINUTE } from '@/const.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; const ACTOR_USERNAME = 'relay.actor' as const;
@ -24,16 +24,8 @@ const relaysCache = new Cache<Relay[]>(
}), }),
); );
export async function getRelayActor(): Promise<ILocalUser> { async function getRelayActor(): Promise<ILocalUser> {
const user = await Users.findOneBy({ return await getSystemUser(ACTOR_USERNAME);
host: IsNull(),
username: ACTOR_USERNAME,
});
if (user) return user as ILocalUser;
const created = await createSystemUser(ACTOR_USERNAME);
return created as ILocalUser;
} }
export async function addRelay(inbox: string): Promise<Relay> { export async function addRelay(inbox: string): Promise<Relay> {

View file

@ -2,6 +2,7 @@ import { v4 as uuid } from 'uuid';
import { IsNull } from 'typeorm'; import { IsNull } from 'typeorm';
import { genRsaKeyPair } from '@/misc/gen-key-pair.js'; import { genRsaKeyPair } from '@/misc/gen-key-pair.js';
import { hashPassword } from '@/misc/password.js'; import { hashPassword } from '@/misc/password.js';
import { Users } from '@/models/index.js';
import { User } from '@/models/entities/user.js'; import { User } from '@/models/entities/user.js';
import { UserProfile } from '@/models/entities/user-profile.js'; import { UserProfile } from '@/models/entities/user-profile.js';
import { genId } from '@/misc/gen-id.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 { db } from '@/db/postgre.js';
import generateNativeUserToken from '@/server/api/common/generate-native-user-token.js'; import generateNativeUserToken from '@/server/api/common/generate-native-user-token.js';
export async function createSystemUser(username: string): Promise<User> { export async function getSystemUser(username: string): Promise<User> {
const exist = await Users.findBy({
usernameLower: username.toLowerCase(),
host: IsNull(),
});
if (exist) return exist;
const password = await hashPassword(uuid()); const password = await hashPassword(uuid());
// Generate secret // Generate secret
@ -22,13 +30,6 @@ export async function createSystemUser(username: string): Promise<User> {
// Start transaction // Start transaction
await db.transaction(async transactionalEntityManager => { 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, { account = await transactionalEntityManager.insert(User, {
id: genId(), id: genId(),
createdAt: new Date(), createdAt: new Date(),