2023-01-13 05:40:33 +01:00
|
|
|
import { Brackets, IsNull } from "typeorm";
|
|
|
|
import config from "@/config/index.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 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 type { Note } from "@/models/entities/note.js";
|
|
|
|
import { checkFetch } from "@/remote/activitypub/check-fetch.js";
|
|
|
|
import { fetchMeta } from "@/misc/fetch-meta.js";
|
|
|
|
import { makePaginationQuery } from "../api/common/make-pagination-query.js";
|
|
|
|
import { setResponseType } from "../activitypub.js";
|
|
|
|
import type Router from "@koa/router";
|
2018-08-14 13:13:32 +02:00
|
|
|
|
2019-09-26 22:50:34 +02:00
|
|
|
export default async (ctx: Router.RouterContext) => {
|
2021-07-20 18:45:41 +02:00
|
|
|
const verify = await checkFetch(ctx.req);
|
2023-01-03 18:12:26 +01:00
|
|
|
if (verify !== 200) {
|
2021-07-20 18:45:41 +02:00
|
|
|
ctx.status = verify;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
const userId = ctx.params.user;
|
2018-08-14 13:13:32 +02:00
|
|
|
|
2022-04-17 13:44:21 +02:00
|
|
|
const sinceId = ctx.request.query.since_id;
|
2023-01-13 05:40:33 +01:00
|
|
|
if (sinceId != null && typeof sinceId !== "string") {
|
2022-04-17 13:44:21 +02:00
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
2018-08-14 13:13:32 +02:00
|
|
|
|
2022-04-17 13:44:21 +02:00
|
|
|
const untilId = ctx.request.query.until_id;
|
2023-01-13 05:40:33 +01:00
|
|
|
if (untilId != null && typeof untilId !== "string") {
|
2022-04-17 13:44:21 +02:00
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
2018-08-14 13:13:32 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const page = ctx.request.query.page === "true";
|
2018-08-14 13:13:32 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
if (countIf((x) => x != null, [sinceId, untilId]) > 1) {
|
2018-08-14 13:13:32 +02:00
|
|
|
ctx.status = 400;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-26 07:34:00 +01:00
|
|
|
const user = await Users.findOneBy({
|
2019-04-07 14:50:36 +02:00
|
|
|
id: userId,
|
2022-03-26 07:34:00 +01:00
|
|
|
host: IsNull(),
|
2022-03-25 08:27:41 +01:00
|
|
|
});
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const limit = 20;
|
|
|
|
const partOf = `${config.url}/users/${userId}/outbox`;
|
|
|
|
|
|
|
|
if (page) {
|
2023-01-13 05:40:33 +01: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 13:13:32 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
const notes = await query.take(limit).getMany();
|
2018-08-14 13:13:32 +02:00
|
|
|
|
|
|
|
if (sinceId) notes.reverse();
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const activities = await Promise.all(
|
|
|
|
notes.map((note) => packActivity(note)),
|
|
|
|
);
|
2018-08-14 13:13:32 +02:00
|
|
|
const rendered = renderOrderedCollectionPage(
|
2019-02-13 15:45:35 +01:00
|
|
|
`${partOf}?${url.query({
|
2023-01-13 05:40:33 +01:00
|
|
|
page: "true",
|
2019-02-13 15:45:35 +01:00
|
|
|
since_id: sinceId,
|
2021-12-09 15:58:30 +01:00
|
|
|
until_id: untilId,
|
2019-02-13 15:45:35 +01:00
|
|
|
})}`,
|
2023-01-13 05:40:33 +01:00
|
|
|
user.notesCount,
|
|
|
|
activities,
|
|
|
|
partOf,
|
|
|
|
notes.length
|
|
|
|
? `${partOf}?${url.query({
|
|
|
|
page: "true",
|
|
|
|
since_id: notes[0].id,
|
|
|
|
})}`
|
|
|
|
: undefined,
|
|
|
|
notes.length
|
|
|
|
? `${partOf}?${url.query({
|
|
|
|
page: "true",
|
|
|
|
until_id: notes[notes.length - 1].id,
|
|
|
|
})}`
|
|
|
|
: 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
|
2023-01-13 05:40:33 +01:00
|
|
|
const rendered = renderOrderedCollection(
|
|
|
|
partOf,
|
|
|
|
user.notesCount,
|
2018-08-14 13:13:32 +02:00
|
|
|
`${partOf}?page=true`,
|
2022-04-17 13:44:21 +02:00
|
|
|
`${partOf}?page=true&since_id=000000000000000000000000`,
|
2018-08-14 13:13:32 +02:00
|
|
|
);
|
2019-01-30 18:29:36 +01:00
|
|
|
ctx.body = renderActivity(rendered);
|
2021-07-20 18:45:41 +02:00
|
|
|
|
2018-08-21 06:48:03 +02:00
|
|
|
setResponseType(ctx);
|
2018-08-14 13:13:32 +02:00
|
|
|
}
|
2021-07-20 18:45:41 +02:00
|
|
|
const meta = await fetchMeta();
|
|
|
|
if (meta.secureMode || meta.privateMode) {
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.set("Cache-Control", "private, max-age=0, must-revalidate");
|
2021-07-20 18:45:41 +02:00
|
|
|
} else {
|
2023-01-13 05:40:33 +01:00
|
|
|
ctx.set("Cache-Control", "public, max-age=180");
|
2021-07-20 18:45:41 +02:00
|
|
|
}
|
2018-08-14 13:13:32 +02:00
|
|
|
};
|
2018-09-07 22:24:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pack Create<Note> or Announce Activity
|
|
|
|
* @param note Note
|
|
|
|
*/
|
2019-04-12 18:43:22 +02:00
|
|
|
export async function packActivity(note: Note): Promise<any> {
|
2023-01-13 05:40:33 +01:00
|
|
|
if (
|
|
|
|
note.renoteId &&
|
|
|
|
note.text == null &&
|
|
|
|
!note.hasPoll &&
|
|
|
|
(note.fileIds == null || note.fileIds.length === 0)
|
|
|
|
) {
|
2022-03-26 07:34:00 +01:00
|
|
|
const renote = await Notes.findOneByOrFail({ id: note.renoteId });
|
2023-01-13 05:40:33 +01:00
|
|
|
return renderAnnounce(
|
|
|
|
renote.uri ? renote.uri : `${config.url}/notes/${renote.id}`,
|
|
|
|
note,
|
|
|
|
);
|
2018-09-07 22:24:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return renderCreate(await renderNote(note, false), note);
|
|
|
|
}
|