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