2018-08-21 04:48:03 +00:00
|
|
|
import * as Router from 'koa-router';
|
2018-08-14 11:13:32 +00:00
|
|
|
import config from '../../config';
|
2018-12-15 16:44:59 +00:00
|
|
|
import $ from 'cafy';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { ID } from '../../misc/cafy-id';
|
2019-02-13 14:45:35 +00:00
|
|
|
import * as url from '../../prelude/url';
|
2019-01-30 17:29:36 +00:00
|
|
|
import { renderActivity } from '../../remote/activitypub/renderer';
|
2018-08-14 11:13:32 +00:00
|
|
|
import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection';
|
|
|
|
import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page';
|
|
|
|
import renderFollowUser from '../../remote/activitypub/renderer/follow-user';
|
2018-08-21 04:48:03 +00:00
|
|
|
import { setResponseType } from '../activitypub';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { Users, Followings } from '../../models';
|
|
|
|
import { LessThan, FindConditions } from 'typeorm';
|
|
|
|
import { Following } from '../../models/entities/following';
|
2018-08-14 11:13:32 +00:00
|
|
|
|
2018-08-21 04:48:03 +00:00
|
|
|
export default async (ctx: Router.IRouterContext) => {
|
2019-04-07 12:50:36 +00:00
|
|
|
const userId = ctx.params.user;
|
2018-08-14 11:13:32 +00:00
|
|
|
|
|
|
|
// Get 'cursor' parameter
|
2019-02-13 14:45:35 +00:00
|
|
|
const [cursor, cursorErr] = $.optional.type(ID).get(ctx.request.query.cursor);
|
2018-08-14 11:13:32 +00:00
|
|
|
|
|
|
|
// Get 'page' parameter
|
2019-02-13 07:33:07 +00:00
|
|
|
const pageErr = !$.optional.str.or(['true', 'false']).ok(ctx.request.query.page);
|
2018-08-14 11:13:32 +00:00
|
|
|
const page: boolean = ctx.request.query.page === 'true';
|
|
|
|
|
|
|
|
// Validate parameters
|
|
|
|
if (cursorErr || pageErr) {
|
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify user
|
2019-04-07 12:50:36 +00:00
|
|
|
const user = await Users.findOne({
|
|
|
|
id: userId,
|
2018-08-14 11:13:32 +00:00
|
|
|
host: null
|
|
|
|
});
|
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (user == null) {
|
2018-08-14 11:13:32 +00:00
|
|
|
ctx.status = 404;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const limit = 10;
|
|
|
|
const partOf = `${config.url}/users/${userId}/following`;
|
|
|
|
|
|
|
|
if (page) {
|
|
|
|
const query = {
|
2019-04-07 12:50:36 +00:00
|
|
|
followerId: user.id
|
|
|
|
} as FindConditions<Following>;
|
2018-08-14 11:13:32 +00:00
|
|
|
|
|
|
|
// カーソルが指定されている場合
|
|
|
|
if (cursor) {
|
2019-04-07 12:50:36 +00:00
|
|
|
query.id = LessThan(cursor);
|
2018-08-14 11:13:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get followings
|
2019-04-07 12:50:36 +00:00
|
|
|
const followings = await Followings.find({
|
|
|
|
where: query,
|
|
|
|
take: limit + 1,
|
|
|
|
order: { id: -1 }
|
|
|
|
});
|
2018-08-14 11:13:32 +00: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 14:45:35 +00:00
|
|
|
`${partOf}?${url.query({
|
|
|
|
page: 'true',
|
|
|
|
cursor
|
|
|
|
})}`,
|
2018-08-14 11:13:32 +00:00
|
|
|
user.followingCount, renderedFollowees, partOf,
|
2019-04-12 16:43:22 +00:00
|
|
|
undefined,
|
2019-02-13 14:45:35 +00:00
|
|
|
inStock ? `${partOf}?${url.query({
|
|
|
|
page: 'true',
|
2019-04-07 12:50:36 +00:00
|
|
|
cursor: followings[followings.length - 1].id
|
2019-04-12 16:43:22 +00:00
|
|
|
})}` : undefined
|
2018-08-14 11:13:32 +00:00
|
|
|
);
|
|
|
|
|
2019-01-30 17:29:36 +00:00
|
|
|
ctx.body = renderActivity(rendered);
|
2018-08-21 04:48:03 +00:00
|
|
|
setResponseType(ctx);
|
2018-08-14 11:13:32 +00:00
|
|
|
} else {
|
|
|
|
// index page
|
2019-04-12 16:43:22 +00:00
|
|
|
const rendered = renderOrderedCollection(partOf, user.followingCount, `${partOf}?page=true`);
|
2019-01-30 17:29:36 +00:00
|
|
|
ctx.body = renderActivity(rendered);
|
2018-09-18 22:17:19 +00:00
|
|
|
ctx.set('Cache-Control', 'private, max-age=0, must-revalidate');
|
2018-08-21 04:48:03 +00:00
|
|
|
setResponseType(ctx);
|
2018-08-14 11:13:32 +00:00
|
|
|
}
|
|
|
|
};
|