Merge pull request #6690 from alexbrazier/feature/mention-names

[NEW] Show full name in mentions if use full name setting enabled
pull/7095/head
Rodrigo Nascimento 9 years ago committed by GitHub
commit 76534cd998
  1. 12
      client/notifications/UsersNameChanged.js
  2. 144
      packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js
  3. 6
      packages/rocketchat-lib/server/methods/getChannelHistory.js
  4. 7
      packages/rocketchat-mentions/Mentions.js
  5. 1
      packages/rocketchat-mentions/client.js
  6. 6
      server/methods/loadHistory.js
  7. 7
      server/stream/messages.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'

@ -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);
}
}

@ -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;
});

@ -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) {

@ -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');

@ -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;
});

@ -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);
}

Loading…
Cancel
Save