Files
cloudron-box/src/updatechecker.js

172 lines
5.4 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
checkAppUpdates: checkAppUpdates,
checkBoxUpdates: checkBoxUpdates,
2016-01-25 16:46:54 -08:00
getUpdateInfo: getUpdateInfo,
resetUpdateInfo: resetUpdateInfo,
2017-07-23 10:53:16 -07:00
resetAppUpdateInfo: resetAppUpdateInfo,
_setUpdateInfo: setUpdateInfo
};
var apps = require('./apps.js'),
appstore = require('./appstore.js'),
2019-03-07 14:27:23 -08:00
assert = require('assert'),
async = require('async'),
constants = require('./constants.js'),
debug = require('debug')('box:updatechecker'),
mailer = require('./mailer.js'),
2019-03-07 13:34:46 -08:00
notifications = require('./notifications.js'),
paths = require('./paths.js'),
safe = require('safetydance'),
2019-03-10 16:05:27 -07:00
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(mailedUser) {
safe.fs.writeFileSync(paths.UPDATE_CHECKER_FILE, JSON.stringify(mailedUser, null, 4), 'utf8');
}
function getUpdateInfo() {
return {
apps: gAppUpdateInfo,
box: gBoxUpdateInfo
};
}
2017-07-23 10:53:16 -07:00
function setUpdateInfo(info) {
gBoxUpdateInfo = info.box;
gAppUpdateInfo = info.apps;
}
2016-01-25 16:46:54 -08:00
function resetUpdateInfo() {
gBoxUpdateInfo = null;
resetAppUpdateInfo();
}
// If no appId provided all apps are reset
function resetAppUpdateInfo(appId) {
if (!appId) {
gAppUpdateInfo = {};
} else {
delete gAppUpdateInfo[appId];
}
2016-01-25 16:46:54 -08:00
}
2016-01-23 05:35:57 -08:00
function checkAppUpdates(callback) {
2019-03-07 14:27:23 -08:00
assert.strictEqual(typeof callback, 'function');
2016-01-23 05:35:57 -08:00
debug('Checking App Updates');
gAppUpdateInfo = { };
var oldState = loadState();
var newState = { }; // create new state so that old app ids are removed
settings.getAppAutoupdatePattern(function (error, result) {
2016-01-23 05:35:57 -08:00
if (error) return callback(error);
const autoupdatesEnabled = (result !== constants.AUTOUPDATE_PATTERN_NEVER);
var notificationPending = [];
2019-05-06 17:42:26 +02:00
apps.getAll(function (error, result) {
if (error) return callback(error);
2019-05-06 17:42:26 +02:00
async.eachSeries(result, function (app, iteratorDone) {
if (app.appStoreId === '') return iteratorDone(); // appStoreId can be '' for dev apps
appstore.getAppUpdate(app, function (error, updateInfo) {
if (error) {
debug('Error getting app update info for %s', app.id, error);
return iteratorDone(); // continue to next
}
2016-11-14 17:00:30 +01:00
// skip if no next version is found
if (!updateInfo) {
delete gAppUpdateInfo[app.id];
return iteratorDone();
}
gAppUpdateInfo[app.id] = updateInfo;
// decide whether to send email
newState[app.id] = updateInfo.manifest.version;
2017-06-19 22:17:43 -07:00
if (oldState[app.id] === newState[app.id]) {
debug('Skipping notification of app update %s since user was already notified', app.id);
return iteratorDone();
}
2019-12-04 13:17:58 -08:00
const canAutoupdateApp = apps.canAutoupdateApp(app, updateInfo.manifest);
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
});
iteratorDone();
2017-06-19 22:17:43 -07:00
});
2019-05-06 22:35:01 +02:00
}, function () {
newState.box = loadState().box; // preserve the latest box state information
saveState(newState);
2019-05-06 22:35:01 +02:00
if (notificationPending.length === 0) return callback();
users.getAdmins(function (error, admins) {
2019-05-06 22:35:01 +02:00
if (error) {
console.error(error);
return callback();
}
2019-05-06 22:35:01 +02:00
async.eachSeries(admins, (admin, done) => mailer.appUpdatesAvailable(admin.email, notificationPending, true /* subscription */, done), callback);
});
});
});
});
}
function checkBoxUpdates(callback) {
2019-03-07 14:27:23 -08:00
assert.strictEqual(typeof callback, 'function');
debug('Checking Box Updates');
gBoxUpdateInfo = null;
appstore.getBoxUpdate(function (error, updateInfo) {
2016-01-23 05:37:22 -08:00
if (error || !updateInfo) return callback(error);
2017-10-25 20:52:05 -07:00
gBoxUpdateInfo = updateInfo;
2017-10-25 20:52:05 -07:00
// decide whether to send email
var state = loadState();
2017-10-25 20:52:05 -07:00
if (state.box === gBoxUpdateInfo.version) {
debug('Skipping notification of box update as user was already notified');
return callback();
}
const changelog = updateInfo.changelog.map((m) => `* ${m}\n`).join('');
const message = `Changelog:\n${changelog}\n\nClick [here](/#/settings) to update.\n\n`;
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);
2017-04-13 23:19:37 -07:00
callback();
});
});
}