2023-01-13 05:40:33 +01:00
|
|
|
import config from "@/config/index.js";
|
2023-11-26 21:33:46 +01:00
|
|
|
import { fetchMeta } from "@/misc/fetch-meta.js";
|
|
|
|
import type { Following } from "@/models/entities/following.js";
|
|
|
|
import { Followings, UserProfiles, Users } from "@/models/index.js";
|
2023-01-13 05:40:33 +01:00
|
|
|
import * as url from "@/prelude/url.js";
|
2023-11-26 21:33:46 +01:00
|
|
|
import { checkFetch } from "@/remote/activitypub/check-fetch.js";
|
|
|
|
import renderFollowUser from "@/remote/activitypub/renderer/follow-user.js";
|
2023-01-13 05:40:33 +01:00
|
|
|
import { renderActivity } from "@/remote/activitypub/renderer/index.js";
|
|
|
|
import renderOrderedCollectionPage from "@/remote/activitypub/renderer/ordered-collection-page.js";
|
2023-11-26 21:33:46 +01:00
|
|
|
import renderOrderedCollection from "@/remote/activitypub/renderer/ordered-collection.js";
|
2023-01-13 05:40:33 +01:00
|
|
|
import type Router from "@koa/router";
|
2023-11-26 21:33:46 +01:00
|
|
|
import { IsNull, LessThan } from "typeorm";
|
|
|
|
import type { FindOptionsWhere } from "typeorm";
|
|
|
|
import { setResponseType } from "../activitypub.js";
|
2018-08-14 13:13:32 +02:00
|
|
|
|
2019-09-26 22:50:34 +02:00
|
|
|
export default async (ctx: Router.RouterContext) => {
|
2021-07-20 18:45:41 +02:00
|
|
|
const verify = await checkFetch(ctx.req);
|
2023-01-03 18:12:26 +01:00
|
|
|
if (verify !== 200) {
|
2021-07-20 18:45:41 +02:00
|
|
|
ctx.status = verify;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
const userId = ctx.params.user;
|
2018-08-14 13:13:32 +02:00
|
|
|
|
2022-04-17 13:44:21 +02:00
|
|
|
const cursor = ctx.request.query.cursor;
|
2023-01-13 05:40:33 +01:00
|
|
|
if (cursor != null && typeof cursor !== "string") {
|
2018-08-14 13:13:32 +02:00
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const page = ctx.request.query.page === "true";
|
2022-04-17 13:44:21 +02:00
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const user = await Users.findOneBy({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: userId,
|
2022-03-26 07:34:00 +01:00
|
|
|
host: IsNull(),
|
2022-03-25 08:27:41 +01:00
|
|
|
});
|
2018-08-14 13:13:32 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
if (user == null) {
|
2018-08-14 13:13:32 +02:00
|
|
|
ctx.status = 404;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-11-07 10:04:32 +01:00
|
|
|
//#region Check ff visibility
|
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") {
|
2021-11-07 10:04:32 +01:00
|
|
|
ctx.status = 403;
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.set("Cache-Control", "public, max-age=30");
|
2021-11-07 10:04:32 +01:00
|
|
|
return;
|
2023-01-13 05:40:33 +01:00
|
|
|
} else if (profile.ffVisibility === "followers") {
|
2021-11-07 10:04:32 +01:00
|
|
|
ctx.status = 403;
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.set("Cache-Control", "public, max-age=30");
|
2021-11-07 10:04:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2018-08-14 13:13:32 +02:00
|
|
|
const limit = 10;
|
|
|
|
const partOf = `${config.url}/users/${userId}/following`;
|
|
|
|
|
|
|
|
if (page) {
|
|
|
|
const query = {
|
2021-12-09 15:58:30 +01:00
|
|
|
followerId: user.id,
|
2022-03-26 07:34:00 +01:00
|
|
|
} as FindOptionsWhere<Following>;
|
2018-08-14 13:13:32 +02:00
|
|
|
|
2022-12-07 18:16:23 +01:00
|
|
|
// If a cursor is specified
|
2018-08-14 13:13:32 +02:00
|
|
|
if (cursor) {
|
2019-04-07 14:50:36 +02:00
|
|
|
query.id = LessThan(cursor);
|
2018-08-14 13:13:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get followings
|
2019-04-07 14:50:36 +02:00
|
|
|
const followings = await Followings.find({
|
|
|
|
where: query,
|
|
|
|
take: limit + 1,
|
2021-12-09 15:58:30 +01:00
|
|
|
order: { id: -1 },
|
2019-04-07 14:50:36 +02:00
|
|
|
});
|
2018-08-14 13:13:32 +02:00
|
|
|
|
2022-12-07 18:16:23 +01:00
|
|
|
// Whether there is a "next page" or not
|
2018-08-14 13:13:32 +02:00
|
|
|
const inStock = followings.length === limit + 1;
|
|
|
|
if (inStock) followings.pop();
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const renderedFollowees = await Promise.all(
|
|
|
|
followings.map((following) => renderFollowUser(following.followeeId)),
|
|
|
|
);
|
2018-08-14 13:13:32 +02:00
|
|
|
const rendered = renderOrderedCollectionPage(
|
2019-02-13 15:45:35 +01:00
|
|
|
`${partOf}?${url.query({
|
2023-01-13 05:40:33 +01:00
|
|
|
page: "true",
|
2021-12-09 15:58:30 +01:00
|
|
|
cursor,
|
2019-02-13 15:45:35 +01:00
|
|
|
})}`,
|
2023-01-13 05:40:33 +01:00
|
|
|
user.followingCount,
|
|
|
|
renderedFollowees,
|
|
|
|
partOf,
|
2019-04-12 18:43:22 +02:00
|
|
|
undefined,
|
2023-01-13 05:40:33 +01:00
|
|
|
inStock
|
|
|
|
? `${partOf}?${url.query({
|
|
|
|
page: "true",
|
|
|
|
cursor: followings[followings.length - 1].id,
|
|
|
|
})}`
|
|
|
|
: undefined,
|
2018-08-14 13:13:32 +02:00
|
|
|
);
|
|
|
|
|
2019-01-30 18:29:36 +01:00
|
|
|
ctx.body = renderActivity(rendered);
|
2018-08-21 06:48:03 +02:00
|
|
|
setResponseType(ctx);
|
2018-08-14 13:13:32 +02:00
|
|
|
} else {
|
|
|
|
// index page
|
2023-01-13 05:40:33 +01:00
|
|
|
const rendered = renderOrderedCollection(
|
|
|
|
partOf,
|
|
|
|
user.followingCount,
|
|
|
|
`${partOf}?page=true`,
|
|
|
|
);
|
2019-01-30 18:29:36 +01:00
|
|
|
ctx.body = renderActivity(rendered);
|
2018-08-21 06:48:03 +02:00
|
|
|
setResponseType(ctx);
|
2018-08-14 13:13:32 +02:00
|
|
|
}
|
2021-07-20 18:45:41 +02:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
if (meta.secureMode || meta.privateMode) {
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.set("Cache-Control", "private, max-age=0, must-revalidate");
|
2021-07-20 18:45:41 +02:00
|
|
|
} else {
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.set("Cache-Control", "public, max-age=180");
|
2021-07-20 18:45:41 +02:00
|
|
|
}
|
2018-08-14 13:13:32 +02:00
|
|
|
};
|