2014-01-21 12:45:27 +04:00
|
|
|
|
2014-02-05 12:40:30 +04:00
|
|
|
var _ = require('lodash'),
|
2014-08-17 10:17:23 +04:00
|
|
|
Promise = require('bluebird'),
|
2016-10-04 18:33:43 +03:00
|
|
|
logging = require('../logging'),
|
2016-10-06 15:27:35 +03:00
|
|
|
errors = require('../errors'),
|
2014-01-21 12:45:27 +04:00
|
|
|
api = require('../api'),
|
|
|
|
loader = require('./loader'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../i18n'),
|
2016-04-11 16:58:41 +03:00
|
|
|
config = require('../config'),
|
2014-01-21 12:45:27 +04:00
|
|
|
// Holds the available apps
|
|
|
|
availableApps = {};
|
|
|
|
|
|
|
|
function getInstalledApps() {
|
2017-04-24 20:41:00 +03:00
|
|
|
return api.settings.read({context: {internal: true}, key: 'installed_apps'}).then(function (response) {
|
2014-04-28 03:28:50 +04:00
|
|
|
var installed = response.settings[0];
|
|
|
|
|
2014-01-21 12:45:27 +04:00
|
|
|
installed.value = installed.value || '[]';
|
|
|
|
|
|
|
|
try {
|
|
|
|
installed = JSON.parse(installed.value);
|
|
|
|
} catch (e) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject(e);
|
2014-01-21 12:45:27 +04:00
|
|
|
}
|
|
|
|
|
2017-05-29 22:48:42 +03:00
|
|
|
return installed.concat(config.get('apps:internal'));
|
2014-01-21 12:45:27 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function saveInstalledApps(installedApps) {
|
|
|
|
return getInstalledApps().then(function (currentInstalledApps) {
|
2017-05-29 22:48:42 +03:00
|
|
|
var updatedAppsInstalled = _.difference(_.uniq(installedApps.concat(currentInstalledApps)), config.get('apps:internal'));
|
2014-01-21 12:45:27 +04:00
|
|
|
|
2017-04-24 20:41:00 +03:00
|
|
|
return api.settings.edit({settings: [{key: 'installed_apps', value: updatedAppsInstalled}]}, {context: {internal: true}});
|
2014-01-21 12:45:27 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init: function () {
|
|
|
|
var appsToLoad;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// We have to parse the value because it's a string
|
2017-04-24 20:41:00 +03:00
|
|
|
api.settings.read({context: {internal: true}, key: 'active_apps'}).then(function (response) {
|
2014-04-28 03:28:50 +04:00
|
|
|
var aApps = response.settings[0];
|
|
|
|
|
2014-01-21 12:45:27 +04:00
|
|
|
appsToLoad = JSON.parse(aApps.value) || [];
|
2016-04-11 16:58:41 +03:00
|
|
|
|
2017-05-29 22:48:42 +03:00
|
|
|
appsToLoad = appsToLoad.concat(config.get('apps:internal'));
|
2014-01-21 12:45:27 +04:00
|
|
|
});
|
2016-10-04 18:33:43 +03:00
|
|
|
} catch (err) {
|
2016-10-06 15:27:35 +03:00
|
|
|
logging.error(new errors.GhostError({
|
|
|
|
err: err,
|
|
|
|
context: i18n.t('errors.apps.failedToParseActiveAppsSettings.context'),
|
|
|
|
help: i18n.t('errors.apps.failedToParseActiveAppsSettings.help')
|
|
|
|
}));
|
2014-08-17 10:17:23 +04:00
|
|
|
|
|
|
|
return Promise.resolve();
|
2014-01-21 12:45:27 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Grab all installed apps, install any not already installed that are in appsToLoad.
|
|
|
|
return getInstalledApps().then(function (installedApps) {
|
|
|
|
var loadedApps = {},
|
|
|
|
recordLoadedApp = function (name, loadedApp) {
|
|
|
|
// After loading the app, add it to our hash of loaded apps
|
|
|
|
loadedApps[name] = loadedApp;
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.resolve(loadedApp);
|
2014-01-21 12:45:27 +04:00
|
|
|
},
|
|
|
|
loadPromises = _.map(appsToLoad, function (app) {
|
|
|
|
// If already installed, just activate the app
|
2016-06-11 21:23:27 +03:00
|
|
|
if (_.includes(installedApps, app)) {
|
2014-01-21 12:45:27 +04:00
|
|
|
return loader.activateAppByName(app).then(function (loadedApp) {
|
|
|
|
return recordLoadedApp(app, loadedApp);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Install, then activate the app
|
|
|
|
return loader.installAppByName(app).then(function () {
|
|
|
|
return loader.activateAppByName(app);
|
|
|
|
}).then(function (loadedApp) {
|
|
|
|
return recordLoadedApp(app, loadedApp);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.all(loadPromises).then(function () {
|
2014-01-21 12:45:27 +04:00
|
|
|
// Save our installed apps to settings
|
|
|
|
return saveInstalledApps(_.keys(loadedApps));
|
|
|
|
}).then(function () {
|
|
|
|
// Extend the loadedApps onto the available apps
|
|
|
|
_.extend(availableApps, loadedApps);
|
2014-08-17 10:17:23 +04:00
|
|
|
}).catch(function (err) {
|
2016-10-06 15:27:35 +03:00
|
|
|
logging.error(new errors.GhostError({
|
|
|
|
err: err,
|
|
|
|
context: i18n.t('errors.apps.appWillNotBeLoaded.error'),
|
|
|
|
help: i18n.t('errors.apps.appWillNotBeLoaded.help')
|
|
|
|
}));
|
2014-01-21 12:45:27 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
availableApps: availableApps
|
2014-08-17 10:17:23 +04:00
|
|
|
};
|