summary refs log tree commit diff
diff options
context:
space:
mode:
authorxnacly <matteogropp@gmail.com>2021-02-21 19:22:32 +0100
committerxnacly <matteogropp@gmail.com>2021-02-21 19:22:32 +0100
commita7132e2ab8024435400f136ace2f269df408f978 (patch)
treeb739b9be07023a2663f656a28b6e678f708a3baa
parentinvite code gen (diff)
downloadserver-a7132e2ab8024435400f136ace2f269df408f978.tar.xz
channel/:id/invites create #79
-rw-r--r--src/routes/api/v8/channels/#CHANNELID/invites.ts46
1 files changed, 45 insertions, 1 deletions
diff --git a/src/routes/api/v8/channels/#CHANNELID/invites.ts b/src/routes/api/v8/channels/#CHANNELID/invites.ts

index 9a4e81fa..ae9940d0 100644 --- a/src/routes/api/v8/channels/#CHANNELID/invites.ts +++ b/src/routes/api/v8/channels/#CHANNELID/invites.ts
@@ -1,4 +1,48 @@ -import { Router } from "express"; +import { Router, Request, Response } from "express"; +import { HTTPError } from "lambert-server"; + +import { check } from "./../../../../../util/instanceOf"; +import { random } from "./../../../../../util/RandomInviteID"; +import { emitEvent } from "../../../../../util/Event"; + +import { InviteCreateSchema } from "./../../../../../schema/Invite"; + +import { getPermission, ChannelModel, InviteModel, InviteCreateEvent } from "fosscord-server-util"; + const router: Router = Router(); +router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) => { + const usID = req.userid; + const chID = BigInt(req.params.CHANNELID); + const channel = await ChannelModel.findOne({ id: chID }).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); + + if (!permission.has("CREATE_INSTANT_INVITE")) { + throw new HTTPError("You aren't authorised to access this endpoint", 401); + } + + const invite = { + code: random(), + temporary: req.body.temporary, + uses: 0, + max_uses: req.body.max_uses, + max_age: req.body.max_age, + created_at: Date.now(), + guild_id: guID, + channel_id: chID, + inviter_id: usID, + }; + + await new InviteModel(invite).save(); // ! samuel ist ein hurensohn + + await emitEvent({ event: "INVITE_CREATE", data: invite } as InviteCreateEvent); + res.status(201).send(invite); +}); + export default router;