diff options
-rw-r--r-- | api/src/routes/channels/#channel_id/invites.ts | 38 |
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) => { |