Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))}
Expand Down
33 changes: 28 additions & 5 deletions ts/hooks/useFetchLatestReleaseFromFileServer.ts
Original file line number Diff line number Diff line change
@@ -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);
}
15 changes: 7 additions & 8 deletions ts/session/fetch_latest_release/index.ts
Original file line number Diff line number Diff line change
@@ -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)

Expand All @@ -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;

Expand All @@ -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;
}
Expand Down Expand Up @@ -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,
};
6 changes: 6 additions & 0 deletions ts/test/session/unit/updater/updater_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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);
});
});