From 5b0f9d31b7ad1d4d78cb0120dedde8d248185e88 Mon Sep 17 00:00:00 2001 From: Dragory <2606411+Dragory@users.noreply.github.com> Date: Mon, 12 Oct 2020 20:54:42 +0300 Subject: [PATCH] Create new message types for staff reply edits/deletions This has backwards-compatibility-breaking changes to the formatters of staff reply edits/deletions, which now only receive the thread message for the edit/deletion with the original data in the thread message's metadata. --- src/data/Thread.js | 31 +++++++++++++++++++++++++++---- src/data/constants.js | 4 +++- src/formatters.js | 40 +++++++++++++++++++++++++--------------- 3 files changed, 55 insertions(+), 20 deletions(-) diff --git a/src/data/Thread.js b/src/data/Thread.js index a6f0de5..a84734e 100644 --- a/src/data/Thread.js +++ b/src/data/Thread.js @@ -745,8 +745,20 @@ class Thread { await bot.editMessage(this.channel_id, threadMessage.inbox_message_id, formattedThreadMessage); if (! opts.quiet) { - const threadNotification = formatters.formatStaffReplyEditNotificationThreadMessage(threadMessage, newText, moderator); - await this.postSystemMessage(threadNotification); + const editThreadMessage = new ThreadMessage({ + message_type: THREAD_MESSAGE_TYPE.REPLY_EDITED, + user_id: null, + user_name: "", + body: "", + is_anonymous: 0, + }); + editThreadMessage.setMetadataValue("originalThreadMessage", threadMessage); + editThreadMessage.setMetadataValue("newBody", newText); + + const threadNotification = formatters.formatStaffReplyEditNotificationThreadMessage(editThreadMessage); + const inboxMessage = await this._postToThreadChannel(threadNotification); + editThreadMessage.inbox_message_id = inboxMessage.id; + await this._addThreadMessageToDB(editThreadMessage.getSQLProps()); } await this._updateThreadMessage(threadMessage.id, { body: newText }); @@ -764,8 +776,19 @@ class Thread { await bot.deleteMessage(this.channel_id, threadMessage.inbox_message_id); if (! opts.quiet) { - const threadNotification = formatters.formatStaffReplyDeletionNotificationThreadMessage(threadMessage, moderator); - await this.postSystemMessage(threadNotification); + const deletionThreadMessage = new ThreadMessage({ + message_type: THREAD_MESSAGE_TYPE.REPLY_DELETED, + user_id: null, + user_name: "", + body: "", + is_anonymous: 0, + }); + deletionThreadMessage.setMetadataValue("originalThreadMessage", threadMessage); + + const threadNotification = formatters.formatStaffReplyDeletionNotificationThreadMessage(deletionThreadMessage); + const inboxMessage = await this._postToThreadChannel(threadNotification); + deletionThreadMessage.inbox_message_id = inboxMessage.id; + await this._addThreadMessageToDB(deletionThreadMessage.getSQLProps()); } await this._deleteThreadMessage(threadMessage.id); diff --git a/src/data/constants.js b/src/data/constants.js index 221becd..992d0a1 100644 --- a/src/data/constants.js +++ b/src/data/constants.js @@ -12,7 +12,9 @@ module.exports = { TO_USER: 4, LEGACY: 5, COMMAND: 6, - SYSTEM_TO_USER: 7 + SYSTEM_TO_USER: 7, + REPLY_EDITED: 8, + REPLY_DELETED: 9, }, // https://discord.com/developers/docs/resources/channel#channel-object-channel-types diff --git a/src/formatters.js b/src/formatters.js index 4216866..a173ac8 100644 --- a/src/formatters.js +++ b/src/formatters.js @@ -30,8 +30,6 @@ const moment = require("moment"); * Function to format the inbox channel notification for a staff reply edit * @callback FormatStaffReplyEditNotificationThreadMessage * @param {ThreadMessage} threadMessage - * @param {string} newText - * @param {Eris.Member} moderator Moderator that edited the message * @return {Eris.MessageContent} Message content to post in the thread channel */ @@ -39,7 +37,6 @@ const moment = require("moment"); * Function to format the inbox channel notification for a staff reply deletion * @callback FormatStaffReplyDeletionNotificationThreadMessage * @param {ThreadMessage} threadMessage - * @param {Eris.Member} moderator Moderator that deleted the message * @return {Eris.MessageContent} Message content to post in the thread channel */ @@ -142,31 +139,35 @@ const defaultFormatters = { return result; }, - formatStaffReplyEditNotificationThreadMessage(threadMessage, newText, moderator) { - let content = `**${moderator.user.username}#${moderator.user.discriminator}** (\`${moderator.id}\`) edited reply \`${threadMessage.message_number}\``; + formatStaffReplyEditNotificationThreadMessage(threadMessage) { + const originalThreadMessage = threadMessage.getMetadataValue("originalThreadMessage"); + const newBody = threadMessage.getMetadataValue("newBody"); - if (threadMessage.body.length < 200 && newText.length < 200) { + let content = `**${originalThreadMessage.user_name}** (\`${originalThreadMessage.user_id}\`) edited reply \`${originalThreadMessage.message_number}\``; + + if (originalThreadMessage.body.length < 200 && newBody.length < 200) { // Show edits of small messages inline - content += ` from \`${utils.disableInlineCode(threadMessage.body)}\` to \`${newText}\``; + content += ` from \`${utils.disableInlineCode(originalThreadMessage.body)}\` to \`${newBody}\``; } else { // Show edits of long messages in two code blocks content += ":"; - content += `\n\nBefore:\n\`\`\`${utils.disableCodeBlocks(threadMessage.body)}\`\`\``; - content += `\nAfter:\n\`\`\`${utils.disableCodeBlocks(newText)}\`\`\``; + content += `\n\nBefore:\n\`\`\`${utils.disableCodeBlocks(originalThreadMessage.body)}\`\`\``; + content += `\nAfter:\n\`\`\`${utils.disableCodeBlocks(newBody)}\`\`\``; } return content; }, - formatStaffReplyDeletionNotificationThreadMessage(threadMessage, moderator) { - let content = `**${moderator.user.username}#${moderator.user.discriminator}** (\`${moderator.id}\`) deleted reply \`${threadMessage.message_number}\``; + formatStaffReplyDeletionNotificationThreadMessage(threadMessage) { + const originalThreadMessage = threadMessage.getMetadataValue("originalThreadMessage"); + let content = `**${originalThreadMessage.user_name}** (\`${originalThreadMessage.user_id}\`) deleted reply \`${originalThreadMessage.message_number}\``; - if (threadMessage.body.length < 200) { + if (originalThreadMessage.body.length < 200) { // Show the original content of deleted small messages inline - content += ` (message content: \`${utils.disableInlineCode(threadMessage.body)}\`)`; + content += ` (message content: \`${utils.disableInlineCode(originalThreadMessage.body)}\`)`; } else { // Show the original content of deleted large messages in a code block - content += ":\n```" + utils.disableCodeBlocks(threadMessage.body) + "```"; + content += ":\n```" + utils.disableCodeBlocks(originalThreadMessage.body) + "```"; } return content; @@ -183,7 +184,7 @@ const defaultFormatters = { }, formatSystemToUserThreadMessage(threadMessage) { - let result = `**[BOT TO USER]** ${threadMessage.body}`; + let result = `**[SYSTEM TO USER]** ${threadMessage.body}`; for (const link of threadMessage.attachments) { result += `\n\n${link}`; @@ -265,6 +266,15 @@ const defaultFormatters = { line += ` [CHAT] [${message.user_name}] ${message.body}`; } else if (message.message_type === THREAD_MESSAGE_TYPE.COMMAND) { line += ` [COMMAND] [${message.user_name}] ${message.body}`; + } else if (message.message_type === THREAD_MESSAGE_TYPE.REPLY_EDITED) { + const originalThreadMessage = message.getMetadataValue("originalThreadMessage"); + line += ` [REPLY EDITED] ${originalThreadMessage.user_name} edited reply ${originalThreadMessage.message_number}:`; + line += `\n\nBefore:\n${originalThreadMessage.body}`; + line += `\n\nAfter:\n${message.getMetadataValue("newBody")}`; + } else if (message.message_type === THREAD_MESSAGE_TYPE.REPLY_DELETED) { + const originalThreadMessage = message.getMetadataValue("originalThreadMessage"); + line += ` [REPLY DELETED] ${originalThreadMessage.user_name} deleted reply ${originalThreadMessage.message_number}:`; + line += `\n\n${originalThreadMessage.body}`; } else { line += ` [${message.user_name}] ${message.body}`; }