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] Additional Livechat iFrame API's #10918

Merged
merged 22 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
8be9ee2
Initial implementation of new Livechat iFrame API's.
renatobecker May 28, 2018
46a3c81
New Livechat iFrame API's added.
renatobecker May 28, 2018
5e3dcbb
Fix trailing-spaces issue.
renatobecker May 29, 2018
c25e005
Fix trailing-spaces.
renatobecker May 29, 2018
da07919
Merge branch 'develop' into livechat-additional-iframe-apis
renatobecker Jun 1, 2018
30bc7c3
Allows you to create / change visitor without passing a token.
renatobecker Jun 1, 2018
fd64d4e
Merge branch 'develop' into livechat-additional-iframe-apis
renatobecker Jun 15, 2018
e098d94
Fixed conflit in package-lock.json file.
renatobecker Jun 15, 2018
72a68e1
Merge branch 'develop' into livechat-additional-iframe-apis
sampaiodiego Jun 18, 2018
4421fb5
Return on error
sampaiodiego Jun 18, 2018
f1180d0
Merge branch 'develop' into livechat-additional-iframe-apis
renatobecker Jun 20, 2018
de54388
Merge branch 'livechat-additional-iframe-apis' of github.com:RocketCh…
renatobecker Jun 20, 2018
6f4e5d1
code style improvements.
renatobecker Jun 20, 2018
15f0e48
Fixes and improvements in Livechat API.
renatobecker Jun 21, 2018
09df51b
Merge branch 'develop' into livechat-additional-iframe-apis
renatobecker Jun 21, 2018
3f9457d
Merge branch 'develop' into livechat-additional-iframe-apis
renatobecker Jun 25, 2018
dec7caf
Merge branch 'develop' into livechat-additional-iframe-apis
renatobecker Jul 3, 2018
8f62e75
Removed ReactiveVar on register helper.
renatobecker Jul 3, 2018
8017a4a
Fixed Livechat agent data
renatobecker Jul 4, 2018
a776d01
Merge branch 'develop' into livechat-additional-iframe-apis
renatobecker Jul 4, 2018
5a05cd7
Merge branch 'develop' into livechat-additional-iframe-apis
renatobecker Jul 4, 2018
03cad51
Updating room/subscription informations when the visitor data is upda…
renatobecker Jul 4, 2018
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
15 changes: 15 additions & 0 deletions packages/rocketchat-livechat/.app/client/lib/_livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ this.Livechat = new (class Livechat {

this.stream = new Meteor.Streamer('livechat-room');

this._guestName = new ReactiveVar();
this._guestEmail = new ReactiveVar();

Tracker.autorun(() => {
if (this._room.get() && visitor.getId()) {
RoomHistoryManager.getMoreIfIsEmpty(this._room.get());
Expand Down Expand Up @@ -121,6 +124,12 @@ this.Livechat = new (class Livechat {
get agent() {
return this._agent.get();
}
get guestName() {
return this._guestName.get();
}
get guestEmail() {
return this._guestEmail.get();
}

set online(value) {
this._online.set(value);
Expand Down Expand Up @@ -198,6 +207,12 @@ this.Livechat = new (class Livechat {
set agent(agentData) {
this._agent.set(agentData);
}
set guestName(name) {
return this._guestName.set(name);
}
set guestEmail(email) {
return this._guestEmail.set(email);
}

ready() {
this._ready.set(true);
Expand Down
8 changes: 8 additions & 0 deletions packages/rocketchat-livechat/.app/client/lib/chatMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ this.ChatMessages = class ChatMessages {
guest.department = Livechat.department;
}

if (Livechat.guestName) {
guest.name = Livechat.guestName;
}

if (Livechat.guestEmail) {
guest.email = Livechat.guestEmail;
}

Meteor.call('livechat:registerGuest', guest, (error, result) => {
if (error) {
return showError(error.reason);
Expand Down
30 changes: 29 additions & 1 deletion packages/rocketchat-livechat/.app/client/lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,35 @@ const api = {

widgetClosed() {
Livechat.setWidgetClosed();
}
},

setGuestToken(token) {
visitor.setToken(token);
},

setGuestName(name) {
visitor.setName(name);
},

setGuestEmail(email) {
visitor.setEmail(email);
},

registerGuest(data) {
if (!(typeof data === 'object')) {
Copy link
Member

Choose a reason for hiding this comment

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

please change to if (typeof data !== 'object') { to help readability

return;
}

if (!data.token) {
data.token = Random.id();
}

Meteor.call('livechat:registerGuest', data, function(error, result) {
if (!error) {
visitor.setToken(result.token);
}
});
}
};

window.addEventListener('message', function(msg) {
Expand Down
24 changes: 17 additions & 7 deletions packages/rocketchat-livechat/.app/client/views/livechatWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import visitor from '../../imports/client/visitor';
function showDepartments() {
return Department.find({ showOnRegistration: true }).count() > 1;
};

Template.livechatWindow.helpers({
title() {
return Livechat.title;
Expand Down Expand Up @@ -90,11 +90,13 @@ Template.livechatWindow.onCreated(function() {
return lng;
};

// get all needed live chat info for the user
Meteor.call('livechat:getInitialData', visitor.getToken(), (err, result) => {
if (err) {
console.error(err);
} else {
this.autorun(() => {
// get all needed live chat info for the user
Meteor.call('livechat:getInitialData', visitor.getToken(), (err, result) => {
if (err) {
return console.error(err);
}

if (!result.enabled) {
Triggers.setDisabled();
return parentCall('removeWidget');
Expand Down Expand Up @@ -129,6 +131,14 @@ Template.livechatWindow.onCreated(function() {

if (result.visitor) {
visitor.setData(result.visitor);

if (visitor.name) {
Livechat.guestName = visitor.name;
}

if (visitor.visitorEmails && visitor.visitorEmails.length > 0) {
Livechat.guestEmail = visitor.visitorEmails[0].address;
}
}

if (result.agentData) {
Expand All @@ -152,7 +162,7 @@ Template.livechatWindow.onCreated(function() {
Livechat.allowSwitchingDepartments = result.allowSwitchingDepartments;

Livechat.ready();
}
});
});

$(window).on('focus', () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/rocketchat-livechat/.app/client/views/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Template.messages.helpers({
};
},
agentData() {
const agent = Livechat.agent;
const agent = Template.instance().agentData.get();
Copy link
Member

Choose a reason for hiding this comment

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

I think this is the same case as Livechat.guestName I've talked before (sry for not have seen this before). Any reason to use a ReactiveVar here instead of keeping Livechat. agent ?

if (!agent) {
return null;
}
Expand Down Expand Up @@ -137,6 +137,12 @@ Template.messages.onCreated(function() {
this.atBottom = true;

this.showOptions = new ReactiveVar(false);
this.agentData = new ReactiveVar(null);

this.autorun(() => {
const agentData = Livechat.agent;
this.agentData.set(agentData);
});

this.updateMessageInputHeight = function(input) {
// Inital height is 28. If the scrollHeight is greater than that( we have more text than area ),
Expand Down
4 changes: 2 additions & 2 deletions packages/rocketchat-livechat/.app/client/views/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ Template.register.events({
const form = e.currentTarget;

const fields = [];
let name;
let email;
let name = Livechat.guestName;
let email = Livechat.guestEmail;

if (Livechat.nameFieldRegistrationForm) {
fields.push('name');
Expand Down
72 changes: 71 additions & 1 deletion packages/rocketchat-livechat/.app/imports/client/visitor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* globals Commands */
/* globals Commands, Livechat */
const msgStream = new Meteor.Streamer('room-messages');

export default {
Expand All @@ -18,6 +18,23 @@ export default {
this.token.set(localStorage.getItem('visitorToken'));
},

reset() {
this.id.set(null);
this.token.set(null);
this.room.set(null);
this.data.set(null);
this.roomToSubscribe.set(null);
this.roomSubscribed = null;

Livechat.room = null;
Livechat.department = null;
Livechat.agent = null;
Livechat.guestName = null;
Livechat.guestEmail = null;

msgStream.unsubscribe('room-messages');
Copy link
Member

Choose a reason for hiding this comment

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

this does not have the desired effect. on my tests after changing the token using setGuestToken, I'm still receiving messages from the old room/token and being notified as well.

},

getId() {
return this.id.get();
},
Expand All @@ -38,6 +55,59 @@ export default {
return this.token.get();
},

setToken(token) {
if ((!token) || (token == this.token.get())) {
Copy link
Member

Choose a reason for hiding this comment

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

you don't need to use this much parenthesis.. without them the code looks much clearer:

if (!token || token === this.token.get()) {

return;
}

this.reset();

localStorage.setItem('visitorToken', token);
this.token.set(token);

Meteor.call('livechat:loginByToken', token, (err, result) => {

if (!result) {
return;
}

if (result._id) {
this.setId(result._id);
return result._id;
}
});
},

setName(name) {
Livechat.guestName = name;

if (!this.getId()) {
return;
}

const data = {
token: this.getToken(),
name
};

Meteor.call('livechat:registerGuest', data);
},

setEmail(email) {
Livechat.guestEmail = email;

if (!this.getId()) {
return;
}

const data = {
token: this.getToken(),
email
};

Meteor.call('livechat:registerGuest', data);
},

setRoom(rid) {
this.room.set(rid);
},
Expand Down
20 changes: 20 additions & 0 deletions packages/rocketchat-livechat/assets/rocket-livechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,22 @@
callHook('setDepartment', department);
};

var setGuestToken = function(token) {
callHook('setGuestToken', token);
};

var setGuestName = function(name) {
callHook('setGuestName', name);
};

var setGuestEmail = function(email) {
callHook('setGuestEmail', email);
};

var registerGuest = function(guest) {
callHook('registerGuest', guest);
};

var clearDepartment = function() {
callHook('clearDepartment');
};
Expand Down Expand Up @@ -722,6 +738,10 @@
setTheme: setTheme,
setDepartment: setDepartment,
clearDepartment: clearDepartment,
setGuestToken: setGuestToken,
setGuestName: setGuestName,
setGuestEmail: setGuestEmail,
registerGuest: registerGuest,

// callbacks
onChatMaximized: function(fn) { registerCallback('chat-maximized', fn); },
Expand Down
6 changes: 3 additions & 3 deletions packages/rocketchat-livechat/server/methods/loginByToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import LivechatVisitors from '../models/LivechatVisitors';

Meteor.methods({
'livechat:loginByToken'(token) {
const user = LivechatVisitors.getVisitorByToken(token, { fields: { _id: 1 } });
const visitor = LivechatVisitors.getVisitorByToken(token, { fields: { _id: 1 } });

if (!user) {
if (!visitor) {
return;
}

return {
_id: user._id
_id: visitor._id
};
}
});
3 changes: 2 additions & 1 deletion packages/rocketchat-livechat/server/methods/registerGuest.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Meteor.methods({
RocketChat.models.Messages.keepHistoryForToken(token);

return {
userId
userId,
token
Copy link
Member

Choose a reason for hiding this comment

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

on pull request #10767 this method will return the whole visitor object, I think you can to the same here (it should avoid future conflicts)

};
}
});