2022-02-27 03:07:39 +01:00
|
|
|
import Router from '@koa/router';
|
|
|
|
import config from '@/config/index.js';
|
2018-12-15 17:44:59 +01:00
|
|
|
import $ from 'cafy';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { ID } from '@/misc/cafy-id.js';
|
|
|
|
import * as url from '@/prelude/url.js';
|
|
|
|
import { renderActivity } from '@/remote/activitypub/renderer/index.js';
|
|
|
|
import renderOrderedCollection from '@/remote/activitypub/renderer/ordered-collection.js';
|
|
|
|
import renderOrderedCollectionPage from '@/remote/activitypub/renderer/ordered-collection-page.js';
|
|
|
|
import renderFollowUser from '@/remote/activitypub/renderer/follow-user.js';
|
|
|
|
import { setResponseType } from '../activitypub.js';
|
|
|
|
import { Users, Followings, UserProfiles } from '@/models/index.js';
|
2019-04-07 14:50:36 +02:00
|
|
|
import { LessThan, FindConditions } from 'typeorm';
|
2022-02-27 03:07:39 +01:00
|
|
|
import { Following } from '@/models/entities/following.js';
|
2022-03-20 21:21:37 +01:00
|
|
|
import { userCache } from './cache.js';
|
2018-08-14 13:13:32 +02:00
|
|
|
|
2019-09-26 22:50:34 +02:00
|
|
|
export default async (ctx: Router.RouterContext) => {
|
2019-04-07 14:50:36 +02:00
|
|
|
const userId = ctx.params.user;
|
2018-08-14 13:13:32 +02:00
|
|
|
|
|
|
|
// Get 'cursor' parameter
|
2022-02-28 17:24:50 +01:00
|
|
|
const [cursor, cursorErr] = $.default.optional.type(ID).get(ctx.request.query.cursor);
|
2018-08-14 13:13:32 +02:00
|
|
|
|
|
|
|
// Get 'page' parameter
|
2022-02-28 17:24:50 +01:00
|
|
|
const pageErr = !$.default.optional.str.or(['true', 'false']).ok(ctx.request.query.page);
|
2018-08-14 13:13:32 +02:00
|
|
|
const page: boolean = ctx.request.query.page === 'true';
|
|
|
|
|
|
|
|
// Validate parameters
|
|
|
|
if (cursorErr || pageErr) {
|
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-20 21:21:37 +01:00
|
|
|
// TODO: typeorm 3.0にしたら .then(x => x || null) は消せる
|
|
|
|
const user = await userCache.fetch(userId, () => Users.findOne({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: userId,
|
2021-12-09 15:58:30 +01:00
|
|
|
host: null,
|
2022-03-20 21:21:37 +01:00
|
|
|
}).then(x => x || null));
|
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
|
|
|
|
const profile = await UserProfiles.findOneOrFail(user.id);
|
|
|
|
|
|
|
|
if (profile.ffVisibility === 'private') {
|
|
|
|
ctx.status = 403;
|
|
|
|
ctx.set('Cache-Control', 'public, max-age=30');
|
|
|
|
return;
|
|
|
|
} else if (profile.ffVisibility === 'followers') {
|
|
|
|
ctx.status = 403;
|
|
|
|
ctx.set('Cache-Control', 'public, max-age=30');
|
|
|
|
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,
|
2019-04-07 14:50:36 +02:00
|
|
|
} as FindConditions<Following>;
|
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
|
|
|
|
|
|
|
// 「次のページ」があるかどうか
|
|
|
|
const inStock = followings.length === limit + 1;
|
|
|
|
if (inStock) followings.pop();
|
|
|
|
|
|
|
|
const renderedFollowees = await Promise.all(followings.map(following => renderFollowUser(following.followeeId)));
|
|
|
|
const rendered = renderOrderedCollectionPage(
|
2019-02-13 15:45:35 +01:00
|
|
|
`${partOf}?${url.query({
|
|
|
|
page: 'true',
|
2021-12-09 15:58:30 +01:00
|
|
|
cursor,
|
2019-02-13 15:45:35 +01:00
|
|
|
})}`,
|
2018-08-14 13:13:32 +02:00
|
|
|
user.followingCount, renderedFollowees, partOf,
|
2019-04-12 18:43:22 +02:00
|
|
|
undefined,
|
2019-02-13 15:45:35 +01:00
|
|
|
inStock ? `${partOf}?${url.query({
|
|
|
|
page: 'true',
|
2021-12-09 15:58:30 +01:00
|
|
|
cursor: followings[followings.length - 1].id,
|
2019-04-12 18:43:22 +02:00
|
|
|
})}` : 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
|
2019-04-12 18:43:22 +02:00
|
|
|
const rendered = renderOrderedCollection(partOf, user.followingCount, `${partOf}?page=true`);
|
2019-01-30 18:29:36 +01:00
|
|
|
ctx.body = renderActivity(rendered);
|
2020-05-15 14:37:09 +02:00
|
|
|
ctx.set('Cache-Control', 'public, max-age=180');
|
2018-08-21 06:48:03 +02:00
|
|
|
setResponseType(ctx);
|
2018-08-14 13:13:32 +02:00
|
|
|
}
|
|
|
|
};
|