diff --git a/ts/components/dialog/user-settings/pages/PreferencesSettingsPage.tsx b/ts/components/dialog/user-settings/pages/PreferencesSettingsPage.tsx index 5c4e0e9cc2..c55465556c 100644 --- a/ts/components/dialog/user-settings/pages/PreferencesSettingsPage.tsx +++ b/ts/components/dialog/user-settings/pages/PreferencesSettingsPage.tsx @@ -19,6 +19,7 @@ import { ToastUtils } from '../../../../session/utils'; import { PanelRadioButton } from '../../../buttons/panel/PanelRadioButton'; import { useHasEnterSendEnabled } from '../../../../state/selectors/settings'; import { UserSettingsModalContainer } from '../components/UserSettingsModalContainer'; +import { fetchLatestReleaseFromFileServerIfEnabled } from '../../../../hooks/useFetchLatestReleaseFromFileServer'; async function toggleStartInTray() { try { @@ -122,7 +123,11 @@ export function PreferencesSettingsPage(modalState: UserSettingsModalState) { subText={{ token: 'permissionsAutoUpdateDescription' }} onClick={async () => { const old = Boolean(window.getSettingValue(SettingsKey.settingsAutoUpdate)); - await window.setSettingValue(SettingsKey.settingsAutoUpdate, !old); + const enabled = !old; + await window.setSettingValue(SettingsKey.settingsAutoUpdate, enabled); + if (enabled) { + await fetchLatestReleaseFromFileServerIfEnabled(); + } forceUpdate(); }} active={Boolean(window.getSettingValue(SettingsKey.settingsAutoUpdate))} diff --git a/ts/hooks/useFetchLatestReleaseFromFileServer.ts b/ts/hooks/useFetchLatestReleaseFromFileServer.ts index d699db589f..c7fa5e9bcc 100644 --- a/ts/hooks/useFetchLatestReleaseFromFileServer.ts +++ b/ts/hooks/useFetchLatestReleaseFromFileServer.ts @@ -1,20 +1,43 @@ import { isEmpty } from 'lodash'; +import { useCallback, useEffect, useRef } from 'react'; import { useSelector } from 'react-redux'; import useInterval from 'react-use/lib/useInterval'; +import { SettingsKey } from '../data/settings-key'; import { fetchLatestRelease } from '../session/fetch_latest_release'; import { UserUtils } from '../session/utils'; import { getOurPrimaryConversation } from '../state/selectors/conversations'; +export async function fetchLatestReleaseFromFileServerIfEnabled() { + if (!window.getSettingValue(SettingsKey.settingsAutoUpdate)) { + return; + } + + const userEd25519SecretKey = (await UserUtils.getUserED25519KeyPairBytes())?.privKeyBytes; + if (userEd25519SecretKey && !isEmpty(userEd25519SecretKey)) { + void fetchLatestRelease.fetchReleaseFromFSAndUpdateMain(userEd25519SecretKey); + } +} + export function useFetchLatestReleaseFromFileServer() { const ourPrimaryConversation = useSelector(getOurPrimaryConversation); + const fetchedOnStartupRef = useRef(false); - useInterval(async () => { + const fetchReleaseIfEnabled = useCallback(async () => { if (!ourPrimaryConversation) { return; } - const userEd25519SecretKey = (await UserUtils.getUserED25519KeyPairBytes())?.privKeyBytes; - if (userEd25519SecretKey && !isEmpty(userEd25519SecretKey)) { - void fetchLatestRelease.fetchReleaseFromFSAndUpdateMain(userEd25519SecretKey); + + await fetchLatestReleaseFromFileServerIfEnabled(); + }, [ourPrimaryConversation]); + + useEffect(() => { + if (fetchedOnStartupRef.current || !ourPrimaryConversation) { + return; } - }, fetchLatestRelease.fetchReleaseFromFileServerInterval); + + fetchedOnStartupRef.current = true; + void fetchReleaseIfEnabled(); + }, [fetchReleaseIfEnabled, ourPrimaryConversation]); + + useInterval(fetchReleaseIfEnabled, fetchLatestRelease.fetchReleaseFromFileServerInterval); } diff --git a/ts/session/fetch_latest_release/index.ts b/ts/session/fetch_latest_release/index.ts index b6e7ecd2f2..6fcd8bb10d 100644 --- a/ts/session/fetch_latest_release/index.ts +++ b/ts/session/fetch_latest_release/index.ts @@ -1,4 +1,4 @@ -// every 1 minute we fetch from the fileserver to check for a new release +// Periodically fetch from the fileserver to check for a new release // * if there is none, no request to github are made. // * if there is a version on the fileserver more recent than our current, we fetch github to get the UpdateInfos and trigger an update as usual (asking user via dialog) @@ -9,9 +9,9 @@ import { getLatestReleaseFromFileServer } from '../apis/file_server_api/FileServ import { isReleaseChannel } from '../../updater/types'; /** - * We don't want to hit the fileserver too often. Only often on start, and then every 30 minutes + * We don't want to hit the fileserver too often. Only often on start, and then every 30 minutes. */ -const skipIfLessThan = DURATION.MINUTES * 30; +const fetchReleaseFromFileServerInterval = DURATION.MINUTES * 30; let lastFetchedTimestamp = Number.MIN_SAFE_INTEGER; @@ -26,9 +26,9 @@ async function fetchReleaseFromFSAndUpdateMain( try { window.log.info('[updater] about to fetchReleaseFromFSAndUpdateMain'); const diff = Date.now() - lastFetchedTimestamp; - if (!force && diff < skipIfLessThan) { + if (!force && diff < fetchReleaseFromFileServerInterval) { window.log.debug( - `[updater] fetched release from fs ${Math.floor(diff / DURATION.MINUTES)} minutes ago, skipping until that's at least ${Math.floor(skipIfLessThan / DURATION.MINUTES)}` + `[updater] fetched release from fs ${Math.floor(diff / DURATION.MINUTES)} minutes ago, skipping until that's at least ${Math.floor(fetchReleaseFromFileServerInterval / DURATION.MINUTES)}` ); return null; } @@ -60,10 +60,9 @@ async function fetchReleaseFromFSAndUpdateMain( export const fetchLatestRelease = { /** - * Try to fetch the latest release from the fileserver every 1 minute. - * If we did fetch a release already in the last 30 minutes, we will skip the call. + * Try to fetch the latest release from the fileserver every 30 minutes. */ - fetchReleaseFromFileServerInterval: DURATION.MINUTES * 1, + fetchReleaseFromFileServerInterval, fetchReleaseFromFSAndUpdateMain, resetForTesting, }; diff --git a/ts/test/session/unit/updater/updater_test.ts b/ts/test/session/unit/updater/updater_test.ts index b3ba424bdb..1ddab8ae58 100644 --- a/ts/test/session/unit/updater/updater_test.ts +++ b/ts/test/session/unit/updater/updater_test.ts @@ -4,6 +4,8 @@ import { readFileSync } from 'fs-extra'; import { isEmpty } from 'lodash'; import { expect } from 'chai'; import { enableLogRedirect } from '../../../test-utils/utils'; +import { DURATION } from '../../../../session/constants'; +import { fetchLatestRelease } from '../../../../session/fetch_latest_release'; describe('Updater', () => { it('package.json target is correct', () => { @@ -25,4 +27,8 @@ describe('Updater', () => { 'If you see this message, just set `enableLogRedirect` to false in `ts/test/test-utils/utils/stubbing.ts' ); }); + + it('checks the file server on the same cadence as the update throttle', () => { + expect(fetchLatestRelease.fetchReleaseFromFileServerInterval).to.equal(30 * DURATION.MINUTES); + }); });