2019-09-27 05:50:34 +09:00
|
|
|
import * as Router from '@koa/router';
|
2021-08-19 21:55:45 +09:00
|
|
|
import config from '@/config/index';
|
2019-02-05 11:48:08 +09:00
|
|
|
import $ from 'cafy';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { ID } from '@/misc/cafy-id';
|
2021-08-19 22:04:15 +09:00
|
|
|
import * as url from '@/prelude/url';
|
2021-08-19 21:55:45 +09:00
|
|
|
import { renderActivity } from '@/remote/activitypub/renderer/index';
|
|
|
|
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';
|
|
|
|
import { setResponseType } from '../activitypub';
|
|
|
|
import { Users, Followings } from '@/models/index';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { LessThan } from 'typeorm';
|
2018-08-14 20:13:32 +09:00
|
|
|
|
2019-09-27 05:50:34 +09:00
|
|
|
export default async (ctx: Router.RouterContext) => {
|
2019-04-07 21:50:36 +09:00
|
|
|
const userId = ctx.params.user;
|
2018-08-14 20:13:32 +09:00
|
|
|
|
|
|
|
// Get 'cursor' parameter
|
2019-02-13 23:45:35 +09:00
|
|
|
const [cursor, cursorErr] = $.optional.type(ID).get(ctx.request.query.cursor);
|
2018-08-14 20:13:32 +09:00
|
|
|
|
|
|
|
// Get 'page' parameter
|
2019-02-13 16:33:07 +09:00
|
|
|
const pageErr = !$.optional.str.or(['true', 'false']).ok(ctx.request.query.page);
|
2018-08-14 20:13:32 +09:00
|
|
|
const page: boolean = ctx.request.query.page === 'true';
|
|
|
|
|
|
|
|
// Validate parameters
|
|
|
|
if (cursorErr || pageErr) {
|
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify user
|
2019-04-07 21:50:36 +09:00
|
|
|
const user = await Users.findOne({
|
|
|
|
id: userId,
|
2018-08-14 20:13:32 +09:00
|
|
|
host: null
|
|
|
|
});
|
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
if (user == null) {
|
2018-08-14 20:13:32 +09:00
|
|
|
ctx.status = 404;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const limit = 10;
|
|
|
|
const partOf = `${config.url}/users/${userId}/followers`;
|
|
|
|
|
|
|
|
if (page) {
|
|
|
|
const query = {
|
2019-04-07 21:50:36 +09:00
|
|
|
followeeId: user.id
|
2018-08-14 20:13:32 +09:00
|
|
|
} as any;
|
|
|
|
|
|
|
|
// カーソルが指定されている場合
|
|
|
|
if (cursor) {
|
2019-04-07 21:50:36 +09:00
|
|
|
query.id = LessThan(cursor);
|
2018-08-14 20:13:32 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get followers
|
2019-04-07 21:50:36 +09:00
|
|
|
const followings = await Followings.find({
|
|
|
|
where: query,
|
|
|
|
take: limit + 1,
|
|
|
|
order: { id: -1 }
|
|
|
|
});
|
2018-08-14 20:13:32 +09:00
|
|
|
|
|
|
|
// 「次のページ」があるかどうか
|
|
|
|
const inStock = followings.length === limit + 1;
|
|
|
|
if (inStock) followings.pop();
|
|
|
|
|
|
|
|
const renderedFollowers = await Promise.all(followings.map(following => renderFollowUser(following.followerId)));
|
|
|
|
const rendered = renderOrderedCollectionPage(
|
2019-02-13 23:45:35 +09:00
|
|
|
`${partOf}?${url.query({
|
|
|
|
page: 'true',
|
|
|
|
cursor
|
|
|
|
})}`,
|
2018-08-14 20:13:32 +09:00
|
|
|
user.followersCount, renderedFollowers, partOf,
|
2019-04-13 01:43:22 +09:00
|
|
|
undefined,
|
2019-02-13 23:45:35 +09:00
|
|
|
inStock ? `${partOf}?${url.query({
|
|
|
|
page: 'true',
|
2019-04-07 21:50:36 +09:00
|
|
|
cursor: followings[followings.length - 1].id
|
2019-04-13 01:43:22 +09:00
|
|
|
})}` : undefined
|
2018-08-14 20:13:32 +09:00
|
|
|
);
|
|
|
|
|
2019-01-31 02:29:36 +09:00
|
|
|
ctx.body = renderActivity(rendered);
|
2018-08-21 13:48:03 +09:00
|
|
|
setResponseType(ctx);
|
2018-08-14 20:13:32 +09:00
|
|
|
} else {
|
|
|
|
// index page
|
2019-04-13 01:43:22 +09:00
|
|
|
const rendered = renderOrderedCollection(partOf, user.followersCount, `${partOf}?page=true`);
|
2019-01-31 02:29:36 +09:00
|
|
|
ctx.body = renderActivity(rendered);
|
2020-05-15 21:37:09 +09:00
|
|
|
ctx.set('Cache-Control', 'public, max-age=180');
|
2018-08-21 13:48:03 +09:00
|
|
|
setResponseType(ctx);
|
2018-08-14 20:13:32 +09:00
|
|
|
}
|
|
|
|
};
|