From 4f51838df8cf3d49d89c6d1cc8f1a4722d81e8bc Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 7 Jul 2020 17:31:05 -0400 Subject: [PATCH] feat: fire new hooks on chat message editing --- public/src/client/chats/messages.js | 6 ++++++ src/messaging/data.js | 14 +++++++++++--- src/messaging/edit.js | 12 ++++++++---- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/public/src/client/chats/messages.js b/public/src/client/chats/messages.js index 8e280e830c..32eb1c262d 100644 --- a/public/src/client/chats/messages.js +++ b/public/src/client/chats/messages.js @@ -130,6 +130,12 @@ define('forum/chats/messages', ['components', 'sounds', 'translator', 'benchpres // message, instead of posting a new one. inputEl.attr('data-mid', messageId).addClass('editing'); inputEl.val(raw).focus(); + + $(window).trigger('action:chat.prepEdit', { + inputEl: inputEl, + messageId: messageId, + roomId: roomId, + }); } }); }; diff --git a/src/messaging/data.js b/src/messaging/data.js index b20e514de4..7a99d648c3 100644 --- a/src/messaging/data.js +++ b/src/messaging/data.js @@ -19,8 +19,8 @@ module.exports = function (Messaging) { const keys = mids.map(mid => 'message:' + mid); const messages = await (fields.length ? db.getObjectsFields(keys, fields) : db.getObjects(keys)); - messages.forEach(message => modifyMessage(message, fields)); - return messages; + + return await Promise.all(messages.map(async (message, idx) => modifyMessage(message, fields, parseInt(mids[idx], 10)))); }; Messaging.getMessageField = async (mid, field) => { @@ -133,7 +133,7 @@ module.exports = function (Messaging) { }; }; -function modifyMessage(message, fields) { +async function modifyMessage(message, fields, mid) { if (message) { db.parseIntFields(message, intFields, fields); if (message.hasOwnProperty('timestamp')) { @@ -143,4 +143,12 @@ function modifyMessage(message, fields) { message.editedISO = utils.toISOString(message.edited); } } + + const payload = await plugins.fireHook('filter:messaging.getFields', { + mid: mid, + message: message, + fields: fields, + }); + + return payload.message; } diff --git a/src/messaging/edit.js b/src/messaging/edit.js index 22a82a0d50..303ff5a2e1 100644 --- a/src/messaging/edit.js +++ b/src/messaging/edit.js @@ -2,6 +2,7 @@ const meta = require('../meta'); const user = require('../user'); +const plugins = require('../plugins'); const sockets = require('../socket.io'); @@ -13,14 +14,17 @@ module.exports = function (Messaging) { if (raw === content) { return; } - if (!String(content).trim()) { - throw new Error('[[error:invalid-chat-message]]'); - } - await Messaging.setMessageFields(mid, { + + const payload = await plugins.fireHook('filter:messaging.edit', { content: content, edited: Date.now(), }); + if (!String(payload.content).trim()) { + throw new Error('[[error:invalid-chat-message]]'); + } + await Messaging.setMessageFields(mid, payload); + // Propagate this change to users in the room const [uids, messages] = await Promise.all([ Messaging.getUidsInRoom(roomId, 0, -1),