jormungandr-bite/packages/backend/src/services/instance-actor.ts

29 lines
806 B
TypeScript
Raw Normal View History

2023-01-12 21:40:33 -07:00
import { createSystemUser } from "./create-system-user.js";
import type { ILocalUser } from "@/models/entities/user.js";
import { Users } from "@/models/index.js";
import { Cache } from "@/misc/cache.js";
import { IsNull } from "typeorm";
2023-01-12 21:40:33 -07:00
const ACTOR_USERNAME = "instance.actor" as const;
2023-07-02 18:55:20 -06:00
const cache = new Cache<ILocalUser>("instanceActor", Infinity);
export async function getInstanceActor(): Promise<ILocalUser> {
2023-07-02 18:37:46 -06:00
const cached = await cache.get(null);
if (cached) return cached;
2023-01-12 21:40:33 -07:00
const user = (await Users.findOneBy({
host: IsNull(),
2021-12-09 07:58:30 -07:00
username: ACTOR_USERNAME,
2023-01-12 21:40:33 -07:00
})) as ILocalUser | undefined;
if (user) {
2023-07-02 18:37:46 -06:00
await cache.set(null, user);
return user;
} else {
2023-01-12 21:40:33 -07:00
const created = (await createSystemUser(ACTOR_USERNAME)) as ILocalUser;
2023-07-02 18:37:46 -06:00
await cache.set(null, created);
return created;
}
}