Update search-by-username-and-host.ts
This commit is contained in:
parent
0cc055de3a
commit
2d196b6779
1 changed files with 45 additions and 24 deletions
|
@ -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,34 +56,59 @@ 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')
|
|
||||||
.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();
|
|
||||||
|
|
||||||
if (users.length < ps.limit!) {
|
if (me) {
|
||||||
const otherUsers = await Users.createQueryBuilder('user')
|
const followingQuery = Followings.createQueryBuilder('following')
|
||||||
.where('user.host IS NOT NULL')
|
.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
|
||||||
|
.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')
|
.andWhere('user.updatedAt IS NOT NULL')
|
||||||
.orderBy('user.updatedAt', 'DESC')
|
.orderBy('user.updatedAt', 'DESC')
|
||||||
.take(ps.limit! - users.length)
|
.take(ps.limit! - users.length)
|
||||||
.getMany();
|
.getMany();
|
||||||
|
|
||||||
users = users.concat(otherUsers);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return await Users.packMany(users, me, { detail: ps.detail });
|
return await Users.packMany(users, me, { detail: ps.detail });
|
||||||
|
|
Loading…
Reference in a new issue