Skip to content

Commit

Permalink
Merge pull request #6690 from alexbrazier/feature/mention-names
Browse files Browse the repository at this point in the history
[NEW] Show full name in mentions if use full name setting enabled
  • Loading branch information
rodrigok committed May 31, 2017
2 parents 42533dd + a626374 commit 76534cd
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 60 deletions.
12 changes: 12 additions & 0 deletions client/notifications/UsersNameChanged.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
144 changes: 86 additions & 58 deletions packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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')) {
Expand Down Expand Up @@ -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);
}
}

Expand Down
6 changes: 6 additions & 0 deletions packages/rocketchat-lib/server/methods/getChannelHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down
7 changes: 5 additions & 2 deletions packages/rocketchat-mentions/Mentions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ export default class {
return `<a class="mention-link mention-link-me mention-link-all background-attention-color">${ match }</a>`;
}

if (message.temp == null && _.findWhere(message.mentions, {username}) == null) {
const mentionObj = _.findWhere(message.mentions, {username});
if (message.temp == null && mentionObj == null) {
return match;
}
return `<a class="mention-link ${ username === me ? 'mention-link-me background-primary-action-color':'' }" data-username="${ username }">${ match }</a>`;
const name = RocketChat.settings.get('UI_Use_Real_Name') && mentionObj && mentionObj.name;

return `<a class="mention-link ${ username === me ? 'mention-link-me background-primary-action-color':'' }" data-username="${ username }" title="${ name ? username : '' }">${ name || match }</a>`;
});
}
replaceChannels(str, message) {
Expand Down
1 change: 1 addition & 0 deletions packages/rocketchat-mentions/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
6 changes: 6 additions & 0 deletions server/methods/loadHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

Expand Down
7 changes: 7 additions & 0 deletions server/stream/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 76534cd

Please sign in to comment.