2020-01-29 21:37:25 +02:00
|
|
|
import $ from 'cafy';
|
2021-08-19 15:55:45 +03:00
|
|
|
import define from '../../define';
|
2021-10-19 18:53:38 +03:00
|
|
|
import { Followings, Users } from '@/models/index';
|
2021-10-17 10:26:35 +03:00
|
|
|
import { Brackets } from 'typeorm';
|
|
|
|
import { USER_ACTIVE_THRESHOLD } from '@/const';
|
2021-10-19 18:53:38 +03:00
|
|
|
import { User } from '@/models/entities/user';
|
2020-01-29 21:37:25 +02:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['users'],
|
|
|
|
|
2020-02-15 14:33:32 +02:00
|
|
|
requireCredential: false as const,
|
2020-01-29 21:37:25 +02: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: {
|
|
|
|
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',
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export default define(meta, async (ps, me) => {
|
2021-10-17 12:38:38 +03:00
|
|
|
const activeThreshold = new Date(Date.now() - (1000 * 60 * 60 * 24 * 30)); // 30日
|
|
|
|
|
2020-01-29 21:37:25 +02: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 12:38:38 +03:00
|
|
|
q.andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' });
|
2020-01-29 21:37:25 +02:00
|
|
|
}
|
|
|
|
|
2020-03-21 13:14:26 +02:00
|
|
|
q.andWhere('user.updatedAt IS NOT NULL');
|
2020-03-21 05:48:25 +02:00
|
|
|
q.orderBy('user.updatedAt', 'DESC');
|
|
|
|
|
2021-10-19 18:53:38 +03:00
|
|
|
const users = await q.take(ps.limit!).getMany();
|
2020-01-29 21:37:25 +02:00
|
|
|
|
|
|
|
return await Users.packMany(users, me, { detail: ps.detail });
|
2020-03-21 05:48:25 +02:00
|
|
|
} else if (ps.username) {
|
2021-10-19 18:53:38 +03: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 21:37:25 +02:00
|
|
|
.andWhere('user.isSuspended = FALSE')
|
2021-10-17 12:38:38 +03:00
|
|
|
.andWhere('user.usernameLower LIKE :username', { username: ps.username.toLowerCase() + '%' })
|
2021-10-19 18:53:38 +03: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');
|
|
|
|
|
|
|
|
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() + '%' })
|
2020-03-21 13:14:26 +02:00
|
|
|
.andWhere('user.updatedAt IS NOT NULL')
|
2020-03-21 05:48:25 +02:00
|
|
|
.orderBy('user.updatedAt', 'DESC')
|
2020-01-29 21:37:25 +02:00
|
|
|
.take(ps.limit! - users.length)
|
|
|
|
.getMany();
|
|
|
|
}
|
|
|
|
|
|
|
|
return await Users.packMany(users, me, { detail: ps.detail });
|
|
|
|
}
|
|
|
|
});
|