mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Hooked up api version compatibility service
refs https://github.com/TryGhost/Toolbox/issues/280 - This is continuation of putting pieces together to allow Ghost notifying owner and admin users about version mismatch errors - This changeset adds the api-version-compatibility intialization during the boot (needed to be able to pospone the send email initialization so that it's testeable from the e2e tests) - There's also a data service which handles fetching/saving notifications and fetching emails of users that should be notified
This commit is contained in:
parent
b7189a99e4
commit
b56557ea04
3 changed files with 76 additions and 0 deletions
|
@ -266,6 +266,7 @@ async function initServices({config}) {
|
||||||
const webhooks = require('./server/services/webhooks');
|
const webhooks = require('./server/services/webhooks');
|
||||||
const appService = require('./frontend/services/apps');
|
const appService = require('./frontend/services/apps');
|
||||||
const limits = require('./server/services/limits');
|
const limits = require('./server/services/limits');
|
||||||
|
const apiVersionCompatibility = require('./server/services/api-version-compatibility');
|
||||||
const scheduling = require('./server/adapters/scheduling');
|
const scheduling = require('./server/adapters/scheduling');
|
||||||
|
|
||||||
const urlUtils = require('./shared/url-utils');
|
const urlUtils = require('./shared/url-utils');
|
||||||
|
@ -286,6 +287,7 @@ async function initServices({config}) {
|
||||||
mega.listen(),
|
mega.listen(),
|
||||||
webhooks.listen(),
|
webhooks.listen(),
|
||||||
appService.init(),
|
appService.init(),
|
||||||
|
apiVersionCompatibility.init(),
|
||||||
scheduling.init({
|
scheduling.init({
|
||||||
apiUrl: urlUtils.urlFor('api', {version: defaultApiVersion, versionType: 'admin'}, true)
|
apiUrl: urlUtils.urlFor('api', {version: defaultApiVersion, versionType: 'admin'}, true)
|
||||||
})
|
})
|
||||||
|
|
24
core/server/services/api-version-compatibility/index.js
Normal file
24
core/server/services/api-version-compatibility/index.js
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
const APIVersionCompatibilityService = require('@tryghost/api-version-compatibility-service');
|
||||||
|
const {GhostMailer} = require('../mail');
|
||||||
|
|
||||||
|
const {
|
||||||
|
getNotificationEmails,
|
||||||
|
fetchNotification,
|
||||||
|
saveNotification
|
||||||
|
} = require('./version-notifications-data-service');
|
||||||
|
|
||||||
|
const ghostMailer = new GhostMailer();
|
||||||
|
|
||||||
|
const init = () => {
|
||||||
|
this.APIVersionCompatibilityServiceInstance = new APIVersionCompatibilityService({
|
||||||
|
sendEmail: (options) => {
|
||||||
|
return ghostMailer.send(options);
|
||||||
|
},
|
||||||
|
fetchEmailsToNotify: getNotificationEmails,
|
||||||
|
fetchHandled: fetchNotification,
|
||||||
|
saveHandled: saveNotification
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.APIVersionCompatibilityServiceInstance;
|
||||||
|
module.exports.init = init;
|
|
@ -0,0 +1,50 @@
|
||||||
|
const settingsService = require('../../services/settings');
|
||||||
|
const models = require('../../models');
|
||||||
|
const settingsBREADServiceInstance = settingsService.getSettingsBREADServiceInstance();
|
||||||
|
|
||||||
|
const internalContext = {
|
||||||
|
internal: true
|
||||||
|
};
|
||||||
|
|
||||||
|
const fetchNotification = async (acceptVersion) => {
|
||||||
|
const setting = await settingsBREADServiceInstance.read('version_notifications', internalContext);
|
||||||
|
const versionNotifications = JSON.parse(setting.version_notifications.value);
|
||||||
|
|
||||||
|
return versionNotifications.find(version => version === acceptVersion);
|
||||||
|
};
|
||||||
|
|
||||||
|
const saveNotification = async (acceptVersion) => {
|
||||||
|
const setting = await settingsBREADServiceInstance.read('version_notifications', internalContext);
|
||||||
|
const versionNotifications = JSON.parse(setting.version_notifications.value);
|
||||||
|
|
||||||
|
if (!versionNotifications.find(version => version === acceptVersion)) {
|
||||||
|
versionNotifications.push(acceptVersion);
|
||||||
|
|
||||||
|
return settingsBREADServiceInstance.edit([{
|
||||||
|
key: 'version_notifications',
|
||||||
|
value: JSON.stringify(versionNotifications)
|
||||||
|
}], {
|
||||||
|
context: internalContext
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getNotificationEmails = async () => {
|
||||||
|
const data = await models.User.findAll(Object.assign({
|
||||||
|
withRelated: ['roles'],
|
||||||
|
filter: 'status:active'
|
||||||
|
}, internalContext));
|
||||||
|
|
||||||
|
const adminEmails = data
|
||||||
|
.toJSON()
|
||||||
|
.filter(user => ['Owner', 'Administrator'].includes(user.roles[0].name))
|
||||||
|
.map(user => user.email);
|
||||||
|
|
||||||
|
return adminEmails;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
fetchNotification,
|
||||||
|
saveNotification,
|
||||||
|
getNotificationEmails
|
||||||
|
};
|
Loading…
Add table
Reference in a new issue