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 all 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
7 changes: 5 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
28 changes: 28 additions & 0 deletions packages/rocketchat-livechat/.app/client/lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,34 @@ 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') {
return;
}

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

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

Expand Down
5 changes: 5 additions & 0 deletions packages/rocketchat-livechat/.app/client/lib/msgTyping.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const MsgTyping = (function() {
const selfTyping = new ReactiveVar(false);
const usersTyping = {};
const dep = new Tracker.Dependency;
let oldRoom;

const addStream = function(room) {
if (!_.isEmpty(usersTyping[room] && usersTyping[room].users)) {
Expand Down Expand Up @@ -38,7 +39,11 @@ export const MsgTyping = (function() {

Tracker.autorun(() => {
if (visitor.getRoom() && visitor.getId()) {
if (oldRoom) {
Notifications.unRoom(oldRoom, 'typing');
}
addStream(visitor.getRoom());
oldRoom = visitor.getRoom();
}
});

Expand Down
36 changes: 26 additions & 10 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,20 @@ 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 {
const loadDepartments = departments => {
Department.remove({});
departments.forEach((department) => {
Department.insert(department);
});
};

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 +138,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 @@ -146,13 +163,12 @@ Template.livechatWindow.onCreated(function() {
Triggers.setTriggers(result.triggers);
Triggers.init();

result.departments.forEach((department) => {
Department.insert(department);
});
loadDepartments(result.departments);

Livechat.allowSwitchingDepartments = result.allowSwitchingDepartments;

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

$(window).on('focus', () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/rocketchat-livechat/.app/client/views/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
</p>
<form id="livechat-registration">
{{#if showNameFieldRegisterForm}}
<input type="text" name="name" id="guestName" placeholder="{{_ "Name"}}">
{{/if}}
<input type="text" name="name" id="guestName" placeholder="{{_ "Name"}}" value={{getName}}>
{{/if}}
{{#if showEmailFieldRegisterForm}}
<input type="email" name="email" id="guestEmail" placeholder="{{_ "Email"}}">
{{/if}}
<input type="email" name="email" id="guestEmail" placeholder="{{_ "Email"}}" value={{getEmail}}>
{{/if}}
{{#if showDepartments}}
<select name="department">
<option value="">{{_ "Select_a_department"}}</option>
Expand Down
16 changes: 11 additions & 5 deletions packages/rocketchat-livechat/.app/client/views/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@ Template.register.helpers({
},
showEmailFieldRegisterForm() {
return Livechat.emailFieldRegistrationForm;
}
},
getName() {
return Livechat.guestName;
},
getEmail() {
return Livechat.guestEmail;
}
});

Template.register.events({
Expand All @@ -41,10 +47,10 @@ 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 Expand Up @@ -106,7 +112,7 @@ Template.register.onCreated(function() {

return valid;
};

this.showError = (msg) => {
$('.error').addClass('show');
this.error.set(msg);
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() {
msgStream.unsubscribe(this.roomSubscribed);

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;
},

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()) {
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
};
}
});
Loading