From b56557ea04ac5e76c59ee00cd65476ac36158f34 Mon Sep 17 00:00:00 2001 From: Naz Date: Fri, 22 Apr 2022 10:29:55 +0800 Subject: [PATCH] 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 --- core/boot.js | 2 + .../api-version-compatibility/index.js | 24 +++++++++ .../version-notifications-data-service.js | 50 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 core/server/services/api-version-compatibility/index.js create mode 100644 core/server/services/api-version-compatibility/version-notifications-data-service.js diff --git a/core/boot.js b/core/boot.js index 7775818a3a..93037872bc 100644 --- a/core/boot.js +++ b/core/boot.js @@ -266,6 +266,7 @@ async function initServices({config}) { const webhooks = require('./server/services/webhooks'); const appService = require('./frontend/services/apps'); const limits = require('./server/services/limits'); + const apiVersionCompatibility = require('./server/services/api-version-compatibility'); const scheduling = require('./server/adapters/scheduling'); const urlUtils = require('./shared/url-utils'); @@ -286,6 +287,7 @@ async function initServices({config}) { mega.listen(), webhooks.listen(), appService.init(), + apiVersionCompatibility.init(), scheduling.init({ apiUrl: urlUtils.urlFor('api', {version: defaultApiVersion, versionType: 'admin'}, true) }) diff --git a/core/server/services/api-version-compatibility/index.js b/core/server/services/api-version-compatibility/index.js new file mode 100644 index 0000000000..b138db93d9 --- /dev/null +++ b/core/server/services/api-version-compatibility/index.js @@ -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; diff --git a/core/server/services/api-version-compatibility/version-notifications-data-service.js b/core/server/services/api-version-compatibility/version-notifications-data-service.js new file mode 100644 index 0000000000..43d1bd0551 --- /dev/null +++ b/core/server/services/api-version-compatibility/version-notifications-data-service.js @@ -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 +};