2019-09-27 05:50:34 +09:00
|
|
|
import * as Router from '@koa/router';
|
2018-08-14 20:13:32 +09:00
|
|
|
import config from '../../config';
|
2018-12-16 01:44:59 +09:00
|
|
|
import $ from 'cafy';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { ID } from '../../misc/cafy-id';
|
2019-01-31 02:29:36 +09:00
|
|
|
import { renderActivity } from '../../remote/activitypub/renderer';
|
2018-08-14 20:13:32 +09:00
|
|
|
import renderOrderedCollection from '../../remote/activitypub/renderer/ordered-collection';
|
|
|
|
import renderOrderedCollectionPage from '../../remote/activitypub/renderer/ordered-collection-page';
|
2018-08-21 13:48:03 +09:00
|
|
|
import { setResponseType } from '../activitypub';
|
2018-08-14 20:13:32 +09:00
|
|
|
import renderNote from '../../remote/activitypub/renderer/note';
|
2018-09-08 05:24:55 +09:00
|
|
|
import renderCreate from '../../remote/activitypub/renderer/create';
|
|
|
|
import renderAnnounce from '../../remote/activitypub/renderer/announce';
|
2018-09-06 02:16:08 +09:00
|
|
|
import { countIf } from '../../prelude/array';
|
2019-02-13 23:45:35 +09:00
|
|
|
import * as url from '../../prelude/url';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { Users, Notes } from '../../models';
|
|
|
|
import { makePaginationQuery } from '../api/common/make-pagination-query';
|
|
|
|
import { Brackets } from 'typeorm';
|
|
|
|
import { Note } from '../../models/entities/note';
|
2019-04-13 01:43:22 +09:00
|
|
|
import { ensure } from '../../prelude/ensure';
|
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 'sinceId' parameter
|
2019-02-13 16:33:07 +09:00
|
|
|
const [sinceId, sinceIdErr] = $.optional.type(ID).get(ctx.request.query.since_id);
|
2018-08-14 20:13:32 +09:00
|
|
|
|
|
|
|
// Get 'untilId' parameter
|
2019-02-13 16:33:07 +09:00
|
|
|
const [untilId, untilIdErr] = $.optional.type(ID).get(ctx.request.query.until_id);
|
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
|
2018-09-06 02:16:08 +09:00
|
|
|
if (sinceIdErr || untilIdErr || pageErr || countIf(x => x != null, [sinceId, untilId]) > 1) {
|
2018-08-14 20:13:32 +09:00
|
|
|
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 = 20;
|
|
|
|
const partOf = `${config.url}/users/${userId}/outbox`;
|
|
|
|
|
|
|
|
if (page) {
|
2019-04-07 21:50:36 +09:00
|
|
|
const query = makePaginationQuery(Notes.createQueryBuilder('note'), sinceId, untilId)
|
|
|
|
.andWhere('note.userId = :userId', { userId: user.id })
|
|
|
|
.andWhere(new Brackets(qb => { qb
|
|
|
|
.where(`note.visibility = 'public'`)
|
|
|
|
.orWhere(`note.visibility = 'home'`);
|
|
|
|
}))
|
|
|
|
.andWhere('note.localOnly = FALSE');
|
2018-08-14 20:13:32 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
const notes = await query.take(limit).getMany();
|
2018-08-14 20:13:32 +09:00
|
|
|
|
|
|
|
if (sinceId) notes.reverse();
|
|
|
|
|
2018-09-08 05:24:55 +09:00
|
|
|
const activities = await Promise.all(notes.map(note => packActivity(note)));
|
2018-08-14 20:13:32 +09:00
|
|
|
const rendered = renderOrderedCollectionPage(
|
2019-02-13 23:45:35 +09:00
|
|
|
`${partOf}?${url.query({
|
|
|
|
page: 'true',
|
|
|
|
since_id: sinceId,
|
|
|
|
until_id: untilId
|
|
|
|
})}`,
|
2018-09-08 05:24:55 +09:00
|
|
|
user.notesCount, activities, partOf,
|
2019-02-13 23:45:35 +09:00
|
|
|
notes.length ? `${partOf}?${url.query({
|
|
|
|
page: 'true',
|
2019-04-07 21:50:36 +09:00
|
|
|
since_id: notes[0].id
|
2019-04-13 01:43:22 +09:00
|
|
|
})}` : undefined,
|
2019-02-13 23:45:35 +09:00
|
|
|
notes.length ? `${partOf}?${url.query({
|
|
|
|
page: 'true',
|
2019-04-07 21:50:36 +09:00
|
|
|
until_id: notes[notes.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-09-19 07:17:19 +09:00
|
|
|
ctx.set('Cache-Control', 'private, max-age=0, must-revalidate');
|
2018-08-21 13:48:03 +09:00
|
|
|
setResponseType(ctx);
|
2018-08-14 20:13:32 +09:00
|
|
|
} else {
|
|
|
|
// index page
|
|
|
|
const rendered = renderOrderedCollection(partOf, user.notesCount,
|
|
|
|
`${partOf}?page=true`,
|
|
|
|
`${partOf}?page=true&since_id=000000000000000000000000`
|
|
|
|
);
|
2019-01-31 02:29:36 +09:00
|
|
|
ctx.body = renderActivity(rendered);
|
2018-09-19 07:17:19 +09:00
|
|
|
ctx.set('Cache-Control', 'private, max-age=0, must-revalidate');
|
2018-08-21 13:48:03 +09:00
|
|
|
setResponseType(ctx);
|
2018-08-14 20:13:32 +09:00
|
|
|
}
|
|
|
|
};
|
2018-09-08 05:24:55 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pack Create<Note> or Announce Activity
|
|
|
|
* @param note Note
|
|
|
|
*/
|
2019-04-13 01:43:22 +09:00
|
|
|
export async function packActivity(note: Note): Promise<any> {
|
2019-04-07 21:50:36 +09:00
|
|
|
if (note.renoteId && note.text == null && !note.hasPoll && (note.fileIds == null || note.fileIds.length == 0)) {
|
2019-04-13 01:43:22 +09:00
|
|
|
const renote = await Notes.findOne(note.renoteId).then(ensure);
|
2019-04-07 21:50:36 +09:00
|
|
|
return renderAnnounce(renote.uri ? renote.uri : `${config.url}/notes/${renote.id}`, note);
|
2018-09-08 05:24:55 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
return renderCreate(await renderNote(note, false), note);
|
|
|
|
}
|