2021-08-19 15:55:45 +03:00
|
|
|
import config from '@/config/index';
|
|
|
|
import define from '../../define';
|
|
|
|
import { Instances } from '@/models/index';
|
|
|
|
import { fetchMeta } from '@/misc/fetch-meta';
|
2019-02-07 11:11:20 +02:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 04:20:58 +02:00
|
|
|
tags: ['federation'],
|
|
|
|
|
2022-01-18 15:27:10 +02:00
|
|
|
requireCredential: false,
|
2019-02-07 11:11:20 +02:00
|
|
|
|
2021-03-06 15:34:11 +02:00
|
|
|
res: {
|
2022-01-18 15:27:10 +02:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 15:34:11 +02:00
|
|
|
items: {
|
2022-01-18 15:27:10 +02:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2021-12-09 16:58:30 +02:00
|
|
|
ref: 'FederationInstance',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 15:27:10 +02:00
|
|
|
} as const;
|
2019-02-07 11:11:20 +02:00
|
|
|
|
2022-02-20 06:15:40 +02:00
|
|
|
export const paramDef = {
|
2022-02-19 07:05:32 +02:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
host: { type: 'string', nullable: true },
|
|
|
|
blocked: { type: 'boolean', nullable: true },
|
|
|
|
notResponding: { type: 'boolean', nullable: true },
|
|
|
|
suspended: { type: 'boolean', nullable: true },
|
|
|
|
federating: { type: 'boolean', nullable: true },
|
|
|
|
subscribing: { type: 'boolean', nullable: true },
|
|
|
|
publishing: { type: 'boolean', nullable: true },
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 30 },
|
|
|
|
offset: { type: 'integer', default: 0 },
|
|
|
|
sort: { type: 'string' },
|
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 19:12:50 +02:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 07:05:32 +02:00
|
|
|
export default define(meta, paramDef, async (ps, me) => {
|
2019-04-07 15:50:36 +03:00
|
|
|
const query = Instances.createQueryBuilder('instance');
|
|
|
|
|
|
|
|
switch (ps.sort) {
|
2020-01-29 21:37:25 +02: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 15:50:36 +03:00
|
|
|
case '+notes': query.orderBy('instance.notesCount', 'DESC'); break;
|
|
|
|
case '-notes': query.orderBy('instance.notesCount', 'ASC'); break;
|
2019-07-13 21:53:45 +03: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 15:50:36 +03: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 02:11:19 +03:00
|
|
|
const meta = await fetchMeta(true);
|
2019-04-07 15:50:36 +03: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 11:11:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
if (typeof ps.notResponding === 'boolean') {
|
|
|
|
if (ps.notResponding) {
|
|
|
|
query.andWhere('instance.isNotResponding = TRUE');
|
|
|
|
} else {
|
|
|
|
query.andWhere('instance.isNotResponding = FALSE');
|
|
|
|
}
|
|
|
|
}
|
2019-02-07 21:26:43 +02:00
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
if (typeof ps.suspended === 'boolean') {
|
|
|
|
if (ps.suspended) {
|
|
|
|
query.andWhere('instance.isSuspended = TRUE');
|
2019-04-07 15:50:36 +03:00
|
|
|
} else {
|
2020-01-29 21:37:25 +02:00
|
|
|
query.andWhere('instance.isSuspended = FALSE');
|
2019-04-07 15:50:36 +03:00
|
|
|
}
|
|
|
|
}
|
2019-02-07 21:26:43 +02:00
|
|
|
|
2020-01-29 21:37:25 +02: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 11:25:16 +03:00
|
|
|
query.andWhere('instance.host like :host', { host: '%' + ps.host.toLowerCase() + '%' });
|
2020-01-29 21:37:25 +02:00
|
|
|
}
|
|
|
|
|
2022-02-19 07:05:32 +02:00
|
|
|
const instances = await query.take(ps.limit).skip(ps.offset).getMany();
|
2019-02-07 11:11:20 +02:00
|
|
|
|
2022-02-19 07:05:32 +02:00
|
|
|
return await Instances.packMany(instances);
|
2019-02-22 04:46:58 +02:00
|
|
|
});
|