2017-02-27 18:53:04 +03:00
|
|
|
/**
|
|
|
|
* Settings Lib
|
|
|
|
* A collection of utilities for handling settings including a cache
|
|
|
|
*/
|
2019-06-25 19:33:56 +03:00
|
|
|
const models = require('../../models');
|
|
|
|
const SettingsCache = require('./cache');
|
2017-02-27 18:53:04 +03:00
|
|
|
|
|
|
|
module.exports = {
|
2020-07-06 18:09:43 +03:00
|
|
|
async init() {
|
|
|
|
const settingsCollection = await models.Settings.populateDefaults();
|
|
|
|
SettingsCache.init(settingsCollection);
|
2020-07-01 19:16:57 +03:00
|
|
|
},
|
|
|
|
|
2020-07-06 18:09:43 +03:00
|
|
|
async reinit() {
|
2020-09-23 16:35:03 +03:00
|
|
|
const oldSettings = SettingsCache.getAll();
|
|
|
|
|
2020-07-01 19:16:57 +03:00
|
|
|
SettingsCache.shutdown();
|
2020-07-06 18:09:43 +03:00
|
|
|
const settingsCollection = await models.Settings.populateDefaults();
|
2020-09-23 16:35:03 +03:00
|
|
|
const newSettings = SettingsCache.init(settingsCollection);
|
|
|
|
|
2020-07-06 18:09:43 +03:00
|
|
|
for (const model of settingsCollection.models) {
|
2020-09-23 16:35:03 +03:00
|
|
|
const key = model.attributes.key;
|
|
|
|
|
|
|
|
// The type of setting is object. That's why we need to compare the value of the `value` property.
|
|
|
|
if (newSettings[key].value !== oldSettings[key].value) {
|
|
|
|
model.emitChange(key + '.' + 'edited', {});
|
|
|
|
}
|
2020-07-06 18:09:43 +03:00
|
|
|
}
|
2020-09-09 15:28:12 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles syncronization of routes.yaml hash loaded in the frontend with
|
|
|
|
* the value stored in the settings table.
|
|
|
|
* getRoutesHash is a function to allow keeping "frontend" decoupled from settings
|
|
|
|
*
|
|
|
|
* @param {function} getRoutesHash function fetching currently loaded routes file hash
|
|
|
|
*/
|
|
|
|
async syncRoutesHash(getRoutesHash) {
|
|
|
|
const currentRoutesHash = await getRoutesHash();
|
|
|
|
|
|
|
|
if (SettingsCache.get('routes_hash') !== currentRoutesHash) {
|
|
|
|
return await models.Settings.edit([{
|
|
|
|
key: 'routes_hash',
|
|
|
|
value: currentRoutesHash
|
|
|
|
}], {context: {internal: true}});
|
|
|
|
}
|
2017-02-27 18:53:04 +03:00
|
|
|
}
|
|
|
|
};
|