2022-02-27 04:07:39 +02:00
|
|
|
import define from '../../define.js';
|
|
|
|
import { UserProfiles, Users } from '@/models/index.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
2021-10-17 10:26:35 +03:00
|
|
|
import { Brackets } from 'typeorm';
|
2018-08-06 12:28:27 +03:00
|
|
|
|
|
|
|
export const meta = {
|
2019-02-23 04:20:58 +02:00
|
|
|
tags: ['users'],
|
|
|
|
|
2022-01-18 15:27:10 +02:00
|
|
|
requireCredential: false,
|
2018-08-06 12:28:27 +03:00
|
|
|
|
2019-02-24 12:42:26 +02:00
|
|
|
res: {
|
2022-01-18 15:27:10 +02:00
|
|
|
type: 'array',
|
|
|
|
optional: false, nullable: false,
|
2019-02-24 12:42:26 +02:00
|
|
|
items: {
|
2022-01-18 15:27:10 +02:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2019-04-23 16:35:26 +03:00
|
|
|
ref: 'User',
|
2021-12-09 16:58:30 +02:00
|
|
|
},
|
2019-02-24 12:42:26 +02:00
|
|
|
},
|
2022-01-18 15:27:10 +02:00
|
|
|
} as const;
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2022-02-20 06:15:40 +02:00
|
|
|
export const paramDef = {
|
2022-02-19 07:05:32 +02:00
|
|
|
type: 'object',
|
|
|
|
properties: {
|
|
|
|
query: { type: 'string' },
|
|
|
|
offset: { type: 'integer', default: 0 },
|
|
|
|
limit: { type: 'integer', minimum: 1, maximum: 100, default: 10 },
|
|
|
|
origin: { type: 'string', enum: ['local', 'remote', 'combined'], default: "combined" },
|
|
|
|
detail: { type: 'boolean', default: true },
|
|
|
|
},
|
|
|
|
required: ['query'],
|
|
|
|
} 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-10-17 14:12:00 +03:00
|
|
|
const isUsername = ps.query.startsWith('@');
|
2018-08-06 12:28:27 +03:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
let users: User[] = [];
|
2018-08-06 12:28:27 +03:00
|
|
|
|
|
|
|
if (isUsername) {
|
2021-10-17 10:26:35 +03:00
|
|
|
const usernameQuery = Users.createQueryBuilder('user')
|
2021-10-17 12:38:38 +03:00
|
|
|
.where('user.usernameLower LIKE :username', { username: ps.query.replace('@', '').toLowerCase() + '%' })
|
2021-10-17 10:26:35 +03:00
|
|
|
.andWhere(new Brackets(qb => { qb
|
2021-10-17 12:38:38 +03:00
|
|
|
.where('user.updatedAt IS NULL')
|
|
|
|
.orWhere('user.updatedAt > :activeThreshold', { activeThreshold: activeThreshold });
|
2021-10-17 10:26:35 +03:00
|
|
|
}))
|
|
|
|
.andWhere('user.isSuspended = FALSE');
|
|
|
|
|
2021-10-17 12:38:38 +03:00
|
|
|
if (ps.origin === 'local') {
|
|
|
|
usernameQuery.andWhere('user.host IS NULL');
|
|
|
|
} else if (ps.origin === 'remote') {
|
|
|
|
usernameQuery.andWhere('user.host IS NOT NULL');
|
2021-10-17 10:26:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
users = await usernameQuery
|
2021-10-17 12:38:38 +03:00
|
|
|
.orderBy('user.updatedAt', 'DESC', 'NULLS LAST')
|
2022-02-19 07:05:32 +02:00
|
|
|
.take(ps.limit)
|
2019-04-07 15:50:36 +03:00
|
|
|
.skip(ps.offset)
|
|
|
|
.getMany();
|
2020-10-17 14:12:00 +03:00
|
|
|
} else {
|
2021-10-17 10:26:35 +03:00
|
|
|
const nameQuery = Users.createQueryBuilder('user')
|
2022-05-14 09:24:44 +03:00
|
|
|
.where(new Brackets(qb => {
|
|
|
|
qb.where('user.name ILIKE :query', { query: '%' + ps.query + '%' });
|
|
|
|
|
|
|
|
// Also search username if it qualifies as username
|
|
|
|
if (Users.validateLocalUsername(ps.query)) {
|
|
|
|
qb.orWhere('user.usernameLower LIKE :username', { username: '%' + ps.query.toLowerCase() + '%' });
|
|
|
|
}
|
|
|
|
}))
|
2021-10-17 10:26:35 +03:00
|
|
|
.andWhere(new Brackets(qb => { qb
|
2021-10-17 12:38:38 +03:00
|
|
|
.where('user.updatedAt IS NULL')
|
|
|
|
.orWhere('user.updatedAt > :activeThreshold', { activeThreshold: activeThreshold });
|
2021-10-17 10:26:35 +03:00
|
|
|
}))
|
|
|
|
.andWhere('user.isSuspended = FALSE');
|
|
|
|
|
2021-10-17 12:38:38 +03:00
|
|
|
if (ps.origin === 'local') {
|
|
|
|
nameQuery.andWhere('user.host IS NULL');
|
|
|
|
} else if (ps.origin === 'remote') {
|
|
|
|
nameQuery.andWhere('user.host IS NOT NULL');
|
2021-10-17 10:26:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
users = await nameQuery
|
2021-10-17 12:38:38 +03:00
|
|
|
.orderBy('user.updatedAt', 'DESC', 'NULLS LAST')
|
2022-02-19 07:05:32 +02:00
|
|
|
.take(ps.limit)
|
2020-10-17 14:12:00 +03:00
|
|
|
.skip(ps.offset)
|
|
|
|
.getMany();
|
|
|
|
|
2022-02-19 07:05:32 +02:00
|
|
|
if (users.length < ps.limit) {
|
2021-10-17 10:26:35 +03:00
|
|
|
const profQuery = UserProfiles.createQueryBuilder('prof')
|
2020-10-17 14:12:00 +03:00
|
|
|
.select('prof.userId')
|
2021-10-17 12:38:38 +03:00
|
|
|
.where('prof.description ILIKE :query', { query: '%' + ps.query + '%' });
|
2021-10-17 10:26:35 +03:00
|
|
|
|
2021-10-17 12:38:38 +03:00
|
|
|
if (ps.origin === 'local') {
|
2021-10-17 10:26:35 +03:00
|
|
|
profQuery.andWhere('prof.userHost IS NULL');
|
2021-10-17 12:38:38 +03:00
|
|
|
} else if (ps.origin === 'remote') {
|
2021-10-17 10:26:35 +03:00
|
|
|
profQuery.andWhere('prof.userHost IS NOT NULL');
|
|
|
|
}
|
|
|
|
|
|
|
|
const query = Users.createQueryBuilder('user')
|
|
|
|
.where(`user.id IN (${ profQuery.getQuery() })`)
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
2021-10-17 12:38:38 +03:00
|
|
|
.where('user.updatedAt IS NULL')
|
|
|
|
.orWhere('user.updatedAt > :activeThreshold', { activeThreshold: activeThreshold });
|
2021-10-17 10:26:35 +03:00
|
|
|
}))
|
|
|
|
.andWhere('user.isSuspended = FALSE')
|
|
|
|
.setParameters(profQuery.getParameters());
|
|
|
|
|
|
|
|
users = users.concat(await query
|
2021-10-17 12:38:38 +03:00
|
|
|
.orderBy('user.updatedAt', 'DESC', 'NULLS LAST')
|
2022-02-19 07:05:32 +02:00
|
|
|
.take(ps.limit)
|
2021-10-17 10:26:35 +03:00
|
|
|
.skip(ps.offset)
|
|
|
|
.getMany()
|
|
|
|
);
|
2018-08-06 12:28:27 +03:00
|
|
|
}
|
|
|
|
}
|
2016-12-29 00:49:51 +02:00
|
|
|
|
2019-04-07 15:50:36 +03:00
|
|
|
return await Users.packMany(users, me, { detail: ps.detail });
|
2019-02-22 04:46:58 +02:00
|
|
|
});
|