Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Rendering of emails and mentions in messages #11165

Merged
merged 8 commits into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/rocketchat-autolinker/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,4 @@ function AutoLinker(message) {
return message;
}

RocketChat.callbacks.add('renderMessage', AutoLinker);
RocketChat.callbacks.add('renderMessage', AutoLinker, RocketChat.callbacks.priority.LOW, 'autolinker');
2 changes: 1 addition & 1 deletion packages/rocketchat-colors/client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ function HexColorPreview(message) {
return message;
}

RocketChat.callbacks.add('renderMessage', HexColorPreview, RocketChat.callbacks.priority.MEDIUM);
RocketChat.callbacks.add('renderMessage', HexColorPreview, RocketChat.callbacks.priority.MEDIUM, 'hexcolor');
2 changes: 1 addition & 1 deletion packages/rocketchat-issuelinks/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ function IssueLink(message) {
return message;
}

RocketChat.callbacks.add('renderMessage', IssueLink, RocketChat.callbacks.priority.MEDIUM);
RocketChat.callbacks.add('renderMessage', IssueLink, RocketChat.callbacks.priority.MEDIUM, 'issuelink');
2 changes: 1 addition & 1 deletion packages/rocketchat-mapview/client/mapview.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ function MapView(message) {
return message;
}

RocketChat.callbacks.add('renderMessage', MapView, RocketChat.callbacks.priority.HIGH);
RocketChat.callbacks.add('renderMessage', MapView, RocketChat.callbacks.priority.HIGH, 'mapview');
27 changes: 10 additions & 17 deletions packages/rocketchat-mentions/Mentions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* Mentions is a named function that will process Mentions
* @param {Object} message - The message object
*/
import _ from 'underscore';
import s from 'underscore.string';
export default class {
constructor({pattern, useRealName, me}) {
Expand All @@ -29,44 +28,38 @@ export default class {
return typeof this._useRealName === 'function' ? this._useRealName() : this._useRealName;
}
get userMentionRegex() {
return new RegExp(`@(${ this.pattern })`, 'gm');
return new RegExp(`(^|\\s)@(${ this.pattern })`, 'gm');
}
get channelMentionRegex() {
return new RegExp(`^#(${ this.pattern })| #(${ this.pattern })`, 'gm');
return new RegExp(`(^|\\s)#(${ this.pattern })`, 'gm');
}
replaceUsers(str, message, me) {
return str.replace(this.userMentionRegex, (match, username) => {
return str.replace(this.userMentionRegex, (match, prefix, username) => {
if (['all', 'here'].includes(username)) {
return `<a class="mention-link mention-link-me mention-link-all">${ match }</a>`;
return `${ prefix }<a class="mention-link mention-link-me mention-link-all">@${ username }</a>`;
}

const mentionObj = _.findWhere(message.mentions, {username});
const mentionObj = message.mentions.find(m => m.username === username);
if (message.temp == null && mentionObj == null) {
return match;
}
const name = this.useRealName && mentionObj && s.escapeHTML(mentionObj.name);

return `<a class="mention-link ${ username === me ? 'mention-link-me':'' }" data-username="${ username }" title="${ name ? username : '' }">${ name || match }</a>`;
return `${ prefix }<a class="mention-link ${ username === me ? 'mention-link-me' : '' }" data-username="${ username }" title="${ name ? username : '' }">${ name || `@${ username }` }</a>`;
});
}
replaceChannels(str, message) {
//since apostrophe escaped contains # we need to unescape it
return str.replace(/&#39;/g, '\'').replace(this.channelMentionRegex, (match, n1, n2) => {
const name = n1 || n2;
if (message.temp == null && _.findWhere(message.channels, {name}) == null) {
return str.replace(/&#39;/g, '\'').replace(this.channelMentionRegex, (match, prefix, name) => {
if (!message.temp && !message.channels.find(c => c.name === name)) {
return match;
}

// remove the link from inside the link and put before
if (/^\s/.test(match)) {
return ` <a class="mention-link" data-channel="${ name }">${ match.trim() }</a>`;
}

return `<a class="mention-link" data-channel="${ name }">${ match }</a>`;
return `${ prefix }<a class="mention-link" data-channel="${ name }">${ `#${ name }` }</a>`;
});
}
getUserMentions(str) {
return str.match(this.userMentionRegex) || [];
return (str.match(this.userMentionRegex) || []).map(match => match.trim());
}
getChannelMentions(str) {
return (str.match(this.channelMentionRegex) || []).map(match => match.trim());
Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-mentions/tests/client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('Mention', function() {
'@rocket.cat',
' @rocket.cat ',
'hello @rocket.cat',
'hello,@rocket.cat',
//'hello,@rocket.cat', // this test case is ignored since is not compatible with the message box behavior
'@rocket.cat, hello',
'@rocket.cat,hello',
'hello @rocket.cat how are you?'
Expand Down