2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { InstancesRepository } from '@/models/index.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { InstanceEntityService } from '@/core/entities/InstanceEntityService.js';
|
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-01-08 11:32:17 +00:00
|
|
|
import { sqlLikeEscape } from '@/misc/sql-like-escape';
|
2019-02-07 09:11:20 +00:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 02:20:58 +00:00
|
|
|
tags: ['federation'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: false,
|
2019-02-07 09:11:20 +00:00
|
|
|
|
2021-03-06 13:34:11 +00:00
|
|
|
res: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 13:34:11 +00:00
|
|
|
items: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2021-12-09 14:58:30 +00:00
|
|
|
ref: 'FederationInstance',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2019-02-07 09:11:20 +00:00
|
|
|
|
2022-02-20 04:15:40 +00:00
|
|
|
export const paramDef = {
|
2022-02-19 05:05:32 +00:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
2022-04-03 04:57:26 +00:00
|
|
|
host: { type: 'string', nullable: true, description: 'Omit or use `null` to not filter by host.' },
|
2022-02-19 05:05:32 +00:00
|
|
|
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 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-09-17 18:27:08 +00:00
|
|
|
@Injectable()
|
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.instancesRepository)
|
|
|
|
private instancesRepository: InstancesRepository,
|
|
|
|
|
|
|
|
private instanceEntityService: InstanceEntityService,
|
|
|
|
private metaService: MetaService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const query = this.instancesRepository.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;
|
2023-01-05 07:38:12 +00:00
|
|
|
case '+latestRequestReceivedAt': query.orderBy('instance.latestRequestReceivedAt', 'DESC', 'NULLS LAST'); break;
|
|
|
|
case '-latestRequestReceivedAt': query.orderBy('instance.latestRequestReceivedAt', 'ASC', 'NULLS FIRST'); break;
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
default: query.orderBy('instance.id', 'DESC'); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof ps.blocked === 'boolean') {
|
|
|
|
const meta = await this.metaService.fetch(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) {
|
2023-01-08 11:32:17 +00:00
|
|
|
query.andWhere('instance.host like :host', { host: '%' + sqlLikeEscape(ps.host.toLowerCase()) + '%' });
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const instances = await query.take(ps.limit).skip(ps.offset).getMany();
|
|
|
|
|
|
|
|
return await this.instanceEntityService.packMany(instances);
|
|
|
|
});
|
2019-04-07 12:50:36 +00:00
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|