2022-02-27 11:07:39 +09:00
|
|
|
import { createSystemUser } from './create-system-user.js';
|
|
|
|
import { ILocalUser } from '@/models/entities/user.js';
|
|
|
|
import { Users } from '@/models/index.js';
|
|
|
|
import { Cache } from '@/misc/cache.js';
|
2022-03-26 15:34:00 +09:00
|
|
|
import { IsNull } from 'typeorm';
|
2020-10-18 01:46:40 +09:00
|
|
|
|
|
|
|
const ACTOR_USERNAME = 'instance.actor' as const;
|
|
|
|
|
2021-03-22 22:28:09 +09:00
|
|
|
const cache = new Cache<ILocalUser>(Infinity);
|
|
|
|
|
2020-10-18 01:46:40 +09:00
|
|
|
export async function getInstanceActor(): Promise<ILocalUser> {
|
2021-03-22 22:28:09 +09:00
|
|
|
const cached = cache.get(null);
|
|
|
|
if (cached) return cached;
|
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
const user = await Users.findOneBy({
|
|
|
|
host: IsNull(),
|
2021-12-09 23:58:30 +09:00
|
|
|
username: ACTOR_USERNAME,
|
2021-03-22 22:28:09 +09:00
|
|
|
}) as ILocalUser | undefined;
|
2020-10-18 01:46:40 +09:00
|
|
|
|
2021-03-22 22:28:09 +09:00
|
|
|
if (user) {
|
|
|
|
cache.set(null, user);
|
|
|
|
return user;
|
|
|
|
} else {
|
|
|
|
const created = await createSystemUser(ACTOR_USERNAME) as ILocalUser;
|
|
|
|
cache.set(null, created);
|
|
|
|
return created;
|
|
|
|
}
|
2020-10-18 01:46:40 +09:00
|
|
|
}
|