Ghost/core/bridge.js
Hannah Wolfe bd597db829
Moved settings/cache to shared/settings-cache
- This is part of the quest to separate the frontend and server & get rid of all the places where there are cross-requires
- At the moment the settings cache is one big shared cache used by the frontend and server liberally
- This change doesn't really solve the fundamental problems, as we still depend on events, and requires from inside frontend
- However it allows us to control the misuse slightly better by getting rid of restricted requires and turning on that eslint ruleset
2021-06-30 15:49:10 +01:00

84 lines
2.8 KiB
JavaScript

/**
* The Bridge
*
* The bridge is responsible for handing communication from the server to the frontend.
* Data should only be flowing server -> frontend.
* As the architecture improves, the number of cross requires here should go down
* Eventually, the aim is to make this a component that is initialised on boot and is either handed to or actively creates the frontend, if the frontend is desired.
*
* This file is a great place for all the cross-component event handling in lieu of refactoring
*/
const errors = require('@tryghost/errors');
const config = require('./shared/config');
const logging = require('@tryghost/logging');
const events = require('./server/lib/common/events');
const tpl = require('@tryghost/tpl');
const themeEngine = require('./frontend/services/theme-engine');
const settingsCache = require('./shared/settings-cache');
const messages = {
activateFailed: 'Unable to activate the theme "{theme}".'
};
class Bridge {
constructor() {
/**
* When locale changes, we reload theme translations
* @deprecated: the term "lang" was deprecated in favor of "locale" publicly in 4.0
*/
events.on('settings.lang.edited', (model) => {
this.getActiveTheme().initI18n({locale: model.get('value')});
});
}
getActiveTheme() {
return themeEngine.getActive();
}
activateTheme(loadedTheme, checkedTheme, error) {
let settings = {
locale: settingsCache.get('lang')
};
// no need to check the score, activation should be used in combination with validate.check
// Use the two theme objects to set the current active theme
try {
let previousGhostAPI;
if (this.getActiveTheme()) {
previousGhostAPI = this.getActiveTheme().engine('ghost-api');
}
themeEngine.setActive(settings, loadedTheme, checkedTheme, error);
const currentGhostAPI = this.getActiveTheme().engine('ghost-api');
if (previousGhostAPI !== undefined && (previousGhostAPI !== currentGhostAPI)) {
events.emit('services.themes.api.changed');
this.reloadFrontend();
}
} catch (err) {
logging.error(new errors.InternalServerError({
message: tpl(messages.activateFailed, {theme: loadedTheme.name}),
err: err
}));
}
}
getFrontendApiVersion() {
if (this.getActiveTheme()) {
return this.getActiveTheme().engine('ghost-api');
} else {
return config.get('api:versions:default');
}
}
reloadFrontend() {
const apiVersion = this.getFrontendApiVersion();
const siteApp = require('./server/web/site/app');
siteApp.reload({apiVersion});
}
}
const bridge = new Bridge();
module.exports = bridge;