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]Renaming of channel breaks links/references. #13269

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 6 additions & 5 deletions packages/rocketchat-mentions/lib/Mentions.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Mentions is a named function that will process Mentions
* @param {Object} message - The message object
*/
import { RocketChat } from 'meteor/rocketchat:lib';
import s from 'underscore.string';
export default class {
constructor({ pattern, useRealName, me }) {
Expand Down Expand Up @@ -48,14 +49,14 @@ export default class {
return `${ prefix }<a class="mention-link ${ username === me ? 'mention-link-me' : '' }" data-username="${ username }" title="${ name ? username : '' }">${ name || `@${ username }` }</a>`;
});
}
replaceChannels(str, message) {
replaceChannels(str, message, externalRoom = undefined) {
// since apostrophe escaped contains # we need to unescape it
return str.replace(/&#39;/g, '\'').replace(this.channelMentionRegex, (match, prefix, name) => {
if (!message.temp && !(message.channels && message.channels.find((c) => c.name === name))) {
return match;
}

return `${ prefix }<a class="mention-link" data-channel="${ name }">${ `#${ name }` }</a>`;
const room = externalRoom || RocketChat.models.Rooms.findOne({ name });
return `${ prefix }<a class="mention-link" data-channel="${ room._id }">${ `#${ name }` }</a>`;
});
}
getUserMentions(str) {
Expand All @@ -64,13 +65,13 @@ export default class {
getChannelMentions(str) {
return (str.match(this.channelMentionRegex) || []).map((match) => match.trim());
}
parse(message) {
parse(message, me, externalRoom = undefined) {
let msg = (message && message.html) || '';
if (!msg.trim()) {
return message;
}
msg = this.replaceUsers(msg, message, this.me);
msg = this.replaceChannels(msg, message, this.me);
msg = this.replaceChannels(msg, message, externalRoom);
message.html = msg;
return message;
}
Expand Down
28 changes: 14 additions & 14 deletions packages/rocketchat-mentions/tests/client.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,17 @@ describe('replace methods', function() {

describe('replaceChannels', () => {
it('should render for #general', () => {
const result = mention.replaceChannels('#general', message);
assert.equal('<a class="mention-link" data-channel="general">#general</a>', result);
const result = mention.replaceChannels('#general', message, { _id: 'GENERAL', name: 'general' });
assert.equal('<a class="mention-link" data-channel="GENERAL">#general</a>', result);
});
const str2 = '#rocket.cat';
it(`should render for ${ str2 }`, () => {
const result = mention.replaceChannels(str2, message);
assert.equal(result, `<a class="mention-link" data-channel="${ str2.replace('#', '') }">${ str2 }</a>`);
const result = mention.replaceChannels(str2, message, { _id: 'rocket.cat', name: 'rocket.cat' });
assert.equal(result, `<a class="mention-link" data-channel="rocket.cat">${ str2 }</a>`);
});
it(`should render for "hello ${ str2 }"`, () => {
const result = mention.replaceChannels(`hello ${ str2 }`, message);
assert.equal(result, `hello <a class="mention-link" data-channel="${ str2.replace('#', '') }">${ str2 }</a>`);
const result = mention.replaceChannels(`hello ${ str2 }`, message, { _id: 'rocket.cat', name: 'rocket.cat' });
assert.equal(result, `hello <a class="mention-link" data-channel="rocket.cat">${ str2 }</a>`);
});
it('should render for unknow/private channel "hello #unknow"', () => {
const result = mention.replaceChannels('hello #unknow', message);
Expand All @@ -280,13 +280,13 @@ describe('replace methods', function() {
describe('parse all', () => {
it('should render for #general', () => {
message.html = '#general';
const result = mention.parse(message, 'me');
assert.equal('<a class="mention-link" data-channel="general">#general</a>', result.html);
const result = mention.parse(message, 'me', { _id: 'GENERAL', name: 'general' });
assert.equal('<a class="mention-link" data-channel="GENERAL">#general</a>', result.html);
});
it('should render for "#general and @rocket.cat', () => {
message.html = '#general and @rocket.cat';
const result = mention.parse(message, 'me');
assert.equal('<a class="mention-link" data-channel="general">#general</a> and <a class="mention-link " data-username="rocket.cat" title="">@rocket.cat</a>', result.html);
const result = mention.parse(message, 'me', { _id: 'GENERAL', name: 'general' });
assert.equal('<a class="mention-link" data-channel="GENERAL">#general</a> and <a class="mention-link " data-username="rocket.cat" title="">@rocket.cat</a>', result.html);
});
it('should render for "', () => {
message.html = '';
Expand All @@ -306,13 +306,13 @@ describe('replace methods', function() {
});
it('should render for #general', () => {
message.html = '#general';
const result = mention.parse(message, 'me');
assert.equal('<a class="mention-link" data-channel="general">#general</a>', result.html);
const result = mention.parse(message, 'me', { _id: 'GENERAL', name: 'general' });
assert.equal('<a class="mention-link" data-channel="GENERAL">#general</a>', result.html);
});
it('should render for "#general and @rocket.cat', () => {
message.html = '#general and @rocket.cat';
const result = mention.parse(message, 'me');
assert.equal('<a class="mention-link" data-channel="general">#general</a> and <a class="mention-link " data-username="rocket.cat" title="rocket.cat">Rocket.Cat</a>', result.html);
const result = mention.parse(message, 'me', { _id: 'GENERAL', name: 'general' });
assert.equal('<a class="mention-link" data-channel="GENERAL">#general</a> and <a class="mention-link " data-username="rocket.cat" title="rocket.cat">Rocket.Cat</a>', result.html);
});
it('should render for "', () => {
message.html = '';
Expand Down
5 changes: 3 additions & 2 deletions packages/rocketchat-ui/client/views/app/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -718,12 +718,13 @@ Template.room.events({
return;
}
const channel = $(e.currentTarget).data('channel');
const room = Rooms.findOne({ _id: channel });
if (channel != null) {
if (Layout.isEmbedded()) {
fireGlobalEvent('click-mention-link', { path: FlowRouter.path('channel', { name: channel }), channel });
fireGlobalEvent('click-mention-link', { path: FlowRouter.path('channel', { name: room.name }), channel });
}

FlowRouter.go('channel', { name: channel }, FlowRouter.current().queryParams);
FlowRouter.go('channel', { name: room.name }, FlowRouter.current().queryParams);
return;
}

Expand Down