From bb6cfb6a546a566e3a0de82d3b6abcaea050a493 Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Mon, 11 Jun 2018 12:02:13 -0300 Subject: [PATCH] [NEW] Disconnect users from websocked when away from the login screen by 10min --- .../client/lib/LoginPresence.js | 52 +++++++++++++++++++ packages/rocketchat-lib/client/lib/index.js | 4 +- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 packages/rocketchat-lib/client/lib/LoginPresence.js diff --git a/packages/rocketchat-lib/client/lib/LoginPresence.js b/packages/rocketchat-lib/client/lib/LoginPresence.js new file mode 100644 index 000000000000..8f251024ea84 --- /dev/null +++ b/packages/rocketchat-lib/client/lib/LoginPresence.js @@ -0,0 +1,52 @@ +export const LoginPresence = { + awayTime: 600000, //10 minutes + started: false, + + startTimer() { + LoginPresence.stopTimer(); + if (!LoginPresence.awayTime) { + return; + } + this.timer = setTimeout(LoginPresence.disconnect, LoginPresence.awayTime); + }, + stopTimer() { + clearTimeout(this.timer); + }, + disconnect() { + const status = Meteor.status(); + if (status && status.status !== 'offline') { + if (!Meteor.userId() && RocketChat.settings.get('Accounts_AllowAnonymousRead') !== true) { + Meteor.disconnect(); + } + } + LoginPresence.stopTimer(); + }, + connect() { + const status = Meteor.status(); + if (status && status.status === 'offline') { + Meteor.reconnect(); + } + }, + start() { + if (LoginPresence.started) { + return; + } + + window.addEventListener('focus', () => { + LoginPresence.stopTimer(); + LoginPresence.connect(); + }); + + window.addEventListener('blur', () => { + LoginPresence.startTimer(); + }); + + if (!window.document.hasFocus()) { + LoginPresence.startTimer(); + } + + LoginPresence.started = true; + } +}; + +LoginPresence.start(); diff --git a/packages/rocketchat-lib/client/lib/index.js b/packages/rocketchat-lib/client/lib/index.js index c67c462f235f..a14a2a86a945 100644 --- a/packages/rocketchat-lib/client/lib/index.js +++ b/packages/rocketchat-lib/client/lib/index.js @@ -11,6 +11,7 @@ import { RocketChatAnnouncement } from './RocketChatAnnouncement'; import { RoomSettingsEnum, RoomTypeConfig, RoomTypeRouteConfig, UiTextContext } from '../../lib/RoomTypeConfig'; import { hide, leave, erase } from './ChannelActions'; import { call } from './callMethod'; +import { LoginPresence } from './LoginPresence'; export { call, @@ -22,5 +23,6 @@ export { RoomTypeConfig, RoomTypeRouteConfig, UiTextContext, - RocketChatAnnouncement + RocketChatAnnouncement, + LoginPresence };