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

index fb6bcb1a..4aa5a5b9 100644 --- a/api/src/routes/channels/#channel_id/index.ts +++ b/api/src/routes/channels/#channel_id/index.ts
@@ -1,4 +1,4 @@ -import { ChannelDeleteEvent, ChannelModel, ChannelUpdateEvent, emitEvent, getPermission, GuildUpdateEvent, toObject } from "@fosscord/util"; +import { ChannelDeleteEvent, Channel, ChannelUpdateEvent, emitEvent, getPermission } from "@fosscord/util"; import { Router, Response, Request } from "express"; import { HTTPError } from "lambert-server"; import { ChannelModifySchema } from "../../../schema/Channel"; @@ -10,28 +10,28 @@ const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); permission.hasThrow("VIEW_CHANNEL"); - return res.send(toObject(channel)); + return res.send(channel); }); router.delete("/", async (req: Request, res: Response) => { const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); - const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel }); + const permission = await getPermission(req.user_id, channel?.guild_id, channel_id); permission.hasThrow("MANAGE_CHANNELS"); // TODO: Dm channel "close" not delete - const data = toObject(channel); + const data = channel; await emitEvent({ event: "CHANNEL_DELETE", data, channel_id } as ChannelDeleteEvent); - await ChannelModel.deleteOne({ id: channel_id }); + await Channel.delete({ id: channel_id }); res.send(data); }); @@ -43,17 +43,19 @@ router.patch("/", check(ChannelModifySchema), async (req: Request, res: Response const permission = await getPermission(req.user_id, undefined, channel_id); permission.hasThrow("MANAGE_CHANNELS"); - const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload, { new: true }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); + channel.assign(payload); - const data = toObject(channel); + await Promise.all([ + channel.save(), + emitEvent({ + event: "CHANNEL_UPDATE", + data: channel, + channel_id + } as ChannelUpdateEvent) + ]); - await emitEvent({ - event: "CHANNEL_UPDATE", - data, - channel_id - } as ChannelUpdateEvent); - - res.send(data); + res.send(channel); }); export default router;