summary refs log tree commit diff
path: root/src/routes/channels/#channel_id/invites.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/routes/channels/#channel_id/invites.ts')
-rw-r--r--src/routes/channels/#channel_id/invites.ts35
1 files changed, 18 insertions, 17 deletions
diff --git a/src/routes/channels/#channel_id/invites.ts b/src/routes/channels/#channel_id/invites.ts

index adfb0688..da802800 100644 --- a/src/routes/channels/#channel_id/invites.ts +++ b/src/routes/channels/#channel_id/invites.ts
@@ -7,21 +7,21 @@ import { emitEvent } from "../../../util/Event"; import { InviteCreateSchema } from "../../../schema/Invite"; -import { getPermission, ChannelModel, InviteModel, InviteCreateEvent } from "fosscord-server-util"; +import { getPermission, ChannelModel, InviteModel, InviteCreateEvent, toObject } from "fosscord-server-util"; const router: Router = Router(); router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) => { - const usID = req.user_id; - const chID = req.params.channel_id; - const channel = await ChannelModel.findOne({ id: chID }).exec(); + const { user_id } = req; + const { channel_id } = req.params; + const channel = await ChannelModel.findOne({ id: channel_id }).exec(); if (!channel || !channel.guild_id) { throw new HTTPError("This channel doesn't exist", 404); } - const { guild_id: guID } = channel; + const { guild_id } = channel; - const permission = await getPermission(usID, guID); + const permission = await getPermission(user_id, guild_id); if (!permission.has("CREATE_INSTANT_INVITE")) { throw new HTTPError("You aren't authorised to access this endpoint", 401); @@ -34,34 +34,35 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) max_uses: req.body.max_uses, max_age: req.body.max_age, created_at: new Date(), - guild_id: guID, - channel_id: chID, - inviter_id: usID, + guild_id, + channel_id: channel_id, + inviter_id: user_id, }; await new InviteModel(invite).save(); - await emitEvent({ event: "INVITE_CREATE", data: invite } as InviteCreateEvent); + await emitEvent({ event: "INVITE_CREATE", data: invite, guild_id } as InviteCreateEvent); res.status(201).send(invite); }); router.get("/", async (req: Request, res: Response) => { - const usID = req.user_id; - const chID = req.params.channel_id; - const channel = await ChannelModel.findOne({ id: chID }).exec(); + const { user_id } = req; + const { channel_id } = req.params; + const channel = await ChannelModel.findOne({ id: channel_id }).exec(); if (!channel || !channel.guild_id) { throw new HTTPError("This channel doesn't exist", 404); } - const { guild_id: guID } = channel; - const permission = await getPermission(usID, guID); + const { guild_id } = channel; + const permission = await getPermission(user_id, guild_id); if (!permission.has("MANAGE_CHANNELS")) { throw new HTTPError("You aren't authorised to access this endpoint", 401); } - const invites = await InviteModel.find({ guild_id: guID }).lean().exec(); - res.status(200).send(invites); + const invites = await InviteModel.find({ guild_id }).exec(); + + res.status(200).send(toObject(invites)); }); export default router;