forked from FoundKeyGang/FoundKey
server: simplify caching for instance actor
This commit is contained in:
parent
b0489abd7f
commit
57299f0df6
1 changed files with 7 additions and 15 deletions
|
@ -1,28 +1,20 @@
|
|||
import { IsNull } from 'typeorm';
|
||||
import { ILocalUser } from '@/models/entities/user.js';
|
||||
import { Users } from '@/models/index.js';
|
||||
import { Cache } from '@/misc/cache.js';
|
||||
import { createSystemUser } from './create-system-user.js';
|
||||
|
||||
const ACTOR_USERNAME = 'instance.actor' as const;
|
||||
|
||||
const cache = new Cache<ILocalUser>(Infinity);
|
||||
let instanceActor = await Users.findOneBy({
|
||||
host: IsNull(),
|
||||
username: ACTOR_USERNAME,
|
||||
}) as ILocalUser | undefined;
|
||||
|
||||
export async function getInstanceActor(): Promise<ILocalUser> {
|
||||
const cached = cache.get(null);
|
||||
if (cached) return cached;
|
||||
|
||||
const user = await Users.findOneBy({
|
||||
host: IsNull(),
|
||||
username: ACTOR_USERNAME,
|
||||
}) as ILocalUser | undefined;
|
||||
|
||||
if (user) {
|
||||
cache.set(null, user);
|
||||
return user;
|
||||
if (instanceActor) {
|
||||
return instanceActor;
|
||||
} else {
|
||||
const created = await createSystemUser(ACTOR_USERNAME) as ILocalUser;
|
||||
cache.set(null, created);
|
||||
instanceActor = await createSystemUser(ACTOR_USERNAME) as ILocalUser;
|
||||
return created;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue