diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-05-22 22:06:18 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-05-22 22:06:18 +0200 |
commit | 71c0050a54b11fec132a5a015da14922eed04cfe (patch) | |
tree | 0a0a768f2d9b1000ba06b914950950b45f4ddedc /src/routes | |
parent | :sparkles: reactions bulk remove (diff) | |
download | server-71c0050a54b11fec132a5a015da14922eed04cfe.tar.xz |
:sparkles: Message edit
Diffstat (limited to 'src/routes')
-rw-r--r-- | src/routes/channels/#channel_id/messages/#message_id/index.ts | 40 | ||||
-rw-r--r-- | src/routes/channels/#channel_id/messages/#message_id/reactions.ts | 2 |
2 files changed, 39 insertions, 3 deletions
diff --git a/src/routes/channels/#channel_id/messages/#message_id/index.ts b/src/routes/channels/#channel_id/messages/#message_id/index.ts index 88b8d89b..5a61b4ad 100644 --- a/src/routes/channels/#channel_id/messages/#message_id/index.ts +++ b/src/routes/channels/#channel_id/messages/#message_id/index.ts @@ -1,11 +1,47 @@ -import { ChannelModel, getPermission, MessageDeleteEvent, MessageModel } from "@fosscord/server-util"; +import { ChannelModel, getPermission, MessageDeleteEvent, MessageModel, MessageUpdateEvent, toObject } from "@fosscord/server-util"; import { Router } from "express"; import { HTTPError } from "lambert-server"; +import { MessageCreateSchema } from "../../../../../schema/Message"; import { emitEvent } from "../../../../../util/Event"; import { check } from "../../../../../util/instanceOf"; +import { handleMessage } from "../../../../../util/Message"; const router = Router(); -// TODO: + +router.patch("/", check(MessageCreateSchema), async (req, res) => { + const { message_id, channel_id } = req.params; + var body = req.body as MessageCreateSchema; + + var message = await MessageModel.findOne({ id: message_id, channel_id }, { author_id: true }).exec(); + if (!message) throw new HTTPError("Message not found", 404); + + const permissions = await getPermission(req.user_id, undefined, channel_id); + + if (req.user_id !== message.author_id) { + permissions.hasThrow("MANAGE_MESSAGES"); + body = { flags: body.flags }; + } + + const opts = await handleMessage({ + ...body, + author_id: message.author_id, + channel_id, + id: message_id, + edited_timestamp: new Date() + }); + + message = await MessageModel.findOneAndUpdate({ id: message_id }, opts).populate("author").exec(); + if (!message) throw new HTTPError("Message not found", 404); + + await emitEvent({ + event: "MESSAGE_UPDATE", + channel_id, + guild_id: message.guild_id, + data: { ...toObject(message), nonce: undefined } + } as MessageUpdateEvent); + + return res.json(toObject(message)); +}); router.delete("/", async (req, res) => { const { message_id, channel_id } = req.params; diff --git a/src/routes/channels/#channel_id/messages/#message_id/reactions.ts b/src/routes/channels/#channel_id/messages/#message_id/reactions.ts index f61977f4..1bfaae39 100644 --- a/src/routes/channels/#channel_id/messages/#message_id/reactions.ts +++ b/src/routes/channels/#channel_id/messages/#message_id/reactions.ts @@ -13,7 +13,7 @@ import { toObject, UserModel } from "@fosscord/server-util"; -import { Request, Response, Router } from "express"; +import { Router } from "express"; import { HTTPError } from "lambert-server"; import { emitEvent } from "../../../../../util/Event"; |