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({
|
||||||
|
host: IsNull(),
|
||||||
|
username: ACTOR_USERNAME,
|
||||||
|
}) as ILocalUser | undefined;
|
||||||
|
|
||||||
export async function getInstanceActor(): Promise<ILocalUser> {
|
export async function getInstanceActor(): Promise<ILocalUser> {
|
||||||
const cached = cache.get(null);
|
if (instanceActor) {
|
||||||
if (cached) return cached;
|
return instanceActor;
|
||||||
|
|
||||||
const user = await Users.findOneBy({
|
|
||||||
host: IsNull(),
|
|
||||||
username: ACTOR_USERNAME,
|
|
||||||
}) as ILocalUser | undefined;
|
|
||||||
|
|
||||||
if (user) {
|
|
||||||
cache.set(null, user);
|
|
||||||
return user;
|
|
||||||
} 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