diff --git a/app/main/autoupdater.js b/app/main/autoupdater.js index 45f13277..34fdc4b4 100644 --- a/app/main/autoupdater.js +++ b/app/main/autoupdater.js @@ -1,11 +1,11 @@ 'use strict'; -const { app, dialog } = require('electron'); +const { app, dialog, shell } = require('electron'); const { autoUpdater } = require('electron-updater'); const isDev = require('electron-is-dev'); const ConfigUtil = require('./../renderer/js/utils/config-util.js'); -function appUpdater() { +function appUpdater(updateFromMenu = false) { // Don't initiate auto-updates in development if (isDev) { return; @@ -17,6 +17,8 @@ function appUpdater() { return; } + let updateAvailable = false; + // Create Logs directory const LogsDir = `${app.getPath('userData')}/Logs`; @@ -30,6 +32,55 @@ function appUpdater() { // Handle auto updates for beta/pre releases autoUpdater.allowPrerelease = ConfigUtil.getConfigItem('betaUpdate') || false; + const eventsListenerRemove = ['update-available', 'update-not-available']; + autoUpdater.on('update-available', info => { + if (updateFromMenu) { + dialog.showMessageBox({ + message: `A new version ${info.version}, of Zulip Desktop is available`, + detail: 'The update will be downloaded in the background. You will be notified when it is ready to be installed.' + }); + + updateAvailable = true; + + // This is to prevent removal of 'update-downloaded' and 'error' event listener. + eventsListenerRemove.forEach(event => { + autoUpdater.removeAllListeners(event); + }); + } + }); + + autoUpdater.on('update-not-available', () => { + if (updateFromMenu) { + dialog.showMessageBox({ + message: 'No updates available', + detail: `You are running the latest version of Zulip Desktop.\nVersion: ${app.getVersion()}` + }); + // Remove all autoUpdator listeners so that next time autoUpdator is manually called these + // listeners don't trigger multiple times. + autoUpdater.removeAllListeners(); + } + }); + + autoUpdater.on('error', error => { + if (updateFromMenu) { + const messageText = (updateAvailable) ? ('Unable to download the updates') : ('Unable to check for updates'); + dialog.showMessageBox({ + type: 'error', + buttons: ['Manual Download', 'Cancel'], + message: messageText, + detail: (error).toString() + `\n\nThe latest version of Zulip Desktop is available at -\nhttps://zulipchat.com/apps/.\n +Current Version: ${app.getVersion()}` + }, response => { + if (response === 0) { + shell.openExternal('https://zulipchat.com/apps/'); + } + }); + // Remove all autoUpdator listeners so that next time autoUpdator is manually called these + // listeners don't trigger multiple times. + autoUpdater.removeAllListeners(); + } + }); + // Ask the user if update is available // eslint-disable-next-line no-unused-vars autoUpdater.on('update-downloaded', event => { diff --git a/app/main/menu.js b/app/main/menu.js index 4f3080a6..4b383be1 100644 --- a/app/main/menu.js +++ b/app/main/menu.js @@ -4,6 +4,7 @@ const path = require('path'); const { app, shell, BrowserWindow, Menu, dialog } = require('electron'); const fs = require('fs-extra'); +const { appUpdater } = require('./autoupdater'); const ConfigUtil = require(__dirname + '/../renderer/js/utils/config-util.js'); const DNDUtil = require(__dirname + '/../renderer/js/utils/dnd-util.js'); @@ -195,7 +196,12 @@ class AppMenu { AppMenu.sendAction('open-about'); } } - }, { + }, { + label: `Check for Update`, + click() { + AppMenu.checkForUpdate(); + } + }, { type: 'separator' }, { label: 'Desktop App Settings', @@ -302,7 +308,12 @@ class AppMenu { AppMenu.sendAction('open-about'); } } - }, { + }, { + label: `Check for Update`, + click() { + AppMenu.checkForUpdate(); + } + }, { type: 'separator' }, { label: 'Desktop App Settings', @@ -399,6 +410,9 @@ class AppMenu { win.webContents.send(action, ...params); } + static checkForUpdate() { + appUpdater(true); + } static resetAppSettings() { const resetAppSettingsMessage = 'By proceeding you will be removing all connected organizations and preferences from Zulip.';