From 234e9b160ee2b40724a6d79576089a261f89350c Mon Sep 17 00:00:00 2001 From: Audric Ackermann Date: Tue, 15 Mar 2022 12:13:33 +1100 Subject: [PATCH] Make sure updater do not hit github before checking fileserver --- config/default.json | 1 - config/integration-test.json | 3 +-- config/production-devprod.json | 3 +-- config/production-devprod1.json | 3 +-- config/production.json | 4 +-- main.js | 18 ++++++------- ts/components/leftpane/ActionsPanel.tsx | 27 +++++++++++++++++-- ts/updater/index.ts | 14 +++++----- ts/updater/updater.ts | 36 ++++++++++++++++++++++--- 9 files changed, 79 insertions(+), 30 deletions(-) diff --git a/config/default.json b/config/default.json index bb1eab171..eb0af8788 100644 --- a/config/default.json +++ b/config/default.json @@ -14,7 +14,6 @@ "url": "https://public.loki.foundation:4433/" } ], - "updatesEnabled": false, "openDevTools": false, "commitHash": "", "import": false diff --git a/config/integration-test.json b/config/integration-test.json index d2b1f6973..ce063c126 100644 --- a/config/integration-test.json +++ b/config/integration-test.json @@ -1,4 +1,3 @@ { - "openDevTools": false, - "updatesEnabled": false + "openDevTools": false } diff --git a/config/production-devprod.json b/config/production-devprod.json index 62437286b..7791e7862 100644 --- a/config/production-devprod.json +++ b/config/production-devprod.json @@ -1,4 +1,3 @@ { - "openDevTools": true, - "updatesEnabled": false + "openDevTools": true } diff --git a/config/production-devprod1.json b/config/production-devprod1.json index 62437286b..7791e7862 100644 --- a/config/production-devprod1.json +++ b/config/production-devprod1.json @@ -1,4 +1,3 @@ { - "openDevTools": true, - "updatesEnabled": false + "openDevTools": true } diff --git a/config/production.json b/config/production.json index 5f2141001..0967ef424 100644 --- a/config/production.json +++ b/config/production.json @@ -1,3 +1 @@ -{ - "updatesEnabled": true -} +{} diff --git a/main.js b/main.js index 1afb728e0..59255c614 100644 --- a/main.js +++ b/main.js @@ -76,6 +76,8 @@ const { installPermissionsHandler } = require('./app/permissions'); let appStartInitialSpellcheckSetting = true; +let latestDesktopRelease; + async function getSpellCheckSetting() { const json = await sql.getItemById('spell-check'); // Default to `true` if setting doesn't exist yet @@ -398,29 +400,27 @@ async function createWindow() { // when you should delete the corresponding element. mainWindow = null; }); + + mainWindow.getLatestDesktopRelease = () => latestDesktopRelease; } ipc.on('show-window', () => { showWindow(); }); +ipc.on('set-release-from-file-server', (_event, releaseGotFromFileServer) => { + latestDesktopRelease = releaseGotFromFileServer; +}); + let isReadyForUpdates = false; async function readyForUpdates() { + console.log('isReadyForUpdates', isReadyForUpdates); if (isReadyForUpdates) { return; } isReadyForUpdates = true; - // disable for now - /* - // First, install requested sticker pack - const incomingUrl = getIncomingUrl(process.argv); - if (incomingUrl) { - handleSgnlLink(incomingUrl); - } - */ - // Second, start checking for app updates try { await updater.start(getMainWindow, userConfig, locale.messages, logger); diff --git a/ts/components/leftpane/ActionsPanel.tsx b/ts/components/leftpane/ActionsPanel.tsx index e30b3ede6..3dd6d69eb 100644 --- a/ts/components/leftpane/ActionsPanel.tsx +++ b/ts/components/leftpane/ActionsPanel.tsx @@ -34,7 +34,7 @@ import { conversationChanged, conversationRemoved } from '../../state/ducks/conv import { editProfileModal, onionPathModal } from '../../state/ducks/modalDialog'; import { uploadOurAvatar } from '../../interactions/conversationInteractions'; import { ModalContainer } from '../dialog/ModalContainer'; -import { debounce } from 'lodash'; +import { debounce, isEmpty, isString } from 'lodash'; // tslint:disable-next-line: no-import-side-effect no-submodule-imports @@ -51,6 +51,8 @@ import { IncomingCallDialog } from '../calling/IncomingCallDialog'; import { SessionIconButton } from '../icon'; import { SessionToastContainer } from '../SessionToastContainer'; import { LeftPaneSectionContainer } from './LeftPaneSectionContainer'; +import { getLatestDesktopReleaseFileToFsV2 } from '../../session/apis/file_server_api/FileServerApiV2'; +import { ipcRenderer } from 'electron'; const Section = (props: { type: SectionType }) => { const ourNumber = useSelector(getOurNumber); @@ -162,7 +164,12 @@ const Section = (props: { type: SectionType }) => { } }; -const cleanUpMediasInterval = DURATION.MINUTES * 30; +const cleanUpMediasInterval = DURATION.MINUTES * 60; + +// every 10 minutes we 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) +const fetchReleaseFromFileServerInterval = DURATION.MINUTES * 10; const setupTheme = () => { const theme = window.Events.getThemeSetting(); @@ -265,6 +272,18 @@ const CallContainer = () => { ); }; +async function fetchReleaseFromFSAndUpdateMain() { + try { + const latest = await getLatestDesktopReleaseFileToFsV2(); + + if (isString(latest) && !isEmpty(latest)) { + ipcRenderer.send('set-release-from-file-server', latest); + } + } catch (e) { + window.log.warn(e); + } +} + /** * ActionsPanel is the far left banner (not the left pane). * The panel with buttons to switch between the message/contact/settings/theme views @@ -289,6 +308,10 @@ export const ActionsPanel = () => { useInterval(cleanUpOldDecryptedMedias, startCleanUpMedia ? cleanUpMediasInterval : null); + useInterval(() => { + void fetchReleaseFromFSAndUpdateMain(); + }, fetchReleaseFromFileServerInterval); + if (!ourPrimaryConversation) { window?.log?.warn('ActionsPanel: ourPrimaryConversation is not set'); return null; diff --git a/ts/updater/index.ts b/ts/updater/index.ts index 4327b7155..6c4e8e136 100644 --- a/ts/updater/index.ts +++ b/ts/updater/index.ts @@ -1,4 +1,3 @@ -import { get as getFromConfig } from 'config'; import { BrowserWindow } from 'electron'; import { start as startUpdater, stop as stopUpdater } from './updater'; import { LoggerType, MessagesType } from './common'; @@ -10,14 +9,16 @@ let config: UserConfig; export async function start( getMainWindow: () => BrowserWindow, userConfig: UserConfig, - messages?: MessagesType, - logger?: LoggerType + messages: MessagesType, + logger: LoggerType ) { if (initialized) { throw new Error('updater/start: Updates have already been initialized!'); } - initialized = true; - config = userConfig; + + if (!userConfig) { + throw new Error('updater/start: userConfig is needed!'); + } if (!messages) { throw new Error('updater/start: Must provide messages!'); @@ -25,6 +26,8 @@ export async function start( if (!logger) { throw new Error('updater/start: Must provide logger!'); } + initialized = true; + config = userConfig; // reused below if (autoUpdateDisabled()) { /* @@ -56,7 +59,6 @@ function autoUpdateDisabled() { return ( process.mas || // From Electron: Mac App Store build - !getFromConfig('updatesEnabled') || // Hard coded config // tslint:disable-next-line: no-backbone-get-set-outside-model !autoUpdate // User setting ); diff --git a/ts/updater/updater.ts b/ts/updater/updater.ts index aa1918b51..5f1662b63 100644 --- a/ts/updater/updater.ts +++ b/ts/updater/updater.ts @@ -21,7 +21,7 @@ let stopped = false; const SECOND = 1000; const MINUTE = SECOND * 60; -const INTERVAL = MINUTE * 30; +const INTERVAL = MINUTE * 1; export async function start( getMainWindow: () => BrowserWindow, @@ -70,16 +70,42 @@ async function checkForUpdates( const canUpdate = await canAutoUpdate(); if (!canUpdate) { + logger.info('checkForUpdates canAutoUpdate false'); return; } - logger.info('auto-update: checking for update...'); + logger.info('auto-update: checkForUpdates...'); isUpdating = true; try { - // Get the update using electron-updater + const latestVersionFromFsFromRenderer = getMainWindow() + ? ((getMainWindow() as any).getLatestDesktopRelease() as string | undefined) + : undefined; + + logger.info('checkForUpdates latestVersionFromFsFromRenderer', latestVersionFromFsFromRenderer); + if (!latestVersionFromFsFromRenderer || !latestVersionFromFsFromRenderer?.length) { + logger.info( + 'testVersionFromFsFromRenderer was not updated yet by renderer. Skipping update check' + ); + return; + } + + const currentVersion = autoUpdater.currentVersion.toString(); + const isMoreRecent = isVersionGreaterThan(latestVersionFromFsFromRenderer, currentVersion); + logger.info('checkForUpdates isMoreRecent', isMoreRecent); + if (!isMoreRecent) { + logger.info( + `Fileserver has no update so we are not looking for an update from github current:${currentVersion} fromFileServer:${latestVersionFromFsFromRenderer}` + ); + return; + } + + // Get the update using electron-updater, this fetches from github const result = await autoUpdater.checkForUpdates(); + + logger.info('checkForUpdates github fetch result:', result); + if (!result.updateInfo) { logger.info('auto-update: no update info received'); @@ -88,6 +114,8 @@ async function checkForUpdates( try { const hasUpdate = isUpdateAvailable(result.updateInfo); + logger.info('checkForUpdates hasUpdate:', hasUpdate); + if (!hasUpdate) { logger.info('auto-update: no update available'); @@ -96,6 +124,8 @@ async function checkForUpdates( logger.info('auto-update: showing download dialog...'); const shouldDownload = await showDownloadUpdateDialog(getMainWindow(), messages); + logger.info('checkForUpdates shouldDownload:', shouldDownload); + if (!shouldDownload) { downloadIgnored = true;