From 2658bcc821c22e137a6eeb9bb74098856a642eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 12 Jan 2023 21:44:36 -0500 Subject: [PATCH] fix: chatwith message fix --- public/language/en-GB/modules.json | 2 ++ src/messaging/data.js | 2 ++ src/messaging/index.js | 29 ++++++++++++++++++++++++++++- src/messaging/rooms.js | 1 + 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/public/language/en-GB/modules.json b/public/language/en-GB/modules.json index 4e700b2c74..e2aec5b3d2 100644 --- a/public/language/en-GB/modules.json +++ b/public/language/en-GB/modules.json @@ -3,6 +3,8 @@ "chat.placeholder": "Type chat message here, drag & drop images, press enter to send", "chat.scroll-up-alert": "You are looking at older messages, click here to go to most recent message.", "chat.usernames-and-x-others": "%1 & %2 others", + "chat.chat-with-usernames": "Chat with %1", + "chat.chat-with-usernames-and-x-others": "Chat with %1 & %2 others", "chat.send": "Send", "chat.no_active": "You have no active chats.", "chat.user_typing": "%1 is typing ...", diff --git a/src/messaging/data.js b/src/messaging/data.js index 6839037678..eaa24a6a6f 100644 --- a/src/messaging/data.js +++ b/src/messaging/data.js @@ -98,6 +98,8 @@ module.exports = function (Messaging) { } else if (index > 0 && message.fromuid !== messages[index - 1].fromuid) { // If the previous message was from the other person, this is also a new set message.newSet = true; + } else if (index > 0 && messages[index - 1].system) { + message.newSet = true; } else if (index === 0) { message.newSet = true; } diff --git a/src/messaging/index.js b/src/messaging/index.js index 0b7d04eae5..bc7d4ee4db 100644 --- a/src/messaging/index.js +++ b/src/messaging/index.js @@ -2,7 +2,7 @@ const validator = require('validator'); - +const nconf = require('nconf'); const db = require('../database'); const user = require('../user'); const privileges = require('../privileges'); @@ -11,6 +11,8 @@ const meta = require('../meta'); const utils = require('../utils'); const translator = require('../translator'); +const relative_path = nconf.get('relative_path'); + const Messaging = module.exports; require('./data')(Messaging); @@ -127,6 +129,9 @@ Messaging.getRecentChats = async (callerUid, uid, start, stop) => { room.usernames = Messaging.generateUsernames(room.users, uid); } }); + await Promise.all(results.roomData.map(async (room) => { + room.chatWithMessage = await Messaging.generateChatWithMessage(room.users, uid); + })); results.roomData = results.roomData.filter(Boolean); const ref = { rooms: results.roomData, nextStart: stop + 1 }; @@ -151,6 +156,28 @@ Messaging.generateUsernames = function (users, excludeUid) { return usernames.join(', '); }; +Messaging.generateChatWithMessage = async function (users, excludeUid) { + users = users.filter(u => u && parseInt(u.uid, 10) !== excludeUid); + const usernames = users.map(u => `${u.username}`); + let compiled = ''; + if (!users.length) { + return '[[modules:chat.no-users-in-room]]'; + } + if (users.length > 3) { + compiled = translator.compile( + 'modules:chat.chat-with-usernames-and-x-others', + usernames.slice(0, 2).join(', '), + usernames.length - 2 + ); + } else { + compiled = translator.compile( + 'modules:chat.chat-with-usernames', + usernames.join(', '), + ); + } + return utils.decodeHTMLEntities(await translator.translate(compiled)); +}; + Messaging.getTeaser = async (uid, roomId) => { const mid = await Messaging.getLatestUndeletedMessage(uid, roomId); if (!mid) { diff --git a/src/messaging/rooms.js b/src/messaging/rooms.js index 948fd88027..4c1d35a1bf 100644 --- a/src/messaging/rooms.js +++ b/src/messaging/rooms.js @@ -249,6 +249,7 @@ module.exports = function (Messaging) { room.canReply = canReply; room.groupChat = room.hasOwnProperty('groupChat') ? room.groupChat : users.length > 2; room.usernames = Messaging.generateUsernames(users, uid); + room.chatWithMessage = await Messaging.generateChatWithMessage(users, uid); room.maximumUsersInChatRoom = meta.config.maximumUsersInChatRoom; room.maximumChatMessageLength = meta.config.maximumChatMessageLength; room.showUserInput = !room.maximumUsersInChatRoom || room.maximumUsersInChatRoom > 2;