diff --git a/packages/rocketchat-mentions/client.coffee b/packages/rocketchat-mentions/client.coffee deleted file mode 100644 index 2132253a829f..000000000000 --- a/packages/rocketchat-mentions/client.coffee +++ /dev/null @@ -1,55 +0,0 @@ -### -# Mentions is a named function that will process Mentions -# @param {Object} message - The message object -### - -class MentionsClient - constructor: (message) -> - if _.trim message.html - msg = message.html - - mentions = [] - - msgMentionRegex = new RegExp '(?:^|\\s|\\n)(?:@)(' + RocketChat.settings.get('UTF8_Names_Validation') + ')', 'g' - message.msg.replace msgMentionRegex, (match, mention) -> - mentions.push mention - - me = Meteor.user()?.username - - if mentions.length isnt 0 - mentions = _.unique mentions - mentions = mentions.join('|') - msg = msg.replace new RegExp("(?:^|\\s|\\n)(@(#{mentions}):?)[:.,\s]?", 'g'), (match, mention, username) -> - if username is 'all' or username is 'here' - return match.replace mention, "#{mention}" - - if not message.temp? - if not _.findWhere(message.mentions, {username: username})? - return match - - classes = 'mention-link' - if username is me - classes += ' mention-link-me background-primary-action-color' - - return match.replace mention, "#{mention}" - - channels = [] - msgChannelRegex = new RegExp '(?:^|\\s|\\n)(?:#)(' + RocketChat.settings.get('UTF8_Names_Validation') + ')', 'g' - message.msg.replace msgChannelRegex, (match, mention) -> - channels.push mention - - if channels.length isnt 0 - channels = _.unique channels - channels = channels.join('|') - msg = msg.replace new RegExp("(?:^|\\s|\\n)(#(#{channels}))[:.,\s]?", 'g'), (match, mention, channel) -> - if not message.temp? - if not _.findWhere(message.channels, {name: channel})? - return match - return match.replace mention, "#{mention}" - - - 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' diff --git a/packages/rocketchat-mentions/client.js b/packages/rocketchat-mentions/client.js new file mode 100644 index 000000000000..041eb0bfb123 --- /dev/null +++ b/packages/rocketchat-mentions/client.js @@ -0,0 +1,41 @@ +/* + * Mentions is a named function that will process Mentions + * @param {Object} message - The message object + */ + +function MentionsClient(message) { + let msg = (message && message.html) || ''; + if (!msg.trim()) { + 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, `${ mention }`); + } + if (message.temp == null && _.findWhere(message.mentions, {username}) == null) { + return match; + } + return match.replace(mention, `${ mention }`); + }); + + 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, `${ mention }`); + }); + 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'); diff --git a/packages/rocketchat-mentions/package.js b/packages/rocketchat-mentions/package.js index 68ea0c3ce2e5..80be96108096 100644 --- a/packages/rocketchat-mentions/package.js +++ b/packages/rocketchat-mentions/package.js @@ -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'); }); diff --git a/packages/rocketchat-mentions/server.coffee b/packages/rocketchat-mentions/server.coffee deleted file mode 100644 index cd03ffd9e82b..000000000000 --- a/packages/rocketchat-mentions/server.coffee +++ /dev/null @@ -1,56 +0,0 @@ -### -# Mentions is a named function that will process Mentions -# @param {Object} message - The message object -### - -class MentionsServer - constructor: (message) -> - # If message starts with /me, replace it for text formatting - mentions = [] - msgMentionRegex = new RegExp '(?:^|\\s|\\n)(?:@)(' + RocketChat.settings.get('UTF8_Names_Validation') + ')', 'g' - message.msg.replace msgMentionRegex, (match, mention) -> - mentions.push mention - if mentions.length isnt 0 - mentions = _.unique mentions - verifiedMentions = [] - mentions.forEach (mention) -> - if mention is 'all' - messageMaxAll = RocketChat.settings.get('Message_MaxAll') - if messageMaxAll > 0 - allChannel = RocketChat.models.Rooms.findOneById message.rid - if allChannel.usernames.length <= messageMaxAll - verifiedMention = - _id: mention - username: mention - else - verifiedMention = - _id: mention - username: mention - else if mention is 'here' - verifiedMention = - _id: mention - username: mention - else - verifiedMention = Meteor.users.findOne({username: mention}, {fields: {_id: 1, username: 1}}) - - verifiedMentions.push verifiedMention if verifiedMention? - if verifiedMentions.length isnt 0 - message.mentions = verifiedMentions - - channels = [] - msgChannelRegex = new RegExp '(?:^|\\s|\\n)(?:#)(' + RocketChat.settings.get('UTF8_Names_Validation') + ')', 'g' - message.msg.replace msgChannelRegex, (match, mention) -> - channels.push mention - - if channels.length isnt 0 - channels = _.unique channels - verifiedChannels = [] - channels.forEach (mention) -> - verifiedChannel = RocketChat.models.Rooms.findOneByNameAndType(mention, 'c', { fields: {_id: 1, name: 1 } }) - verifiedChannels.push verifiedChannel if verifiedChannel? - - if verifiedChannels.length isnt 0 - message.channels = verifiedChannels - return message - -RocketChat.callbacks.add 'beforeSaveMessage', MentionsServer, RocketChat.callbacks.priority.HIGH, 'mentions' diff --git a/packages/rocketchat-mentions/server.js b/packages/rocketchat-mentions/server.js new file mode 100644 index 000000000000..66aa59c08661 --- /dev/null +++ b/packages/rocketchat-mentions/server.js @@ -0,0 +1,49 @@ +/* +* Mentions is a named function that will process Mentions +* @param {Object} message - The message object +*/ + +function MentionsServer(message) { + 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');