2023-01-13 05:40:33 +01:00
|
|
|
import { IsNull } from "typeorm";
|
|
|
|
import { Users, Followings, UserProfiles } from "@/models/index.js";
|
|
|
|
import { toPunyNullable } from "@/misc/convert-host.js";
|
|
|
|
import define from "../../define.js";
|
|
|
|
import { ApiError } from "../../error.js";
|
|
|
|
import { makePaginationQuery } from "../../common/make-pagination-query.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,
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
description: "Show everyone that this user is following.",
|
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-04-07 14:50:36 +02:00
|
|
|
items: {
|
2023-01-13 05:40:33 +01:00
|
|
|
type: "object",
|
|
|
|
optional: false,
|
|
|
|
nullable: false,
|
|
|
|
ref: "Following",
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
2019-02-24 11:42:26 +01:00
|
|
|
},
|
|
|
|
|
2019-02-22 03:46:58 +01:00
|
|
|
errors: {
|
|
|
|
noSuchUser: {
|
2023-01-13 05:40:33 +01:00
|
|
|
message: "No such user.",
|
|
|
|
code: "NO_SUCH_USER",
|
|
|
|
id: "63e4aba4-4156-4e53-be25-c9559e42d71b",
|
2021-11-07 10:04:32 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
forbidden: {
|
2023-01-13 05:40:33 +01:00
|
|
|
message: "Forbidden.",
|
|
|
|
code: "FORBIDDEN",
|
|
|
|
id: "f6cdb0df-c19f-ec5c-7dbb-0ba84a1f92ba",
|
2021-11-07 10:04:32 +01:00
|
|
|
},
|
2023-04-25 17:31:05 +02:00
|
|
|
cannot_find: {
|
|
|
|
message: "Cannot find the following.",
|
|
|
|
code: "CANNOT_FIND",
|
|
|
|
id: "7a55f0d7-8e06-4a7e-9c77-ee7d59b25a82",
|
2023-04-26 03:21:15 +02: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-02-19 06:05:32 +01:00
|
|
|
properties: {
|
2023-01-13 05:40:33 +01:00
|
|
|
sinceId: { type: "string", format: "misskey:id" },
|
|
|
|
untilId: { type: "string", format: "misskey:id" },
|
|
|
|
limit: { type: "integer", minimum: 1, maximum: 100, default: 10 },
|
2022-02-19 06:05:32 +01:00
|
|
|
},
|
2022-04-03 06:57:26 +02:00
|
|
|
anyOf: [
|
|
|
|
{
|
|
|
|
properties: {
|
2023-01-13 05:40:33 +01:00
|
|
|
userId: { type: "string", format: "misskey:id" },
|
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
|
|
|
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", "host"],
|
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) => {
|
2023-01-13 05:40:33 +01:00
|
|
|
const user = await Users.findOneBy(
|
|
|
|
ps.userId != null
|
|
|
|
? { id: ps.userId }
|
|
|
|
: {
|
|
|
|
usernameLower: ps.username!.toLowerCase(),
|
|
|
|
host: toPunyNullable(ps.host) ?? IsNull(),
|
|
|
|
},
|
|
|
|
);
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (user == null) {
|
2019-02-22 03:46:58 +01:00
|
|
|
throw new ApiError(meta.errors.noSuchUser);
|
2016-12-28 23:49:51 +01:00
|
|
|
}
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const profile = await UserProfiles.findOneByOrFail({ userId: user.id });
|
2021-11-07 10:04:32 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
if (profile.ffVisibility === "private") {
|
|
|
|
if (me == null || me.id !== user.id) {
|
2021-11-07 10:04:32 +01:00
|
|
|
throw new ApiError(meta.errors.forbidden);
|
|
|
|
}
|
2023-01-13 05:40:33 +01:00
|
|
|
} else if (profile.ffVisibility === "followers") {
|
2021-11-07 10:04:32 +01:00
|
|
|
if (me == null) {
|
|
|
|
throw new ApiError(meta.errors.forbidden);
|
|
|
|
} else if (me.id !== user.id) {
|
2022-03-26 07:34:00 +01:00
|
|
|
const following = await Followings.findOneBy({
|
2021-11-07 10:04:32 +01:00
|
|
|
followeeId: user.id,
|
|
|
|
followerId: me.id,
|
|
|
|
});
|
|
|
|
if (following == null) {
|
2023-04-25 17:31:05 +02:00
|
|
|
throw new ApiError(meta.errors.cannot_find);
|
2021-11-07 10:04:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const query = makePaginationQuery(
|
|
|
|
Followings.createQueryBuilder("following"),
|
|
|
|
ps.sinceId,
|
|
|
|
ps.untilId,
|
|
|
|
)
|
|
|
|
.andWhere("following.followerId = :userId", { userId: user.id })
|
|
|
|
.innerJoinAndSelect("following.followee", "followee");
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const followings = await query.take(ps.limit).getMany();
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
return await Followings.packMany(followings, me, { populateFollowee: true });
|
2019-02-22 03:46:58 +01:00
|
|
|
});
|