Skip to content

Commit

Permalink
fix: System idle threshold (#1844)
Browse files Browse the repository at this point in the history
* Replace Redux actions with IPC handler

* Sanitize user preferences

* Use stricter types
  • Loading branch information
tassoevan authored Oct 22, 2020
1 parent de0fe7f commit 6c6e2a9
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 38 deletions.
8 changes: 4 additions & 4 deletions src/injected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ const start = (): void => {

Tracker.autorun(() => {
const uid = Meteor.userId();
const isAutoAwayEnabled = getUserPreference(uid, 'enableAutoAway');
const idleThreshold = getUserPreference(uid, 'idleTimeLimit');
const isAutoAwayEnabled: unknown = getUserPreference(uid, 'enableAutoAway');
const idleThreshold: unknown = getUserPreference(uid, 'idleTimeLimit');

if (isAutoAwayEnabled) {
delete UserPresence.awayTime;
UserPresence.start();
}

window.RocketChatDesktop.setUserPresenceDetection({
isAutoAwayEnabled,
idleThreshold,
isAutoAwayEnabled: Boolean(isAutoAwayEnabled),
idleThreshold: idleThreshold ? Number(idleThreshold) : null,
setUserOnline: (online) => {
if (!online) {
Meteor.call('UserPresence:away');
Expand Down
3 changes: 3 additions & 0 deletions src/ipc/channels.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { AnyAction } from 'redux';

import { SystemIdleState } from '../userPresence/common';

type ChannelToArgsMap = {
'redux/get-initial-state': () => unknown;
'redux/action-dispatched': (action: AnyAction) => void;
'servers/fetch-info': (urlHref: string) => [urlHref: string, version: string];
'notifications/fetch-icon': (urlHref: string) => string;
'power-monitor/get-system-idle-state': (idleThreshold: number) => SystemIdleState;
};

export type Channel = keyof ChannelToArgsMap;
Expand Down
6 changes: 0 additions & 6 deletions src/userPresence/actions.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { powerMonitor } from 'electron';

export const SYSTEM_IDLE_STATE_REQUESTED = 'system/idle-state-resquested';
export const SYSTEM_IDLE_STATE_RESPONDED = 'system/idle-state-responded';
export const SYSTEM_LOCKING_SCREEN = 'system/locking-screen';
export const SYSTEM_SUSPENDING = 'system/suspending';

export type UserPresenceActionTypeToPayloadMap = {
[SYSTEM_IDLE_STATE_REQUESTED]: number;
[SYSTEM_IDLE_STATE_RESPONDED]: ReturnType<typeof powerMonitor.getSystemIdleState>;
[SYSTEM_LOCKING_SCREEN]: never;
[SYSTEM_LOCKING_SCREEN]: never;
[SYSTEM_SUSPENDING]: never;
Expand Down
3 changes: 3 additions & 0 deletions src/userPresence/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { PowerMonitor } from 'electron';

export type SystemIdleState = ReturnType<PowerMonitor['getSystemIdleState']>;
20 changes: 7 additions & 13 deletions src/userPresence/main.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { powerMonitor } from 'electron';

import { dispatch, listen } from '../store';
import { handle } from '../ipc/main';
import { dispatch } from '../store';
import {
SYSTEM_SUSPENDING,
SYSTEM_LOCKING_SCREEN,
SYSTEM_IDLE_STATE_REQUESTED,
SYSTEM_IDLE_STATE_RESPONDED,
} from './actions';

export const setupPowerMonitor = (): void => {
Expand All @@ -17,14 +16,9 @@ export const setupPowerMonitor = (): void => {
dispatch({ type: SYSTEM_LOCKING_SCREEN });
});

listen(SYSTEM_IDLE_STATE_REQUESTED, (action) => {
dispatch({
type: SYSTEM_IDLE_STATE_RESPONDED,
payload: powerMonitor.getSystemIdleState(action.payload),
meta: {
response: true,
id: action.meta?.id,
},
});
});
handle(
'power-monitor/get-system-idle-state',
async (_webContents, idleThreshold) =>
powerMonitor.getSystemIdleState(idleThreshold),
);
};
20 changes: 5 additions & 15 deletions src/userPresence/preload.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { powerMonitor } from 'electron';

import { listen, request } from '../store';
import { invoke } from '../ipc/renderer';
import { listen } from '../store';
import {
SYSTEM_SUSPENDING,
SYSTEM_LOCKING_SCREEN,
SYSTEM_IDLE_STATE_REQUESTED,
SYSTEM_IDLE_STATE_RESPONDED,
} from './actions';

type SystemIdleState = ReturnType<typeof powerMonitor.getSystemIdleState>;
import { SystemIdleState } from './common';

let isAutoAwayEnabled: boolean;
let idleThreshold: number;
let idleThreshold: number | null;
let goOnline = (): void => undefined;
let goAway = (): void => undefined;

Expand All @@ -22,13 +18,7 @@ const setupUserPresenceListening = (): void => {
return;
}

const state: SystemIdleState = await request<
typeof SYSTEM_IDLE_STATE_REQUESTED,
typeof SYSTEM_IDLE_STATE_RESPONDED
>({
type: SYSTEM_IDLE_STATE_REQUESTED,
payload: idleThreshold,
});
const state = await invoke('power-monitor/get-system-idle-state', idleThreshold);

if (prevState === state) {
setTimeout(pollSystemIdleState, 1000);
Expand Down

0 comments on commit 6c6e2a9

Please sign in to comment.