Skip to content
Snippets Groups Projects
search-by-username-and-host.ts 3.91 KiB
Newer Older
import { Brackets } from 'typeorm';
import { Inject, Injectable } from '@nestjs/common';
syuilo's avatar
syuilo committed
import type { UsersRepository, FollowingsRepository } from '@/models/index.js';
import type { User } from '@/models/entities/User.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { UserEntityService } from '@/core/entities/UserEntityService.js';
import { DI } from '@/di-symbols.js';
syuilo's avatar
syuilo committed
import { sqlLikeEscape } from '@/misc/sql-like-escape.js';
syuilo's avatar
syuilo committed

export const meta = {
	tags: ['users'],

syuilo's avatar
syuilo committed

	description: 'Search for a user by username and/or host.',

syuilo's avatar
syuilo committed
	res: {
		type: 'array',
		optional: false, nullable: false,
syuilo's avatar
syuilo committed
		items: {
			type: 'object',
			optional: false, nullable: false,
syuilo's avatar
syuilo committed
			ref: 'User',
syuilo's avatar
syuilo committed
		},
syuilo's avatar
syuilo committed
	},
syuilo's avatar
syuilo committed

tamaina's avatar
tamaina committed
export const paramDef = {
	type: 'object',
	properties: {
		limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
		detail: { type: 'boolean', default: true },
	},
		{
			properties: {
				username: { type: 'string', nullable: true },
			},
syuilo's avatar
syuilo committed
			required: ['username'],
		},
		{
			properties: {
				host: { type: 'string', nullable: true },
			},
syuilo's avatar
syuilo committed
			required: ['host'],
syuilo's avatar
syuilo committed
// TODO: avatar,bannerをJOINしたいけどエラーになる

syuilo's avatar
syuilo committed
// eslint-disable-next-line import/no-default-export
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> {
	constructor(
		@Inject(DI.usersRepository)
		private usersRepository: UsersRepository,

		@Inject(DI.followingsRepository)
		private followingsRepository: FollowingsRepository,

		private userEntityService: UserEntityService,
	) {
		super(meta, paramDef, async (ps, me) => {
			const setUsernameAndHostQuery = (query = this.usersRepository.createQueryBuilder('user')) => {
				if (ps.username) {
syuilo's avatar
syuilo committed
					query.andWhere('user.usernameLower LIKE :username', { username: sqlLikeEscape(ps.username.toLowerCase()) + '%' });
				if (ps.host) {
					if (ps.host === this.config.hostname || ps.host === '.') {
						query.andWhere('user.host IS NULL');
					} else {
						query.andWhere('user.host LIKE :host', {
syuilo's avatar
syuilo committed
							host: sqlLikeEscape(ps.host.toLowerCase()) + '%',
			const activeThreshold = new Date(Date.now() - (1000 * 60 * 60 * 24 * 30)); // 30日
			if (me) {
				const followingQuery = this.followingsRepository.createQueryBuilder('following')
					.select('following.followeeId')
					.where('following.followerId = :followerId', { followerId: me.id });
				const query = setUsernameAndHostQuery()
					.andWhere(`user.id IN (${ followingQuery.getQuery() })`)
					.andWhere('user.id != :meId', { meId: me.id })
					.andWhere('user.isSuspended = FALSE')
					.andWhere(new Brackets(qb => { qb
						.where('user.updatedAt IS NULL')
						.orWhere('user.updatedAt > :activeThreshold', { activeThreshold: activeThreshold });
					}));
				users = await query
					.orderBy('user.usernameLower', 'ASC')
					.take(ps.limit)
					.getMany();
				if (users.length < ps.limit) {
					const otherQuery = setUsernameAndHostQuery()
						.andWhere(`user.id NOT IN (${ followingQuery.getQuery() })`)
						.andWhere('user.isSuspended = FALSE')
						.andWhere('user.updatedAt IS NOT NULL');
						.orderBy('user.updatedAt', 'DESC')
						.take(ps.limit - users.length)
						.getMany();

					users = users.concat(otherUsers);
				}
			} else {
				const query = setUsernameAndHostQuery()
					.andWhere('user.isSuspended = FALSE')
					.andWhere('user.updatedAt IS NOT NULL');

				users = await query
					.orderBy('user.updatedAt', 'DESC')
					.take(ps.limit - users.length)
					.getMany();
syuilo's avatar
syuilo committed

			return await this.userEntityService.packMany(users, me, { detail: !!ps.detail });