From 7bb9c00c333bc56825204491a9b84b00b40a729e Mon Sep 17 00:00:00 2001 From: Tasso Evangelista Date: Sun, 31 Mar 2019 22:03:57 -0300 Subject: [PATCH 1/2] Only set user presence as online when auto away detection is disabled --- src/preload/userPresence.js | 50 +++++++++++++++++++++++++------------ 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/src/preload/userPresence.js b/src/preload/userPresence.js index 447a207293..27244baf36 100644 --- a/src/preload/userPresence.js +++ b/src/preload/userPresence.js @@ -2,10 +2,13 @@ import { ipcRenderer } from 'electron'; import { getMeteor, getTracker, getGetUserPreference, getUserPresence } from './rocketChat'; -let maximumIdleTime = 10 * 1000; -const idleDetectionInterval = 1 * 1000; +let idleDetectionInterval; +let maximumIdleTime; +let wasUserPresent = false; +let autoAwayEnabled = false; +let intervalHandler; -function onChangeUserPresence(isUserPresent) { +function setUserPresence(isUserPresent) { const UserPresence = getUserPresence(); if (!UserPresence) { @@ -14,26 +17,31 @@ function onChangeUserPresence(isUserPresent) { if (isUserPresent) { UserPresence.setOnline(); - return; + } else { + UserPresence.setAway(); } - - UserPresence.setAway(); } -let wasUserPresent = false; - function pollUserPresence() { - let isUserPresent = false; + let isUserPresent = true; try { const idleTime = ipcRenderer.sendSync('request-system-idle-time'); isUserPresent = idleTime < maximumIdleTime; + const changed = isUserPresent !== wasUserPresent; + + if (autoAwayEnabled && changed) { + setUserPresence(isUserPresent); + return; + } + + if (!autoAwayEnabled && isUserPresent) { + setUserPresence(isUserPresent); + return; + } } catch (error) { console.error(error); - } - - if (isUserPresent !== wasUserPresent) { - onChangeUserPresence(isUserPresent); + } finally { wasUserPresent = isUserPresent; } } @@ -53,12 +61,22 @@ function handleUserPresence() { } const userId = Meteor.userId(); - if (getUserPreference(userId, 'enableAutoAway')) { + autoAwayEnabled = getUserPreference(userId, 'enableAutoAway'); + + if (autoAwayEnabled) { + idleDetectionInterval = 1 * 1000; maximumIdleTime = (getUserPreference(userId, 'idleTimeLimit') || 300) * 1000; + } else { + idleDetectionInterval = 10 * 1000; + maximumIdleTime = 10 * 1000; + } + + if (intervalHandler) { + clearInterval(intervalHandler); } - }); - setInterval(pollUserPresence, idleDetectionInterval); + intervalHandler = setInterval(pollUserPresence, idleDetectionInterval); + }); } From 153c258af6194e1f82c30aac9de94ac522afd013 Mon Sep 17 00:00:00 2001 From: Tasso Evangelista Date: Tue, 16 Apr 2019 02:12:40 -0300 Subject: [PATCH 2/2] Disable user presence polling for users which disabled auto away --- src/preload/userPresence.js | 106 +++++++++++++++++------------------- 1 file changed, 49 insertions(+), 57 deletions(-) diff --git a/src/preload/userPresence.js b/src/preload/userPresence.js index 27244baf36..5ca1b85aad 100644 --- a/src/preload/userPresence.js +++ b/src/preload/userPresence.js @@ -2,82 +2,74 @@ import { ipcRenderer } from 'electron'; import { getMeteor, getTracker, getGetUserPreference, getUserPresence } from './rocketChat'; -let idleDetectionInterval; -let maximumIdleTime; -let wasUserPresent = false; -let autoAwayEnabled = false; -let intervalHandler; +const pollUserPresence = (UserPresence, maximumIdleTime) => { + let wasUserPresent = false; + + return () => { + let isUserPresent = true; + + try { + const idleTime = ipcRenderer.sendSync('request-system-idle-time'); + isUserPresent = idleTime < maximumIdleTime; + + if (isUserPresent === wasUserPresent) { + return; + } + + if (isUserPresent) { + UserPresence.setOnline(); + } else { + UserPresence.setAway(); + } + } catch (error) { + console.error(error); + } finally { + wasUserPresent = isUserPresent; + } + }; +}; -function setUserPresence(isUserPresent) { +const handleUserPresence = () => { + const Meteor = getMeteor(); + const Tracker = getTracker(); + const getUserPreference = getGetUserPreference(); const UserPresence = getUserPresence(); - if (!UserPresence) { + if (!Meteor || !Tracker || !getUserPreference || !UserPresence) { return; } - if (isUserPresent) { - UserPresence.setOnline(); - } else { - UserPresence.setAway(); - } -} + let intervalID; -function pollUserPresence() { - let isUserPresent = true; + Tracker.autorun(() => { + if (intervalID) { + clearInterval(intervalID); + intervalID = null; + } - try { - const idleTime = ipcRenderer.sendSync('request-system-idle-time'); - isUserPresent = idleTime < maximumIdleTime; - const changed = isUserPresent !== wasUserPresent; + const uid = Meteor.userId(); - if (autoAwayEnabled && changed) { - setUserPresence(isUserPresent); + if (!uid) { return; } - if (!autoAwayEnabled && isUserPresent) { - setUserPresence(isUserPresent); - return; - } - } catch (error) { - console.error(error); - } finally { - wasUserPresent = isUserPresent; - } -} + delete UserPresence.awayTime; + UserPresence.start(); -function handleUserPresence() { - const Meteor = getMeteor(); - const Tracker = getTracker(); - const getUserPreference = getGetUserPreference(); - - if (!Meteor || !Tracker || !getUserPreference) { - return; - } + const isAutoAwayEnabled = getUserPreference(uid, 'enableAutoAway'); - Tracker.autorun(() => { - if (!Meteor.userId()) { + if (!isAutoAwayEnabled) { + UserPresence.setOnline(); return; } - const userId = Meteor.userId(); - autoAwayEnabled = getUserPreference(userId, 'enableAutoAway'); + const maximumIdleTime = (getUserPreference(uid, 'idleTimeLimit') || 300) * 1000; + const idleTimeDetectionInterval = maximumIdleTime / 2; + const callback = pollUserPresence(UserPresence, maximumIdleTime); - if (autoAwayEnabled) { - idleDetectionInterval = 1 * 1000; - maximumIdleTime = (getUserPreference(userId, 'idleTimeLimit') || 300) * 1000; - } else { - idleDetectionInterval = 10 * 1000; - maximumIdleTime = 10 * 1000; - } - - if (intervalHandler) { - clearInterval(intervalHandler); - } - - intervalHandler = setInterval(pollUserPresence, idleDetectionInterval); + intervalID = setInterval(callback, idleTimeDetectionInterval); }); -} +}; export default () => {