summary refs log tree commit diff
path: root/src/api/routes/channels/#channel_id/invites.ts
blob: b02f65d3736abc09dd6b4e87050eb6e999193c7a (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
	Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
	Copyright (C) 2023 Spacebar and Spacebar Contributors
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU Affero General Public License as published
	by the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Affero General Public License for more details.
	
	You should have received a copy of the GNU Affero General Public License
	along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

import { random, route } from "@spacebar/api";
import {
	Channel,
	Guild,
	Invite,
	InviteCreateEvent,
	PublicInviteRelation,
	User,
	emitEvent,
	isTextChannel,
} from "@spacebar/util";
import { Request, Response, Router } from "express";
import { HTTPError } from "lambert-server";

const router: Router = Router();

router.post(
	"/",
	route({
		requestBody: "InviteCreateSchema",
		permission: "CREATE_INSTANT_INVITE",
		right: "CREATE_INVITES",
		responses: {
			201: {
				body: "Invite",
			},
			404: {},
			400: {
				body: "APIErrorResponse",
			},
		},
	}),
	async (req: Request, res: Response) => {
		const { user_id } = req;
		const { channel_id } = req.params;
		const channel = await Channel.findOneOrFail({
			where: { id: channel_id },
			select: ["id", "name", "type", "guild_id"],
		});
		isTextChannel(channel.type);

		if (!channel.guild_id) {
			throw new HTTPError("This channel doesn't exist", 404);
		}
		const { guild_id } = channel;

		const expires_at = new Date(req.body.max_age * 1000 + Date.now());

		const invite = await Invite.create({
			code: random(),
			temporary: req.body.temporary || true,
			uses: 0,
			max_uses: req.body.max_uses,
			max_age: req.body.max_age,
			expires_at,
			created_at: new Date(),
			guild_id,
			channel_id: channel_id,
			inviter_id: user_id,
		}).save();
		const data = invite.toJSON();
		data.inviter = await User.getPublicUser(req.user_id);
		data.guild = await Guild.findOne({ where: { id: guild_id } });
		data.channel = channel;

		await emitEvent({
			event: "INVITE_CREATE",
			data,
			guild_id,
		} as InviteCreateEvent);
		res.status(201).send(data);
	},
);

router.get(
	"/",
	route({
		permission: "MANAGE_CHANNELS",
		responses: {
			200: {
				body: "APIInviteArray",
			},
			404: {},
		},
	}),
	async (req: Request, res: Response) => {
		const { channel_id } = req.params;
		const channel = await Channel.findOneOrFail({
			where: { id: channel_id },
		});

		if (!channel.guild_id) {
			throw new HTTPError("This channel doesn't exist", 404);
		}
		const { guild_id } = channel;

		const invites = await Invite.find({
			where: { guild_id },
			relations: PublicInviteRelation,
		});

		res.status(200).send(invites);
	},
);

export default router;