2023-01-13 05:40:33 +01:00
|
|
|
import deleteFollowing from "@/services/following/delete.js";
|
|
|
|
import define from "../../define.js";
|
|
|
|
import { ApiError } from "../../error.js";
|
|
|
|
import { getUser } from "../../common/getters.js";
|
|
|
|
import { Followings, Users } from "@/models/index.js";
|
|
|
|
import { HOUR } from "@/const.js";
|
2021-12-03 03:14:44 +01:00
|
|
|
|
|
|
|
export const meta = {
|
2023-01-13 05:40:33 +01:00
|
|
|
tags: ["following", "users"],
|
2021-12-03 03:14:44 +01:00
|
|
|
|
|
|
|
limit: {
|
2022-07-14 00:01:23 +02:00
|
|
|
duration: HOUR,
|
2021-12-09 15:58:30 +01:00
|
|
|
max: 100,
|
2021-12-03 03:14:44 +01:00
|
|
|
},
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2021-12-03 03:14:44 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
kind: "write:following",
|
2021-12-03 03:14:44 +01:00
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
2023-01-13 05:40:33 +01:00
|
|
|
message: "No such user.",
|
|
|
|
code: "NO_SUCH_USER",
|
|
|
|
id: "5b12c78d-2b28-4dca-99d2-f56139b42ff8",
|
2021-12-03 03:14:44 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
followerIsYourself: {
|
2023-01-13 05:40:33 +01:00
|
|
|
message: "Follower is yourself.",
|
|
|
|
code: "FOLLOWER_IS_YOURSELF",
|
|
|
|
id: "07dc03b9-03da-422d-885b-438313707662",
|
2021-12-03 03:14:44 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
notFollowing: {
|
2023-01-13 05:40:33 +01:00
|
|
|
message: "The other use is not following you.",
|
|
|
|
code: "NOT_FOLLOWING",
|
|
|
|
id: "5dbf82f5-c92b-40b1-87d1-6c8c0741fd09",
|
2021-12-03 03:14:44 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "object",
|
|
|
|
optional: false,
|
|
|
|
nullable: false,
|
|
|
|
ref: "UserLite",
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2021-12-03 03:14:44 +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
|
|
|
userId: { type: "string", format: "misskey:id" },
|
2022-02-19 06:05:32 +01:00
|
|
|
},
|
2023-01-13 05:40:33 +01:00
|
|
|
required: ["userId"],
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
|
|
|
|
2023-01-13 05:54:33 +01:00
|
|
|
|
2022-02-19 06:05:32 +01:00
|
|
|
export default define(meta, paramDef, async (ps, user) => {
|
2021-12-03 03:14:44 +01:00
|
|
|
const followee = user;
|
|
|
|
|
|
|
|
// Check if the follower is yourself
|
|
|
|
if (user.id === ps.userId) {
|
|
|
|
throw new ApiError(meta.errors.followerIsYourself);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get follower
|
2023-01-13 05:40:33 +01:00
|
|
|
const follower = await getUser(ps.userId).catch((e) => {
|
|
|
|
if (e.id === "15348ddd-432d-49c2-8a5a-8069753becff")
|
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
2021-12-03 03:14:44 +01:00
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Check not following
|
2022-03-26 07:34:00 +01:00
|
|
|
const exist = await Followings.findOneBy({
|
2021-12-03 03:14:44 +01:00
|
|
|
followerId: follower.id,
|
2021-12-09 15:58:30 +01:00
|
|
|
followeeId: followee.id,
|
2021-12-03 03:14:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
if (exist == null) {
|
|
|
|
throw new ApiError(meta.errors.notFollowing);
|
|
|
|
}
|
|
|
|
|
|
|
|
await deleteFollowing(follower, followee);
|
|
|
|
|
|
|
|
return await Users.pack(followee.id, user);
|
|
|
|
});
|