2023-07-27 14:31:52 +09:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-09-15 14:28:29 +09:00
|
|
|
import type { UsersRepository } from '@/models/_.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { QueryService } from '@/core/QueryService.js';
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2016-12-29 07:49:51 +09:00
|
|
|
|
2018-11-02 12:49:08 +09:00
|
|
|
export const meta = {
|
2019-02-23 11:20:58 +09:00
|
|
|
tags: ['users'],
|
|
|
|
|
2022-01-18 22:27:10 +09:00
|
|
|
requireCredential: false,
|
2018-11-02 12:49:08 +09:00
|
|
|
|
2019-02-24 19:42:26 +09:00
|
|
|
res: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-24 19:42:26 +09:00
|
|
|
items: {
|
2022-01-18 22:27:10 +09:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
|
|
|
ref: 'UserDetailed',
|
2021-12-09 23:58:30 +09:00
|
|
|
},
|
2019-02-24 19:42:26 +09:00
|
|
|
},
|
2022-01-18 22:27:10 +09:00
|
|
|
} as const;
|
2017-03-04 04:28:38 +09:00
|
|
|
|
2022-02-20 13:15:40 +09:00
|
|
|
export const paramDef = {
|
2022-02-19 14:05:32 +09:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
offset: { type: 'integer', default: 0 },
|
|
|
|
sort: { type: 'string', enum: ['+follower', '-follower', '+createdAt', '-createdAt', '+updatedAt', '-updatedAt'] },
|
2023-01-12 21:02:26 +09:00
|
|
|
state: { type: 'string', enum: ['all', 'alive'], default: 'all' },
|
2022-06-14 18:01:23 +09:00
|
|
|
origin: { type: 'string', enum: ['combined', 'local', 'remote'], default: 'local' },
|
2022-06-30 15:32:11 +09:00
|
|
|
hostname: {
|
|
|
|
type: 'string',
|
|
|
|
nullable: true,
|
|
|
|
default: null,
|
|
|
|
description: 'The local host is represented with `null`.',
|
|
|
|
},
|
2022-02-19 14:05:32 +09:00
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
@Injectable()
|
2023-08-17 21:20:58 +09:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-18 03:27:08 +09:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
2019-02-16 06:50:58 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private queryService: QueryService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-05-07 17:36:13 +09:00
|
|
|
const query = this.usersRepository.createQueryBuilder('user')
|
|
|
|
.where('user.isExplorable = TRUE')
|
|
|
|
.andWhere('user.isSuspended = FALSE');
|
2019-02-16 06:50:58 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
switch (ps.state) {
|
|
|
|
case 'alive': query.andWhere('user.updatedAt > :date', { date: new Date(Date.now() - 1000 * 60 * 60 * 24 * 5) }); break;
|
|
|
|
}
|
2018-11-23 08:01:14 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
switch (ps.origin) {
|
|
|
|
case 'local': query.andWhere('user.host IS NULL'); break;
|
|
|
|
case 'remote': query.andWhere('user.host IS NOT NULL'); break;
|
|
|
|
}
|
2022-06-30 15:32:11 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (ps.hostname) {
|
|
|
|
query.andWhere('user.host = :hostname', { hostname: ps.hostname.toLowerCase() });
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ps.sort) {
|
|
|
|
case '+follower': query.orderBy('user.followersCount', 'DESC'); break;
|
|
|
|
case '-follower': query.orderBy('user.followersCount', 'ASC'); break;
|
|
|
|
case '+createdAt': query.orderBy('user.createdAt', 'DESC'); break;
|
|
|
|
case '-createdAt': query.orderBy('user.createdAt', 'ASC'); break;
|
|
|
|
case '+updatedAt': query.andWhere('user.updatedAt IS NOT NULL').orderBy('user.updatedAt', 'DESC'); break;
|
|
|
|
case '-updatedAt': query.andWhere('user.updatedAt IS NOT NULL').orderBy('user.updatedAt', 'ASC'); break;
|
|
|
|
default: query.orderBy('user.id', 'ASC'); break;
|
|
|
|
}
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
if (me) this.queryService.generateMutedUserQueryForUsers(query, me);
|
|
|
|
if (me) this.queryService.generateBlockQueryForUsers(query, me);
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2023-07-08 16:53:07 +09:00
|
|
|
query.limit(ps.limit);
|
2023-07-25 19:21:50 +09:00
|
|
|
query.offset(ps.offset);
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
const users = await query.getMany();
|
2019-04-07 21:50:36 +09:00
|
|
|
|
2022-09-18 03:27:08 +09:00
|
|
|
return await this.userEntityService.packMany(users, me, { detail: true });
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|