persist update indicator across restarts

part of #749
This commit is contained in:
Girish Ramakrishnan
2020-12-20 14:17:39 -08:00
parent 9683bb6408
commit 65290e52f7
4 changed files with 31 additions and 277 deletions

View File

@@ -4,10 +4,7 @@ exports = module.exports = {
checkForUpdates,
getUpdateInfo,
resetUpdateInfo,
resetAppUpdateInfo,
_setUpdateInfo: setUpdateInfo,
_checkAppUpdates: checkAppUpdates
};
@@ -24,42 +21,16 @@ var apps = require('./apps.js'),
settings = require('./settings.js'),
users = require('./users.js');
var gAppUpdateInfo = { }, // id -> update info { creationDate, manifest }
gBoxUpdateInfo = null; // { version, changelog, upgrade, sourceTarballUrl }
function loadState() {
var state = safe.JSON.parse(safe.fs.readFileSync(paths.UPDATE_CHECKER_FILE, 'utf8'));
return state || { };
}
function saveState(state) {
function setUpdateInfo(state) {
console.dir(state);
// appid -> update info { creationDate, manifest }
// box -> { version, changelog, upgrade, sourceTarballUrl }
safe.fs.writeFileSync(paths.UPDATE_CHECKER_FILE, JSON.stringify(state, null, 4), 'utf8');
}
function getUpdateInfo() {
return {
apps: gAppUpdateInfo,
box: gBoxUpdateInfo
};
}
function setUpdateInfo(info) {
gBoxUpdateInfo = info.box;
gAppUpdateInfo = info.apps;
}
function resetUpdateInfo() {
gBoxUpdateInfo = null;
resetAppUpdateInfo();
}
// If no appId provided all apps are reset
function resetAppUpdateInfo(appId) {
if (!appId) {
gAppUpdateInfo = {};
} else {
delete gAppUpdateInfo[appId];
}
const state = safe.JSON.parse(safe.fs.readFileSync(paths.UPDATE_CHECKER_FILE, 'utf8'));
return state || {};
}
function checkAppUpdates(options, callback) {
@@ -68,9 +39,8 @@ function checkAppUpdates(options, callback) {
debug('Checking App Updates');
gAppUpdateInfo = { };
var oldState = loadState();
var newState = { }; // create new state so that old app ids are removed
let state = getUpdateInfo();
let newState = { }; // create new state so that old app ids are removed
settings.getAutoupdatePattern(function (error, result) {
if (error) return callback(error);
@@ -90,36 +60,25 @@ function checkAppUpdates(options, callback) {
return iteratorDone(); // continue to next
}
// skip if no next version is found
if (!updateInfo) {
delete gAppUpdateInfo[app.id];
return iteratorDone();
}
if (!updateInfo) return iteratorDone(); // skip if no next version is found
gAppUpdateInfo[app.id] = updateInfo;
newState[app.id] = updateInfo;
// decide whether to send email
newState[app.id] = updateInfo.manifest.version;
if (oldState[app.id] === newState[app.id]) {
debug('Skipping notification of app update %s since user was already notified', app.id);
if (safe.query(state[app.id], 'manifest.version') === updateInfo.manifest.version) {
debug(`Skipping app update notification of ${app.id} since user was already notified of ${updateInfo.manifest.version}`);
return iteratorDone();
}
const canAutoupdateApp = apps.canAutoupdateApp(app, updateInfo);
if (autoupdatesEnabled && canAutoupdateApp) return iteratorDone();
debug('Notifying of app update for %s from %s to %s', app.id, app.manifest.version, updateInfo.manifest.version);
notificationPending.push({
app: app,
updateInfo: updateInfo
});
debug(`Notifying of app update for ${app.id} from ${app.manifest.version} to ${updateInfo.manifest.version}`);
notificationPending.push({ app, updateInfo });
iteratorDone();
});
}, function () {
newState.box = loadState().box; // preserve the latest box state information
saveState(newState);
if ('box' in state) newState.box = state.box; // preserve the latest box state information
setUpdateInfo(newState);
if (notificationPending.length === 0) return callback();
@@ -142,17 +101,12 @@ function checkBoxUpdates(options, callback) {
debug('Checking Box Updates');
gBoxUpdateInfo = null;
appstore.getBoxUpdate(options, function (error, updateInfo) {
if (error || !updateInfo) return callback(error);
gBoxUpdateInfo = updateInfo;
let state = getUpdateInfo();
// decide whether to send email
var state = loadState();
if (state.box === gBoxUpdateInfo.version) {
if (state.box && state.box.version === updateInfo.version) {
debug('Skipping notification of box update as user was already notified');
return callback();
}
@@ -164,8 +118,8 @@ function checkBoxUpdates(options, callback) {
notifications.alert(notifications.ALERT_BOX_UPDATE, `Cloudron v${updateInfo.version} is available`, message, function (error) {
if (error) return callback(error);
state.box = updateInfo.version;
saveState(state);
state.box = updateInfo;
setUpdateInfo(state);
callback();
});