From 2d196b6779e2c4a0594fdd512c6cd4caaba043b0 Mon Sep 17 00:00:00 2001 From: syuilo Date: Wed, 20 Oct 2021 00:53:38 +0900 Subject: [PATCH] Update search-by-username-and-host.ts --- .../users/search-by-username-and-host.ts | 69 ++++++++++++------- 1 file changed, 45 insertions(+), 24 deletions(-) diff --git a/src/server/api/endpoints/users/search-by-username-and-host.ts b/src/server/api/endpoints/users/search-by-username-and-host.ts index 3b8d024af5..f03a55449d 100644 --- a/src/server/api/endpoints/users/search-by-username-and-host.ts +++ b/src/server/api/endpoints/users/search-by-username-and-host.ts @@ -1,8 +1,9 @@ import $ from 'cafy'; import define from '../../define'; -import { Users } from '@/models/index'; +import { Followings, Users } from '@/models/index'; import { Brackets } from 'typeorm'; import { USER_ACTIVE_THRESHOLD } from '@/const'; +import { User } from '@/models/entities/user'; export const meta = { tags: ['users'], @@ -18,11 +19,6 @@ export const meta = { validator: $.optional.nullable.str, }, - offset: { - validator: $.optional.num.min(0), - default: 0, - }, - limit: { validator: $.optional.num.range(1, 100), default: 10, @@ -60,34 +56,59 @@ export default define(meta, async (ps, me) => { q.andWhere('user.updatedAt IS NOT NULL'); q.orderBy('user.updatedAt', 'DESC'); - const users = await q.take(ps.limit!).skip(ps.offset).getMany(); + const users = await q.take(ps.limit!).getMany(); return await Users.packMany(users, me, { detail: ps.detail }); } else if (ps.username) { - let users = await Users.createQueryBuilder('user') - .where('user.host IS NULL') - .andWhere('user.isSuspended = FALSE') - .andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' }) - .andWhere(new Brackets(qb => { qb - .where('user.updatedAt IS NULL') - .orWhere('user.updatedAt > :activeThreshold', { activeThreshold: activeThreshold }); - })) - .orderBy('user.updatedAt', 'DESC', 'NULLS LAST') - .take(ps.limit!) - .skip(ps.offset) - .getMany(); + let users: User[] = []; - if (users.length < ps.limit!) { - const otherUsers = await Users.createQueryBuilder('user') - .where('user.host IS NOT NULL') + if (me) { + const followingQuery = Followings.createQueryBuilder('following') + .select('following.followeeId') + .where('following.followerId = :followerId', { followerId: me.id }); + + const query = Users.createQueryBuilder('user') + .where(`user.id IN (${ followingQuery.getQuery() })`) + .andWhere(`user.id != :meId`, { meId: me.id }) .andWhere('user.isSuspended = FALSE') .andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' }) + .andWhere(new Brackets(qb => { qb + .where('user.updatedAt IS NULL') + .orWhere('user.updatedAt > :activeThreshold', { activeThreshold: activeThreshold }); + })); + + query.setParameters(followingQuery.getParameters()); + + users = await query + .orderBy('user.usernameLower', 'ASC') + .take(ps.limit!) + .getMany(); + + if (users.length < ps.limit!) { + const otherQuery = await Users.createQueryBuilder('user') + .where(`user.id NOT IN (${ followingQuery.getQuery() })`) + .andWhere(`user.id != :meId`, { meId: me.id }) + .andWhere('user.isSuspended = FALSE') + .andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' }) + .andWhere('user.updatedAt IS NOT NULL'); + + otherQuery.setParameters(followingQuery.getParameters()); + + const otherUsers = await otherQuery + .orderBy('user.updatedAt', 'DESC') + .take(ps.limit! - users.length) + .getMany(); + + users = users.concat(otherUsers); + } + } else { + users = await Users.createQueryBuilder('user') + .where('user.isSuspended = FALSE') + .andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' }) .andWhere('user.updatedAt IS NOT NULL') .orderBy('user.updatedAt', 'DESC') .take(ps.limit! - users.length) .getMany(); - - users = users.concat(otherUsers); } return await Users.packMany(users, me, { detail: ps.detail });