Skip to content

Commit

Permalink
[FIX] Only set user presence as online when auto away detection is di…
Browse files Browse the repository at this point in the history
…sabled (#1164)

* Only set user presence as online when auto away detection is disabled

* Disable user presence polling for users which disabled auto away
  • Loading branch information
tassoevan authored Apr 16, 2019
1 parent e8ae8c5 commit a46f631
Showing 1 changed file with 52 additions and 42 deletions.
94 changes: 52 additions & 42 deletions src/preload/userPresence.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,64 +2,74 @@ import { ipcRenderer } from 'electron';
import { getMeteor, getTracker, getGetUserPreference, getUserPresence } from './rocketChat';


let maximumIdleTime = 10 * 1000;
const idleDetectionInterval = 1 * 1000;
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 onChangeUserPresence(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();
return;
}
let intervalID;

UserPresence.setAway();
}

let wasUserPresent = false;

function pollUserPresence() {
let isUserPresent = false;
Tracker.autorun(() => {
if (intervalID) {
clearInterval(intervalID);
intervalID = null;
}

try {
const idleTime = ipcRenderer.sendSync('request-system-idle-time');
isUserPresent = idleTime < maximumIdleTime;
} catch (error) {
console.error(error);
}
const uid = Meteor.userId();

if (isUserPresent !== wasUserPresent) {
onChangeUserPresence(isUserPresent);
wasUserPresent = isUserPresent;
}
}
if (!uid) {
return;
}

function handleUserPresence() {
const Meteor = getMeteor();
const Tracker = getTracker();
const getUserPreference = getGetUserPreference();
delete UserPresence.awayTime;
UserPresence.start();

if (!Meteor || !Tracker || !getUserPreference) {
return;
}
const isAutoAwayEnabled = getUserPreference(uid, 'enableAutoAway');

Tracker.autorun(() => {
if (!Meteor.userId()) {
if (!isAutoAwayEnabled) {
UserPresence.setOnline();
return;
}

const userId = Meteor.userId();
if (getUserPreference(userId, 'enableAutoAway')) {
maximumIdleTime = (getUserPreference(userId, 'idleTimeLimit') || 300) * 1000;
}
});
const maximumIdleTime = (getUserPreference(uid, 'idleTimeLimit') || 300) * 1000;
const idleTimeDetectionInterval = maximumIdleTime / 2;
const callback = pollUserPresence(UserPresence, maximumIdleTime);

setInterval(pollUserPresence, idleDetectionInterval);
}
intervalID = setInterval(callback, idleTimeDetectionInterval);
});
};


export default () => {
Expand Down

0 comments on commit a46f631

Please sign in to comment.