From e8432e0dc079ef87f70396771803ba95827413de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20J=C3=A4gle?= Date: Wed, 25 Apr 2018 21:38:18 +0200 Subject: [PATCH 1/2] Fix livechat notifications for guest pool based routing --- .../server/lib/sendNotificationsOnMessage.js | 32 ++++++++++++++++++- .../server/models/LivechatInquiry.js | 10 ++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index c4b7c9fc3ffd..e65060ba3444 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -63,7 +63,7 @@ function notifyDesktopUser(userId, user, message, room, duration) { if (UI_Use_Real_Name) { message.msg = replaceMentionedUsernamesWithFullNames(message.msg, message.mentions); } - let title = UI_Use_Real_Name ? user.name : `@${ user.username }`; + let title = UI_Use_Real_Name && user.name ? user.name : `@${ user.username }`; if (room.t !== 'd' && room.name) { title += ` @ #${ room.name }`; } @@ -116,6 +116,27 @@ function messageContainsHighlight(message, highlights) { return has; } +/** + * Detects the agents to be notified. + * This is essential for guest pool routing configuration. + * We'll only notify online agents. + * If the configuration allows to leave a message even if offline, the agents returning + * to the online status have to check the queue manually - which should be good practice + // + * @param {*} room The livechat room + */ +function getLivechatAgentIdsForRoom(room) { + const inquiry = RocketChat.models.LivechatInquiry.findOneByRoomId(room._id); + const department = inquiry ? inquiry.department : null; + let agents = []; + if (department) { + agents = RocketChat.models.LivechatDepartmentAgents.getOnlineForDepartment(department).fetch(); + } else { + agents = RocketChat.models.Users.findOnlineAgents().fetch(); + } + return agents.map((agent)=>agent.agentId); +} + function getBadgeCount(userId) { const subscriptions = RocketChat.models.Subscriptions.findUnreadByUserId(userId).fetch(); @@ -440,6 +461,15 @@ RocketChat.callbacks.add('afterSaveMessage', function(message, room, userId) { userIdsToPushNotify = userIdsToPushNotify.concat(highlightsIds); } + // for live chat rooms which are configured as guest pool, all agents + // of the targeted department need to be notified + if (room.t === 'l' && !room.servedBy) { + const agentIds = getLivechatAgentIdsForRoom(room); + userIdsForAudio = userIdsForAudio.concat(agentIds); + userIdsToNotify = userIdsToNotify.concat(agentIds); + userIdsToPushNotify = userIdsToPushNotify.concat(agentIds); + } + userIdsToNotify = _.without(_.compact(_.unique(userIdsToNotify)), message.u._id); userIdsToPushNotify = _.without(_.compact(_.unique(userIdsToPushNotify)), message.u._id); userIdsForAudio = _.without(_.compact(_.unique(userIdsForAudio)), message.u._id); diff --git a/packages/rocketchat-livechat/server/models/LivechatInquiry.js b/packages/rocketchat-livechat/server/models/LivechatInquiry.js index 197bb4dbaf57..89234c52dd64 100644 --- a/packages/rocketchat-livechat/server/models/LivechatInquiry.js +++ b/packages/rocketchat-livechat/server/models/LivechatInquiry.js @@ -7,8 +7,8 @@ class LivechatInquiry extends RocketChat.models._Base { this.tryEnsureIndex({ 'message': 1 }); // message sent by the client this.tryEnsureIndex({ 'ts': 1 }); // timestamp this.tryEnsureIndex({ 'code': 1 }); // (for routing) - this.tryEnsureIndex({ 'agents': 1}); // Id's of the agents who can see the inquiry (handle departments) - this.tryEnsureIndex({ 'status': 1}); // 'open', 'taken' + this.tryEnsureIndex({ 'agents': 1 }); // Id's of the agents who can see the inquiry (handle departments) + this.tryEnsureIndex({ 'status': 1 }); // 'open', 'taken' } findOneById(inquiryId) { @@ -58,7 +58,7 @@ class LivechatInquiry extends RocketChat.models._Base { * return the status of the inquiry (open or taken) */ getStatus(inquiryId) { - return this.findOne({'_id': inquiryId}).status; + return this.findOne({ '_id': inquiryId }).status; } updateVisitorStatus(token, status) { @@ -75,6 +75,10 @@ class LivechatInquiry extends RocketChat.models._Base { return this.update(query, update); } + + findOneByRoomId(roomId) { + return this.findOne({ rid: roomId }); + } } RocketChat.models.LivechatInquiry = new LivechatInquiry(); From 904d42c1050e1fe6e01d1390b0590d06534a3cbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20J=C3=A4gle?= Date: Wed, 25 Apr 2018 22:26:04 +0200 Subject: [PATCH 2/2] Fix livechat notifications for queues without departments --- .../server/lib/sendNotificationsOnMessage.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js index e65060ba3444..e6709025eeef 100644 --- a/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js +++ b/packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js @@ -128,13 +128,13 @@ function messageContainsHighlight(message, highlights) { function getLivechatAgentIdsForRoom(room) { const inquiry = RocketChat.models.LivechatInquiry.findOneByRoomId(room._id); const department = inquiry ? inquiry.department : null; - let agents = []; + let agentIds = []; if (department) { - agents = RocketChat.models.LivechatDepartmentAgents.getOnlineForDepartment(department).fetch(); + agentIds = RocketChat.models.LivechatDepartmentAgents.getOnlineForDepartment(department).fetch().map((agent)=>agent.agentId); } else { - agents = RocketChat.models.Users.findOnlineAgents().fetch(); + agentIds = RocketChat.models.Users.findOnlineAgents().fetch().map((user)=>user._id); } - return agents.map((agent)=>agent.agentId); + return agentIds; } function getBadgeCount(userId) {