iceshrimp/packages/backend/src/services/chart/charts/per-user-following.ts
ThatOneCalculator ac64087a1e no more eslint
2023-01-12 20:54:33 -08:00

69 lines
2 KiB
TypeScript

import type { KVs } from "../core.js";
import Chart from "../core.js";
import { Followings, Users } from "@/models/index.js";
import { Not, IsNull } from "typeorm";
import type { User } from "@/models/entities/user.js";
import { name, schema } from "./entities/per-user-following.js";
/**
* ユーザーごとのフォローに関するチャート
*/
export default class PerUserFollowingChart extends Chart<typeof schema> {
constructor() {
super(name, schema, true);
}
protected async tickMajor(
group: string,
): Promise<Partial<KVs<typeof schema>>> {
const [
localFollowingsCount,
localFollowersCount,
remoteFollowingsCount,
remoteFollowersCount,
] = await Promise.all([
Followings.countBy({ followerId: group, followeeHost: IsNull() }),
Followings.countBy({ followeeId: group, followerHost: IsNull() }),
Followings.countBy({ followerId: group, followeeHost: Not(IsNull()) }),
Followings.countBy({ followeeId: group, followerHost: Not(IsNull()) }),
]);
return {
"local.followings.total": localFollowingsCount,
"local.followers.total": localFollowersCount,
"remote.followings.total": remoteFollowingsCount,
"remote.followers.total": remoteFollowersCount,
};
}
protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
return {};
}
public async update(
follower: { id: User["id"]; host: User["host"] },
followee: { id: User["id"]; host: User["host"] },
isFollow: boolean,
): Promise<void> {
const prefixFollower = Users.isLocalUser(follower) ? "local" : "remote";
const prefixFollowee = Users.isLocalUser(followee) ? "local" : "remote";
this.commit(
{
[`${prefixFollower}.followings.total`]: isFollow ? 1 : -1,
[`${prefixFollower}.followings.inc`]: isFollow ? 1 : 0,
[`${prefixFollower}.followings.dec`]: isFollow ? 0 : 1,
},
follower.id,
);
this.commit(
{
[`${prefixFollowee}.followers.total`]: isFollow ? 1 : -1,
[`${prefixFollowee}.followers.inc`]: isFollow ? 1 : 0,
[`${prefixFollowee}.followers.dec`]: isFollow ? 0 : 1,
},
followee.id,
);
}
}