From 612893f88479e4e1d8163859647764387c368611 Mon Sep 17 00:00:00 2001 From: Ritik Srivastava Date: Mon, 27 Dec 2021 10:06:36 +0530 Subject: [PATCH 1/2] new keep alive feature on livechat widget --- app/livechat/server/api/lib/livechat.js | 1 + app/livechat/server/config.js | 8 ++++ app/livechat/server/lib/Livechat.js | 1 + app/livechat/server/methods/getInitialData.js | 1 + .../server/methods/setUpConnection.js | 38 ++++++++++++++++++- packages/rocketchat-i18n/i18n/en.i18n.json | 2 + 6 files changed, 49 insertions(+), 2 deletions(-) diff --git a/app/livechat/server/api/lib/livechat.js b/app/livechat/server/api/lib/livechat.js index 701b5f7f3d259..7cb590380c14f 100644 --- a/app/livechat/server/api/lib/livechat.js +++ b/app/livechat/server/api/lib/livechat.js @@ -171,6 +171,7 @@ export function settings(url) { livechat_kill_switch_message: initSettings.Livechat_kill_switch_message, livechat_friendly_chat: initSettings.Livechat_friendly_chat, livechat_enable_avatar: initSettings.Livechat_enable_avatar, + Livechat_close_room_on_user_disconnect: initSettings.Livechat_close_room_on_user_disconnect, }, theme: { title: initSettings.Livechat_title, diff --git a/app/livechat/server/config.js b/app/livechat/server/config.js index 6234bf1a86a6e..fa2b7b8fd7309 100644 --- a/app/livechat/server/config.js +++ b/app/livechat/server/config.js @@ -28,6 +28,14 @@ Meteor.startup(function() { i18nDescription: 'Livechat_enable_avatar_description', }); + settings.add('Livechat_close_room_on_user_disconnect', true, { + type: 'boolean', + group: 'Omnichannel', + section: 'Livechat', + public: true, + i18nDescription: 'Livechat_close_room_on_user_disconnect_description', + }); + settings.add('Livechat_title', 'Rocket.Chat', { type: 'string', group: 'Omnichannel', diff --git a/app/livechat/server/lib/Livechat.js b/app/livechat/server/lib/Livechat.js index 7584f9cd9b255..caf2eea66ab17 100644 --- a/app/livechat/server/lib/Livechat.js +++ b/app/livechat/server/lib/Livechat.js @@ -533,6 +533,7 @@ export const Livechat = { const rcSettings = {}; Settings.findNotHiddenPublic([ + 'Livechat_close_room_on_user_disconnect', 'Livechat_friendly_chat', 'Livechat_enable_avatar', 'Livechat_title', diff --git a/app/livechat/server/methods/getInitialData.js b/app/livechat/server/methods/getInitialData.js index a00cb996acd85..56f92b3a27a15 100644 --- a/app/livechat/server/methods/getInitialData.js +++ b/app/livechat/server/methods/getInitialData.js @@ -97,6 +97,7 @@ Meteor.methods({ info.livechat_kill_switch_message = initSettings.Livechat_kill_switch_message; info.livechat_friendly_chat = initSettings.Livechat_friendly_chat; info.livechat_enable_avatar = initSettings.Livechat_enable_avatar; + info.Livechat_close_room_on_user_disconnect = initSettings.Livechat_close_room_on_user_disconnect; info.agentData = room && room[0] && room[0].servedBy && Users.getAgentInfo(room[0].servedBy._id); diff --git a/app/livechat/server/methods/setUpConnection.js b/app/livechat/server/methods/setUpConnection.js index 21dd64263ef9b..2121b271a6285 100644 --- a/app/livechat/server/methods/setUpConnection.js +++ b/app/livechat/server/methods/setUpConnection.js @@ -1,20 +1,54 @@ import { Meteor } from 'meteor/meteor'; import { check } from 'meteor/check'; +import { TAPi18n } from 'meteor/rocketchat:tap-i18n'; +import { settings } from '../../../settings'; +import { LivechatRooms, LivechatVisitors, LivechatDepartment } from '../../../models/server/index'; import { Livechat } from '../lib/Livechat'; +const clients = {}; + Meteor.methods({ 'livechat:setUpConnection'(data) { check(data, { token: String, + isMobile: Boolean, }); - const { token } = data; + const { token, isMobile } = data; + clients[token] = clients[token] ? clients[token] + 1 : 1; if (!this.connection.livechatToken) { this.connection.livechatToken = token; + this.connection.isMobile = isMobile; this.connection.onClose(() => { - Livechat.notifyGuestStatusChanged(token, 'offline'); + clients[token] && (clients[token] -= 1); + + const visitor = LivechatVisitors.getVisitorByToken(token); + const room = LivechatRooms.findOneOpenByVisitorToken(token); + const department = LivechatDepartment.findOneByIdOrName(room.departmentId); + const comment = TAPi18n.__('Closed_by_visitor', { lng: (visitor && visitor.language) || settings.get('Language') || 'en' }); + const logOutput = { + rid: room._id, + currentBot: room.servedBy, + currentDepartment: { + id: department._id, + name: department.name, + email: department.email, + }, + isHandedOverFromDialogFlow: !!room?.customFields?.isHandedOverFromDialogFlow, + agentAssigned: !!room?.customFields?.agentAssigned, + }; + console.error('Browser closed by user :', JSON.stringify(logOutput)); + + if (this?.connection?.isMobile) { + return; + } + if (clients[token] === 0 && settings.get('Livechat_close_room_on_user_disconnect')) { + delete clients[token]; + Livechat.notifyGuestStatusChanged(token, 'offline'); + return Livechat.closeRoom({ visitor, room, comment }); + } }); } }, diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 827e7d578ae28..f83974cd5e8fc 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -2588,6 +2588,8 @@ "Livechat": "Livechat", "Livechat_friendly_chat": "Enable Friendly Chat", "Livechat_friendly_chat_description": "Enables friendly chat UI in Livechat widget", + "Livechat_close_room_on_user_disconnect": "Close room when user disconnects", + "Livechat_close_room_on_user_disconnect_description": "Close room when user closes browser or is disconnected", "Livechat_enable_avatar": "Show Avatar", "Livechat_enable_avatar_description": "Show Avatars in Livechat widget", "Livechat_abandoned_rooms_action": "How to handle Visitor Abandonment", From c3f56c15eafcf4b09cfa2fb495e7929ad506519c Mon Sep 17 00:00:00 2001 From: Ritik Srivastava Date: Mon, 27 Dec 2021 10:38:59 +0530 Subject: [PATCH 2/2] Add user disconnect switch to config.ts file --- app/livechat/server/config.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/livechat/server/config.ts b/app/livechat/server/config.ts index 942381c8dd6af..808dacecb2ed5 100644 --- a/app/livechat/server/config.ts +++ b/app/livechat/server/config.ts @@ -66,6 +66,14 @@ Meteor.startup(function() { i18nDescription: 'Livechat_enable_avatar_description', }); + this.add('Livechat_close_room_on_user_disconnect', true, { + type: 'boolean', + group: 'Omnichannel', + section: 'Livechat', + public: true, + i18nDescription: 'Livechat_close_room_on_user_disconnect_description', + }); + this.add('Livechat_title', 'Rocket.Chat', { type: 'string', group: 'Omnichannel',