egirlskey/packages/backend/src/server/api/endpoints/users/search-by-username-and-host.ts

117 lines
3.3 KiB
TypeScript
Raw Normal View History

import $ from 'cafy';
import define from '../../define';
2021-10-19 15:53:38 +00:00
import { Followings, Users } from '@/models/index';
import { Brackets } from 'typeorm';
import { USER_ACTIVE_THRESHOLD } from '@/const';
2021-10-19 15:53:38 +00:00
import { User } from '@/models/entities/user';
export const meta = {
tags: ['users'],
2020-02-15 12:33:32 +00:00
requireCredential: false as const,
params: {
username: {
validator: $.optional.nullable.str,
},
host: {
validator: $.optional.nullable.str,
},
limit: {
validator: $.optional.num.range(1, 100),
default: 10,
},
detail: {
validator: $.optional.bool,
default: true,
},
},
res: {
type: 'array' as const,
optional: false as const, nullable: false as const,
items: {
type: 'object' as const,
optional: false as const, nullable: false as const,
ref: 'User',
2021-12-09 14:58:30 +00:00
},
},
};
export default define(meta, async (ps, me) => {
2021-10-17 09:38:38 +00:00
const activeThreshold = new Date(Date.now() - (1000 * 60 * 60 * 24 * 30)); // 30日
if (ps.host) {
const q = Users.createQueryBuilder('user')
.where('user.isSuspended = FALSE')
.andWhere('user.host LIKE :host', { host: ps.host.toLowerCase() + '%' });
if (ps.username) {
2021-10-17 09:38:38 +00:00
q.andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' });
}
q.andWhere('user.updatedAt IS NOT NULL');
2020-03-21 03:48:25 +00:00
q.orderBy('user.updatedAt', 'DESC');
2021-10-19 15:53:38 +00:00
const users = await q.take(ps.limit!).getMany();
return await Users.packMany(users, me, { detail: ps.detail });
2020-03-21 03:48:25 +00:00
} else if (ps.username) {
2021-10-19 15:53:38 +00:00
let users: User[] = [];
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')
2021-10-17 09:38:38 +00:00
.andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' })
2021-10-19 15:53:38 +00:00
.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');
2021-10-19 16:15:14 +00:00
2021-10-19 15:53:38 +00:00
otherQuery.setParameters(followingQuery.getParameters());
2021-10-19 16:15:14 +00:00
2021-10-19 15:53:38 +00:00
const otherUsers = await otherQuery
.orderBy('user.updatedAt', 'DESC')
.take(ps.limit! - users.length)
.getMany();
2021-10-19 16:15:14 +00:00
2021-10-19 15:53:38 +00:00
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')
2020-03-21 03:48:25 +00:00
.orderBy('user.updatedAt', 'DESC')
.take(ps.limit! - users.length)
.getMany();
}
return await Users.packMany(users, me, { detail: ps.detail });
}
});