feat: fire new hooks on chat message editing

v1.18.x
Julian Lam 5 years ago
parent 7b04d8970d
commit 4f51838df8

@ -130,6 +130,12 @@ define('forum/chats/messages', ['components', 'sounds', 'translator', 'benchpres
// message, instead of posting a new one. // message, instead of posting a new one.
inputEl.attr('data-mid', messageId).addClass('editing'); inputEl.attr('data-mid', messageId).addClass('editing');
inputEl.val(raw).focus(); inputEl.val(raw).focus();
$(window).trigger('action:chat.prepEdit', {
inputEl: inputEl,
messageId: messageId,
roomId: roomId,
});
} }
}); });
}; };

@ -19,8 +19,8 @@ module.exports = function (Messaging) {
const keys = mids.map(mid => 'message:' + mid); const keys = mids.map(mid => 'message:' + mid);
const messages = await (fields.length ? db.getObjectsFields(keys, fields) : db.getObjects(keys)); 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) => { 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) { if (message) {
db.parseIntFields(message, intFields, fields); db.parseIntFields(message, intFields, fields);
if (message.hasOwnProperty('timestamp')) { if (message.hasOwnProperty('timestamp')) {
@ -143,4 +143,12 @@ function modifyMessage(message, fields) {
message.editedISO = utils.toISOString(message.edited); message.editedISO = utils.toISOString(message.edited);
} }
} }
const payload = await plugins.fireHook('filter:messaging.getFields', {
mid: mid,
message: message,
fields: fields,
});
return payload.message;
} }

@ -2,6 +2,7 @@
const meta = require('../meta'); const meta = require('../meta');
const user = require('../user'); const user = require('../user');
const plugins = require('../plugins');
const sockets = require('../socket.io'); const sockets = require('../socket.io');
@ -13,14 +14,17 @@ module.exports = function (Messaging) {
if (raw === content) { if (raw === content) {
return; return;
} }
if (!String(content).trim()) {
throw new Error('[[error:invalid-chat-message]]'); const payload = await plugins.fireHook('filter:messaging.edit', {
}
await Messaging.setMessageFields(mid, {
content: content, content: content,
edited: Date.now(), 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 // Propagate this change to users in the room
const [uids, messages] = await Promise.all([ const [uids, messages] = await Promise.all([
Messaging.getUidsInRoom(roomId, 0, -1), Messaging.getUidsInRoom(roomId, 0, -1),

Loading…
Cancel
Save