From 4c34378b9d9d93e17c654b47f4161b6a1a685f47 Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Tue, 5 May 2020 13:24:49 -0300 Subject: [PATCH] [FIX] Reactions may present empty names of who reacted when using Real Names (#17536) --- app/utils/server/lib/normalizeMessagesForUser.js | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/app/utils/server/lib/normalizeMessagesForUser.js b/app/utils/server/lib/normalizeMessagesForUser.js index 30cc42bfa188..bcd395fa640b 100644 --- a/app/utils/server/lib/normalizeMessagesForUser.js +++ b/app/utils/server/lib/normalizeMessagesForUser.js @@ -11,6 +11,10 @@ const filterStarred = (message, uid) => { // TODO: we should let clients get user names on demand instead of doing this +function getNameOfUsername(users, username) { + return users.get(username) || username; +} + export const normalizeMessagesForUser = (messages, uid) => { // if not using real names, there is nothing else to do if (!settings.get('UI_Use_Real_Name')) { @@ -33,7 +37,7 @@ export const normalizeMessagesForUser = (messages, uid) => { .forEach((reaction) => reaction.usernames.forEach((username) => usernames.add(username))); }); - const users = {}; + const names = new Map(); Users.findUsersByUsernames([...usernames.values()], { fields: { @@ -41,20 +45,19 @@ export const normalizeMessagesForUser = (messages, uid) => { name: 1, }, }).forEach((user) => { - users[user.username] = user.name; + names.set(user.username, user.name); }); messages.forEach((message) => { if (!message.u) { return; } - message.u.name = users[message.u.username]; + message.u.name = getNameOfUsername(names, message.u.username); - (message.mentions || []).forEach((mention) => { mention.name = users[mention.username]; }); + (message.mentions || []).forEach((mention) => { mention.name = getNameOfUsername(names, mention.username); }); Object.keys(message.reactions || {}).forEach((reaction) => { - const names = message.reactions[reaction].usernames.map((username) => users[username]); - message.reactions[reaction].names = names; + message.reactions[reaction].names = message.reactions[reaction].usernames.map((username) => getNameOfUsername(names, username)); }); });