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 { 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 { Cache } from '@/misc/cache.js';
|
|
||||||
import { createSystemUser } from './create-system-user.js';
|
import { createSystemUser } from './create-system-user.js';
|
||||||
|
|
||||||
const ACTOR_USERNAME = 'instance.actor' as const;
|
const ACTOR_USERNAME = 'instance.actor' as const;
|
||||||
|
|
||||||
const cache = new Cache<ILocalUser>(Infinity);
|
let instanceActor = await Users.findOneBy({
|
||||||
|
|
||||||
export async function getInstanceActor(): Promise<ILocalUser> {
|
|
||||||
const cached = cache.get(null);
|
|
||||||
if (cached) return cached;
|
|
||||||
|
|
||||||
const user = await Users.findOneBy({
|
|
||||||
host: IsNull(),
|
host: IsNull(),
|
||||||
username: ACTOR_USERNAME,
|
username: ACTOR_USERNAME,
|
||||||
}) as ILocalUser | undefined;
|
}) as ILocalUser | undefined;
|
||||||
|
|
||||||
if (user) {
|
export async function getInstanceActor(): Promise<ILocalUser> {
|
||||||
cache.set(null, user);
|
if (instanceActor) {
|
||||||
return user;
|
return instanceActor;
|
||||||
} else {
|
} else {
|
||||||
const created = await createSystemUser(ACTOR_USERNAME) as ILocalUser;
|
instanceActor = await createSystemUser(ACTOR_USERNAME) as ILocalUser;
|
||||||
cache.set(null, created);
|
|
||||||
return created;
|
return created;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue