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 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 });