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'),
|
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'),
|
2015-09-22 15:34:19 -07:00
|
|
|
paths = require('./paths.js'),
|
2015-07-20 00:09:47 -07:00
|
|
|
safe = require('safetydance'),
|
2017-04-13 01:31:22 -07:00
|
|
|
settings = require('./settings.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
|
|
|
|
2016-01-23 05:31:55 -08:00
|
|
|
var NOOP_CALLBACK = function (error) { if (error) debug(error); };
|
|
|
|
|
|
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) {
|
|
|
|
|
callback = callback || NOOP_CALLBACK; // null when called from a timer task
|
|
|
|
|
|
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
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
apps.getAll(function (error, apps) {
|
2016-01-23 05:35:57 -08:00
|
|
|
if (error) return callback(error);
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
async.eachSeries(apps, function (app, iteratorDone) {
|
|
|
|
|
if (app.appStoreId === '') return iteratorDone(); // appStoreId can be '' for dev apps
|
|
|
|
|
|
2017-04-13 01:23:11 -07:00
|
|
|
appstore.getAppUpdate(app, function (error, updateInfo) {
|
2016-07-27 18:25:53 -07:00
|
|
|
if (error) {
|
|
|
|
|
debug('Error getting app update info for %s', app.id, error);
|
|
|
|
|
return iteratorDone(); // continue to next
|
|
|
|
|
}
|
2015-07-20 00:09:47 -07:00
|
|
|
|
2016-11-14 17:00:30 +01:00
|
|
|
// skip if no next version is found
|
|
|
|
|
if (!updateInfo) {
|
|
|
|
|
delete gAppUpdateInfo[app.id];
|
|
|
|
|
return iteratorDone();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
gAppUpdateInfo[app.id] = updateInfo;
|
2016-07-27 17:46:58 -07:00
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
// decide whether to send email
|
|
|
|
|
newState[app.id] = updateInfo.manifest.version;
|
2016-01-23 05:56:05 -08:00
|
|
|
|
2016-07-27 18:25:53 -07:00
|
|
|
if (oldState[app.id] === newState[app.id]) {
|
|
|
|
|
debug('Skipping notification of app update %s since user was already notified', app.id);
|
2017-06-19 22:17:43 -07:00
|
|
|
return iteratorDone();
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-21 22:17:32 -07:00
|
|
|
appstore.getSubscription(function (error, result) {
|
2017-06-19 22:17:43 -07:00
|
|
|
if (error) {
|
2017-06-19 22:18:31 -07:00
|
|
|
debug('Error getting subscription for %s', app.id, error);
|
2017-06-19 22:17:43 -07:00
|
|
|
return iteratorDone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// always send notifications if user is on the free plan
|
|
|
|
|
if (result.plan.id === 'free' || result.plan.id === 'undecided') {
|
|
|
|
|
debug('Notifying user of app update for %s from %s to %s', app.id, app.manifest.version, updateInfo.manifest.version);
|
2017-11-02 18:31:51 -07:00
|
|
|
mailer.appUpdateAvailable(app, false /* subscription */, updateInfo);
|
2017-06-19 22:17:43 -07:00
|
|
|
return iteratorDone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// only send notifications if update pattern is 'never'
|
2018-02-06 18:57:30 +01:00
|
|
|
settings.getAppAutoupdatePattern(function (error, result) {
|
2017-06-19 14:34:34 +02:00
|
|
|
if (error) {
|
|
|
|
|
debug(error);
|
2017-06-19 22:17:43 -07:00
|
|
|
} else if (result === constants.AUTOUPDATE_PATTERN_NEVER) {
|
2017-06-19 13:27:08 +02:00
|
|
|
debug('Notifying user of app update for %s from %s to %s', app.id, app.manifest.version, updateInfo.manifest.version);
|
2017-11-02 18:31:51 -07:00
|
|
|
mailer.appUpdateAvailable(app, true /* hasSubscription */, updateInfo);
|
2017-06-19 13:27:08 +02:00
|
|
|
}
|
2017-01-27 09:24:36 -08:00
|
|
|
|
2017-06-19 22:17:43 -07:00
|
|
|
iteratorDone();
|
2017-01-27 09:24:36 -08:00
|
|
|
});
|
2017-06-19 22:17:43 -07:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
2016-07-27 17:46:58 -07:00
|
|
|
}, function () {
|
2016-08-02 17:38:59 -07:00
|
|
|
newState.box = loadState().box; // preserve the latest box state information
|
2016-07-27 17:46:58 -07:00
|
|
|
saveState(newState);
|
|
|
|
|
callback();
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-01-23 05:31:55 -08:00
|
|
|
function checkBoxUpdates(callback) {
|
|
|
|
|
callback = callback || NOOP_CALLBACK; // null when called from a timer task
|
|
|
|
|
|
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
|
|
|
|
2017-10-25 20:52:05 -07:00
|
|
|
appstore.getSubscription(function (error, result) {
|
|
|
|
|
if (error) return callback(error);
|
2017-01-27 08:03:55 -08:00
|
|
|
|
2017-10-25 20:52:05 -07:00
|
|
|
function done() {
|
|
|
|
|
state.box = updateInfo.version;
|
|
|
|
|
saveState(state);
|
|
|
|
|
callback();
|
2017-01-27 08:03:55 -08:00
|
|
|
}
|
|
|
|
|
|
2017-10-25 20:52:05 -07:00
|
|
|
// always send notifications if user is on the free plan
|
|
|
|
|
if (result.plan.id === 'free' || result.plan.id === 'undecided') {
|
2017-11-02 18:31:51 -07:00
|
|
|
mailer.boxUpdateAvailable(false /* hasSubscription */, updateInfo.version, updateInfo.changelog);
|
2017-10-25 20:52:05 -07:00
|
|
|
return done();
|
|
|
|
|
}
|
2017-01-27 08:03:55 -08:00
|
|
|
|
2017-10-25 20:52:05 -07:00
|
|
|
// only send notifications if update pattern is 'never'
|
2018-02-06 18:57:30 +01:00
|
|
|
settings.getAppAutoupdatePattern(function (error, result) {
|
2017-10-25 20:52:05 -07:00
|
|
|
if (error) debug(error);
|
2017-11-02 18:31:51 -07:00
|
|
|
else if (result === constants.AUTOUPDATE_PATTERN_NEVER) mailer.boxUpdateAvailable(true /* hasSubscription */, updateInfo.version, updateInfo.changelog);
|
2017-04-13 23:19:37 -07:00
|
|
|
|
2017-10-25 20:52:05 -07:00
|
|
|
done();
|
2017-04-13 23:19:37 -07:00
|
|
|
});
|
2016-01-23 05:31:55 -08:00
|
|
|
});
|
2015-07-20 00:09:47 -07:00
|
|
|
});
|
|
|
|
|
}
|