summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-22 21:13:14 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-22 21:13:14 +0200
commitb5f8a2e4e1ee52025886d8404a8a277737e634c0 (patch)
treea776f772214c90ecb7d98fc1b2a592a0bed7e72d /src
parentMerge pull request #144 from afeuerstein/master (diff)
downloadserver-b5f8a2e4e1ee52025886d8404a8a277737e634c0.tar.xz
:sparkles: reactions bulk remove
Diffstat (limited to 'src')
-rw-r--r--src/routes/channels/#channel_id/messages/#message_id/reactions.ts62
1 files changed, 62 insertions, 0 deletions
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 be635197..f61977f4 100644 --- a/src/routes/channels/#channel_id/messages/#message_id/reactions.ts +++ b/src/routes/channels/#channel_id/messages/#message_id/reactions.ts
@@ -5,6 +5,8 @@ import { MemberModel, MessageModel, MessageReactionAddEvent, + MessageReactionRemoveAllEvent, + MessageReactionRemoveEmojiEvent, MessageReactionRemoveEvent, PartialEmoji, PublicUserProjection, @@ -33,6 +35,66 @@ function getEmoji(emoji: string): PartialEmoji { }; } +router.delete("/", async (req, res) => { + const { message_id, channel_id } = req.params; + + const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); + if (!channel) throw new HTTPError("Channel not found", 404); + + const permissions = await getPermission(req.user_id, undefined, channel_id); + permissions.hasThrow("MANAGE_MESSAGES"); + + const message = await MessageModel.findOneAndUpdate({ id: message_id, channel_id }, { reactions: [] }).exec(); + if (!message) throw new HTTPError("Message not found", 404); + + await emitEvent({ + event: "MESSAGE_REACTION_REMOVE_ALL", + channel_id, + guild_id: channel.guild_id, + data: { + channel_id, + message_id, + guild_id: channel.guild_id + } + } as MessageReactionRemoveAllEvent); + + res.sendStatus(204); +}); + +router.delete("/:emoji", async (req, res) => { + const { message_id, channel_id } = req.params; + const emoji = getEmoji(req.params.emoji); + + const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); + if (!channel) throw new HTTPError("Channel not found", 404); + + const permissions = await getPermission(req.user_id, undefined, channel_id); + permissions.hasThrow("MANAGE_MESSAGES"); + + const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); + if (!message) throw new HTTPError("Message not found", 404); + + const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name); + if (!already_added) throw new HTTPError("Reaction not found", 404); + message.reactions.remove(already_added); + + await MessageModel.updateOne({ id: message_id, channel_id }, message).exec(); + + await emitEvent({ + event: "MESSAGE_REACTION_REMOVE_EMOJI", + channel_id, + guild_id: channel.guild_id, + data: { + channel_id, + message_id, + guild_id: channel.guild_id, + emoji + } + } as MessageReactionRemoveEmojiEvent); + + res.sendStatus(204); +}); + router.get("/:emoji", async (req, res) => { const { message_id, channel_id } = req.params; const emoji = getEmoji(req.params.emoji);