summaryrefslogtreecommitdiff
path: root/src/services/instance-actor.ts
blob: 9b9c746061dbdb5bc3e07aa0a0b811607b9a3648 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { createSystemUser } from './create-system-user';
import { ILocalUser } from '../models/entities/user';
import { Users } from '../models';
import { Cache } from '@/misc/cache';

const ACTOR_USERNAME = 'instance.actor' as const;

const cache = new Cache<ILocalUser>(Infinity);

export async function getInstanceActor(): Promise<ILocalUser> {
	const cached = cache.get(null);
	if (cached) return cached;

	const user = await Users.findOne({
		host: null,
		username: ACTOR_USERNAME
	}) as ILocalUser | undefined;

	if (user) {
		cache.set(null, user);
		return user;
	} else {
		const created = await createSystemUser(ACTOR_USERNAME) as ILocalUser;
		cache.set(null, created);
		return created;
	}
}