diff options
Diffstat (limited to 'api/src/routes/invites/index.ts')
-rw-r--r-- | api/src/routes/invites/index.ts | 19 |
1 files changed, 7 insertions, 12 deletions
diff --git a/api/src/routes/invites/index.ts b/api/src/routes/invites/index.ts index a488c44b..23fa3aec 100644 --- a/api/src/routes/invites/index.ts +++ b/api/src/routes/invites/index.ts @@ -1,14 +1,12 @@ import { Router, Request, Response } from "express"; -import { getPermission, Guild, Invite } from "@fosscord/util"; +import { getPermission, Guild, Invite, Member } from "@fosscord/util"; import { HTTPError } from "lambert-server"; -import { addMember } from "../../util/Member"; const router: Router = Router(); router.get("/:code", async (req: Request, res: Response) => { const { code } = req.params; const invite = await Invite.findOneOrFail({ code }); - if (!invite) throw new HTTPError("Unknown Invite", 404); res.status(200).send(invite); }); @@ -16,11 +14,11 @@ router.get("/:code", async (req: Request, res: Response) => { router.post("/:code", async (req: Request, res: Response) => { const { code } = req.params; - const invite = await Invite.findOneOrFailAndUpdate({ code }, { $inc: { uses: 1 } }, { new: true }); - if (!invite) throw new HTTPError("Unknown Invite", 404); - if (invite.uses >= invite.max_uses) await Invite.deleteOne({ code }); + const invite = await Invite.findOneOrFail({ code }); + if (invite.uses++ >= invite.max_uses) await Invite.delete({ code }); + else await invite.save(); - await addMember(req.user_id, invite.guild_id); + await Member.addToGuild(req.user_id, invite.guild_id); res.status(200).send(invite); }); @@ -30,15 +28,12 @@ router.delete("/:code", async (req: Request, res: Response) => { const invite = await Invite.findOneOrFail({ code }); const { guild_id, channel_id } = invite; - const guild = await Guild.findOneOrFail({ id: guild_id }); - const permission = await getPermission(req.user_id, guild_id, channel_id, { guild }); + const permission = await getPermission(req.user_id, guild_id, channel_id); if (!permission.has("MANAGE_GUILD") && !permission.has("MANAGE_CHANNELS")) throw new HTTPError("You missing the MANAGE_GUILD or MANAGE_CHANNELS permission", 401); - await Invite.deleteOne({ code }); - - await Guild.update({ vanity_url_code: code }, { $unset: { vanity_url_code: 1 } }).catch((e) => {}); + await Promise.all([Invite.delete({ code }), Guild.update({ vanity_url_code: code }, { vanity_url_code: undefined })]); res.json({ invite: invite }); }); |