2015-07-20 00:09:47 -07:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
checkAppUpdates: checkAppUpdates,
|
|
|
|
|
checkBoxUpdates: checkBoxUpdates,
|
|
|
|
|
|
2016-01-25 16:46:54 -08:00
|
|
|
getUpdateInfo: getUpdateInfo,
|
2016-11-07 15:14:06 +01:00
|
|
|
resetUpdateInfo: resetUpdateInfo,
|
2017-07-23 10:53:16 -07:00
|
|
|
resetAppUpdateInfo: resetAppUpdateInfo,
|
|
|
|
|
|
|
|
|
|
_setUpdateInfo: setUpdateInfo
|
2015-07-20 00:09:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var apps = require('./apps.js'),
|
2017-04-13 01:23:11 -07:00
|
|
|
appstore = require('./appstore.js'),
|
2019-03-07 14:27:23 -08:00
|
|
|
assert = require('assert'),
|
2015-07-20 00:09:47 -07:00
|
|
|
async = require('async'),
|
2017-01-27 09:24:36 -08:00
|
|
|
constants = require('./constants.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
debug = require('debug')('box:updatechecker'),
|
|
|
|
|
mailer = require('./mailer.js'),
|
2019-03-07 13:34:46 -08:00
|
|
|
notifications = require('./notifications.js'),
|
2015-09-22 15:34:19 -07:00
|
|
|
paths = require('./paths.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
safe = require('safetydance'),
|
2019-03-10 16:05:27 -07:00
|
|
|
settings = require('./settings.js'),
|
|
|
|
|
users = require('./users.js');
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
var gAppUpdateInfo = { }, // id -> update info { creationDate, manifest }
|
2016-01-25 13:57:59 -08:00
|
|
|
gBoxUpdateInfo = null; // { version, changelog, upgrade, sourceTarballUrl }
|
2015-09-22 15:34:19 -07:00
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
|
|
|
|
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;
|
2016-11-07 15:14:06 +01:00
|
|
|
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
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
debug('Checking App Updates');
|
|
|
|
|
|
2016-01-23 11:08:01 -08:00
|
|
|
gAppUpdateInfo = { };
|
2016-07-27 18:25:53 -07:00
|
|
|
var oldState = loadState();
|
2016-08-02 17:38:59 -07:00
|
|
|
var newState = { }; // create new state so that old app ids are removed
|
2016-01-23 11:08:01 -08:00
|
|
|
|
2019-05-06 16:31:57 +02:00
|
|
|
settings.getAppAutoupdatePattern(function (error, result) {
|
2016-01-23 05:35:57 -08:00
|
|
|
if (error) return callback(error);
|
2019-05-06 16:31:57 +02:00
|
|
|
const autoupdatesEnabled = (result !== constants.AUTOUPDATE_PATTERN_NEVER);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2019-05-06 22:09:18 +02:00
|
|
|
var notificationPending = [];
|
|
|
|
|
|
2019-05-06 17:42:26 +02:00
|
|
|
apps.getAll(function (error, result) {
|
2019-05-06 16:31:57 +02:00
|
|
|
if (error) return callback(error);
|
2016-07-27 18:25:53 -07:00
|
|
|
|
2019-05-06 17:42:26 +02:00
|
|
|
async.eachSeries(result, function (app, iteratorDone) {
|
2019-05-06 16:31:57 +02:00
|
|
|
if (app.appStoreId === '') return iteratorDone(); // appStoreId can be '' for dev apps
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2019-05-06 16:31:57 +02:00
|
|
|
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
|
|
|
|
2019-05-06 16:31:57 +02:00
|
|
|
// skip if no next version is found
|
|
|
|
|
if (!updateInfo) {
|
|
|
|
|
delete gAppUpdateInfo[app.id];
|
|
|
|
|
return iteratorDone();
|
|
|
|
|
}
|
2016-07-27 17:46:58 -07:00
|
|
|
|
2019-05-06 16:31:57 +02:00
|
|
|
gAppUpdateInfo[app.id] = updateInfo;
|
2016-01-23 05:56:05 -08:00
|
|
|
|
2019-05-06 16:31:57 +02:00
|
|
|
// decide whether to send email
|
|
|
|
|
newState[app.id] = updateInfo.manifest.version;
|
2017-06-19 22:17:43 -07:00
|
|
|
|
2019-05-06 16:31:57 +02: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-05-03 19:56:25 -07:00
|
|
|
|
2019-12-04 13:17:58 -08:00
|
|
|
const canAutoupdateApp = apps.canAutoupdateApp(app, updateInfo.manifest);
|
|
|
|
|
if (autoupdatesEnabled && canAutoupdateApp) return iteratorDone();
|
2019-05-06 16:31:57 +02:00
|
|
|
|
|
|
|
|
debug('Notifying of app update for %s from %s to %s', app.id, app.manifest.version, updateInfo.manifest.version);
|
2019-05-06 22:09:18 +02:00
|
|
|
notificationPending.push({
|
|
|
|
|
app: app,
|
|
|
|
|
updateInfo: updateInfo
|
2017-01-27 09:24:36 -08:00
|
|
|
});
|
2019-05-06 22:09:18 +02:00
|
|
|
|
|
|
|
|
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:09:18 +02:00
|
|
|
|
2019-05-06 22:35:01 +02:00
|
|
|
if (notificationPending.length === 0) return callback();
|
2019-05-06 22:09:18 +02:00
|
|
|
|
2020-02-21 12:17:06 -08:00
|
|
|
users.getAdmins(function (error, admins) {
|
2019-05-06 22:35:01 +02:00
|
|
|
if (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
2019-05-06 22:09:18 +02:00
|
|
|
|
2019-05-06 22:35:01 +02:00
|
|
|
async.eachSeries(admins, (admin, done) => mailer.appUpdatesAvailable(admin.email, notificationPending, true /* subscription */, done), callback);
|
|
|
|
|
});
|
2019-05-06 22:09:18 +02:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-23 05:31:55 -08:00
|
|
|
function checkBoxUpdates(callback) {
|
2019-03-07 14:27:23 -08:00
|
|
|
assert.strictEqual(typeof callback, 'function');
|
2016-01-23 05:31:55 -08:00
|
|
|
|
2015-07-20 00:09:47 -07:00
|
|
|
debug('Checking Box Updates');
|
|
|
|
|
|
2016-01-23 11:08:01 -08:00
|
|
|
gBoxUpdateInfo = null;
|
|
|
|
|
|
2017-04-13 01:31:22 -07:00
|
|
|
appstore.getBoxUpdate(function (error, updateInfo) {
|
2016-01-23 05:37:22 -08:00
|
|
|
if (error || !updateInfo) return callback(error);
|
2016-01-23 05:31:55 -08:00
|
|
|
|
2017-10-25 20:52:05 -07:00
|
|
|
gBoxUpdateInfo = updateInfo;
|
2016-01-23 05:31:55 -08:00
|
|
|
|
2017-10-25 20:52:05 -07:00
|
|
|
// decide whether to send email
|
|
|
|
|
var state = loadState();
|
2015-09-22 15:34:19 -07:00
|
|
|
|
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();
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2019-03-07 14:32:31 -08:00
|
|
|
const changelog = updateInfo.changelog.map((m) => `* ${m}\n`).join('');
|
|
|
|
|
|
2019-03-07 14:49:38 -08:00
|
|
|
const message = `Changelog:\n${changelog}\n\nClick [here](/#/settings) to update.\n\n`;
|
2017-01-27 08:03:55 -08:00
|
|
|
|
2019-03-07 14:49:38 -08:00
|
|
|
notifications.alert(notifications.ALERT_BOX_UPDATE, `Cloudron v${updateInfo.version} is available`, message, function (error) {
|
2019-03-07 14:32:31 -08:00
|
|
|
if (error) return callback(error);
|
2017-01-27 08:03:55 -08:00
|
|
|
|
2019-03-07 14:32:31 -08:00
|
|
|
state.box = updateInfo.version;
|
|
|
|
|
saveState(state);
|
2017-04-13 23:19:37 -07:00
|
|
|
|
2019-03-07 14:32:31 -08:00
|
|
|
callback();
|
2016-01-23 05:31:55 -08:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|