2019-02-07 10:11:20 +01:00
|
|
|
import $ from 'cafy';
|
2021-03-06 14:34:11 +01:00
|
|
|
import config from '../../../../config';
|
2019-02-07 10:11:20 +01:00
|
|
|
import define from '../../define';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { Instances } from '../../../../models';
|
2019-04-24 01:11:19 +02:00
|
|
|
import { fetchMeta } from '../../../../misc/fetch-meta';
|
2019-02-07 10:11:20 +01:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['federation'],
|
|
|
|
|
2020-02-15 13:33:32 +01:00
|
|
|
requireCredential: false as const,
|
2019-02-07 10:11:20 +01:00
|
|
|
|
|
|
|
params: {
|
2020-01-29 20:37:25 +01:00
|
|
|
host: {
|
|
|
|
validator: $.optional.nullable.str,
|
|
|
|
},
|
|
|
|
|
2019-02-08 12:56:16 +01:00
|
|
|
blocked: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.nullable.bool,
|
2019-02-08 12:56:16 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
notResponding: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.nullable.bool,
|
2019-02-08 12:56:16 +01:00
|
|
|
},
|
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
suspended: {
|
|
|
|
validator: $.optional.nullable.bool,
|
|
|
|
},
|
|
|
|
|
|
|
|
federating: {
|
|
|
|
validator: $.optional.nullable.bool,
|
|
|
|
},
|
|
|
|
|
|
|
|
subscribing: {
|
|
|
|
validator: $.optional.nullable.bool,
|
|
|
|
},
|
|
|
|
|
|
|
|
publishing: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.nullable.bool,
|
2019-02-07 20:26:43 +01:00
|
|
|
},
|
|
|
|
|
2019-02-07 10:11:20 +01:00
|
|
|
limit: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.num.range(1, 100),
|
2019-02-07 10:11:20 +01:00
|
|
|
default: 30
|
|
|
|
},
|
|
|
|
|
|
|
|
offset: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.num.min(0),
|
2019-02-07 10:11:20 +01:00
|
|
|
default: 0
|
|
|
|
},
|
|
|
|
|
|
|
|
sort: {
|
2019-02-13 08:33:07 +01:00
|
|
|
validator: $.optional.str,
|
2019-02-07 10:11:20 +01:00
|
|
|
}
|
2021-03-06 14:34:11 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
|
|
|
type: 'array' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
items: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
ref: 'FederationInstance'
|
|
|
|
}
|
2019-02-07 10:11:20 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
export default define(meta, async (ps, me) => {
|
2019-04-07 14:50:36 +02:00
|
|
|
const query = Instances.createQueryBuilder('instance');
|
|
|
|
|
|
|
|
switch (ps.sort) {
|
2020-01-29 20:37:25 +01:00
|
|
|
case '+pubSub': query.orderBy('instance.followingCount', 'DESC').orderBy('instance.followersCount', 'DESC'); break;
|
|
|
|
case '-pubSub': query.orderBy('instance.followingCount', 'ASC').orderBy('instance.followersCount', 'ASC'); break;
|
2019-04-07 14:50:36 +02:00
|
|
|
case '+notes': query.orderBy('instance.notesCount', 'DESC'); break;
|
|
|
|
case '-notes': query.orderBy('instance.notesCount', 'ASC'); break;
|
2019-07-13 20:53:45 +02:00
|
|
|
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;
|
2019-04-07 14:50:36 +02:00
|
|
|
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') {
|
2019-04-24 01:11:19 +02:00
|
|
|
const meta = await fetchMeta(true);
|
2019-04-07 14:50:36 +02:00
|
|
|
if (ps.blocked) {
|
|
|
|
query.andWhere('instance.host IN (:...blocks)', { blocks: meta.blockedHosts });
|
|
|
|
} else {
|
|
|
|
query.andWhere('instance.host NOT IN (:...blocks)', { blocks: meta.blockedHosts });
|
2019-02-07 10:11:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (typeof ps.notResponding === 'boolean') {
|
|
|
|
if (ps.notResponding) {
|
|
|
|
query.andWhere('instance.isNotResponding = TRUE');
|
|
|
|
} else {
|
|
|
|
query.andWhere('instance.isNotResponding = FALSE');
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 20:26:43 +01:00
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
if (typeof ps.suspended === 'boolean') {
|
|
|
|
if (ps.suspended) {
|
|
|
|
query.andWhere('instance.isSuspended = TRUE');
|
2019-04-07 14:50:36 +02:00
|
|
|
} else {
|
2020-01-29 20:37:25 +01:00
|
|
|
query.andWhere('instance.isSuspended = FALSE');
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
}
|
2019-02-07 20:26:43 +01:00
|
|
|
|
2020-01-29 20:37:25 +01:00
|
|
|
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) {
|
2020-05-10 10:25:16 +02:00
|
|
|
query.andWhere('instance.host like :host', { host: '%' + ps.host.toLowerCase() + '%' });
|
2020-01-29 20:37:25 +01:00
|
|
|
}
|
|
|
|
|
2019-04-12 18:43:22 +02:00
|
|
|
const instances = await query.take(ps.limit!).skip(ps.offset).getMany();
|
2019-02-07 10:11:20 +01:00
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
return instances;
|
|
|
|
});
|