diff --git a/client/notifications/UsersNameChanged.js b/client/notifications/UsersNameChanged.js
index 77d153ba708d..68f23ea3e133 100644
--- a/client/notifications/UsersNameChanged.js
+++ b/client/notifications/UsersNameChanged.js
@@ -10,6 +10,18 @@ Meteor.startup(function() {
multi: true
});
+ RocketChat.models.Messages.update({
+ mentions: {
+ $elemMatch: { _id }
+ }
+ }, {
+ $set: {
+ 'mentions.$.name': name
+ }
+ }, {
+ multi: true
+ });
+
RocketChat.models.Subscriptions.update({
name: username,
t: 'd'
diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js
index b4802e357f85..7beab8fdfcfa 100644
--- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js
+++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js
@@ -1,6 +1,81 @@
/* globals Push */
import moment from 'moment';
+/**
+ * Replaces @username with full name
+ *
+ * @param {string} message The message to replace
+ * @param {object[]} mentions Array of mentions used to make replacements
+ *
+ * @returns {string}
+ */
+function replaceMentionedUsernamesWithFullNames(message, mentions) {
+ if (!mentions || !mentions.length) {
+ return message;
+ }
+ mentions.forEach((mention) => {
+ const user = RocketChat.models.Users.findOneById(mention._id);
+ if (user && user.name) {
+ message = message.replace(`@${ mention.username }`, user.name);
+ }
+ });
+ return message;
+}
+
+/**
+ * Send notification to user
+ *
+ * @param {string} userId The user to notify
+ * @param {object} user The sender
+ * @param {object} room The room send from
+ * @param {number} duration Duration of notification
+ */
+function notifyUser(userId, user, message, room, duration) {
+ const UI_Use_Real_Name = RocketChat.settings.get('UI_Use_Real_Name') === true;
+ if (UI_Use_Real_Name) {
+ message.msg = replaceMentionedUsernamesWithFullNames(message.msg, message.mentions);
+ }
+ let title = UI_Use_Real_Name ? user.name : `@${ user.username }`;
+ if (room.t !== 'd' && room.name) {
+ title += ` @ #${ room.name }`;
+ }
+ RocketChat.Notifications.notifyUser(userId, 'notification', {
+ title,
+ text: message.msg,
+ duration,
+ payload: {
+ _id: message._id,
+ rid: message.rid,
+ sender: message.u,
+ type: room.t,
+ name: room.name
+ }
+ });
+}
+
+/**
+ * Checks if a message contains a user highlight
+ *
+ * @param {string} message
+ * @param {array|undefined} highlights
+ *
+ * @returns {boolean}
+ */
+function messageContainsHighlight(message, highlights) {
+ if (! highlights || highlights.length === 0) { return false; }
+
+ let has = false;
+ highlights.some(function(highlight) {
+ const regexp = new RegExp(s.escapeRegExp(highlight), 'i');
+ if (regexp.test(message.msg)) {
+ has = true;
+ return true;
+ }
+ });
+
+ return has;
+}
+
function getBadgeCount(userId) {
const subscriptions = RocketChat.models.Subscriptions.findUnreadByUserId(userId).fetch();
@@ -24,7 +99,13 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) {
/*
Increment unread couter if direct messages
*/
- const settings = {};
+ const settings = {
+ alwaysNotifyDesktopUsers: [],
+ dontNotifyDesktopUsers: [],
+ alwaysNotifyMobileUsers: [],
+ dontNotifyMobileUsers: [],
+ desktopNotificationDurations: {}
+ };
/**
* Checks if a given user can be notified
@@ -43,35 +124,6 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) {
return (settings[types[type][0]].indexOf(id) === -1 || settings[types[type][1]].indexOf(id) !== -1);
}
- /**
- * Checks if a message contains a user highlight
- *
- * @param {string} message
- * @param {array|undefined} highlights
- *
- * @returns {boolean}
- */
- function messageContainsHighlight(message, highlights) {
- if (! highlights || highlights.length === 0) { return false; }
-
- let has = false;
- highlights.some(function(highlight) {
- const regexp = new RegExp(s.escapeRegExp(highlight), 'i');
- if (regexp.test(message.msg)) {
- has = true;
- return true;
- }
- });
-
- return has;
- }
-
- settings.alwaysNotifyDesktopUsers = [];
- settings.dontNotifyDesktopUsers = [];
- settings.alwaysNotifyMobileUsers = [];
- settings.dontNotifyMobileUsers = [];
- settings.desktopNotificationDurations = {};
-
const notificationPreferencesByRoom = RocketChat.models.Subscriptions.findNotificationPreferencesByRoom(room._id);
notificationPreferencesByRoom.forEach(function(subscription) {
if (subscription.disableNotifications) {
@@ -140,18 +192,8 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) {
}
if ((userOfMention != null) && canBeNotified(userOfMentionId, 'mobile')) {
- RocketChat.Notifications.notifyUser(userOfMention._id, 'notification', {
- title: RocketChat.settings.get('UI_Use_Real_Name') ? user.name : `@${ user.username }`,
- text: message.msg,
- duration: settings.desktopNotificationDurations[userOfMention._id],
- payload: {
- _id: message._id,
- rid: message.rid,
- sender: message.u,
- type: room.t,
- name: room.name
- }
- });
+ const duration = settings.desktopNotificationDurations[userOfMention._id];
+ notifyUser(userOfMention._id, user, message, room, duration);
}
if ((userOfMention != null) && canBeNotified(userOfMentionId, 'desktop')) {
@@ -287,22 +329,8 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room) {
if (userIdsToNotify.length > 0) {
for (const usersOfMentionId of userIdsToNotify) {
- let title = `@${ user.username }`;
- if (room.name) {
- title += ` @ #${ room.name }`;
- }
- RocketChat.Notifications.notifyUser(usersOfMentionId, 'notification', {
- title,
- text: message.msg,
- duration: settings.desktopNotificationDurations[usersOfMentionId],
- payload: {
- _id: message._id,
- rid: message.rid,
- sender: message.u,
- type: room.t,
- name: room.name
- }
- });
+ const duration = settings.desktopNotificationDurations[usersOfMentionId];
+ notifyUser(usersOfMentionId, user, message, room, duration);
}
}
diff --git a/packages/rocketchat-lib/server/methods/getChannelHistory.js b/packages/rocketchat-lib/server/methods/getChannelHistory.js
index e2f40ebe748a..0f3194991f56 100644
--- a/packages/rocketchat-lib/server/methods/getChannelHistory.js
+++ b/packages/rocketchat-lib/server/methods/getChannelHistory.js
@@ -57,6 +57,12 @@ Meteor.methods({
const user = RocketChat.models.Users.findOneById(message.u._id);
message.u.name = user && user.name;
}
+ if (message.mentions && message.mentions.length && UI_Use_Real_Name) {
+ message.mentions.forEach((mention) => {
+ const user = RocketChat.models.Users.findOneById(mention._id);
+ mention.name = user && user.name;
+ });
+ }
return message;
});
diff --git a/packages/rocketchat-mentions/Mentions.js b/packages/rocketchat-mentions/Mentions.js
index 16082524887a..b4d8d6317abc 100644
--- a/packages/rocketchat-mentions/Mentions.js
+++ b/packages/rocketchat-mentions/Mentions.js
@@ -32,10 +32,13 @@ export default class {
return `${ match }`;
}
- if (message.temp == null && _.findWhere(message.mentions, {username}) == null) {
+ const mentionObj = _.findWhere(message.mentions, {username});
+ if (message.temp == null && mentionObj == null) {
return match;
}
- return `${ match }`;
+ const name = RocketChat.settings.get('UI_Use_Real_Name') && mentionObj && mentionObj.name;
+
+ return `${ name || match }`;
});
}
replaceChannels(str, message) {
diff --git a/packages/rocketchat-mentions/client.js b/packages/rocketchat-mentions/client.js
index 3bab2bd276b8..ca2d366ab90a 100644
--- a/packages/rocketchat-mentions/client.js
+++ b/packages/rocketchat-mentions/client.js
@@ -8,5 +8,6 @@ const MentionsClient = new Mentions({
return me && me.username;
}
});
+
RocketChat.callbacks.add('renderMessage', (message) => MentionsClient.parse(message), RocketChat.callbacks.priority.MEDIUM, 'mentions-message');
RocketChat.callbacks.add('renderMentions', (message) => MentionsClient.parse(message), RocketChat.callbacks.priority.MEDIUM, 'mentions-mentions');
diff --git a/server/methods/loadHistory.js b/server/methods/loadHistory.js
index 01d1720ec5c0..31858c9aa821 100644
--- a/server/methods/loadHistory.js
+++ b/server/methods/loadHistory.js
@@ -70,6 +70,12 @@ Meteor.methods({
const user = RocketChat.models.Users.findOneById(message.u._id);
message.u.name = user && user.name;
}
+ if (message.mentions && message.mentions.length && UI_Use_Real_Name) {
+ message.mentions.forEach((mention) => {
+ const user = RocketChat.models.Users.findOneById(mention._id);
+ mention.name = user && user.name;
+ });
+ }
return message;
});
diff --git a/server/stream/messages.js b/server/stream/messages.js
index cffeb442a7cb..96afa00f6bc7 100644
--- a/server/stream/messages.js
+++ b/server/stream/messages.js
@@ -51,6 +51,13 @@ Meteor.startup(function() {
const user = RocketChat.models.Users.findOneById(record.u._id);
record.u.name = user && user.name;
}
+
+ if (record.mentions && record.mentions.length && UI_Use_Real_Name) {
+ record.mentions.forEach((mention) => {
+ const user = RocketChat.models.Users.findOneById(mention._id);
+ mention.name = user && user.name;
+ });
+ }
msgStream.emitWithoutBroadcast('__my_messages__', record, {});
return msgStream.emitWithoutBroadcast(record.rid, record);
}