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

118 lines
3.3 KiB
TypeScript
Raw Normal View History

import $ from 'cafy';
import define from '../../define';
2021-10-20 00:53:38 +09:00
import { Followings, Users } from '@/models/index';
import { Brackets } from 'typeorm';
import { USER_ACTIVE_THRESHOLD } from '@/const';
2021-10-20 00:53:38 +09:00
import { User } from '@/models/entities/user';
export const meta = {
tags: ['users'],
2020-02-15 21:33:32 +09: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 23:58:30 +09:00
},
},
};
2022-01-03 02:12:50 +09:00
// eslint-disable-next-line import/no-default-export
export default define(meta, async (ps, me) => {
2021-10-17 18:38:38 +09: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 18:38:38 +09:00
q.andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' });
}
q.andWhere('user.updatedAt IS NOT NULL');
2020-03-21 12:48:25 +09:00
q.orderBy('user.updatedAt', 'DESC');
2021-10-20 00:53:38 +09:00
const users = await q.take(ps.limit!).getMany();
return await Users.packMany(users, me, { detail: ps.detail });
2020-03-21 12:48:25 +09:00
} else if (ps.username) {
2021-10-20 00:53:38 +09: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 18:38:38 +09:00
.andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' })
2021-10-20 00:53:38 +09: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-20 01:15:14 +09:00
2021-10-20 00:53:38 +09:00
otherQuery.setParameters(followingQuery.getParameters());
2021-10-20 01:15:14 +09:00
2021-10-20 00:53:38 +09:00
const otherUsers = await otherQuery
.orderBy('user.updatedAt', 'DESC')
.take(ps.limit! - users.length)
.getMany();
2021-10-20 01:15:14 +09:00
2021-10-20 00:53:38 +09: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 12:48:25 +09:00
.orderBy('user.updatedAt', 'DESC')
.take(ps.limit! - users.length)
.getMany();
}
return await Users.packMany(users, me, { detail: ps.detail });
}
});