summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-03 15:48:24 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-03 15:48:24 +0200
commitebd0537c5c44f0bd9309710ba149e090027027ed (patch)
treea5b0d92ba109158f931280a576abb80ba437f6d5
parent:zap: improve invite generation (diff)
downloadserver-ebd0537c5c44f0bd9309710ba149e090027027ed.tar.xz
:zap: improve invite generation
-rw-r--r--api/src/routes/channels/#channel_id/invites.ts38
1 files changed, 28 insertions, 10 deletions
diff --git a/api/src/routes/channels/#channel_id/invites.ts b/api/src/routes/channels/#channel_id/invites.ts
index 35006ac4..fcc8a1ad 100644
--- a/api/src/routes/channels/#channel_id/invites.ts
+++ b/api/src/routes/channels/#channel_id/invites.ts
@@ -6,26 +6,42 @@ import { random } from "../../../util/RandomInviteID";
 
 import { InviteCreateSchema } from "../../../schema/Invite";
 
-import { getPermission, Channel, Invite, InviteCreateEvent, emitEvent } from "@fosscord/util";
+import { getPermission, Channel, Invite, InviteCreateEvent, emitEvent, User, Guild } from "@fosscord/util";
+import { isTextChannel } from "./messages";
 
 const router: Router = Router();
 
 router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) => {
 	const { user_id } = req;
 	const { channel_id } = req.params;
-	const channel = await Channel.findOneOrFail({ id: channel_id });
+	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 permission = await getPermission(user_id, guild_id);
+	const permission = await getPermission(user_id, guild_id, undefined, {
+		guild_select: [
+			"banner",
+			"description",
+			"features",
+			"icon",
+			"id",
+			"name",
+			"nsfw",
+			"nsfw_level",
+			"splash",
+			"vanity_url_code",
+			"verification_level"
+		] as (keyof Guild)[]
+	});
 	permission.hasThrow("CREATE_INSTANT_INVITE");
 
 	const expires_at = new Date(req.body.max_age * 1000 + Date.now());
 
-	const invite = {
+	const invite = await new Invite({
 		code: random(),
 		temporary: req.body.temporary,
 		uses: 0,
@@ -36,12 +52,14 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response)
 		guild_id,
 		channel_id: channel_id,
 		inviter_id: user_id
-	};
-
-	await new Invite(invite).save();
-
-	await emitEvent({ event: "INVITE_CREATE", data: invite, guild_id } as InviteCreateEvent);
-	res.status(201).send(invite);
+	}).save();
+	const data = invite.toJSON();
+	data.inviter = await User.getPublicUser(req.user_id);
+	data.guild = permission.cache.guild;
+	data.channel = channel;
+
+	await emitEvent({ event: "INVITE_CREATE", data, guild_id } as InviteCreateEvent);
+	res.status(201).send(data);
 });
 
 router.get("/", async (req: Request, res: Response) => {