summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/mastodon/argsUtils.ts
blob: 167d493ab602d047362aa723dbf8d6b32fb9d243 (plain)
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
/*
 * SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */

// Keys taken from:
// - https://docs.joinmastodon.org/methods/accounts/#statuses
// - https://docs.joinmastodon.org/methods/timelines/#public
// - https://docs.joinmastodon.org/methods/timelines/#tag
export interface TimelineArgs {
	max_id?: string;
	min_id?: string;
	since_id?: string;
	limit?: string;
	offset?: string;
	local?: string;
	pinned?: string;
	exclude_reblogs?: string;
	exclude_replies?: string;
	only_media?: string;
}

// Values taken from https://docs.joinmastodon.org/client/intro/#boolean
export function toBoolean(value: string | undefined): boolean | undefined {
	if (!value) return undefined;
	return !['0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].includes(value);
}

export function toInt(value: string | undefined): number | undefined {
	if (!value) return undefined;
	return parseInt(value);
}

export function parseTimelineArgs(q: TimelineArgs) {
	return {
		max_id: q.max_id,
		min_id: q.min_id,
		since_id: q.since_id,
		limit: typeof(q.limit) === 'string' ? parseInt(q.limit, 10) : undefined,
		offset: typeof(q.offset) === 'string' ? parseInt(q.offset, 10) : undefined,
		local: typeof(q.local) === 'string' ? toBoolean(q.local) : undefined,
		pinned: typeof(q.pinned) === 'string' ? toBoolean(q.pinned) : undefined,
		exclude_reblogs: typeof(q.exclude_reblogs) === 'string' ? toBoolean(q.exclude_reblogs) : undefined,
		exclude_replies: typeof(q.exclude_replies) === 'string' ? toBoolean(q.exclude_replies) : undefined,
		only_media: typeof(q.only_media) === 'string' ? toBoolean(q.only_media) : undefined,
	};
}