summary refs log tree commit diff
path: root/api
diff options
context:
space:
mode:
authorxnacly <matteogropp@protonmail.com>2021-09-03 20:00:21 +0200
committerxnacly <matteogropp@protonmail.com>2021-09-03 20:00:21 +0200
commit77a87acfe6b06952b199b9760b6c688c1e0ce963 (patch)
tree105598c663d607291ea88b2c68b332b263168e8a /api
parentfixed 'undefined not found' + error not showing in client (diff)
parentMerge branch 'master' of https://github.com/fosscord/fosscord-api (diff)
downloadserver-77a87acfe6b06952b199b9760b6c688c1e0ce963.tar.xz
Merge branch 'master' of https://github.com/fosscord/fosscord-server
Diffstat (limited to 'api')
-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) => {