Skip to content

Commit

Permalink
[NEW] Additional Livechat iFrame API's (#10918)
Browse files Browse the repository at this point in the history
  • Loading branch information
renatobecker authored and sampaiodiego committed Jul 6, 2018
1 parent 22d1cae commit 8014455
Show file tree
Hide file tree
Showing 12 changed files with 203 additions and 25 deletions.
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

0 comments on commit 8014455

Please sign in to comment.