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.
inputEl.attr('data-mid', messageId).addClass('editing');
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 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;
}

@ -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),

Loading…
Cancel
Save