Moved activate from themes to the bridge
refs: https://github.com/TryGhost/Ghost/commit/bf0823c9a2ddbc93ad0ddcec36eed130c8c5a203
- continuing the work of splitting up the theme service into logical components
Theme activations are a trickier piece of the theme split puzzle because they are called from the API and theme service on boot in different ways.
Activations require a theme to have been validated at different levels. Validations are also tricky - do they belong to the theme engine, or the theme service?
There are then several different flows for activations:
- On Boot
- API "activate" call
- API override on upload or install via setFromZip, which is a method in the storage layer
These calls all have quite different logical flows at the moment, and need to be unified
For now, I've moved the existing "activate" function onto the bridge. This allows the theme service to be split from the frontend, and refactoring can start from there.
I hope to move this so there is less code in the actual bridge very soon, but my goal is not to require any server packages in the frontend part of this
I think ideally:
- all activation code, including validation, should probably be part of the theme engine
- the theme engine should offer 3 methods: getActive() canActivate() and activate()
- the theme service is then only responsible for loading themes in and out of storage, JSON responses for the API, and handing themes to the frontend via the bridge at the appropriate moment
2021-04-27 14:49:48 +03:00
|
|
|
/**
|
|
|
|
* 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');
|
2021-04-26 16:38:57 +03:00
|
|
|
const config = require('./shared/config');
|
Moved activate from themes to the bridge
refs: https://github.com/TryGhost/Ghost/commit/bf0823c9a2ddbc93ad0ddcec36eed130c8c5a203
- continuing the work of splitting up the theme service into logical components
Theme activations are a trickier piece of the theme split puzzle because they are called from the API and theme service on boot in different ways.
Activations require a theme to have been validated at different levels. Validations are also tricky - do they belong to the theme engine, or the theme service?
There are then several different flows for activations:
- On Boot
- API "activate" call
- API override on upload or install via setFromZip, which is a method in the storage layer
These calls all have quite different logical flows at the moment, and need to be unified
For now, I've moved the existing "activate" function onto the bridge. This allows the theme service to be split from the frontend, and refactoring can start from there.
I hope to move this so there is less code in the actual bridge very soon, but my goal is not to require any server packages in the frontend part of this
I think ideally:
- all activation code, including validation, should probably be part of the theme engine
- the theme engine should offer 3 methods: getActive() canActivate() and activate()
- the theme service is then only responsible for loading themes in and out of storage, JSON responses for the API, and handing themes to the frontend via the bridge at the appropriate moment
2021-04-27 14:49:48 +03:00
|
|
|
const logging = require('./shared/logging');
|
2021-04-27 16:18:04 +03:00
|
|
|
const events = require('./server/lib/common/events');
|
2021-05-03 19:29:44 +03:00
|
|
|
const i18n = require('./shared/i18n');
|
2021-04-26 16:38:57 +03:00
|
|
|
const themeEngine = require('./frontend/services/theme-engine');
|
2021-05-04 18:49:35 +03:00
|
|
|
const settingsCache = require('./server/services/settings/cache');
|
2021-04-23 16:19:18 +03:00
|
|
|
|
|
|
|
class Bridge {
|
|
|
|
constructor() {
|
2021-04-26 13:53:15 +03:00
|
|
|
/**
|
|
|
|
* When locale changes, we reload theme translations
|
2021-06-09 18:32:48 +03:00
|
|
|
* @deprecated: the term "lang" was deprecated in favor of "locale" publicly in 4.0
|
2021-04-26 13:53:15 +03:00
|
|
|
*/
|
2021-05-04 18:49:35 +03:00
|
|
|
events.on('settings.lang.edited', (model) => {
|
|
|
|
this.getActiveTheme().initI18n({locale: model.get('value')});
|
2021-04-26 13:53:15 +03:00
|
|
|
});
|
2021-04-23 16:19:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getActiveTheme() {
|
|
|
|
return themeEngine.getActive();
|
|
|
|
}
|
|
|
|
|
Moved activate from themes to the bridge
refs: https://github.com/TryGhost/Ghost/commit/bf0823c9a2ddbc93ad0ddcec36eed130c8c5a203
- continuing the work of splitting up the theme service into logical components
Theme activations are a trickier piece of the theme split puzzle because they are called from the API and theme service on boot in different ways.
Activations require a theme to have been validated at different levels. Validations are also tricky - do they belong to the theme engine, or the theme service?
There are then several different flows for activations:
- On Boot
- API "activate" call
- API override on upload or install via setFromZip, which is a method in the storage layer
These calls all have quite different logical flows at the moment, and need to be unified
For now, I've moved the existing "activate" function onto the bridge. This allows the theme service to be split from the frontend, and refactoring can start from there.
I hope to move this so there is less code in the actual bridge very soon, but my goal is not to require any server packages in the frontend part of this
I think ideally:
- all activation code, including validation, should probably be part of the theme engine
- the theme engine should offer 3 methods: getActive() canActivate() and activate()
- the theme service is then only responsible for loading themes in and out of storage, JSON responses for the API, and handing themes to the frontend via the bridge at the appropriate moment
2021-04-27 14:49:48 +03:00
|
|
|
activateTheme(loadedTheme, checkedTheme, error) {
|
2021-05-04 18:49:35 +03:00
|
|
|
let settings = {
|
|
|
|
locale: settingsCache.get('lang')
|
|
|
|
};
|
Moved activate from themes to the bridge
refs: https://github.com/TryGhost/Ghost/commit/bf0823c9a2ddbc93ad0ddcec36eed130c8c5a203
- continuing the work of splitting up the theme service into logical components
Theme activations are a trickier piece of the theme split puzzle because they are called from the API and theme service on boot in different ways.
Activations require a theme to have been validated at different levels. Validations are also tricky - do they belong to the theme engine, or the theme service?
There are then several different flows for activations:
- On Boot
- API "activate" call
- API override on upload or install via setFromZip, which is a method in the storage layer
These calls all have quite different logical flows at the moment, and need to be unified
For now, I've moved the existing "activate" function onto the bridge. This allows the theme service to be split from the frontend, and refactoring can start from there.
I hope to move this so there is less code in the actual bridge very soon, but my goal is not to require any server packages in the frontend part of this
I think ideally:
- all activation code, including validation, should probably be part of the theme engine
- the theme engine should offer 3 methods: getActive() canActivate() and activate()
- the theme service is then only responsible for loading themes in and out of storage, JSON responses for the API, and handing themes to the frontend via the bridge at the appropriate moment
2021-04-27 14:49:48 +03:00
|
|
|
// 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');
|
|
|
|
}
|
|
|
|
|
2021-05-04 18:49:35 +03:00
|
|
|
themeEngine.setActive(settings, loadedTheme, checkedTheme, error);
|
Moved activate from themes to the bridge
refs: https://github.com/TryGhost/Ghost/commit/bf0823c9a2ddbc93ad0ddcec36eed130c8c5a203
- continuing the work of splitting up the theme service into logical components
Theme activations are a trickier piece of the theme split puzzle because they are called from the API and theme service on boot in different ways.
Activations require a theme to have been validated at different levels. Validations are also tricky - do they belong to the theme engine, or the theme service?
There are then several different flows for activations:
- On Boot
- API "activate" call
- API override on upload or install via setFromZip, which is a method in the storage layer
These calls all have quite different logical flows at the moment, and need to be unified
For now, I've moved the existing "activate" function onto the bridge. This allows the theme service to be split from the frontend, and refactoring can start from there.
I hope to move this so there is less code in the actual bridge very soon, but my goal is not to require any server packages in the frontend part of this
I think ideally:
- all activation code, including validation, should probably be part of the theme engine
- the theme engine should offer 3 methods: getActive() canActivate() and activate()
- the theme service is then only responsible for loading themes in and out of storage, JSON responses for the API, and handing themes to the frontend via the bridge at the appropriate moment
2021-04-27 14:49:48 +03:00
|
|
|
const currentGhostAPI = this.getActiveTheme().engine('ghost-api');
|
|
|
|
|
|
|
|
if (previousGhostAPI !== undefined && (previousGhostAPI !== currentGhostAPI)) {
|
|
|
|
events.emit('services.themes.api.changed');
|
|
|
|
const siteApp = require('./server/web/site/app');
|
|
|
|
siteApp.reload();
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
logging.error(new errors.InternalServerError({
|
|
|
|
message: i18n.t('errors.middleware.themehandler.activateFailed', {theme: loadedTheme.name}),
|
|
|
|
err: err
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 16:19:18 +03:00
|
|
|
getFrontendApiVersion() {
|
|
|
|
if (this.getActiveTheme()) {
|
|
|
|
return this.getActiveTheme().engine('ghost-api');
|
|
|
|
} else {
|
|
|
|
return config.get('api:versions:default');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const bridge = new Bridge();
|
|
|
|
|
|
|
|
module.exports = bridge;
|