Skip to content

Commit 2e70b51

Browse files
abhigyankakashnimare
authored andcommitted
menu: Add option to check for updates.
Fixes: #479.
1 parent 51e414a commit 2e70b51

File tree

2 files changed

+69
-4
lines changed

2 files changed

+69
-4
lines changed

app/main/autoupdater.js

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use strict';
2-
const { app, dialog } = require('electron');
2+
const { app, dialog, shell } = require('electron');
33
const { autoUpdater } = require('electron-updater');
44
const isDev = require('electron-is-dev');
55

66
const ConfigUtil = require('./../renderer/js/utils/config-util.js');
77

8-
function appUpdater() {
8+
function appUpdater(updateFromMenu = false) {
99
// Don't initiate auto-updates in development
1010
if (isDev) {
1111
return;
@@ -17,6 +17,8 @@ function appUpdater() {
1717
return;
1818
}
1919

20+
let updateAvailable = false;
21+
2022
// Create Logs directory
2123
const LogsDir = `${app.getPath('userData')}/Logs`;
2224

@@ -30,6 +32,55 @@ function appUpdater() {
3032
// Handle auto updates for beta/pre releases
3133
autoUpdater.allowPrerelease = ConfigUtil.getConfigItem('betaUpdate') || false;
3234

35+
const eventsListenerRemove = ['update-available', 'update-not-available'];
36+
autoUpdater.on('update-available', info => {
37+
if (updateFromMenu) {
38+
dialog.showMessageBox({
39+
message: `A new version ${info.version}, of Zulip Desktop is available`,
40+
detail: 'The update will be downloaded in the background. You will be notified when it is ready to be installed.'
41+
});
42+
43+
updateAvailable = true;
44+
45+
// This is to prevent removal of 'update-downloaded' and 'error' event listener.
46+
eventsListenerRemove.forEach(event => {
47+
autoUpdater.removeAllListeners(event);
48+
});
49+
}
50+
});
51+
52+
autoUpdater.on('update-not-available', () => {
53+
if (updateFromMenu) {
54+
dialog.showMessageBox({
55+
message: 'No updates available',
56+
detail: `You are running the latest version of Zulip Desktop.\nVersion: ${app.getVersion()}`
57+
});
58+
// Remove all autoUpdator listeners so that next time autoUpdator is manually called these
59+
// listeners don't trigger multiple times.
60+
autoUpdater.removeAllListeners();
61+
}
62+
});
63+
64+
autoUpdater.on('error', error => {
65+
if (updateFromMenu) {
66+
const messageText = (updateAvailable) ? ('Unable to download the updates') : ('Unable to check for updates');
67+
dialog.showMessageBox({
68+
type: 'error',
69+
buttons: ['Manual Download', 'Cancel'],
70+
message: messageText,
71+
detail: (error).toString() + `\n\nThe latest version of Zulip Desktop is available at -\nhttps://zulipchat.com/apps/.\n
72+
Current Version: ${app.getVersion()}`
73+
}, response => {
74+
if (response === 0) {
75+
shell.openExternal('https://zulipchat.com/apps/');
76+
}
77+
});
78+
// Remove all autoUpdator listeners so that next time autoUpdator is manually called these
79+
// listeners don't trigger multiple times.
80+
autoUpdater.removeAllListeners();
81+
}
82+
});
83+
3384
// Ask the user if update is available
3485
// eslint-disable-next-line no-unused-vars
3586
autoUpdater.on('update-downloaded', event => {

app/main/menu.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const path = require('path');
44
const { app, shell, BrowserWindow, Menu, dialog } = require('electron');
55

66
const fs = require('fs-extra');
7+
const { appUpdater } = require('./autoupdater');
78

89
const ConfigUtil = require(__dirname + '/../renderer/js/utils/config-util.js');
910
const DNDUtil = require(__dirname + '/../renderer/js/utils/dnd-util.js');
@@ -195,7 +196,12 @@ class AppMenu {
195196
AppMenu.sendAction('open-about');
196197
}
197198
}
198-
}, {
199+
}, {
200+
label: `Check for Update`,
201+
click() {
202+
AppMenu.checkForUpdate();
203+
}
204+
}, {
199205
type: 'separator'
200206
}, {
201207
label: 'Desktop App Settings',
@@ -302,7 +308,12 @@ class AppMenu {
302308
AppMenu.sendAction('open-about');
303309
}
304310
}
305-
}, {
311+
}, {
312+
label: `Check for Update`,
313+
click() {
314+
AppMenu.checkForUpdate();
315+
}
316+
}, {
306317
type: 'separator'
307318
}, {
308319
label: 'Desktop App Settings',
@@ -399,6 +410,9 @@ class AppMenu {
399410
win.webContents.send(action, ...params);
400411
}
401412

413+
static checkForUpdate() {
414+
appUpdater(true);
415+
}
402416
static resetAppSettings() {
403417
const resetAppSettingsMessage = 'By proceeding you will be removing all connected organizations and preferences from Zulip.';
404418

0 commit comments

Comments
 (0)