summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/admin/relays/add.ts
blob: e10bd92c8d8c0c021af019bb0dc6fcd8a454a520 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { URL } from 'url';
import $ from 'cafy';
import define from '../../../define';
import { addRelay } from '../../../../../services/relay';
import { ApiError } from '../../../error';

export const meta = {
	desc: {
		'ja-JP': 'リレーを追加します。',
		'en-US': 'Add relay'
	},

	tags: ['admin'],

	requireCredential: true as const,
	requireModerator: true as const,

	params: {
		inbox: {
			validator: $.str
		},
	},

	errors: {
		invalidUrl: {
			message: 'Invalid URL',
			code: 'INVALID_URL',
			id: 'fb8c92d3-d4e5-44e7-b3d4-800d5cef8b2c'
		},
	},

	res: {
		type: 'object' as const,
		optional: false as const, nullable: false as const,
		properties: {
			id: {
				type: 'string' as const,
				optional: false as const, nullable: false as const,
				format: 'id'
			},
			inbox: {
				type: 'string' as const,
				optional: false as const, nullable: false as const,
				format: 'url'
			},
			status: {
				type: 'string' as const,
				optional: false as const, nullable: false as const,
				default: 'requesting',
				enum: [
					'requesting',
					'accepted',
					'rejected'
				]
			}
		}
	}
};

export default define(meta, async (ps, user) => {
	try {
		if (new URL(ps.inbox).protocol !== 'https:') throw 'https only';
	} catch {
		throw new ApiError(meta.errors.invalidUrl);
	}

	return await addRelay(ps.inbox);
});