2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-06-14 11:01:23 +02:00
|
|
|
import { Brackets } from 'typeorm';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { UsersRepository, UserProfilesRepository } from '@/models/_.js';
|
2023-08-16 10:51:28 +02:00
|
|
|
import type { MiUser } from '@/models/entities/User.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-01-09 00:58:16 +01:00
|
|
|
import { sqlLikeEscape } from '@/misc/sql-like-escape.js';
|
2018-08-06 11:28:27 +02:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 03:20:58 +01:00
|
|
|
tags: ['users'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: false,
|
2018-08-06 11:28:27 +02:00
|
|
|
|
2022-06-10 07:25:20 +02:00
|
|
|
description: 'Search for users.',
|
|
|
|
|
2019-02-24 11:42:26 +01:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-24 11:42:26 +01:00
|
|
|
items: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 15:35:26 +02:00
|
|
|
ref: 'User',
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2019-02-24 11:42:26 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
query: { type: 'string' },
|
|
|
|
offset: { type: 'integer', default: 0 },
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
2022-06-14 11:01:23 +02:00
|
|
|
origin: { type: 'string', enum: ['local', 'remote', 'combined'], default: 'combined' },
|
2022-02-19 06:05:32 +01:00
|
|
|
detail: { type: 'boolean', default: true },
|
|
|
|
},
|
|
|
|
required: ['query'],
|
|
|
|
} as const;
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
2023-08-17 14:20:58 +02:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-17 20:27:08 +02:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.userProfilesRepository)
|
|
|
|
private userProfilesRepository: UserProfilesRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
|
|
|
const activeThreshold = new Date(Date.now() - (1000 * 60 * 60 * 24 * 30)); // 30日
|
|
|
|
|
2023-07-08 10:41:52 +02:00
|
|
|
ps.query = ps.query.trim();
|
2022-09-17 20:27:08 +02:00
|
|
|
const isUsername = ps.query.startsWith('@');
|
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
let users: MiUser[] = [];
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
if (isUsername) {
|
|
|
|
const usernameQuery = this.usersRepository.createQueryBuilder('user')
|
2023-01-08 12:32:17 +01:00
|
|
|
.where('user.usernameLower LIKE :username', { username: sqlLikeEscape(ps.query.replace('@', '').toLowerCase()) + '%' })
|
2022-09-17 20:27:08 +02:00
|
|
|
.andWhere(new Brackets(qb => { qb
|
|
|
|
.where('user.updatedAt IS NULL')
|
|
|
|
.orWhere('user.updatedAt > :activeThreshold', { activeThreshold: activeThreshold });
|
|
|
|
}))
|
|
|
|
.andWhere('user.isSuspended = FALSE');
|
|
|
|
|
|
|
|
if (ps.origin === 'local') {
|
|
|
|
usernameQuery.andWhere('user.host IS NULL');
|
|
|
|
} else if (ps.origin === 'remote') {
|
|
|
|
usernameQuery.andWhere('user.host IS NOT NULL');
|
|
|
|
}
|
|
|
|
|
|
|
|
users = await usernameQuery
|
|
|
|
.orderBy('user.updatedAt', 'DESC', 'NULLS LAST')
|
2023-07-08 09:53:07 +02:00
|
|
|
.limit(ps.limit)
|
2023-07-25 12:21:50 +02:00
|
|
|
.offset(ps.offset)
|
2022-09-17 20:27:08 +02:00
|
|
|
.getMany();
|
|
|
|
} else {
|
|
|
|
const nameQuery = this.usersRepository.createQueryBuilder('user')
|
2023-07-08 00:08:16 +02:00
|
|
|
.where(new Brackets(qb => {
|
2023-01-08 12:32:17 +01:00
|
|
|
qb.where('user.name ILIKE :query', { query: '%' + sqlLikeEscape(ps.query) + '%' });
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
// Also search username if it qualifies as username
|
|
|
|
if (this.userEntityService.validateLocalUsername(ps.query)) {
|
2023-01-08 12:32:17 +01:00
|
|
|
qb.orWhere('user.usernameLower LIKE :username', { username: '%' + sqlLikeEscape(ps.query.toLowerCase()) + '%' });
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
}))
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
|
|
|
.where('user.updatedAt IS NULL')
|
|
|
|
.orWhere('user.updatedAt > :activeThreshold', { activeThreshold: activeThreshold });
|
|
|
|
}))
|
|
|
|
.andWhere('user.isSuspended = FALSE');
|
|
|
|
|
|
|
|
if (ps.origin === 'local') {
|
|
|
|
nameQuery.andWhere('user.host IS NULL');
|
|
|
|
} else if (ps.origin === 'remote') {
|
|
|
|
nameQuery.andWhere('user.host IS NOT NULL');
|
|
|
|
}
|
|
|
|
|
|
|
|
users = await nameQuery
|
|
|
|
.orderBy('user.updatedAt', 'DESC', 'NULLS LAST')
|
2023-07-08 09:53:07 +02:00
|
|
|
.limit(ps.limit)
|
2023-07-25 12:21:50 +02:00
|
|
|
.offset(ps.offset)
|
2022-09-17 20:27:08 +02:00
|
|
|
.getMany();
|
|
|
|
|
|
|
|
if (users.length < ps.limit) {
|
|
|
|
const profQuery = this.userProfilesRepository.createQueryBuilder('prof')
|
|
|
|
.select('prof.userId')
|
2023-01-08 12:32:17 +01:00
|
|
|
.where('prof.description ILIKE :query', { query: '%' + sqlLikeEscape(ps.query) + '%' });
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
if (ps.origin === 'local') {
|
|
|
|
profQuery.andWhere('prof.userHost IS NULL');
|
|
|
|
} else if (ps.origin === 'remote') {
|
|
|
|
profQuery.andWhere('prof.userHost IS NOT NULL');
|
|
|
|
}
|
|
|
|
|
|
|
|
const query = this.usersRepository.createQueryBuilder('user')
|
|
|
|
.where(`user.id IN (${ profQuery.getQuery() })`)
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
|
|
|
.where('user.updatedAt IS NULL')
|
|
|
|
.orWhere('user.updatedAt > :activeThreshold', { activeThreshold: activeThreshold });
|
|
|
|
}))
|
|
|
|
.andWhere('user.isSuspended = FALSE')
|
|
|
|
.setParameters(profQuery.getParameters());
|
|
|
|
|
|
|
|
users = users.concat(await query
|
|
|
|
.orderBy('user.updatedAt', 'DESC', 'NULLS LAST')
|
2023-07-08 09:53:07 +02:00
|
|
|
.limit(ps.limit)
|
2023-07-25 12:21:50 +02:00
|
|
|
.offset(ps.offset)
|
2022-09-17 20:27:08 +02:00
|
|
|
.getMany(),
|
|
|
|
);
|
2022-05-14 08:24:44 +02:00
|
|
|
}
|
2021-10-17 09:26:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
return await this.userEntityService.packMany(users, me, { detail: ps.detail });
|
|
|
|
});
|
2018-08-06 11:28:27 +02:00
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|