2022-02-27 02:07:39 +00:00
|
|
|
import Router from '@koa/router';
|
|
|
|
import config from '@/config/index.js';
|
2018-12-15 16:44:59 +00:00
|
|
|
import $ from 'cafy';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { ID } from '@/misc/cafy-id.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 { setResponseType } from '../activitypub.js';
|
|
|
|
import renderNote from '@/remote/activitypub/renderer/note.js';
|
|
|
|
import renderCreate from '@/remote/activitypub/renderer/create.js';
|
|
|
|
import renderAnnounce from '@/remote/activitypub/renderer/announce.js';
|
|
|
|
import { countIf } from '@/prelude/array.js';
|
|
|
|
import * as url from '@/prelude/url.js';
|
|
|
|
import { Users, Notes } from '@/models/index.js';
|
|
|
|
import { makePaginationQuery } from '../api/common/make-pagination-query.js';
|
2019-04-07 12:50:36 +00:00
|
|
|
import { Brackets } from 'typeorm';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { Note } from '@/models/entities/note.js';
|
2018-08-14 11:13:32 +00:00
|
|
|
|
2019-09-26 20:50:34 +00:00
|
|
|
export default async (ctx: Router.RouterContext) => {
|
2019-04-07 12:50:36 +00:00
|
|
|
const userId = ctx.params.user;
|
2018-08-14 11:13:32 +00:00
|
|
|
|
|
|
|
// Get 'sinceId' parameter
|
2022-02-28 16:24:50 +00:00
|
|
|
const [sinceId, sinceIdErr] = $.default.optional.type(ID).get(ctx.request.query.since_id);
|
2018-08-14 11:13:32 +00:00
|
|
|
|
|
|
|
// Get 'untilId' parameter
|
2022-02-28 16:24:50 +00:00
|
|
|
const [untilId, untilIdErr] = $.default.optional.type(ID).get(ctx.request.query.until_id);
|
2018-08-14 11:13:32 +00:00
|
|
|
|
|
|
|
// Get 'page' parameter
|
2022-02-28 16:24:50 +00:00
|
|
|
const pageErr = !$.default.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
|
2018-09-05 17:16:08 +00:00
|
|
|
if (sinceIdErr || untilIdErr || pageErr || countIf(x => x != null, [sinceId, untilId]) > 1) {
|
2018-08-14 11:13:32 +00:00
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-25 07:27:41 +00:00
|
|
|
const user = await Users.findOne({
|
2019-04-07 12:50:36 +00:00
|
|
|
id: userId,
|
2021-12-09 14:58:30 +00:00
|
|
|
host: null,
|
2022-03-25 07:27:41 +00:00
|
|
|
});
|
2018-08-14 11:13:32 +00:00
|
|
|
|
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 = 20;
|
|
|
|
const partOf = `${config.url}/users/${userId}/outbox`;
|
|
|
|
|
|
|
|
if (page) {
|
2019-04-07 12:50:36 +00: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 11:13:32 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
const notes = await query.take(limit).getMany();
|
2018-08-14 11:13:32 +00:00
|
|
|
|
|
|
|
if (sinceId) notes.reverse();
|
|
|
|
|
2018-09-07 20:24:55 +00:00
|
|
|
const activities = await Promise.all(notes.map(note => packActivity(note)));
|
2018-08-14 11:13:32 +00:00
|
|
|
const rendered = renderOrderedCollectionPage(
|
2019-02-13 14:45:35 +00:00
|
|
|
`${partOf}?${url.query({
|
|
|
|
page: 'true',
|
|
|
|
since_id: sinceId,
|
2021-12-09 14:58:30 +00:00
|
|
|
until_id: untilId,
|
2019-02-13 14:45:35 +00:00
|
|
|
})}`,
|
2018-09-07 20:24:55 +00:00
|
|
|
user.notesCount, activities, partOf,
|
2019-02-13 14:45:35 +00:00
|
|
|
notes.length ? `${partOf}?${url.query({
|
|
|
|
page: 'true',
|
2021-12-09 14:58:30 +00:00
|
|
|
since_id: notes[0].id,
|
2019-04-12 16:43:22 +00:00
|
|
|
})}` : undefined,
|
2019-02-13 14:45:35 +00:00
|
|
|
notes.length ? `${partOf}?${url.query({
|
|
|
|
page: 'true',
|
2021-12-09 14:58:30 +00:00
|
|
|
until_id: notes[notes.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
|
|
|
|
const rendered = renderOrderedCollection(partOf, user.notesCount,
|
|
|
|
`${partOf}?page=true`,
|
|
|
|
`${partOf}?page=true&since_id=000000000000000000000000`
|
|
|
|
);
|
2019-01-30 17:29:36 +00:00
|
|
|
ctx.body = renderActivity(rendered);
|
2020-05-15 12:37:09 +00:00
|
|
|
ctx.set('Cache-Control', 'public, max-age=180');
|
2018-08-21 04:48:03 +00:00
|
|
|
setResponseType(ctx);
|
2018-08-14 11:13:32 +00:00
|
|
|
}
|
|
|
|
};
|
2018-09-07 20:24:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pack Create<Note> or Announce Activity
|
|
|
|
* @param note Note
|
|
|
|
*/
|
2019-04-12 16:43:22 +00:00
|
|
|
export async function packActivity(note: Note): Promise<any> {
|
2020-04-03 23:46:54 +00:00
|
|
|
if (note.renoteId && note.text == null && !note.hasPoll && (note.fileIds == null || note.fileIds.length === 0)) {
|
2021-02-13 06:33:38 +00:00
|
|
|
const renote = await Notes.findOneOrFail(note.renoteId);
|
2019-04-07 12:50:36 +00:00
|
|
|
return renderAnnounce(renote.uri ? renote.uri : `${config.url}/notes/${renote.id}`, note);
|
2018-09-07 20:24:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return renderCreate(await renderNote(note, false), note);
|
|
|
|
}
|