2020-10-17 19:46:40 +03:00
|
|
|
import { createSystemUser } from './create-system-user';
|
|
|
|
import { ILocalUser } from '../models/entities/user';
|
|
|
|
import { Users } from '../models';
|
2021-03-23 10:43:07 +02:00
|
|
|
import { Cache } from '@/misc/cache';
|
2020-10-17 19:46:40 +03:00
|
|
|
|
|
|
|
const ACTOR_USERNAME = 'instance.actor' as const;
|
|
|
|
|
2021-03-22 15:28:09 +02:00
|
|
|
const cache = new Cache<ILocalUser>(Infinity);
|
|
|
|
|
2020-10-17 19:46:40 +03:00
|
|
|
export async function getInstanceActor(): Promise<ILocalUser> {
|
2021-03-22 15:28:09 +02:00
|
|
|
const cached = cache.get(null);
|
|
|
|
if (cached) return cached;
|
|
|
|
|
2020-10-17 19:46:40 +03:00
|
|
|
const user = await Users.findOne({
|
|
|
|
host: null,
|
|
|
|
username: ACTOR_USERNAME
|
2021-03-22 15:28:09 +02:00
|
|
|
}) as ILocalUser | undefined;
|
2020-10-17 19:46:40 +03:00
|
|
|
|
2021-03-22 15:28:09 +02: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-17 19:46:40 +03:00
|
|
|
}
|