Skip to content

Commit

Permalink
[FIX] Reactions may present empty names of who reacted when using Rea…
Browse files Browse the repository at this point in the history
…l Names (#17536)
  • Loading branch information
rodrigok authored and sampaiodiego committed May 11, 2020
1 parent 485be3b commit 4c34378
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions app/utils/server/lib/normalizeMessagesForUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -33,28 +37,27 @@ 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: {
username: 1,
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));
});
});

Expand Down

0 comments on commit 4c34378

Please sign in to comment.