diff --git a/src/injected.ts b/src/injected.ts index 6ecfcfc1a8..3342381227 100644 --- a/src/injected.ts +++ b/src/injected.ts @@ -50,8 +50,8 @@ 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; @@ -59,8 +59,8 @@ const start = (): void => { } window.RocketChatDesktop.setUserPresenceDetection({ - isAutoAwayEnabled, - idleThreshold, + isAutoAwayEnabled: Boolean(isAutoAwayEnabled), + idleThreshold: idleThreshold ? Number(idleThreshold) : null, setUserOnline: (online) => { if (!online) { Meteor.call('UserPresence:away'); diff --git a/src/ipc/channels.ts b/src/ipc/channels.ts index a845babe3e..940474e031 100644 --- a/src/ipc/channels.ts +++ b/src/ipc/channels.ts @@ -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; diff --git a/src/userPresence/actions.ts b/src/userPresence/actions.ts index c398e82892..596c8a7552 100644 --- a/src/userPresence/actions.ts +++ b/src/userPresence/actions.ts @@ -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; [SYSTEM_LOCKING_SCREEN]: never; [SYSTEM_LOCKING_SCREEN]: never; [SYSTEM_SUSPENDING]: never; diff --git a/src/userPresence/common.ts b/src/userPresence/common.ts new file mode 100644 index 0000000000..530f97dbba --- /dev/null +++ b/src/userPresence/common.ts @@ -0,0 +1,3 @@ +import type { PowerMonitor } from 'electron'; + +export type SystemIdleState = ReturnType; diff --git a/src/userPresence/main.ts b/src/userPresence/main.ts index 6b937ee43f..7dff27516a 100644 --- a/src/userPresence/main.ts +++ b/src/userPresence/main.ts @@ -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 => { @@ -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), + ); }; diff --git a/src/userPresence/preload.ts b/src/userPresence/preload.ts index 642409d43c..fbf8b08912 100644 --- a/src/userPresence/preload.ts +++ b/src/userPresence/preload.ts @@ -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; +import { SystemIdleState } from './common'; let isAutoAwayEnabled: boolean; -let idleThreshold: number; +let idleThreshold: number | null; let goOnline = (): void => undefined; let goAway = (): void => undefined; @@ -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);