From 014d64330cdb43b2adf36c35b1c5a0cefce2c5aa Mon Sep 17 00:00:00 2001 From: TheConnMan Date: Mon, 1 Feb 2016 20:50:10 -0500 Subject: [PATCH] Send emails to offline users when mentioned in a channel --- .../server/functions/sendMessage.coffee | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/packages/rocketchat-lib/server/functions/sendMessage.coffee b/packages/rocketchat-lib/server/functions/sendMessage.coffee index fcb2666b535..3fb1732bb38 100644 --- a/packages/rocketchat-lib/server/functions/sendMessage.coffee +++ b/packages/rocketchat-lib/server/functions/sendMessage.coffee @@ -88,7 +88,7 @@ RocketChat.sendMessage = (user, message, room, options) -> toAll = mentionIds.indexOf('all') > -1 if mentionIds.length > 0 - usersOfMention = RocketChat.models.Users.find({_id: {$in: mentionIds}}, {fields: {_id: 1, username: 1}}).fetch() + usersOfMention = RocketChat.models.Users.find({_id: {$in: mentionIds}}, {fields: {_id: 1, username: 1, status: 1, emails: 1}}).fetch() if room.t is 'c' and !toAll for usersOfMentionItem in usersOfMention @@ -111,17 +111,22 @@ RocketChat.sendMessage = (user, message, room, options) -> userIdsToNotify = _.pluck(usersOfMention, '_id') userIdsToPushNotify = userIdsToNotify + offlineMentionsRoom = _.filter usersOfMention, (user) -> + user.status is 'offline' + # If the message is @all, notify all room users except for the sender. if toAll and room.usernames?.length > 0 usersOfRoom = RocketChat.models.Users.find({ username: {$in: room.usernames}, _id: {$ne: user._id}}, - {fields: {_id: 1, username: 1, status: 1}}) + {fields: {_id: 1, username: 1, status: 1, emails: 1}}) .fetch() onlineUsersOfRoom = _.filter usersOfRoom, (user) -> user.status in ['online', 'away', 'busy'] userIdsToNotify = _.union userIdsToNotify, _.pluck(onlineUsersOfRoom, '_id') userIdsToPushNotify = _.union userIdsToPushNotify, _.pluck(usersOfRoom, '_id') + offlineMentionsRoom = _.filter usersOfRoom, (user) -> + user.status is 'offline' if userIdsToNotify.length > 0 for usersOfMentionId in userIdsToNotify @@ -153,6 +158,13 @@ RocketChat.sendMessage = (user, message, room, options) -> query: userId: $in: userIdsToPushNotify + if offlineMentionsRoom.length > 0 + for offlineUser in offlineMentionsRoom + Email.send + to: offlineUser.emails[0].address + from: RocketChat.settings.get('From_Email') + subject: "[#{RocketChat.settings.get('Site_Name')}] You have been mentioned by #{user.username} in ##{room.name}" + html: "> " + message.msg ### Update all other subscriptions to alert their owners but witout incrementing the unread counter, as it is only for mentions and direct messages