Update search-by-username-and-host.ts

This commit is contained in:
syuilo 2021-10-20 00:53:38 +09:00
parent 0cc055de3a
commit 2d196b6779
1 changed files with 45 additions and 24 deletions

View File

@ -1,8 +1,9 @@
import $ from 'cafy'; import $ from 'cafy';
import define from '../../define'; import define from '../../define';
import { Users } from '@/models/index'; import { Followings, Users } from '@/models/index';
import { Brackets } from 'typeorm'; import { Brackets } from 'typeorm';
import { USER_ACTIVE_THRESHOLD } from '@/const'; import { USER_ACTIVE_THRESHOLD } from '@/const';
import { User } from '@/models/entities/user';
export const meta = { export const meta = {
tags: ['users'], tags: ['users'],
@ -18,11 +19,6 @@ export const meta = {
validator: $.optional.nullable.str, validator: $.optional.nullable.str,
}, },
offset: {
validator: $.optional.num.min(0),
default: 0,
},
limit: { limit: {
validator: $.optional.num.range(1, 100), validator: $.optional.num.range(1, 100),
default: 10, default: 10,
@ -60,35 +56,60 @@ export default define(meta, async (ps, me) => {
q.andWhere('user.updatedAt IS NOT NULL'); q.andWhere('user.updatedAt IS NOT NULL');
q.orderBy('user.updatedAt', 'DESC'); 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 }); return await Users.packMany(users, me, { detail: ps.detail });
} else if (ps.username) { } else if (ps.username) {
let users = await Users.createQueryBuilder('user') let users: User[] = [];
.where('user.host IS 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.isSuspended = FALSE')
.andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' }) .andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' })
.andWhere(new Brackets(qb => { qb .andWhere(new Brackets(qb => { qb
.where('user.updatedAt IS NULL') .where('user.updatedAt IS NULL')
.orWhere('user.updatedAt > :activeThreshold', { activeThreshold: activeThreshold }); .orWhere('user.updatedAt > :activeThreshold', { activeThreshold: activeThreshold });
})) }));
.orderBy('user.updatedAt', 'DESC', 'NULLS LAST')
query.setParameters(followingQuery.getParameters());
users = await query
.orderBy('user.usernameLower', 'ASC')
.take(ps.limit!) .take(ps.limit!)
.skip(ps.offset)
.getMany(); .getMany();
if (users.length < ps.limit!) { if (users.length < ps.limit!) {
const otherUsers = await Users.createQueryBuilder('user') const otherQuery = await Users.createQueryBuilder('user')
.where('user.host IS NOT NULL') .where(`user.id NOT IN (${ followingQuery.getQuery() })`)
.andWhere(`user.id != :meId`, { meId: me.id })
.andWhere('user.isSuspended = FALSE') .andWhere('user.isSuspended = FALSE')
.andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' }) .andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' })
.andWhere('user.updatedAt IS NOT NULL') .andWhere('user.updatedAt IS NOT NULL');
otherQuery.setParameters(followingQuery.getParameters());
const otherUsers = await otherQuery
.orderBy('user.updatedAt', 'DESC') .orderBy('user.updatedAt', 'DESC')
.take(ps.limit! - users.length) .take(ps.limit! - users.length)
.getMany(); .getMany();
users = users.concat(otherUsers); 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();
}
return await Users.packMany(users, me, { detail: ps.detail }); return await Users.packMany(users, me, { detail: ps.detail });
} }