2023-01-13 05:40:33 +01:00
|
|
|
import { pushUserToUserList } from "@/services/user-list/push.js";
|
|
|
|
import { UserLists, UserListJoinings, Blockings } from "@/models/index.js";
|
|
|
|
import define from "../../../define.js";
|
|
|
|
import { ApiError } from "../../../error.js";
|
|
|
|
import { getUser } from "../../../common/getters.js";
|
2018-04-24 13:58:29 +02:00
|
|
|
|
2018-07-16 21:36:44 +02:00
|
|
|
export const meta = {
|
2023-01-13 05:40:33 +01:00
|
|
|
tags: ["lists", "users"],
|
2019-02-23 03:20:58 +01:00
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2018-07-16 21:36:44 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
kind: "write:account",
|
2018-11-01 19:32:24 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
description: "Add a user to an existing list.",
|
2022-06-10 07:25:20 +02:00
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
errors: {
|
|
|
|
noSuchList: {
|
2023-01-13 05:40:33 +01:00
|
|
|
message: "No such list.",
|
|
|
|
code: "NO_SUCH_LIST",
|
|
|
|
id: "2214501d-ac96-4049-b717-91e42272a711",
|
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: "a89abd3d-f0bc-4cce-beb1-2f446f4f1e6a",
|
2019-02-22 03:46:58 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
alreadyAdded: {
|
2023-01-13 05:40:33 +01:00
|
|
|
message: "That user has already been added to that list.",
|
|
|
|
code: "ALREADY_ADDED",
|
|
|
|
id: "1de7c884-1595-49e9-857e-61f12f4d4fc5",
|
2021-08-17 14:48:59 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
youHaveBeenBlocked: {
|
2023-01-13 05:40:33 +01:00
|
|
|
message:
|
|
|
|
"You cannot push this user because you have been blocked by this user.",
|
|
|
|
code: "YOU_HAVE_BEEN_BLOCKED",
|
|
|
|
id: "990232c5-3f9d-4d83-9f3f-ef27b6332a4b",
|
2021-08-17 14:48:59 +02:00
|
|
|
},
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-07-16 21:36:44 +02: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
|
|
|
listId: { type: "string", format: "misskey:id" },
|
|
|
|
userId: { type: "string", format: "misskey:id" },
|
2022-02-19 06:05:32 +01:00
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
required: ["listId", "userId"],
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
|
|
|
|
|
|
|
export default define(meta, paramDef, async (ps, me) => {
|
2018-04-24 13:58:29 +02:00
|
|
|
// Fetch the list
|
2023-07-18 16:00:48 +02:00
|
|
|
const userList = await UserLists.findOneBy({
|
|
|
|
id: ps.listId,
|
|
|
|
userId: me.id,
|
2018-04-24 13:58:29 +02:00
|
|
|
});
|
|
|
|
|
2023-07-18 16:00:48 +02:00
|
|
|
if (!userList) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.noSuchList);
|
2018-04-24 13:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the user
|
2023-01-13 05:40:33 +01:00
|
|
|
const user = await getUser(ps.userId).catch((e) => {
|
|
|
|
if (e.id === "15348ddd-432d-49c2-8a5a-8069753becff")
|
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
2019-02-22 06:02:56 +01:00
|
|
|
throw e;
|
2018-04-24 13:58:29 +02:00
|
|
|
});
|
|
|
|
|
2021-08-17 14:48:59 +02:00
|
|
|
// Check blocking
|
|
|
|
if (user.id !== me.id) {
|
2023-07-13 18:10:13 +02:00
|
|
|
const isBlocked = await Blockings.exist({
|
|
|
|
where: {
|
|
|
|
blockerId: user.id,
|
|
|
|
blockeeId: me.id,
|
|
|
|
},
|
2021-08-17 14:48:59 +02:00
|
|
|
});
|
2023-07-13 18:10:13 +02:00
|
|
|
if (isBlocked) {
|
2021-08-17 14:48:59 +02:00
|
|
|
throw new ApiError(meta.errors.youHaveBeenBlocked);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-13 18:10:13 +02:00
|
|
|
const exist = await UserListJoinings.exist({
|
|
|
|
where: {
|
2023-07-18 16:00:48 +02:00
|
|
|
userListId: ps.listId,
|
2023-07-13 18:10:13 +02:00
|
|
|
userId: user.id,
|
|
|
|
},
|
2019-04-07 14:50:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (exist) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.alreadyAdded);
|
2018-04-24 13:58:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Push the user
|
2019-05-18 13:36:33 +02:00
|
|
|
await pushUserToUserList(user, userList);
|
2019-02-22 03:46:58 +01:00
|
|
|
});
|