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

converted rocketchat-mentions coffee to js #6467

Merged
merged 2 commits into from
Mar 29, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
55 changes: 0 additions & 55 deletions packages/rocketchat-mentions/client.coffee

This file was deleted.

41 changes: 41 additions & 0 deletions packages/rocketchat-mentions/client.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Mentions is a named function that will process Mentions
* @param {Object} message - The message object
*/

const MentionsClient = function MentionsClient(message) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the var declaration

let msg = message.html;
if (!msg.trim()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can msg be undefined?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that this parameter will not be undefined, but... I put a test let msg = (message && message.html) || '';

return message;
}
const msgMentionRegex = new RegExp(`(?:^|\\s|\\n)(@(${ RocketChat.settings.get('UTF8_Names_Validation') }):?)[:.,\s]?`, 'g');

let me = Meteor.user();
me = me ? me.username : null;

msg = msg.replace(msgMentionRegex, function(match, mention, username) {
if (['all', 'here'].includes(username)) {
return match.replace(mention, `<a class="mention-link mention-link-me mention-link-all background-attention-color">${ mention }</a>`);
}
if (message.temp == null && _.findWhere(message.mentions, {username}) == null) {
return match;
}
return match.replace(mention, `<a class="mention-link ${ username === me ? 'mention-link-me background-primary-action-color':'' }" data-username="${ username }">${ mention }</a>`);
});

const msgChannelRegex = new RegExp(`(?:^|\\s|\\n)(#(${ RocketChat.settings.get('UTF8_Names_Validation') }))[:.,\s]?`, 'g');

msg = msg.replace(msgChannelRegex, function(match, mention, name) {
if (message.temp == null && _.findWhere(message.channels, {name}) == null) {
return match;
}
return match.replace(mention, `<a class="mention-link" data-channel="${ name }">${ mention }</a>`);
});
message.html = msg;
return message;
};


RocketChat.callbacks.add('renderMessage', MentionsClient, RocketChat.callbacks.priority.MEDIUM, 'mentions-message');

RocketChat.callbacks.add('renderMentions', MentionsClient, RocketChat.callbacks.priority.MEDIUM, 'mentions-mentions');
5 changes: 2 additions & 3 deletions packages/rocketchat-mentions/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ Package.describe({
Package.onUse(function(api) {
api.use([
'ecmascript',
'coffeescript',
'rocketchat:lib'
]);

api.addFiles('server.coffee', 'server');
api.addFiles('client.coffee', 'client');
api.addFiles('server.js', 'server');
api.addFiles('client.js', 'client');
});
56 changes: 0 additions & 56 deletions packages/rocketchat-mentions/server.coffee

This file was deleted.

49 changes: 49 additions & 0 deletions packages/rocketchat-mentions/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Mentions is a named function that will process Mentions
* @param {Object} message - The message object
*/

const MentionsServer = function(message) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use named function instead of var declaration

const msgMentionRegex = new RegExp(`(?:^|\\s|\\n)(?:@)(${ RocketChat.settings.get('UTF8_Names_Validation') })`, 'g');
const mentionsAll = [];
const userMentions = [];
let mentions = message.msg.match(msgMentionRegex);
if (mentions) {
mentions.forEach((m) => {
const mention = m.trim().substr(1);
if (mention !== 'all' && mention !== 'here') {
return userMentions.push(mention);
}
if (mention === 'all') {
const messageMaxAll = RocketChat.settings.get('Message_MaxAll');
const allChannel = RocketChat.models.Rooms.findOneById(message.rid);
if (messageMaxAll !== 0 && allChannel.usernames.length >= messageMaxAll) {
return;
}
}
mentionsAll.push({
_id: mention,
username: mention
});
});
mentions = userMentions.length ? Meteor.users.find({ username: {$in: _.unique(userMentions)}}, { fields: {_id: true, username: true }}).fetch() : [];

const verifiedMentions = [...mentionsAll, ...mentions];
if (verifiedMentions.length !== 0) {
message.mentions = verifiedMentions;
}
}

const msgChannelRegex = new RegExp(`(?:^|\\s|\\n)(?:#)(${ RocketChat.settings.get('UTF8_Names_Validation') })`, 'g');
let channels = message.msg.match(msgChannelRegex);
if (channels) {
channels = channels.map(c => c.trim().substr(1));
const verifiedChannels = RocketChat.models.Rooms.find({ name: {$in: _.unique(channels)}, t: 'c' }, { fields: {_id: 1, name: 1 }}).fetch();
if (verifiedChannels.length !== 0) {
message.channels = verifiedChannels;
}
}
return message;
};

RocketChat.callbacks.add('beforeSaveMessage', MentionsServer, RocketChat.callbacks.priority.HIGH, 'mentions');