diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-04-27 07:04:06 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-04-27 07:04:06 +0200 |
commit | 5e9fb8a2a2b77a5476b999178a6804a0744695f6 (patch) | |
tree | 97e0740762c7b1d055ae72d0ebcd714a2814e1ef /src/routes | |
parent | :art: refactor to use easier permission api (diff) | |
download | server-5e9fb8a2a2b77a5476b999178a6804a0744695f6.tar.xz |
:sparkles: Channel get Route
Diffstat (limited to 'src/routes')
-rw-r--r-- | src/routes/channels/#channel_id/index.ts | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/src/routes/channels/#channel_id/index.ts b/src/routes/channels/#channel_id/index.ts index d1c64267..d66b7570 100644 --- a/src/routes/channels/#channel_id/index.ts +++ b/src/routes/channels/#channel_id/index.ts @@ -8,54 +8,53 @@ const router: Router = Router(); // TODO: delete channel // TODO: Get channel -router.delete("/", async (req, res) => { - const { channel_id } = req.params +router.get("/", async (req, res) => { + const { channel_id } = req.params; const channel = await ChannelModel.findOne({ id: channel_id }).exec(); if (!channel) throw new HTTPError("Channel not found", 404); - if (channel.guild_id) { - const permission = await getPermission(req.user_id, channel.guild_id) - permission.hasThrow("MANAGE_CHANNELS") - } + const permission = await getPermission(req.user_id, channel.guild_id, channel_id); + permission.hasThrow("VIEW_CHANNEL"); - // TODO Channel Update Gateway event will fire for each of them + return res.send(toObject(channel)); +}); - await ChannelModel.deleteOne({ id: channel_id }) +router.delete("/", async (req, res) => { + const { channel_id } = req.params; - // TODO: Dm channel "close" not delete - - await emitEvent({ event: "CHANNEL_DELETE", data: channel, guild_id: channel_id, channel_id} as ChannelDeleteEvent); + const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel }); + permission.hasThrow("MANAGE_CHANNELS"); + + await ChannelModel.deleteOne({ id: channel_id }); + // TODO: Dm channel "close" not delete const data = toObject(channel); - //TODO: Reload channel list if request successful - res.send(data) -}) -// should be good now + await emitEvent({ event: "CHANNEL_DELETE", data, guild_id: channel?.guild_id, channel_id } as ChannelDeleteEvent); + + res.send(data); +}); router.patch("/", check(ChannelModifySchema), async (req, res) => { - var payload = req.body as ChannelModifySchema //new data - const { channel_id } = req.params - var channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); - if (!channel) throw new HTTPError("Channel not found", 404); + var payload = req.body as ChannelModifySchema; + const { channel_id } = req.params; - const permission = await getPermission(req.user_id, channel.guild_id, channel_id) - permission.hasThrow("MANAGE_CHANNELS") - channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload).exec() - if (!channel) throw new HTTPError("Channel not found", 404); + const permission = await getPermission(req.user_id, undefined, channel_id); + permission.hasThrow("MANAGE_CHANNELS"); - //const data = toObject(channel); - //TODO: Reload channel list if request successful + const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload).exec(); + if (!channel) throw new HTTPError("Channel not found", 404); await emitEvent({ event: "CHANNEL_UPDATE", - data: channel, + data: toObject(channel), guild_id: channel.guild_id, - } as ChannelUpdateEvent) + channel_id, + } as ChannelUpdateEvent); res.send(toObject(channel)); -}) +}); export default router; - |