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

Fix livechat notifications for guest pool based routing #327

Merged
merged 2 commits into from
Jun 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 31 additions & 1 deletion packages/rocketchat-lib/server/lib/sendNotificationsOnMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }`;
}
Expand Down Expand Up @@ -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 agentIds = [];
if (department) {
agentIds = RocketChat.models.LivechatDepartmentAgents.getOnlineForDepartment(department).fetch().map((agent)=>agent.agentId);
} else {
agentIds = RocketChat.models.Users.findOnlineAgents().fetch().map((user)=>user._id);
}
return agentIds;
}

function getBadgeCount(userId) {
const subscriptions = RocketChat.models.Subscriptions.findUnreadByUserId(userId).fetch();

Expand Down Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions packages/rocketchat-livechat/server/models/LivechatInquiry.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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();