summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/federation/stats.ts
blob: cbe47dc7cbc272f3875d2a580b99282fe57fbe7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { IsNull, MoreThan, Not } from 'typeorm';
import { Followings, Instances } from '@/models/index.js';
import { awaitAll } from '@/prelude/await-all.js';
import define from '../../define.js';

export const meta = {
	tags: ['federation'],

	requireCredential: false,

	allowGet: true,
	cacheSec: 60 * 60,
} as const;

export const paramDef = {
	type: 'object',
	properties: {
		limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
	},
	required: [],
} as const;

// eslint-disable-next-line import/no-default-export
export default define(meta, paramDef, async (ps) => {
	const [topSubInstances, topPubInstances, allSubCount, allPubCount] = await Promise.all([
		Instances.find({
			where: {
				followersCount: MoreThan(0),
			},
			order: {
				followersCount: 'DESC',
			},
			take: ps.limit,
		}),
		Instances.find({
			where: {
				followingCount: MoreThan(0),
			},
			order: {
				followingCount: 'DESC',
			},
			take: ps.limit,
		}),
		Followings.count({
			where: {
				followeeHost: Not(IsNull()),
			},
		}),
		Followings.count({
			where: {
				followerHost: Not(IsNull()),
			},
		}),
	]);

	const gotSubCount = topSubInstances.map(x => x.followersCount).reduce((a, b) => a + b, 0);
	const gotPubCount = topSubInstances.map(x => x.followingCount).reduce((a, b) => a + b, 0);

	return await awaitAll({
		topSubInstances: Instances.packMany(topSubInstances),
		otherFollowersCount: Math.max(0, allSubCount - gotSubCount),
		topPubInstances: Instances.packMany(topPubInstances),
		otherFollowingCount: Math.max(0, allPubCount - gotPubCount),
	});
});