summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/federation/instances.ts
blob: 6b6fe42a403eed6c263790fdca5c03a481dd0159 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import $ from 'cafy';
import define from '../../define';
import { Instances } from '../../../../models';
import { fetchMeta } from '../../../../misc/fetch-meta';

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

	requireCredential: false as const,

	params: {
		host: {
			validator: $.optional.nullable.str,
		},

		blocked: {
			validator: $.optional.nullable.bool,
		},

		notResponding: {
			validator: $.optional.nullable.bool,
		},

		suspended: {
			validator: $.optional.nullable.bool,
		},

		federating: {
			validator: $.optional.nullable.bool,
		},

		subscribing: {
			validator: $.optional.nullable.bool,
		},

		publishing: {
			validator: $.optional.nullable.bool,
		},

		limit: {
			validator: $.optional.num.range(1, 100),
			default: 30
		},

		offset: {
			validator: $.optional.num.min(0),
			default: 0
		},

		sort: {
			validator: $.optional.str,
		}
	}
};

export default define(meta, async (ps, me) => {
	const query = Instances.createQueryBuilder('instance');

	switch (ps.sort) {
		case '+pubSub': query.orderBy('instance.followingCount', 'DESC').orderBy('instance.followersCount', 'DESC'); break;
		case '-pubSub': query.orderBy('instance.followingCount', 'ASC').orderBy('instance.followersCount', 'ASC'); break;
		case '+notes': query.orderBy('instance.notesCount', 'DESC'); break;
		case '-notes': query.orderBy('instance.notesCount', 'ASC'); break;
		case '+users': query.orderBy('instance.usersCount', 'DESC'); break;
		case '-users': query.orderBy('instance.usersCount', 'ASC'); break;
		case '+following': query.orderBy('instance.followingCount', 'DESC'); break;
		case '-following': query.orderBy('instance.followingCount', 'ASC'); break;
		case '+followers': query.orderBy('instance.followersCount', 'DESC'); break;
		case '-followers': query.orderBy('instance.followersCount', 'ASC'); break;
		case '+caughtAt': query.orderBy('instance.caughtAt', 'DESC'); break;
		case '-caughtAt': query.orderBy('instance.caughtAt', 'ASC'); break;
		case '+lastCommunicatedAt': query.orderBy('instance.lastCommunicatedAt', 'DESC'); break;
		case '-lastCommunicatedAt': query.orderBy('instance.lastCommunicatedAt', 'ASC'); break;
		case '+driveUsage': query.orderBy('instance.driveUsage', 'DESC'); break;
		case '-driveUsage': query.orderBy('instance.driveUsage', 'ASC'); break;
		case '+driveFiles': query.orderBy('instance.driveFiles', 'DESC'); break;
		case '-driveFiles': query.orderBy('instance.driveFiles', 'ASC'); break;

		default: query.orderBy('instance.id', 'DESC'); break;
	}

	if (typeof ps.blocked === 'boolean') {
		const meta = await fetchMeta(true);
		if (ps.blocked) {
			query.andWhere('instance.host IN (:...blocks)', { blocks: meta.blockedHosts });
		} else {
			query.andWhere('instance.host NOT IN (:...blocks)', { blocks: meta.blockedHosts });
		}
	}

	if (typeof ps.notResponding === 'boolean') {
		if (ps.notResponding) {
			query.andWhere('instance.isNotResponding = TRUE');
		} else {
			query.andWhere('instance.isNotResponding = FALSE');
		}
	}

	if (typeof ps.suspended === 'boolean') {
		if (ps.suspended) {
			query.andWhere('instance.isSuspended = TRUE');
		} else {
			query.andWhere('instance.isSuspended = FALSE');
		}
	}

	if (typeof ps.federating === 'boolean') {
		if (ps.federating) {
			query.andWhere('((instance.followingCount > 0) OR (instance.followersCount > 0))');
		} else {
			query.andWhere('((instance.followingCount = 0) AND (instance.followersCount = 0))');
		}
	}

	if (typeof ps.subscribing === 'boolean') {
		if (ps.subscribing) {
			query.andWhere('instance.followersCount > 0');
		} else {
			query.andWhere('instance.followersCount = 0');
		}
	}

	if (typeof ps.publishing === 'boolean') {
		if (ps.publishing) {
			query.andWhere('instance.followingCount > 0');
		} else {
			query.andWhere('instance.followingCount = 0');
		}
	}

	if (ps.host) {
		query.andWhere('instance.host like :host', { host: '%' + ps.host.toLowerCase() + '%' });
	}

	const instances = await query.take(ps.limit!).skip(ps.offset).getMany();

	return instances;
});