mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-11-24 06:35:49 +03:00
Removed backwards compatibility for slack setting
refs: https://github.com/TryGhost/Team/issues/1625 - we changed slack to be two separate settings some time ago - this cleans up the compatibility code
This commit is contained in:
parent
93339b3e42
commit
3f0172e755
@ -5,58 +5,9 @@ const settingsCache = require('../../../../../../shared/settings-cache');
|
||||
const {WRITABLE_KEYS_ALLOWLIST} = require('../../../../../../shared/labs');
|
||||
|
||||
const DEPRECATED_SETTINGS = [
|
||||
'bulk_email_settings',
|
||||
'slack'
|
||||
'slack', 'bulk_email_settings'
|
||||
];
|
||||
|
||||
const deprecatedSupportedSettingsOneToManyMap = {
|
||||
slack: [{
|
||||
from: '[0].url',
|
||||
to: {
|
||||
key: 'slack_url',
|
||||
group: 'slack',
|
||||
type: 'string'
|
||||
}
|
||||
}, {
|
||||
from: '[0].username',
|
||||
to: {
|
||||
key: 'slack_username',
|
||||
group: 'slack',
|
||||
type: 'string'
|
||||
}
|
||||
}]
|
||||
};
|
||||
|
||||
const getMappedDeprecatedSettings = (settings) => {
|
||||
const mappedSettings = [];
|
||||
|
||||
for (const key in deprecatedSupportedSettingsOneToManyMap) {
|
||||
const deprecatedSetting = settings.find(setting => setting.key === key);
|
||||
|
||||
if (deprecatedSetting) {
|
||||
let deprecatedSettingValue;
|
||||
|
||||
try {
|
||||
deprecatedSettingValue = JSON.parse(deprecatedSetting.value);
|
||||
} catch (err) {
|
||||
// ignore the value if it's invalid
|
||||
}
|
||||
|
||||
if (deprecatedSettingValue) {
|
||||
deprecatedSupportedSettingsOneToManyMap[key].forEach(({from, to}) => {
|
||||
const value = _.get(deprecatedSettingValue, from);
|
||||
mappedSettings.push({
|
||||
key: to.key,
|
||||
value: value
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return mappedSettings;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
browse(apiConfig, frame) {
|
||||
if (frame.options.type) {
|
||||
@ -84,16 +35,6 @@ module.exports = {
|
||||
return !settingFlagsArr.includes('RO');
|
||||
});
|
||||
|
||||
const mappedDeprecatedSettings = getMappedDeprecatedSettings(frame.data.settings);
|
||||
mappedDeprecatedSettings.forEach((setting) => {
|
||||
// NOTE: give priority for non-deprecated setting values if they exist
|
||||
const nonDeprecatedExists = frame.data.settings.find(s => s.key === setting.key);
|
||||
|
||||
if (!nonDeprecatedExists) {
|
||||
frame.data.settings.push(setting);
|
||||
}
|
||||
});
|
||||
|
||||
frame.data.settings.forEach((setting) => {
|
||||
// CASE: transform objects/arrays into string (we store stringified objects in the db)
|
||||
// @TODO: This belongs into the model layer. We should stringify before saving and parse when fetching from db.
|
||||
|
@ -1,37 +1,7 @@
|
||||
const _ = require('lodash');
|
||||
|
||||
const extraAttrs = require('../utils/extra-attrs');
|
||||
const url = require('../utils/url');
|
||||
|
||||
module.exports = (attrs, frame) => {
|
||||
module.exports = (attrs) => {
|
||||
url.forSettings(attrs);
|
||||
extraAttrs.forSettings(attrs, frame);
|
||||
|
||||
// NOTE: The cleanup of deprecated ghost_head/ghost_foot has to happen here
|
||||
// because codeinjection_head/codeinjection_foot are assigned on a previous
|
||||
// `forSettings` step. This logic can be rewritten once we get rid of deprecated
|
||||
// fields completely.
|
||||
if (_.isArray(attrs)) {
|
||||
const keysToFilter = [];
|
||||
|
||||
// NOTE: to support edits of deprecated 'slack' setting artificial 'slack_url' and 'slack_username'
|
||||
// were added to the request body in the input serializer. These should not be returned in response
|
||||
// body unless directly requested
|
||||
if (frame.original.body && frame.original.body.settings) {
|
||||
const requestedEditSlackUrl = frame.original.body.settings.find(s => s.key === 'slack_url');
|
||||
const requestedEditSlackUsername = frame.original.body.settings.find(s => s.key === 'slack_username');
|
||||
|
||||
if (!requestedEditSlackUrl) {
|
||||
keysToFilter.push('slack_url');
|
||||
}
|
||||
|
||||
if (!requestedEditSlackUsername) {
|
||||
keysToFilter.push('slack_username');
|
||||
}
|
||||
}
|
||||
|
||||
attrs = _.filter(attrs, attr => !(keysToFilter.includes(attr.key)));
|
||||
}
|
||||
|
||||
return attrs;
|
||||
};
|
||||
|
@ -31,56 +31,3 @@ module.exports.forPost = (frame, model, attrs) => {
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports.forSettings = (attrs, frame) => {
|
||||
const _ = require('lodash');
|
||||
|
||||
// @TODO: https://github.com/TryGhost/Ghost/issues/10106
|
||||
// @NOTE: Admin & Content API return a different format, needs two mappers
|
||||
if (_.isArray(attrs)) {
|
||||
// CASE: read single setting
|
||||
if (frame.original.params && frame.original.params.key) {
|
||||
if (frame.original.params.key === 'slack_url'
|
||||
|| frame.original.params.key === 'slack_username') {
|
||||
return;
|
||||
}
|
||||
}
|
||||
// CASE: edit
|
||||
if (frame.original.body && frame.original.body.settings) {
|
||||
frame.original.body.settings.forEach((setting) => {
|
||||
if (setting.key === 'slack') {
|
||||
const slackURL = _.cloneDeep(_.find(attrs, {key: 'slack_url'}));
|
||||
const slackUsername = _.cloneDeep(_.find(attrs, {key: 'slack_username'}));
|
||||
|
||||
if (slackURL || slackUsername) {
|
||||
const slack = slackURL || slackUsername;
|
||||
slack.key = 'slack';
|
||||
slack.value = JSON.stringify([{
|
||||
url: slackURL && slackURL.value,
|
||||
username: slackUsername && slackUsername.value
|
||||
}]);
|
||||
|
||||
attrs.push(slack);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// CASE: browse all settings, add extra keys and keep deprecated
|
||||
const slackURL = _.cloneDeep(_.find(attrs, {key: 'slack_url'}));
|
||||
const slackUsername = _.cloneDeep(_.find(attrs, {key: 'slack_username'}));
|
||||
|
||||
if (slackURL || slackUsername) {
|
||||
const slack = slackURL || slackUsername;
|
||||
slack.key = 'slack';
|
||||
slack.value = JSON.stringify([{
|
||||
url: slackURL && slackURL.value,
|
||||
username: slackUsername && slackUsername.value
|
||||
}]);
|
||||
|
||||
attrs.push(slack);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
@ -174,16 +174,6 @@ Object {
|
||||
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||
"value": false,
|
||||
},
|
||||
Object {
|
||||
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||
"flags": null,
|
||||
"group": "slack",
|
||||
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||
"key": "slack",
|
||||
"type": "string",
|
||||
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||
"value": "[{\\"url\\":\\"https://overrides.tld\\",\\"username\\":\\"New Slack Username\\"}]",
|
||||
},
|
||||
],
|
||||
}
|
||||
`;
|
||||
@ -192,7 +182,7 @@ exports[`Settings API Can edit a setting 2: [headers] 1`] = `
|
||||
Object {
|
||||
"access-control-allow-origin": "http://127.0.0.1:2369",
|
||||
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
|
||||
"content-length": "4086",
|
||||
"content-length": "3829",
|
||||
"content-type": "application/json; charset=utf-8",
|
||||
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
|
||||
"vary": "Origin, Accept-Encoding",
|
||||
@ -921,16 +911,6 @@ Object {
|
||||
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||
"value": null,
|
||||
},
|
||||
Object {
|
||||
"created_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||
"flags": null,
|
||||
"group": "slack",
|
||||
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
|
||||
"key": "slack",
|
||||
"type": "string",
|
||||
"updated_at": StringMatching /\\\\d\\{4\\}-\\\\d\\{2\\}-\\\\d\\{2\\}T\\\\d\\{2\\}:\\\\d\\{2\\}:\\\\d\\{2\\}\\\\\\.000Z/,
|
||||
"value": "[{\\"url\\":\\"\\",\\"username\\":\\"Ghost\\"}]",
|
||||
},
|
||||
],
|
||||
}
|
||||
`;
|
||||
@ -951,7 +931,7 @@ exports[`Settings API Can request all settings 2: [headers] 1`] = `
|
||||
Object {
|
||||
"access-control-allow-origin": "http://127.0.0.1:2369",
|
||||
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
|
||||
"content-length": "14854",
|
||||
"content-length": "14631",
|
||||
"content-type": "application/json; charset=utf-8",
|
||||
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
|
||||
"vary": "Origin, Accept-Encoding",
|
||||
|
@ -2,7 +2,7 @@ const assert = require('assert');
|
||||
const {agentProvider, fixtureManager, mockManager, matchers} = require('../../utils/e2e-framework');
|
||||
const {stringMatching, anyEtag, anyObjectId, anyISODateTime} = matchers;
|
||||
|
||||
const CURRENT_SETTINGS_COUNT = 70;
|
||||
const CURRENT_SETTINGS_COUNT = 69;
|
||||
|
||||
const settingsMatcher = {
|
||||
id: anyObjectId,
|
||||
@ -75,13 +75,6 @@ describe('Settings API', function () {
|
||||
label: 'label1'
|
||||
}])
|
||||
},
|
||||
{
|
||||
key: 'slack',
|
||||
value: JSON.stringify([{
|
||||
url: 'https://overrides.tld',
|
||||
username: 'Overrides Username'
|
||||
}])
|
||||
},
|
||||
{
|
||||
key: 'slack_username',
|
||||
value: 'New Slack Username'
|
||||
|
@ -284,11 +284,6 @@ const defaultSettingsKeyTypes = [
|
||||
type: 'string',
|
||||
group: 'amp'
|
||||
},
|
||||
{
|
||||
key: 'slack',
|
||||
type: 'string',
|
||||
group: 'slack'
|
||||
},
|
||||
{
|
||||
key: 'slack_url',
|
||||
type: 'string',
|
||||
@ -645,7 +640,7 @@ describe('Settings API (canary)', function () {
|
||||
headers['x-cache-invalidate'].should.eql('/*');
|
||||
should.exist(putBody);
|
||||
|
||||
putBody.settings.length.should.equal(3);
|
||||
putBody.settings.length.should.equal(2);
|
||||
|
||||
putBody.settings[0].key.should.eql('unsplash');
|
||||
should.equal(putBody.settings[0].value, true);
|
||||
@ -653,12 +648,6 @@ describe('Settings API (canary)', function () {
|
||||
putBody.settings[1].key.should.eql('title');
|
||||
should.equal(putBody.settings[1].value, 'New Value');
|
||||
|
||||
putBody.settings[2].key.should.eql('slack');
|
||||
should.equal(putBody.settings[2].value, JSON.stringify([{
|
||||
url: 'https://newurl.tld/slack',
|
||||
username: 'New Slack Username'
|
||||
}]));
|
||||
|
||||
localUtils.API.checkResponse(putBody, 'settings');
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user