2020-01-29 19:37:25 +00:00
|
|
|
import $ from 'cafy';
|
2021-08-19 12:55:45 +00:00
|
|
|
import define from '../../define';
|
2021-10-19 15:53:38 +00:00
|
|
|
import { Followings, Users } from '@/models/index';
|
2021-10-17 07:26:35 +00:00
|
|
|
import { Brackets } from 'typeorm';
|
|
|
|
import { USER_ACTIVE_THRESHOLD } from '@/const';
|
2021-10-19 15:53:38 +00:00
|
|
|
import { User } from '@/models/entities/user';
|
2020-01-29 19:37:25 +00:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['users'],
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
requireCredential: false,
|
2020-01-29 19:37:25 +00:00
|
|
|
|
|
|
|
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: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2020-01-29 19:37:25 +00:00
|
|
|
items: {
|
2022-01-18 13:27:10 +00:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2020-01-29 19:37:25 +00:00
|
|
|
ref: 'User',
|
2021-12-09 14:58:30 +00:00
|
|
|
},
|
2020-01-29 19:37:25 +00:00
|
|
|
},
|
2022-01-18 13:27:10 +00:00
|
|
|
} as const;
|
2020-01-29 19:37:25 +00:00
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2020-01-29 19:37:25 +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日
|
|
|
|
|
2020-01-29 19:37:25 +00:00
|
|
|
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() + '%' });
|
2020-01-29 19:37:25 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 11:14:26 +00:00
|
|
|
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();
|
2020-01-29 19:37:25 +00:00
|
|
|
|
|
|
|
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 })
|
2020-01-29 19:37:25 +00:00
|
|
|
.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() + '%' })
|
2020-03-21 11:14:26 +00:00
|
|
|
.andWhere('user.updatedAt IS NOT NULL')
|
2020-03-21 03:48:25 +00:00
|
|
|
.orderBy('user.updatedAt', 'DESC')
|
2020-01-29 19:37:25 +00:00
|
|
|
.take(ps.limit! - users.length)
|
|
|
|
.getMany();
|
|
|
|
}
|
|
|
|
|
2022-01-18 13:27:10 +00:00
|
|
|
return await Users.packMany(users, me, { detail: !!ps.detail });
|
2020-01-29 19:37:25 +00:00
|
|
|
}
|
|
|
|
});
|