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'],
|
|
|
|
|
2022-01-18 15:27:10 +02:00
|
|
|
requireCredential: false,
|
2020-01-29 21:37:25 +02:00
|
|
|
|
|
|
|
res: {
|
2022-01-18 15:27:10 +02:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2020-01-29 21:37:25 +02:00
|
|
|
items: {
|
2022-01-18 15:27:10 +02:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2020-01-29 21:37:25 +02:00
|
|
|
ref: 'User',
|
2021-12-09 16:58:30 +02:00
|
|
|
},
|
2020-01-29 21:37:25 +02:00
|
|
|
},
|
2022-01-18 15:27:10 +02:00
|
|
|
} as const;
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2022-02-20 06:15:40 +02:00
|
|
|
export const paramDef = {
|
2022-02-19 07:05:32 +02:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
username: { type: 'string', nullable: true },
|
|
|
|
host: { type: 'string', nullable: true },
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
detail: { type: 'boolean', default: true },
|
|
|
|
},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-01-02 19:12:50 +02:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2022-02-19 07:05:32 +02:00
|
|
|
export default define(meta, paramDef, 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');
|
|
|
|
|
2022-02-19 07:05:32 +02: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')
|
2022-02-19 07:05:32 +02:00
|
|
|
.take(ps.limit)
|
2021-10-19 18:53:38 +03:00
|
|
|
.getMany();
|
|
|
|
|
2022-02-19 07:05:32 +02:00
|
|
|
if (users.length < ps.limit) {
|
2021-10-19 18:53:38 +03:00
|
|
|
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 19:15:14 +03:00
|
|
|
|
2021-10-19 18:53:38 +03:00
|
|
|
otherQuery.setParameters(followingQuery.getParameters());
|
2021-10-19 19:15:14 +03:00
|
|
|
|
2021-10-19 18:53:38 +03:00
|
|
|
const otherUsers = await otherQuery
|
|
|
|
.orderBy('user.updatedAt', 'DESC')
|
2022-02-19 07:05:32 +02:00
|
|
|
.take(ps.limit - users.length)
|
2021-10-19 18:53:38 +03:00
|
|
|
.getMany();
|
2021-10-19 19:15:14 +03:00
|
|
|
|
2021-10-19 18:53:38 +03: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 13:14:26 +02:00
|
|
|
.andWhere('user.updatedAt IS NOT NULL')
|
2020-03-21 05:48:25 +02:00
|
|
|
.orderBy('user.updatedAt', 'DESC')
|
2022-02-19 07:05:32 +02:00
|
|
|
.take(ps.limit - users.length)
|
2020-01-29 21:37:25 +02:00
|
|
|
.getMany();
|
|
|
|
}
|
|
|
|
|
2022-01-18 15:27:10 +02:00
|
|
|
return await Users.packMany(users, me, { detail: !!ps.detail });
|
2020-01-29 21:37:25 +02:00
|
|
|
}
|
2022-02-03 10:21:52 +02:00
|
|
|
|
|
|
|
return [];
|
2020-01-29 21:37:25 +02:00
|
|
|
});
|