summary refs log tree commit diff
path: root/src/activitypub/federation/OrderedCollection.ts
blob: f3b8feea71356fe0fb330aa7008a93b69e5d529d (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
import { APOrderedCollection, AnyAPObject } from "activitypub-types";
import { ACTIVITYSTREAMS_CONTEXT } from "./utils";

export const makeOrderedCollection = async <T extends AnyAPObject>(opts: {
	page: boolean;
	min_id?: string;
	max_id?: string;
	id: string;
	getTotalElements: () => Promise<number>;
	getElements: (before?: string, after?: string) => Promise<T[]>;
}): Promise<APOrderedCollection> => {
	const { page, min_id, max_id, id, getTotalElements, getElements } = opts;

	if (!page)
		return {
			"@context": ACTIVITYSTREAMS_CONTEXT,
			id: id,
			type: "OrderedCollection",
			totalItems: await getTotalElements(),
			first: new URL(`${id}?page=true`),
			last: new URL(`${id}?page=true&min_id=0`),
		};

	const after = min_id ? `${min_id}` : undefined;
	const before = max_id ? `${max_id}` : undefined;

	const elems = await getElements(before, after);

	// TODO: we need to specify next/prev props
	// and should probably let the caller of this function specify what they are
	// along with first/last
	return {
		"@context": ACTIVITYSTREAMS_CONTEXT,
		id: `${id}?page=true`,
		type: "OrderedCollection",
		first: new URL(`${id}?page=true`),
		last: new URL(`${id}?page=true&min_id=0`),
		totalItems: await getTotalElements(),
		orderedItems: elems,
	};
};