2023-01-13 05:40:33 +01:00
|
|
|
import type { FindOptionsWhere } from "typeorm";
|
|
|
|
import { In, IsNull } from "typeorm";
|
|
|
|
import { resolveUser } from "@/remote/resolve-user.js";
|
|
|
|
import { Users } from "@/models/index.js";
|
|
|
|
import type { User } from "@/models/entities/user.js";
|
|
|
|
import define from "../../define.js";
|
|
|
|
import { apiLogger } from "../../logger.js";
|
|
|
|
import { ApiError } from "../../error.js";
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2018-11-01 19:32:24 +01: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-03-27 09:51:12 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
description: "Show the properties of a user.",
|
2022-06-10 07:25:20 +02:00
|
|
|
|
2019-02-23 03:20:58 +01:00
|
|
|
res: {
|
2023-01-13 05:40:33 +01:00
|
|
|
optional: false,
|
|
|
|
nullable: false,
|
2022-01-18 14:27:10 +01:00
|
|
|
oneOf: [
|
|
|
|
{
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "object",
|
|
|
|
ref: "UserDetailed",
|
2022-01-18 14:27:10 +01:00
|
|
|
},
|
|
|
|
{
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "array",
|
2022-01-18 14:27:10 +01:00
|
|
|
items: {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "object",
|
|
|
|
ref: "UserDetailed",
|
2022-04-05 17:04:25 +02:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
},
|
2022-04-05 17:04:25 +02:00
|
|
|
],
|
2019-02-23 03:20:58 +01:00
|
|
|
},
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
errors: {
|
|
|
|
failedToResolveRemoteUser: {
|
2023-01-13 05:40:33 +01:00
|
|
|
message: "Failed to resolve remote user.",
|
|
|
|
code: "FAILED_TO_RESOLVE_REMOTE_USER",
|
|
|
|
id: "ef7b9be4-9cba-4e6f-ab41-90ed171c7d3c",
|
|
|
|
kind: "server",
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
noSuchUser: {
|
2023-01-13 05:40:33 +01:00
|
|
|
message: "No such user.",
|
|
|
|
code: "NO_SUCH_USER",
|
|
|
|
id: "4362f8dc-731f-4ad8-a694-be5a88922a24",
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-11-01 19:32:24 +01:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "object",
|
2022-04-03 06:57:26 +02:00
|
|
|
anyOf: [
|
|
|
|
{
|
|
|
|
properties: {
|
2023-05-31 05:10:12 +02:00
|
|
|
userId: { type: "string" },
|
2022-04-03 06:57:26 +02:00
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
required: ["userId"],
|
2022-04-03 06:57:26 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
properties: {
|
2023-01-13 05:40:33 +01:00
|
|
|
userIds: {
|
|
|
|
type: "array",
|
|
|
|
uniqueItems: true,
|
|
|
|
items: {
|
|
|
|
type: "string",
|
|
|
|
},
|
|
|
|
},
|
2022-04-03 06:57:26 +02:00
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
required: ["userIds"],
|
2022-04-03 06:57:26 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
properties: {
|
2023-01-13 05:40:33 +01:00
|
|
|
username: { type: "string" },
|
2022-04-03 06:57:26 +02:00
|
|
|
host: {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "string",
|
2022-04-03 06:57:26 +02:00
|
|
|
nullable: true,
|
2023-01-13 05:40:33 +01:00
|
|
|
description: "The local host is represented with `null`.",
|
2022-04-03 06:57:26 +02:00
|
|
|
},
|
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
required: ["username"],
|
2022-04-03 06:57:26 +02:00
|
|
|
},
|
|
|
|
],
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
|
|
|
|
|
|
|
export default define(meta, paramDef, async (ps, me) => {
|
2018-11-01 19:32:24 +01:00
|
|
|
let user;
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2020-01-01 18:47:20 +01:00
|
|
|
const isAdminOrModerator = me && (me.isAdmin || me.isModerator);
|
|
|
|
|
2018-11-01 19:32:24 +01:00
|
|
|
if (ps.userIds) {
|
2019-04-15 05:54:42 +02:00
|
|
|
if (ps.userIds.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2023-05-31 05:10:12 +02:00
|
|
|
const isUrl = ps.userIds[0].startsWith("http");
|
|
|
|
let users: User[];
|
|
|
|
if (isUrl) {
|
|
|
|
users = await Users.findBy(
|
|
|
|
isAdminOrModerator
|
|
|
|
? { uri: In(ps.userIds) }
|
|
|
|
: { uri: In(ps.userIds), isSuspended: false },
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
users = await Users.findBy(
|
|
|
|
isAdminOrModerator
|
|
|
|
? { id: In(ps.userIds) }
|
|
|
|
: { id: In(ps.userIds), isSuspended: false },
|
|
|
|
);
|
|
|
|
}
|
2017-02-22 05:08:33 +01:00
|
|
|
|
2020-11-08 04:40:31 +01:00
|
|
|
// リクエストされた通りに並べ替え
|
2021-03-24 03:05:37 +01:00
|
|
|
const _users: User[] = [];
|
2020-11-08 04:40:31 +01:00
|
|
|
for (const id of ps.userIds) {
|
2023-05-31 05:10:12 +02:00
|
|
|
const res = users.find((x) => (isUrl ? x.uri === id : x.id === id));
|
|
|
|
if (res) _users.push(res);
|
2020-11-08 04:40:31 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
return await Promise.all(
|
|
|
|
_users.map((u) =>
|
|
|
|
Users.pack(u, me, {
|
|
|
|
detail: true,
|
|
|
|
}),
|
|
|
|
),
|
|
|
|
);
|
2018-03-27 09:51:12 +02:00
|
|
|
} else {
|
2018-04-25 15:37:08 +02:00
|
|
|
// Lookup user
|
2023-01-13 05:40:33 +01:00
|
|
|
if (typeof ps.host === "string" && typeof ps.username === "string") {
|
|
|
|
user = await resolveUser(ps.username, ps.host).catch((e) => {
|
2019-02-03 10:16:57 +01:00
|
|
|
apiLogger.warn(`failed to resolve remote user: ${e}`);
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.failedToResolveRemoteUser);
|
|
|
|
});
|
2018-04-25 15:37:08 +02:00
|
|
|
} else {
|
2023-01-13 05:40:33 +01:00
|
|
|
const q: FindOptionsWhere<User> =
|
|
|
|
ps.userId != null
|
2023-05-31 05:10:12 +02:00
|
|
|
? ps.userId.startsWith("http")
|
|
|
|
? { uri: ps.userId }
|
|
|
|
: { id: ps.userId }
|
2023-01-13 05:40:33 +01:00
|
|
|
: { usernameLower: ps.username!.toLowerCase(), host: IsNull() };
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
user = await Users.findOneBy(q);
|
2019-02-15 15:43:49 +01:00
|
|
|
}
|
2018-03-27 09:51:12 +02:00
|
|
|
|
2020-01-01 18:47:20 +01:00
|
|
|
if (user == null || (!isAdminOrModerator && user.isSuspended)) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
2018-03-27 09:51:12 +02:00
|
|
|
}
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
return await Users.pack(user, me, {
|
2021-12-09 15:58:30 +01:00
|
|
|
detail: true,
|
2019-02-22 03:46:58 +01:00
|
|
|
});
|
2018-04-25 15:37:08 +02:00
|
|
|
}
|
2019-02-22 03:46:58 +01:00
|
|
|
});
|