From 801445594217f6866c8996a521ff03027221a681 Mon Sep 17 00:00:00 2001 From: Renato Becker Date: Fri, 6 Jul 2018 17:37:49 -0300 Subject: [PATCH] [NEW] Additional Livechat iFrame API's (#10918) --- package-lock.json | 7 +- .../.app/client/lib/_livechat.js | 15 ++++ .../.app/client/lib/chatMessages.js | 8 +++ .../.app/client/lib/hooks.js | 28 ++++++++ .../.app/client/lib/msgTyping.js | 5 ++ .../.app/client/views/livechatWindow.js | 36 +++++++--- .../.app/client/views/register.html | 8 +-- .../.app/client/views/register.js | 16 +++-- .../.app/imports/client/visitor.js | 72 ++++++++++++++++++- .../assets/rocket-livechat.js | 20 ++++++ .../server/methods/loginByToken.js | 6 +- .../server/methods/registerGuest.js | 7 ++ 12 files changed, 203 insertions(+), 25 deletions(-) diff --git a/package-lock.json b/package-lock.json index 1587fdbdda34..bcecc3b632dc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -4481,6 +4481,7 @@ "version": "0.1.7", "resolved": "https://registry.npmjs.org/errno/-/errno-0.1.7.tgz", "integrity": "sha512-MfrRBDWzIWifgq6tJj60gkAwtLNb6sQPlcFrSOflcP1aFmmruKQ2wRnze/8V6kgyz7H3FF8Npzv78mZ7XLLflg==", + "optional": true, "requires": { "prr": "1.0.1" } @@ -9490,7 +9491,8 @@ "natives": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/natives/-/natives-1.1.4.tgz", - "integrity": "sha512-Q29yeg9aFKwhLVdkTAejM/HvYG0Y1Am1+HUkFQGn5k2j8GS+v60TVmZh6nujpEAj/qql+wGUrlryO8bF+b1jEg==" + "integrity": "sha512-Q29yeg9aFKwhLVdkTAejM/HvYG0Y1Am1+HUkFQGn5k2j8GS+v60TVmZh6nujpEAj/qql+wGUrlryO8bF+b1jEg==", + "optional": true }, "natural-compare": { "version": "1.4.0", @@ -11222,7 +11224,8 @@ "prr": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/prr/-/prr-1.0.1.tgz", - "integrity": "sha1-0/wRS6BplaRexok/SEzrHXj19HY=" + "integrity": "sha1-0/wRS6BplaRexok/SEzrHXj19HY=", + "optional": true }, "pseudomap": { "version": "1.0.2", diff --git a/packages/rocketchat-livechat/.app/client/lib/_livechat.js b/packages/rocketchat-livechat/.app/client/lib/_livechat.js index 541af4c2a826..13aae330ea16 100644 --- a/packages/rocketchat-livechat/.app/client/lib/_livechat.js +++ b/packages/rocketchat-livechat/.app/client/lib/_livechat.js @@ -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()); @@ -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); @@ -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); diff --git a/packages/rocketchat-livechat/.app/client/lib/chatMessages.js b/packages/rocketchat-livechat/.app/client/lib/chatMessages.js index 245e998e5e98..6797cace30df 100644 --- a/packages/rocketchat-livechat/.app/client/lib/chatMessages.js +++ b/packages/rocketchat-livechat/.app/client/lib/chatMessages.js @@ -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); diff --git a/packages/rocketchat-livechat/.app/client/lib/hooks.js b/packages/rocketchat-livechat/.app/client/lib/hooks.js index 89436156dc19..93ba1a6f11c6 100644 --- a/packages/rocketchat-livechat/.app/client/lib/hooks.js +++ b/packages/rocketchat-livechat/.app/client/lib/hooks.js @@ -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); + } + }); } }; diff --git a/packages/rocketchat-livechat/.app/client/lib/msgTyping.js b/packages/rocketchat-livechat/.app/client/lib/msgTyping.js index 378af82a34aa..addcfa608ef4 100644 --- a/packages/rocketchat-livechat/.app/client/lib/msgTyping.js +++ b/packages/rocketchat-livechat/.app/client/lib/msgTyping.js @@ -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)) { @@ -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(); } }); diff --git a/packages/rocketchat-livechat/.app/client/views/livechatWindow.js b/packages/rocketchat-livechat/.app/client/views/livechatWindow.js index 414bd2cdf9a1..07aaa0912f9c 100644 --- a/packages/rocketchat-livechat/.app/client/views/livechatWindow.js +++ b/packages/rocketchat-livechat/.app/client/views/livechatWindow.js @@ -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; @@ -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'); @@ -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) { @@ -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', () => { diff --git a/packages/rocketchat-livechat/.app/client/views/register.html b/packages/rocketchat-livechat/.app/client/views/register.html index 142ca7637593..1e3e533b7e34 100644 --- a/packages/rocketchat-livechat/.app/client/views/register.html +++ b/packages/rocketchat-livechat/.app/client/views/register.html @@ -9,11 +9,11 @@

{{#if showNameFieldRegisterForm}} - - {{/if}} + + {{/if}} {{#if showEmailFieldRegisterForm}} - - {{/if}} + + {{/if}} {{#if showDepartments}}