summary refs log tree commit diff
path: root/src/activitypub/routes/channel/#channel_id/outbox.ts
blob: 287c66606d08aa399d0181fb06f9af2fac57b7a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import { makeOrderedCollection } from "@spacebar/ap";
import { route } from "@spacebar/api";
import { Config, Message, Snowflake } from "@spacebar/util";
import { Router } from "express";
import { FindManyOptions, FindOperator, LessThan, MoreThan } from "typeorm";

const router = Router();
export default router;

router.get("/", route({}), async (req, res) => {
	// TODO: authentication

	const { channel_id } = req.params;
	const { page, min_id, max_id } = req.query;

	const { webDomain } = Config.get().federation;

	const ret = await makeOrderedCollection(
		req,
		`https://${webDomain}/fed/channels/${channel_id}/outbox`,
		() => Message.count({ where: { channel_id } }),
		async (before, after) => {
			const query: FindManyOptions<Message> & {
				where: { id?: FindOperator<string> | FindOperator<string>[] };
			} = {
				order: { timestamp: "DESC" },
				take: 20,
				where: { channel_id: channel_id },
				relations: ["author"],
			};

			if (after) {
				if (BigInt(after) > BigInt(Snowflake.generate())) return [];
				query.where.id = MoreThan(after);
			} else if (before) {
				if (BigInt(before) > BigInt(Snowflake.generate())) return [];
				query.where.id = LessThan(before);
			}

			const messages = await Message.find(query);

			return messages.map((x) => ({
				...x,
				toAP: () => x.toAnnounceAP(),
			}));
		},
	);

	return res.json(ret);
});