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