2023-01-13 05:40:33 +01:00
|
|
|
import type { KVs } from "../core.js";
|
|
|
|
import Chart from "../core.js";
|
|
|
|
import { Followings, Instances } from "@/models/index.js";
|
|
|
|
import { name, schema } from "./entities/federation.js";
|
|
|
|
import { fetchMeta } from "@/misc/fetch-meta.js";
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2021-12-14 10:12:37 +01:00
|
|
|
/**
|
|
|
|
* フェデレーションに関するチャート
|
|
|
|
*/
|
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-05 16:13:52 +01:00
|
|
|
export default class FederationChart extends Chart<typeof schema> {
|
2019-04-07 14:50:36 +02:00
|
|
|
constructor() {
|
|
|
|
super(name, schema);
|
|
|
|
}
|
|
|
|
|
2022-02-10 09:45:12 +01:00
|
|
|
protected async tickMajor(): Promise<Partial<KVs<typeof schema>>> {
|
2023-01-13 05:40:33 +01:00
|
|
|
return {};
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
|
2022-02-10 09:45:12 +01:00
|
|
|
protected async tickMinor(): Promise<Partial<KVs<typeof schema>>> {
|
2022-03-04 08:26:10 +01:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const suspendedInstancesQuery = Instances.createQueryBuilder("instance")
|
|
|
|
.select("instance.host")
|
|
|
|
.where("instance.isSuspended = true");
|
2022-03-04 08:26:10 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const pubsubSubQuery = Followings.createQueryBuilder("f")
|
|
|
|
.select("f.followerHost")
|
|
|
|
.where("f.followerHost IS NOT NULL");
|
2022-02-23 08:17:16 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const subInstancesQuery = Followings.createQueryBuilder("f")
|
|
|
|
.select("f.followeeHost")
|
|
|
|
.where("f.followeeHost IS NOT NULL");
|
2022-03-08 14:56:46 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const pubInstancesQuery = Followings.createQueryBuilder("f")
|
|
|
|
.select("f.followerHost")
|
|
|
|
.where("f.followerHost IS NOT NULL");
|
2022-03-08 14:56:46 +01:00
|
|
|
|
|
|
|
const [sub, pub, pubsub, subActive, pubActive] = await Promise.all([
|
2023-01-13 05:40:33 +01:00
|
|
|
Followings.createQueryBuilder("following")
|
|
|
|
.select("COUNT(DISTINCT following.followeeHost)")
|
|
|
|
.where("following.followeeHost IS NOT NULL")
|
|
|
|
.andWhere(
|
|
|
|
meta.blockedHosts.length === 0
|
|
|
|
? "1=1"
|
|
|
|
: "following.followeeHost NOT IN (:...blocked)",
|
|
|
|
{ blocked: meta.blockedHosts },
|
|
|
|
)
|
|
|
|
.andWhere(
|
|
|
|
`following.followeeHost NOT IN (${suspendedInstancesQuery.getQuery()})`,
|
|
|
|
)
|
2022-02-10 09:45:12 +01:00
|
|
|
.getRawOne()
|
2023-01-13 05:40:33 +01:00
|
|
|
.then((x) => parseInt(x.count, 10)),
|
|
|
|
Followings.createQueryBuilder("following")
|
|
|
|
.select("COUNT(DISTINCT following.followerHost)")
|
|
|
|
.where("following.followerHost IS NOT NULL")
|
|
|
|
.andWhere(
|
|
|
|
meta.blockedHosts.length === 0
|
|
|
|
? "1=1"
|
|
|
|
: "following.followerHost NOT IN (:...blocked)",
|
|
|
|
{ blocked: meta.blockedHosts },
|
|
|
|
)
|
|
|
|
.andWhere(
|
|
|
|
`following.followerHost NOT IN (${suspendedInstancesQuery.getQuery()})`,
|
|
|
|
)
|
2022-02-10 09:45:12 +01:00
|
|
|
.getRawOne()
|
2023-01-13 05:40:33 +01:00
|
|
|
.then((x) => parseInt(x.count, 10)),
|
|
|
|
Followings.createQueryBuilder("following")
|
|
|
|
.select("COUNT(DISTINCT following.followeeHost)")
|
|
|
|
.where("following.followeeHost IS NOT NULL")
|
|
|
|
.andWhere(
|
|
|
|
meta.blockedHosts.length === 0
|
|
|
|
? "1=1"
|
|
|
|
: "following.followeeHost NOT IN (:...blocked)",
|
|
|
|
{ blocked: meta.blockedHosts },
|
|
|
|
)
|
|
|
|
.andWhere(
|
|
|
|
`following.followeeHost NOT IN (${suspendedInstancesQuery.getQuery()})`,
|
|
|
|
)
|
|
|
|
.andWhere(`following.followeeHost IN (${pubsubSubQuery.getQuery()})`)
|
2022-02-23 08:17:16 +01:00
|
|
|
.setParameters(pubsubSubQuery.getParameters())
|
|
|
|
.getRawOne()
|
2023-01-13 05:40:33 +01:00
|
|
|
.then((x) => parseInt(x.count, 10)),
|
|
|
|
Instances.createQueryBuilder("instance")
|
|
|
|
.select("COUNT(instance.id)")
|
|
|
|
.where(`instance.host IN (${subInstancesQuery.getQuery()})`)
|
|
|
|
.andWhere(
|
|
|
|
meta.blockedHosts.length === 0
|
|
|
|
? "1=1"
|
|
|
|
: "instance.host NOT IN (:...blocked)",
|
|
|
|
{ blocked: meta.blockedHosts },
|
|
|
|
)
|
|
|
|
.andWhere("instance.isSuspended = false")
|
|
|
|
.andWhere("instance.lastCommunicatedAt > :gt", {
|
|
|
|
gt: new Date(Date.now() - 1000 * 60 * 60 * 24 * 30),
|
|
|
|
})
|
2022-03-08 14:56:46 +01:00
|
|
|
.getRawOne()
|
2023-01-13 05:40:33 +01:00
|
|
|
.then((x) => parseInt(x.count, 10)),
|
|
|
|
Instances.createQueryBuilder("instance")
|
|
|
|
.select("COUNT(instance.id)")
|
|
|
|
.where(`instance.host IN (${pubInstancesQuery.getQuery()})`)
|
|
|
|
.andWhere(
|
|
|
|
meta.blockedHosts.length === 0
|
|
|
|
? "1=1"
|
|
|
|
: "instance.host NOT IN (:...blocked)",
|
|
|
|
{ blocked: meta.blockedHosts },
|
|
|
|
)
|
|
|
|
.andWhere("instance.isSuspended = false")
|
|
|
|
.andWhere("instance.lastCommunicatedAt > :gt", {
|
|
|
|
gt: new Date(Date.now() - 1000 * 60 * 60 * 24 * 30),
|
|
|
|
})
|
2022-03-08 14:56:46 +01:00
|
|
|
.getRawOne()
|
2023-01-13 05:40:33 +01:00
|
|
|
.then((x) => parseInt(x.count, 10)),
|
2022-02-10 09:45:12 +01:00
|
|
|
]);
|
|
|
|
|
|
|
|
return {
|
2023-01-13 05:40:33 +01:00
|
|
|
sub: sub,
|
|
|
|
pub: pub,
|
|
|
|
pubsub: pubsub,
|
|
|
|
subActive: subActive,
|
|
|
|
pubActive: pubActive,
|
2022-02-10 09:45:12 +01:00
|
|
|
};
|
2022-02-05 16:13:52 +01:00
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-02-08 15:43:51 +01:00
|
|
|
public async deliverd(host: string, succeeded: boolean): Promise<void> {
|
2023-01-13 05:40:33 +01:00
|
|
|
await this.commit(
|
|
|
|
succeeded
|
|
|
|
? {
|
|
|
|
deliveredInstances: [host],
|
|
|
|
}
|
|
|
|
: {
|
|
|
|
stalled: [host],
|
|
|
|
},
|
|
|
|
);
|
2022-02-05 16:13:52 +01:00
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-02-05 16:13:52 +01:00
|
|
|
public async inbox(host: string): Promise<void> {
|
|
|
|
await this.commit({
|
2023-01-13 05:40:33 +01:00
|
|
|
inboxInstances: [host],
|
2019-04-07 14:50:36 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|