mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-12-22 08:20:36 +01:00
feat: add toMid to chat messages
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
const validator = require('validator');
|
||||
|
||||
const db = require('../database');
|
||||
@@ -73,16 +74,7 @@ module.exports = function (Messaging) {
|
||||
message.roomId = String(message.roomId || roomId);
|
||||
});
|
||||
|
||||
messages = await Promise.all(messages.map(async (message) => {
|
||||
if (message.system) {
|
||||
message.content = validator.escape(String(message.content));
|
||||
return message;
|
||||
}
|
||||
|
||||
const result = await Messaging.parse(message.content, message.fromuid, uid, roomId, isNew);
|
||||
message.content = result;
|
||||
return message;
|
||||
}));
|
||||
await parseMessages(messages, uid, roomId, isNew);
|
||||
|
||||
if (messages.length > 1) {
|
||||
// Add a spacer in between messages with time gaps between them
|
||||
@@ -96,7 +88,7 @@ module.exports = function (Messaging) {
|
||||
message.newSet = true;
|
||||
} else if (index > 0 && messages[index - 1].system) {
|
||||
message.newSet = true;
|
||||
} else if (index === 0) {
|
||||
} else if (index === 0 || message.toMid) {
|
||||
message.newSet = true;
|
||||
}
|
||||
|
||||
@@ -111,7 +103,7 @@ module.exports = function (Messaging) {
|
||||
const fields = await Messaging.getMessageFields(mid, ['fromuid', 'timestamp']);
|
||||
if ((messages[0].timestamp > fields.timestamp + Messaging.newMessageCutoff) ||
|
||||
(messages[0].fromuid !== fields.fromuid) ||
|
||||
messages[0].system) {
|
||||
messages[0].system || messages[0].toMid) {
|
||||
// If it's been 5 minutes, this is a new set of messages
|
||||
messages[0].newSet = true;
|
||||
}
|
||||
@@ -120,6 +112,8 @@ module.exports = function (Messaging) {
|
||||
}
|
||||
}
|
||||
|
||||
await addParentMessages(messages, uid, roomId);
|
||||
|
||||
const data = await plugins.hooks.fire('filter:messaging.getMessages', {
|
||||
messages: messages,
|
||||
uid: uid,
|
||||
@@ -130,6 +124,60 @@ module.exports = function (Messaging) {
|
||||
|
||||
return data && data.messages;
|
||||
};
|
||||
|
||||
async function addParentMessages(messages, uid, roomId) {
|
||||
let parentMids = messages.map(msg => (msg && msg.hasOwnProperty('toMid') ? parseInt(msg.toMid, 10) : null)).filter(Boolean);
|
||||
|
||||
if (!parentMids.length) {
|
||||
return;
|
||||
}
|
||||
parentMids = _.uniq(parentMids);
|
||||
const parentMessages = await Messaging.getMessagesFields(parentMids, [
|
||||
'fromuid', 'content', 'timestamp',
|
||||
]);
|
||||
const parentUids = _.uniq(parentMessages.map(msg => msg && msg.fromuid));
|
||||
const usersMap = _.zipObject(
|
||||
parentUids,
|
||||
await user.getUsersFields(parentUids, ['uid', 'username', 'userslug', 'picture'])
|
||||
);
|
||||
|
||||
await Promise.all(parentMessages.map(async (parentMsg) => {
|
||||
const foundMsg = messages.find(msg => parseInt(msg.mid, 10) === parseInt(parentMsg.mid, 10));
|
||||
if (foundMsg) {
|
||||
parentMsg.content = foundMsg.content;
|
||||
return;
|
||||
}
|
||||
parentMsg.content = await parseMessage(parentMsg, uid, roomId, false);
|
||||
}));
|
||||
|
||||
const parents = {};
|
||||
parentMessages.forEach((msg, i) => {
|
||||
if (usersMap[msg.fromuid]) {
|
||||
msg.user = usersMap[msg.fromuid];
|
||||
parents[parentMids[i]] = msg;
|
||||
}
|
||||
});
|
||||
|
||||
messages.forEach((msg) => {
|
||||
if (parents[msg.toMid]) {
|
||||
msg.parent = parents[msg.toMid];
|
||||
msg.parent.mid = msg.toMid;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function parseMessages(messages, uid, roomId, isNew) {
|
||||
await Promise.all(messages.map(async (message) => {
|
||||
message.content = await parseMessage(message, uid, roomId, isNew);
|
||||
}));
|
||||
}
|
||||
async function parseMessage(message, uid, roomId, isNew) {
|
||||
if (message.system) {
|
||||
return validator.escape(String(message.content));
|
||||
}
|
||||
|
||||
return await Messaging.parse(message.content, message.fromuid, uid, roomId, isNew);
|
||||
}
|
||||
};
|
||||
|
||||
async function modifyMessage(message, fields, mid) {
|
||||
|
||||
Reference in New Issue
Block a user