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

[NEW] Add impersonate option for livechat triggers #9107

Merged
merged 4 commits into from
Dec 27, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 4 additions & 1 deletion packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,7 @@
"Current_Chats": "Current Chats",
"Current_Status": "Current Status",
"Custom": "Custom",
"Custom_agent": "Custom agent",
"Custom_Emoji": "Custom Emoji",
"Custom_Emoji_Add": "Add New Emoji",
"Custom_Emoji_Added_Successfully": "Custom emoji added successfully",
Expand Down Expand Up @@ -822,6 +823,7 @@
"Iframe_Integration_send_target_origin_Description": "Origin with protocol prefix, which commands are sent to e.g. 'https://localhost', or * to allow sending to anywhere.",
"IMAP_intercepter_already_running": "IMAP intercepter already running",
"IMAP_intercepter_Not_running": "IMAP intercepter Not running",
"Impersonate_next_agent_from_queue": "Impersonate next agent from queue",
"Impersonate_user": "Impersonate User",
"Impersonate_user_description": "When enabled, integration posts as the user that triggered integration",
"Import": "Import",
Expand Down Expand Up @@ -1606,6 +1608,7 @@
"Select_a_department": "Select a department",
"Select_a_user": "Select a user",
"Select_an_avatar": "Select an avatar",
"Select_an_option": "Select an option",
"Select_file": "Select file",
"Select_role": "Select a Role",
"Select_service_to_login": "Select a service to login to load your picture or upload one directly from your computer",
Expand Down Expand Up @@ -2122,4 +2125,4 @@
"your_message_optional": "your message (optional)",
"Your_password_is_wrong": "Your password is wrong!",
"Your_push_was_sent_to_s_devices": "Your push was sent to %s devices"
}
}
11 changes: 10 additions & 1 deletion packages/rocketchat-livechat/app/client/lib/chatMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,16 @@ this.ChatMessages = class ChatMessages {
};
MsgTyping.stop(rid);

Meteor.call('sendMessageLivechat', msgObject, (error, result) => {
let agent;
const currentAgent = !visitor.roomSubscribed && Livechat.agent;
if (currentAgent) {
agent = {
_id: currentAgent._id,
username: currentAgent.username
};
}

Meteor.call('sendMessageLivechat', msgObject, agent, (error, result) => {
if (error) {
ChatMessage.update(msgObject._id, { $set: { error: true } });
showError(error.reason);
Expand Down
70 changes: 57 additions & 13 deletions packages/rocketchat-livechat/app/client/lib/triggers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
/* globals Livechat */

function getAgent(triggerAction) {
return new Promise((resolve, reject) => {
const { params } = triggerAction;
if (params.sender === 'queue') {
const cache = localStorage.getItem('triggerAgent');
if (cache) {
const cacheAgent = JSON.parse(cache);

// cache valid for 1h
if (cacheAgent.ts && Date.now() - cacheAgent.ts < 3600000) {
return resolve(cacheAgent.agent);
}
}

Meteor.call('livechat:getNextAgent', {
token: visitor.getToken(),
department: Livechat.department
}, (error, result) => {
if (error) {
return reject(error);
}
localStorage.setItem('triggerAgent', JSON.stringify({
agent: result,
ts: Date.now()
}));

resolve(result);
});
} else if (params.sender === 'custom') {
resolve({
username: params.name
});
} else {
reject('Unknown sender');
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 it will never resolve if(params.sender !== 'custom')

});
}

this.Triggers = (function() {
let triggers = [];
let initiated = false;
Expand All @@ -13,23 +53,27 @@ this.Triggers = (function() {
// flag to skip the trigger if the action is 'send-message'
trigger.skip = true;

let roomId = visitor.getRoom();
getAgent(action).then((agent) => {
let roomId = visitor.getRoom();

if (!roomId) {
roomId = Random.id();
visitor.setRoom(roomId);
}
if (!roomId) {
roomId = Random.id();
visitor.setRoom(roomId);
}

Session.set('triggered', true);
ChatMessage.insert({
msg: action.params.msg,
rid: roomId,
u: agent
});

Session.set('triggered', true);
ChatMessage.insert({
msg: action.params.msg,
rid: roomId,
u: {
username: action.params.name
if (agent._id) {
Livechat.agent = agent;
}
});

parentCall('openWidget');
parentCall('openWidget');
});
}
});
};
Expand Down
6 changes: 4 additions & 2 deletions packages/rocketchat-livechat/client/stylesheets/livechat.less
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
.trigger-value {
width: 70%;

input {
display: inline-block !important;
textarea, input, select {
display: block !important;
width: auto !important;
margin-bottom: 4px;
min-width: 50%;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@ Template.livechatTriggersForm.events({

$('.each-action').each(function() {
if ($('.trigger-action', this).val() === 'send-message') {
const params = {
sender: $('[name=send-message-sender]', this).val(),
msg: $('[name=send-message-msg]', this).val()
};
if (params.sender === 'custom') {
params.name = $('[name=send-message-name]', this).val();
}
data.actions.push({
name: $('.trigger-action', this).val(),
params: {
name: $('[name=send-message-name]', this).val(),
msg: $('[name=send-message-msg]', this).val()
}
params
});
} else {
data.actions.push({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
</div>
<div class="trigger-value">
<div class="send-message {{hiddenValue 'send-message'}}">
<input type="text" name="send-message-name" placeholder="{{_ "Name_of_agent"}}" value="{{params.name}}" size="15">
<input type="text" name="send-message-msg" placeholder="{{_ "Message"}}" value="{{params.msg}}">
<select name="send-message-sender">
<option value="">{{_ "Select_an_option"}}</option>
<option value="queue" selected="{{senderSelected 'queue'}}" disabled="{{disableIfGuestPool}}">{{_ "Impersonate_next_agent_from_queue"}}</option>
<option value="custom" selected="{{senderSelected 'custom'}}">{{_ "Custom_agent"}}</option>
</select>
<input type="text" name="send-message-name" placeholder="{{_ "Name_of_agent"}}" value="{{params.name}}" class="{{showCustomName}}">
<textarea name="send-message-msg" placeholder="{{_ "Message"}}" rows="3">{{params.msg}}</textarea>
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,35 @@ Template.livechatTriggerAction.helpers({
} else if (this.name !== current) {
return 'hidden';
}
},
showCustomName() {
return Template.instance().sender.get() === 'custom' ? '' : 'hidden';
},
senderSelected(current) {
return this.params && this.params.sender === current ? true : false;
},
disableIfGuestPool() {
return RocketChat.settings.get('Livechat_Routing_Method') === 'Guest_Pool';
}
});

Template.livechatTriggerAction.events({
'change .trigger-action'(e, instance) {
instance.$('.trigger-action-value ').addClass('hidden');
instance.$(`.${ e.currentTarget.value }`).removeClass('hidden');
},
'change [name=send-message-sender]'(e, instance) {
instance.sender.set(e.currentTarget.value);
}
});

Template.livechatTriggerAction.onCreated(function() {
this.firstAction = true;

this.sender = new ReactiveVar('');

const data = Template.currentData();
if (data && data.name === 'send-message') {
this.sender.set(data.params.sender);
}
});
1 change: 1 addition & 0 deletions packages/rocketchat-livechat/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ Package.onUse(function(api) {
api.addFiles('server/methods/getCustomFields.js', 'server');
api.addFiles('server/methods/getAgentData.js', 'server');
api.addFiles('server/methods/getInitialData.js', 'server');
api.addFiles('server/methods/getNextAgent.js', 'server');
api.addFiles('server/methods/loginByToken.js', 'server');
api.addFiles('server/methods/pageVisited.js', 'server');
api.addFiles('server/methods/registerGuest.js', 'server');
Expand Down
35 changes: 23 additions & 12 deletions packages/rocketchat-livechat/server/lib/Livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,21 @@ RocketChat.Livechat = {
return RocketChat.models.Users.findOnlineAgents();
}
},
getRoom(guest, message, roomInfo) {
getRequiredDepartment(onlineRequired = true) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 something like:

const departments = RocketChat.models.LivechatDepartment.findEnabledWithAgents();
 return departments.fetch().find(dept => {
 	if (!dept.showOnRegistration) { return false; }
 	if (!onlineRequired) { return true; }
 	const onlineAgents = RocketChat.models.LivechatDepartmentAgents.getOnlineForDepartment(dept._id);
 	return onlineAgents.count() > 0
 });

const departments = RocketChat.models.LivechatDepartment.findEnabledWithAgents();

return departments.fetch().find((dept) => {
if (!dept.showOnRegistration) {
return false;
}
if (!onlineRequired) {
return true;
}
const onlineAgents = RocketChat.models.LivechatDepartmentAgents.getOnlineForDepartment(dept._id);
return onlineAgents.count() > 0;
});
},
getRoom(guest, message, roomInfo, agent) {
let room = RocketChat.models.Rooms.findOneById(message.rid);
let newRoom = false;

Expand All @@ -44,20 +58,17 @@ RocketChat.Livechat = {

if (room == null) {
// if no department selected verify if there is at least one active and pick the first
if (!guest.department) {
const departments = RocketChat.models.LivechatDepartment.findEnabledWithAgents();
if (departments.count() > 0) {
departments.forEach((dept) => {
if (!guest.department && dept.showOnRegistration) {
guest.department = dept._id;
}
});
if (!agent && !guest.department) {
const department = this.getRequiredDepartment();

if (department) {
guest.department = department._id;
}
}

// delegate room creation to QueueMethods
const routingMethod = RocketChat.settings.get('Livechat_Routing_Method');
room = RocketChat.QueueMethods[routingMethod](guest, message, roomInfo);
room = RocketChat.QueueMethods[routingMethod](guest, message, roomInfo, agent);

newRoom = true;
} else {
Expand All @@ -69,8 +80,8 @@ RocketChat.Livechat = {

return { room, newRoom };
},
sendMessage({ guest, message, roomInfo }) {
const { room, newRoom } = this.getRoom(guest, message, roomInfo);
sendMessage({ guest, message, roomInfo, agent }) {
const { room, newRoom } = this.getRoom(guest, message, roomInfo, agent);
if (guest.name) {
message.alias = guest.name;
}
Expand Down
8 changes: 5 additions & 3 deletions packages/rocketchat-livechat/server/lib/QueueMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ RocketChat.QueueMethods = {
* default method where the agent with the least number
* of open chats is paired with the incoming livechat
*/
'Least_Amount'(guest, message, roomInfo) {
const agent = RocketChat.Livechat.getNextAgent(guest.department);
'Least_Amount'(guest, message, roomInfo, agent) {
if (!agent) {
throw new Meteor.Error('no-agent-online', 'Sorry, no online agents');
agent = RocketChat.Livechat.getNextAgent(guest.department);
if (!agent) {
throw new Meteor.Error('no-agent-online', 'Sorry, no online agents');
}
}

const roomCode = RocketChat.models.Rooms.getNextLivechatRoomCode();
Expand Down
25 changes: 25 additions & 0 deletions packages/rocketchat-livechat/server/methods/getNextAgent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Meteor.methods({
'livechat:getNextAgent'({ token, department }) {
check(token, String);

const room = RocketChat.models.Rooms.findOpenByVisitorToken(token).fetch();

if (room && room.length > 0) {
return;
}

if (!department) {
const requireDeparment = RocketChat.Livechat.getRequiredDepartment();
if (requireDeparment) {
department = requireDeparment._id;
}
}

const agent = RocketChat.Livechat.getNextAgent(department);
if (!agent) {
return;
}

return RocketChat.models.Users.getAgentInfo(agent.agentId);
}
});
17 changes: 15 additions & 2 deletions packages/rocketchat-livechat/server/methods/sendMessageLivechat.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Meteor.methods({
sendMessageLivechat(message) {
sendMessageLivechat(message, agent) {
check(message.rid, String);
check(message.token, String);

Expand All @@ -11,6 +11,19 @@ Meteor.methods({
}
});

return RocketChat.Livechat.sendMessage({ guest, message });
if (agent && agent.username) {
const onlineAgents = RocketChat.models.Users.findOnlineUserFromList(agent.username);

if (onlineAgents.count() === 0) {
agent = null;
} else {
agent = {
agentId: agent._id,
username: agent.username
};
}
}

return RocketChat.Livechat.sendMessage({ guest, message, agent });
}
});