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";
|
2020-10-17 10:46:40 -06:00
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
const ACTOR_USERNAME = "instance.actor" as const;
|
2020-10-17 10:46:40 -06:00
|
|
|
|
2023-07-02 18:55:20 -06:00
|
|
|
const cache = new Cache<ILocalUser>("instanceActor", Infinity);
|
2021-03-22 07:28:09 -06:00
|
|
|
|
2020-10-17 10:46:40 -06:00
|
|
|
export async function getInstanceActor(): Promise<ILocalUser> {
|
2023-07-02 18:37:46 -06:00
|
|
|
const cached = await cache.get(null);
|
2021-03-22 07:28:09 -06:00
|
|
|
if (cached) return cached;
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
const user = (await Users.findOneBy({
|
2022-03-26 00:34:00 -06:00
|
|
|
host: IsNull(),
|
2021-12-09 07:58:30 -07:00
|
|
|
username: ACTOR_USERNAME,
|
2023-01-12 21:40:33 -07:00
|
|
|
})) as ILocalUser | undefined;
|
2020-10-17 10:46:40 -06:00
|
|
|
|
2021-03-22 07:28:09 -06:00
|
|
|
if (user) {
|
2023-07-02 18:37:46 -06:00
|
|
|
await cache.set(null, user);
|
2021-03-22 07:28:09 -06:00
|
|
|
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);
|
2021-03-22 07:28:09 -06:00
|
|
|
return created;
|
|
|
|
}
|
2020-10-17 10:46:40 -06:00
|
|
|
}
|