From daa570b35b98e271a7aa976274ada2ca9f40704b Mon Sep 17 00:00:00 2001 From: Ry Jones Date: Wed, 27 Dec 2017 11:32:00 -0800 Subject: [PATCH] [NEW] Add mention-here method #7631 closes #7631 Signed-off-by: Ry Jones --- .../server/startup.js | 1 + packages/rocketchat-lib/package.js | 1 + .../server/methods/filterATHereTag.js | 34 +++++++++++++++++++ 3 files changed, 36 insertions(+) create mode 100644 packages/rocketchat-lib/server/methods/filterATHereTag.js diff --git a/packages/rocketchat-authorization/server/startup.js b/packages/rocketchat-authorization/server/startup.js index bd1ea3ff6e44..10b8846b01e2 100644 --- a/packages/rocketchat-authorization/server/startup.js +++ b/packages/rocketchat-authorization/server/startup.js @@ -40,6 +40,7 @@ Meteor.startup(function() { { _id: 'manage-own-integrations', roles : ['admin', 'bot'] }, { _id: 'manage-oauth-apps', roles : ['admin'] }, { _id: 'mention-all', roles : ['admin', 'owner', 'moderator', 'user'] }, + { _id: 'mention-here', roles : ['admin', 'owner', 'moderator', 'user'] }, { _id: 'mute-user', roles : ['admin', 'owner', 'moderator'] }, { _id: 'remove-user', roles : ['admin', 'owner', 'moderator'] }, { _id: 'run-import', roles : ['admin'] }, diff --git a/packages/rocketchat-lib/package.js b/packages/rocketchat-lib/package.js index d7eedb158ad6..a4460633043d 100644 --- a/packages/rocketchat-lib/package.js +++ b/packages/rocketchat-lib/package.js @@ -165,6 +165,7 @@ Package.onUse(function(api) { api.addFiles('server/methods/deleteUserOwnAccount.js', 'server'); api.addFiles('server/methods/filterBadWords.js', ['server']); api.addFiles('server/methods/filterATAllTag.js', 'server'); + api.addFiles('server/methods/filterATHereTag.js', 'server'); api.addFiles('server/methods/getChannelHistory.js', 'server'); api.addFiles('server/methods/getFullUserData.js', 'server'); api.addFiles('server/methods/getRoomJoinCode.js', 'server'); diff --git a/packages/rocketchat-lib/server/methods/filterATHereTag.js b/packages/rocketchat-lib/server/methods/filterATHereTag.js new file mode 100644 index 000000000000..c2df29e7d58e --- /dev/null +++ b/packages/rocketchat-lib/server/methods/filterATHereTag.js @@ -0,0 +1,34 @@ +import _ from 'underscore'; + +RocketChat.callbacks.add('beforeSaveMessage', function(message) { + // Test if the message mentions include @here. + if (message.mentions != null && + _.pluck(message.mentions, '_id').some((item) => item === 'here')) { + + // Check if the user has permissions to use @here in both global and room scopes. + if (!RocketChat.authz.hasPermission(message.u._id, 'mention-here') && !RocketChat.authz.hasPermission(message.u._id, 'mention-here', message.rid)) { + + // Get the language of the user for the error notification. + const language = RocketChat.models.Users.findOneById(message.u._id).language; + const action = TAPi18n.__('Notify_active_in_this_room', {}, language); + + // Add a notification to the chat, informing the user that this + // action is not allowed. + RocketChat.Notifications.notifyUser(message.u._id, 'message', { + _id: Random.id(), + rid: message.rid, + ts: new Date, + msg: TAPi18n.__('error-action-not-allowed', { action }, language) + }); + + // Also throw to stop propagation of 'sendMessage'. + throw new Meteor.Error('error-action-not-allowed', 'Notify here in this room not allowed', { + method: 'filterATHereTag', + action: 'Notify_active_in_this_room' + }); + } + } + + return message; + +}, 1, 'filterATHereTag');