diff --git a/assets/javascripts/discourse/components/custom-field-input.js.es6 b/assets/javascripts/discourse/components/custom-field-input.js.es6 index a673112b..7ee70716 100644 --- a/assets/javascripts/discourse/components/custom-field-input.js.es6 +++ b/assets/javascripts/discourse/components/custom-field-input.js.es6 @@ -1,27 +1,27 @@ import Component from "@ember/component"; import discourseComputed, { observes } from "discourse-common/utils/decorators"; import { alias, equal, or } from "@ember/object/computed"; -import { computed } from "@ember/object"; import I18n from "I18n"; -const klasses = ["topic", "post", "group", "category"]; -const types = ["string", "boolean", "integer", "json"]; -const subscriptionTypes = { - klass: ["group", "category"], - type: ["json"], -}; +import wizardSchema, { + requiringAdditionalSubscription, + subscriptionLevel, +} from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema"; -const generateContent = function (array, type, subscribed = false) { - return array.reduce((result, key) => { - let subArr = subscriptionTypes[type]; - let subscription = subArr && subArr.includes(key); - if (!subscription || subscribed) { - result.push({ - id: key, - name: I18n.t(`admin.wizard.custom_field.${type}.${key}`), - subscription, - }); - } +const generateContent = function (kategory, subscription) { + let unsubscribedCustomFields = requiringAdditionalSubscription( + subscription, + "custom_fields", + kategory + ); + return wizardSchema.custom_field[kategory].reduce((result, item) => { + let disabled = unsubscribedCustomFields.includes(item); + result.push({ + id: item, + name: I18n.t(`admin.wizard.custom_field.${kategory}.${item}`), + subscription: subscriptionLevel(item, "custom_fields", kategory), + disabled, + }); return result; }, []); }; @@ -32,12 +32,6 @@ export default Component.extend({ postSerializers: ["post"], groupSerializers: ["basic_group"], categorySerializers: ["basic_category"], - klassContent: computed("subscribed", function () { - return generateContent(klasses, "klass", this.subscribed); - }), - typeContent: computed("subscribed", function () { - return generateContent(types, "type", this.subscribed); - }), showInputs: or("field.new", "field.edit"), classNames: ["custom-field-input"], loading: or("saving", "destroying"), @@ -54,12 +48,26 @@ export default Component.extend({ const serializers = this.get(`${klass}Serializers`); if (serializers) { - return generateContent(serializers, "serializers", this.subscribed); - } else { - return []; + return serializers.reduce((result, key) => { + result.push({ + id: key, + name: I18n.t(`admin.wizard.custom_field.serializers.${key}`), + }); + return result; + }, []); } }, + @discourseComputed("subscription") + customFieldTypes(subscription) { + return generateContent("type", subscription); + }, + + @discourseComputed("subscription") + customFieldKlasses(subscription) { + return generateContent("klass", subscription); + }, + @observes("field.klass") clearSerializersWhenClassChanges() { this.set("field.serializers", null); diff --git a/assets/javascripts/discourse/components/subscription-container.js.es6 b/assets/javascripts/discourse/components/subscription-container.js.es6 index 620c6d88..a12b8949 100644 --- a/assets/javascripts/discourse/components/subscription-container.js.es6 +++ b/assets/javascripts/discourse/components/subscription-container.js.es6 @@ -4,18 +4,22 @@ import discourseComputed from "discourse-common/utils/decorators"; export default Component.extend({ classNameBindings: [":subscription-container", "subscribed"], - @discourseComputed('subscribed') + @discourseComputed("subscribed") subscribedIcon(subscribed) { - return subscribed ? 'check' : 'dash'; + return subscribed ? "check" : "dash"; }, - @discourseComputed('subscribed') + @discourseComputed("subscribed") subscribedLabel(subscribed) { - return `admin.wizard.subscription_container.${subscribed ? 'subscribed' : 'not_subscribed'}.label`; + return `admin.wizard.subscription_container.${ + subscribed ? "subscribed" : "not_subscribed" + }.label`; }, - @discourseComputed('subscribed') + @discourseComputed("subscribed") subscribedTitle(subscribed) { - return `admin.wizard.subscription_container.${subscribed ? 'subscribed' : 'not_subscribed'}.title`; - } -}); \ No newline at end of file + return `admin.wizard.subscription_container.${ + subscribed ? "subscribed" : "not_subscribed" + }.title`; + }, +}); diff --git a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 index ffe5b514..6cf22942 100644 --- a/assets/javascripts/discourse/components/wizard-custom-action.js.es6 +++ b/assets/javascripts/discourse/components/wizard-custom-action.js.es6 @@ -1,5 +1,8 @@ import { default as discourseComputed } from "discourse-common/utils/decorators"; -import wizardSchema from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema"; +import wizardSchema, { + requiringAdditionalSubscription, + subscriptionLevel, +} from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema"; import { empty, equal, or } from "@ember/object/computed"; import { notificationLevels, selectKitContent } from "../lib/wizard"; import { computed } from "@ember/object"; @@ -94,17 +97,21 @@ export default Component.extend(UndoChanges, { return apis.find((a) => a.name === api).endpoints; }, - @discourseComputed("subscribed") - actionTypes(subscribed) { + @discourseComputed("subscription") + actionTypes(subscription) { + let unsubscribedActions = requiringAdditionalSubscription( + subscription, + "actions", + "" + ); return Object.keys(wizardSchema.action.types).reduce((result, type) => { - let subscription = wizardSchema.action.subscriptionTypes.includes(type); - if (subscribed || !subscription) { - result.push({ - id: type, - name: I18n.t(`admin.wizard.action.${type}.label`), - subscription, - }); - } + let disabled = unsubscribedActions.includes(type); + result.push({ + id: type, + name: I18n.t(`admin.wizard.action.${type}.label`), + subscription: subscriptionLevel(type, "actions", ""), + disabled, + }); return result; }, []); }, diff --git a/assets/javascripts/discourse/components/wizard-notice.js.es6 b/assets/javascripts/discourse/components/wizard-notice.js.es6 index 15da3f35..fcd77606 100644 --- a/assets/javascripts/discourse/components/wizard-notice.js.es6 +++ b/assets/javascripts/discourse/components/wizard-notice.js.es6 @@ -4,33 +4,39 @@ import { not, notEmpty } from "@ember/object/computed"; import I18n from "I18n"; export default Component.extend({ - classNameBindings: [':wizard-notice', 'notice.type', 'dismissed', 'expired', 'resolved'], + classNameBindings: [ + ":wizard-notice", + "notice.type", + "dismissed", + "expired", + "resolved", + ], showFull: false, - resolved: notEmpty('notice.expired_at'), - dismissed: notEmpty('notice.dismissed_at'), - canDismiss: not('dismissed'), + resolved: notEmpty("notice.expired_at"), + dismissed: notEmpty("notice.dismissed_at"), + canDismiss: not("dismissed"), - @discourseComputed('notice.type') + @discourseComputed("notice.type") title(type) { return I18n.t(`admin.wizard.notice.title.${type}`); }, - @discourseComputed('notice.type') + @discourseComputed("notice.type") icon(type) { return { - plugin_status_warning: 'exclamation-circle', - plugin_status_connection_error: 'bolt', - subscription_messages_connection_error: 'bolt', - info: 'info-circle' + plugin_status_warning: "exclamation-circle", + plugin_status_connection_error: "bolt", + subscription_messages_connection_error: "bolt", + info: "info-circle", }[type]; }, actions: { dismiss() { - this.set('dismissing', true); + this.set("dismissing", true); this.notice.dismiss().then(() => { - this.set('dismissing', false); + this.set("dismissing", false); }); - } - } -}); \ No newline at end of file + }, + }, +}); diff --git a/assets/javascripts/discourse/components/wizard-subscription-selector.js.es6 b/assets/javascripts/discourse/components/wizard-subscription-selector.js.es6 index ec94bce5..ea1aa5d9 100644 --- a/assets/javascripts/discourse/components/wizard-subscription-selector.js.es6 +++ b/assets/javascripts/discourse/components/wizard-subscription-selector.js.es6 @@ -7,7 +7,8 @@ export default SingleSelectComponent.extend({ autoFilterable: false, filterable: false, showFullTitle: true, - headerComponent: "wizard-subscription-selector/wizard-subscription-selector-header", + headerComponent: + "wizard-subscription-selector/wizard-subscription-selector-header", caretUpIcon: "caret-up", caretDownIcon: "caret-down", }, diff --git a/assets/javascripts/discourse/components/wizard-subscription-selector/wizard-subscription-selector-row.js.es6 b/assets/javascripts/discourse/components/wizard-subscription-selector/wizard-subscription-selector-row.js.es6 index 23034ac1..1d43047a 100644 --- a/assets/javascripts/discourse/components/wizard-subscription-selector/wizard-subscription-selector-row.js.es6 +++ b/assets/javascripts/discourse/components/wizard-subscription-selector/wizard-subscription-selector-row.js.es6 @@ -1,3 +1,20 @@ import SelectKitRowComponent from "select-kit/components/select-kit/select-kit-row"; +import { default as discourseComputed } from "discourse-common/utils/decorators"; -export default SelectKitRowComponent.extend(); +export default SelectKitRowComponent.extend({ + classNameBindings: ["isDisabled:disabled"], + + @discourseComputed("item") + isDisabled() { + return this.item.disabled; + }, + + click(event) { + event.preventDefault(); + event.stopPropagation(); + if (!this.item.disabled) { + this.selectKit.select(this.rowValue, this.item); + } + return false; + }, +}); diff --git a/assets/javascripts/discourse/components/wizard-subscription.js.es6 b/assets/javascripts/discourse/components/wizard-subscription.js.es6 index 0d839a5f..29e47bc2 100644 --- a/assets/javascripts/discourse/components/wizard-subscription.js.es6 +++ b/assets/javascripts/discourse/components/wizard-subscription.js.es6 @@ -25,7 +25,9 @@ export default Component.extend({ @discourseComputed("stateClass") stateLabel(stateClass) { - return I18n.t(`admin.wizard.subscription.subscription.status.${stateClass}`); + return I18n.t( + `admin.wizard.subscription.subscription.status.${stateClass}` + ); }, actions: { diff --git a/assets/javascripts/discourse/connectors/admin-dashboard-top/custom-wizard-important-notice.js.es6 b/assets/javascripts/discourse/connectors/admin-dashboard-top/custom-wizard-important-notice.js.es6 index 43a2152b..5962f255 100644 --- a/assets/javascripts/discourse/connectors/admin-dashboard-top/custom-wizard-important-notice.js.es6 +++ b/assets/javascripts/discourse/connectors/admin-dashboard-top/custom-wizard-important-notice.js.es6 @@ -6,11 +6,11 @@ export default { }, setupComponent() { - const controller = getOwner(this).lookup('controller:admin-dashboard'); - const importantNotice = controller.get('customWizardImportantNotice'); + const controller = getOwner(this).lookup("controller:admin-dashboard"); + const importantNotice = controller.get("customWizardImportantNotice"); if (importantNotice) { - this.set('importantNotice', importantNotice); + this.set("importantNotice", importantNotice); } - } -}; \ No newline at end of file + }, +}; diff --git a/assets/javascripts/discourse/controllers/admin-wizards-api-show.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards-api-show.js.es6 index 5dba2d7f..7d3c1084 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards-api-show.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards-api-show.js.es6 @@ -11,7 +11,7 @@ export default Controller.extend({ queryParams: ["refresh_list"], loadingSubscriptions: false, notAuthorized: not("api.authorized"), - endpointMethods: selectKitContent(["GET", "PUT", "POST", "PATCH", "DELETE"]), + endpointMethods: selectKitContent(["PUT", "POST", "PATCH", "DELETE"]), showRemove: not("isNew"), showRedirectUri: and("threeLeggedOauth", "api.name"), responseIcon: null, diff --git a/assets/javascripts/discourse/controllers/admin-wizards.js.es6 b/assets/javascripts/discourse/controllers/admin-wizards.js.es6 index a94222c1..d128d851 100644 --- a/assets/javascripts/discourse/controllers/admin-wizards.js.es6 +++ b/assets/javascripts/discourse/controllers/admin-wizards.js.es6 @@ -8,13 +8,13 @@ export default Controller.extend({ ajax(`/admin/wizards/notice/${this.id}`, { type: "DELETE", }) - .then(result => { + .then((result) => { if (result.success) { const notices = this.notices; - notices.removeObject(notices.findBy('id', noticeId)); + notices.removeObject(notices.findBy("id", noticeId)); } }) .catch(popupAjaxError); - } - } -}); \ No newline at end of file + }, + }, +}); diff --git a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 index f53dc2cd..21a9d745 100644 --- a/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 +++ b/assets/javascripts/discourse/initializers/custom-wizard-edits.js.es6 @@ -21,27 +21,37 @@ export default { }; withPluginApi("0.8.36", (api) => { - api.modifyClass('route:admin-dashboard', { + api.modifyClass("route:admin-dashboard", { afterModel() { - return CustomWizardNotice.list().then(result => { + return CustomWizardNotice.list().then((result) => { if (result && result.length) { - this.set('notices', A(result.map(n => CustomWizardNotice.create(n)))); + this.set( + "notices", + A(result.map((n) => CustomWizardNotice.create(n))) + ); } - }); + }); }, setupController(controller) { if (this.notices) { - let pluginStatusConnectionError = this.notices.filter(n => n.type === 'plugin_status_connection_error')[0]; - let pluginStatusWarning = this.notices.filter(n => n.type === 'plugin_status_warning')[0]; + let pluginStatusConnectionError = this.notices.filter( + (n) => n.type === "plugin_status_connection_error" + )[0]; + let pluginStatusWarning = this.notices.filter( + (n) => n.type === "plugin_status_warning" + )[0]; if (pluginStatusConnectionError || pluginStatusWarning) { - controller.set('customWizardImportantNotice', pluginStatusConnectionError || pluginStatusWarning); + controller.set( + "customWizardImportantNotice", + pluginStatusConnectionError || pluginStatusWarning + ); } } this._super(...arguments); - } + }, }); }); }, diff --git a/assets/javascripts/discourse/lib/wizard-schema.js.es6 b/assets/javascripts/discourse/lib/wizard-schema.js.es6 index 5d876ed0..af4fa758 100644 --- a/assets/javascripts/discourse/lib/wizard-schema.js.es6 +++ b/assets/javascripts/discourse/lib/wizard-schema.js.es6 @@ -193,32 +193,110 @@ const action = { "members_visibility_level", ], required: ["id", "type"], - subscriptionTypes: [ - "send_message", - "add_to_group", - "create_category", - "create_group", - "send_to_api", - ], - required: ["id", "type"], - proTypes: [ - "send_message", - "add_to_group", - "create_category", - "create_group", - "send_to_api", - ], dependent: {}, objectArrays: {}, }; +const custom_field = { + klass: ["topic", "post", "group", "category"], + type: ["string", "boolean", "integer", "json"], +}; + +const subscription_levels = { + standard: { + actions: ["send_message", "add_to_group", "watch_categories"], + custom_fields: { + klass: [], + type: ["json"], + }, + }, + + business: { + actions: ["create_category", "create_group", "send_to_api"], + custom_fields: { + klass: ["group", "category"], + type: [], + }, + }, +}; + const wizardSchema = { wizard, step, field, + custom_field, action, + subscription_levels, }; +export function requiringAdditionalSubscription( + currentSubscription, + category, + subCategory +) { + switch (category) { + case "actions": + switch (currentSubscription) { + case "business": + return []; + case "standard": + return subscription_levels["business"][category]; + default: + return subscription_levels["standard"][category].concat( + subscription_levels["business"][category] + ); + } + case "custom_fields": + switch (currentSubscription) { + case "business": + return []; + case "standard": + return subscription_levels["business"][category][subCategory]; + default: + return subscription_levels["standard"][category][subCategory].concat( + subscription_levels["business"][category][subCategory] + ); + } + default: + return []; + } +} + +export function subscriptionLevel(type, category, subCategory) { + switch (category) { + case "actions": + if (subscription_levels["business"].actions.includes(type)) { + return "business"; + } else { + if (subscription_levels["standard"].actions.includes(type)) { + return "standard"; + } else { + return ""; + } + } + case "custom_fields": + if ( + subscription_levels["business"].custom_fields[subCategory].includes( + type + ) + ) { + return "business"; + } else { + if ( + subscription_levels["standard"].custom_fields[subCategory].includes( + type + ) + ) { + return "standard"; + } else { + return ""; + } + } + default: + return ""; + } +} + export function buildFieldTypes(types) { wizardSchema.field.types = types; } diff --git a/assets/javascripts/discourse/models/custom-wizard-notice.js.es6 b/assets/javascripts/discourse/models/custom-wizard-notice.js.es6 index a6b47c40..1eb21e73 100644 --- a/assets/javascripts/discourse/models/custom-wizard-notice.js.es6 +++ b/assets/javascripts/discourse/models/custom-wizard-notice.js.es6 @@ -6,18 +6,20 @@ const CustomWizardNotice = EmberObject.extend(); CustomWizardNotice.reopen({ dismiss() { - return ajax(`/admin/wizards/notice/${this.id}`, { type: 'PUT' }).then(result => { - if (result.success) { - this.set('dismissed_at', result.dismissed_at); - } - }).catch(popupAjaxError); - } + return ajax(`/admin/wizards/notice/${this.id}`, { type: "PUT" }) + .then((result) => { + if (result.success) { + this.set("dismissed_at", result.dismissed_at); + } + }) + .catch(popupAjaxError); + }, }); CustomWizardNotice.reopenClass({ list() { - return ajax('/admin/wizards/notice').catch(popupAjaxError); - } + return ajax("/admin/wizards/notice").catch(popupAjaxError); + }, }); -export default CustomWizardNotice; \ No newline at end of file +export default CustomWizardNotice; diff --git a/assets/javascripts/discourse/routes/admin-wizards-custom-fields.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-custom-fields.js.es6 index ca9c4c40..4992f92d 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-custom-fields.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards-custom-fields.js.es6 @@ -10,10 +10,12 @@ export default DiscourseRoute.extend({ setupController(controller, model) { const customFields = A(model.custom_fields || []); const subscribed = model.subscribed; + const subscription = model.subscription; controller.setProperties({ customFields, subscribed, + subscription, }); }, }); diff --git a/assets/javascripts/discourse/routes/admin-wizards-wizard-show.js.es6 b/assets/javascripts/discourse/routes/admin-wizards-wizard-show.js.es6 index 046fc6d4..d6263471 100644 --- a/assets/javascripts/discourse/routes/admin-wizards-wizard-show.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards-wizard-show.js.es6 @@ -40,6 +40,7 @@ export default DiscourseRoute.extend({ currentAction: wizard.actions[0], creating: model.create, subscribed: parentModel.subscribed, + subscription: parentModel.subscription, }; controller.setProperties(props); diff --git a/assets/javascripts/discourse/routes/admin-wizards.js.es6 b/assets/javascripts/discourse/routes/admin-wizards.js.es6 index da184d93..de67a8b9 100644 --- a/assets/javascripts/discourse/routes/admin-wizards.js.es6 +++ b/assets/javascripts/discourse/routes/admin-wizards.js.es6 @@ -4,16 +4,17 @@ import { A } from "@ember/array"; export default DiscourseRoute.extend({ model() { - return ajax('/admin/wizards'); + return ajax("/admin/wizards"); }, setupController(controller, model) { - controller.set('notices', A(model.notices)); + controller.set("notices", A(model.notices)); + controller.set("api_section", model.api_section); }, afterModel(model, transition) { if (transition.targetName === "adminWizards.index") { this.transitionTo("adminWizardsWizard"); } - } + }, }); diff --git a/assets/javascripts/discourse/templates/admin-wizards-custom-fields.hbs b/assets/javascripts/discourse/templates/admin-wizards-custom-fields.hbs index 361d2d44..dceed458 100644 --- a/assets/javascripts/discourse/templates/admin-wizards-custom-fields.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards-custom-fields.hbs @@ -33,7 +33,8 @@ field=field removeField=(action "removeField") saveField=(action "saveField") - subscribed=subscribed}} + subscribed=subscribed + subscription=subscription}} {{/each}} diff --git a/assets/javascripts/discourse/templates/admin-wizards-wizard-show.hbs b/assets/javascripts/discourse/templates/admin-wizards-wizard-show.hbs index 3966a50c..abf06ba9 100644 --- a/assets/javascripts/discourse/templates/admin-wizards-wizard-show.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards-wizard-show.hbs @@ -178,7 +178,8 @@ apis=apis removeAction="removeAction" wizardFields=wizardFields - subscribed=subscribed}} + subscribed=subscribed + subscription=subscription}} {{/each}}
diff --git a/assets/javascripts/discourse/templates/admin-wizards.hbs b/assets/javascripts/discourse/templates/admin-wizards.hbs index 5ab666c7..aa3b2cf5 100644 --- a/assets/javascripts/discourse/templates/admin-wizards.hbs +++ b/assets/javascripts/discourse/templates/admin-wizards.hbs @@ -2,7 +2,7 @@ {{nav-item route="adminWizardsWizard" label="admin.wizard.nav_label"}} {{nav-item route="adminWizardsCustomFields" label="admin.wizard.custom_field.nav_label"}} {{nav-item route="adminWizardsSubmissions" label="admin.wizard.submissions.nav_label"}} - {{#if siteSettings.wizard_apis_enabled}} + {{#if api_section}} {{nav-item route="adminWizardsApi" label="admin.wizard.api.nav_label"}} {{/if}} {{nav-item route="adminWizardsLogs" label="admin.wizard.log.nav_label"}} diff --git a/assets/javascripts/discourse/templates/components/custom-field-input.hbs b/assets/javascripts/discourse/templates/components/custom-field-input.hbs index ac7e8689..b4ddcc81 100644 --- a/assets/javascripts/discourse/templates/components/custom-field-input.hbs +++ b/assets/javascripts/discourse/templates/components/custom-field-input.hbs @@ -2,14 +2,14 @@ {{wizard-subscription-selector value=field.klass - content=klassContent + content=customFieldKlasses none="admin.wizard.custom_field.klass.select" onChange=(action (mut field.klass))}} {{wizard-subscription-selector value=field.type - content=typeContent + content=customFieldTypes none="admin.wizard.custom_field.type.select" onChange=(action (mut field.type))}} diff --git a/assets/javascripts/discourse/templates/components/wizard-notice.hbs b/assets/javascripts/discourse/templates/components/wizard-notice.hbs index 6a7d0afb..89bbf5d4 100644 --- a/assets/javascripts/discourse/templates/components/wizard-notice.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-notice.hbs @@ -33,7 +33,7 @@
- {{{notice.message}}} + {{html-safe notice.message}}
{{#if importantOnDashboard}} diff --git a/assets/javascripts/discourse/templates/components/wizard-subscription-selector/wizard-subscription-selector-row.hbs b/assets/javascripts/discourse/templates/components/wizard-subscription-selector/wizard-subscription-selector-row.hbs index e2650408..ecd77cb1 100644 --- a/assets/javascripts/discourse/templates/components/wizard-subscription-selector/wizard-subscription-selector-row.hbs +++ b/assets/javascripts/discourse/templates/components/wizard-subscription-selector/wizard-subscription-selector-row.hbs @@ -10,6 +10,8 @@
{{html-safe label}} {{#if item.subscription}} - {{i18n "admin.wizard.subscription.label"}} + + {{item.subscription}} + {{/if}}
diff --git a/assets/javascripts/wizard/lib/text-lite.js.es6 b/assets/javascripts/wizard/lib/text-lite.js.es6 index c93f6708..cc161426 100644 --- a/assets/javascripts/wizard/lib/text-lite.js.es6 +++ b/assets/javascripts/wizard/lib/text-lite.js.es6 @@ -7,7 +7,7 @@ import { getOwner } from "discourse-common/lib/get-owner"; export function cook(text, options) { if (!options) { options = buildOptions({ - getURL: getURL, + getURL, siteSettings: getOwner(this).lookup("site-settings:main"), }); } diff --git a/assets/javascripts/wizard/lib/user-search.js.es6 b/assets/javascripts/wizard/lib/user-search.js.es6 index 8ef96266..e7171f18 100644 --- a/assets/javascripts/wizard/lib/user-search.js.es6 +++ b/assets/javascripts/wizard/lib/user-search.js.es6 @@ -27,12 +27,12 @@ function performSearch( // need to be able to cancel this oldSearch = $.ajax(getUrl("/u/search/users"), { data: { - term: term, + term, topic_id: topicId, include_groups: includeGroups, include_mentionable_groups: includeMentionableGroups, include_messageable_groups: includeMessageableGroups, - group: group, + group, topic_allowed_users: allowedUsers, }, }); diff --git a/assets/stylesheets/admin/admin.scss b/assets/stylesheets/admin/admin.scss index 862f6761..94fe6839 100644 --- a/assets/stylesheets/admin/admin.scss +++ b/assets/stylesheets/admin/admin.scss @@ -788,11 +788,6 @@ vertical-align: middle; } -.subscription-label { - color: var(--tertiary); - font-size: 0.75em; -} - .admin-wizards-subscription { .admin-wizard-controls { h3, @@ -853,14 +848,21 @@ } .wizard-subscription-selector.select-kit.single-select { - .select-kit-row .texts { - display: flex; - align-items: center; + .select-kit-row { + .texts { + display: flex; + align-items: center; + } + &.disabled { + background: var(--primary-low); + } } .subscription-label { margin-left: 0.75em; padding-top: 0.25em; + color: var(--tertiary); + font-size: 0.75em; } } @@ -924,7 +926,7 @@ } .d-icon { - margin-right: .4em; + margin-right: 0.4em; } .notice-header { @@ -935,9 +937,9 @@ border: 1px solid var(--primary); display: inline-flex; align-items: center; - padding: 0 .5em; + padding: 0 0.5em; margin-right: 1em; - font-size: .9em; + font-size: 0.9em; line-height: 25px; min-height: 25px; box-sizing: border-box; @@ -963,12 +965,12 @@ .notice-issued, .notice-resolved { - margin-right: .3em; + margin-right: 0.3em; } .notice-message { p { - margin: .5em 0; + margin: 0.5em 0; } p:last-of-type { diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 61e8274c..7c3b449b 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -466,7 +466,8 @@ en: subscription: nav_label: Subscription - label: Subscription + label: In subscription + additional_label: "Add subscription" title: Custom Wizard Subscription authorize: Authorize authorized: Authorized @@ -474,7 +475,7 @@ en: not_subscribed: You're not currently subscribed subscription: title: - community: Community Subscription + standard: Standard Subscription business: Business Subscription status: active: Active diff --git a/config/settings.yml b/config/settings.yml index d7b34aa9..c4337d92 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -15,9 +15,6 @@ plugins: refresh: true type: list list_type: compact - wizard_apis_enabled: - client: true - default: false wizard_important_notices_on_dashboard: client: true - default: true \ No newline at end of file + default: true diff --git a/controllers/custom_wizard/admin/admin.rb b/controllers/custom_wizard/admin/admin.rb index ff01ddac..8a3b69ac 100644 --- a/controllers/custom_wizard/admin/admin.rb +++ b/controllers/custom_wizard/admin/admin.rb @@ -4,6 +4,8 @@ class CustomWizard::AdminController < ::Admin::AdminController def index render_json_dump( + #TODO replace with appropriate static? + api_section: ["business"].include?(CustomWizard::Subscription.type), notices: ActiveModel::ArraySerializer.new( CustomWizard::Notice.list, each_serializer: CustomWizard::NoticeSerializer diff --git a/controllers/custom_wizard/admin/custom_fields.rb b/controllers/custom_wizard/admin/custom_fields.rb index d10f82ed..1cd20f5d 100644 --- a/controllers/custom_wizard/admin/custom_fields.rb +++ b/controllers/custom_wizard/admin/custom_fields.rb @@ -3,7 +3,8 @@ class CustomWizard::AdminCustomFieldsController < CustomWizard::AdminController def index render_json_dump( custom_fields: custom_field_list, - subscribed: CustomWizard::Subscription.subscribed? + subscribed: CustomWizard::Subscription.subscribed?, + subscription: CustomWizard::Subscription.type ) end diff --git a/controllers/custom_wizard/admin/wizard.rb b/controllers/custom_wizard/admin/wizard.rb index fdf338bf..e6ea684a 100644 --- a/controllers/custom_wizard/admin/wizard.rb +++ b/controllers/custom_wizard/admin/wizard.rb @@ -11,7 +11,8 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController field_types: CustomWizard::Field.types, realtime_validations: CustomWizard::RealtimeValidation.types, custom_fields: custom_field_list, - subscribed: CustomWizard::Subscription.subscribed? + subscribed: CustomWizard::Subscription.subscribed?, + subscription: CustomWizard::Subscription.type ) end diff --git a/lib/custom_wizard/api/endpoint.rb b/lib/custom_wizard/api/endpoint.rb index 9dbbc590..97fc1127 100644 --- a/lib/custom_wizard/api/endpoint.rb +++ b/lib/custom_wizard/api/endpoint.rb @@ -74,7 +74,7 @@ class CustomWizard::Api::Endpoint headers["Authorization"] = auth_string if auth_string headers["Content-Type"] = content_type if content_type - connection = Excon.new(UrlHelper.encode_and_parse(endpoint.url), headers: headers) + connection = Excon.new(endpoint.url, headers: headers) params = { method: endpoint.method } diff --git a/lib/custom_wizard/custom_field.rb b/lib/custom_wizard/custom_field.rb index eb93e292..ed0c9594 100644 --- a/lib/custom_wizard/custom_field.rb +++ b/lib/custom_wizard/custom_field.rb @@ -17,9 +17,8 @@ class ::CustomWizard::CustomField category: ["basic_category"], post: ["post"] } - SUBSCRIPTION_CLASSES ||= ['category', 'group'] + TYPES ||= ["string", "boolean", "integer", "json"] - SUBSCRIPTION_TYPES ||= ["json"] LIST_CACHE_KEY ||= 'custom_field_list' def self.serializers @@ -85,7 +84,7 @@ class ::CustomWizard::CustomField next end - if attr == 'klass' && SUBSCRIPTION_CLASSES.include?(value) && !@subscription.subscribed? + if attr == 'klass' && @subscription.requires_additional_subscription("custom_fields", "klass").include?(value) add_error(I18n.t("wizard.custom_field.error.subscription_type", type: value)) end @@ -100,7 +99,7 @@ class ::CustomWizard::CustomField add_error(I18n.t("#{i18n_key}.unsupported_type", type: value)) end - if attr == 'type' && SUBSCRIPTION_TYPES.include?(value) && !@subscription.subscribed? + if attr == 'type' && @subscription.requires_additional_subscription("custom_fields", "type").include?(value) add_error(I18n.t("wizard.custom_field.error.subscription_type", type: value)) end diff --git a/lib/custom_wizard/subscription.rb b/lib/custom_wizard/subscription.rb index 11e6d4d3..7bac7f59 100644 --- a/lib/custom_wizard/subscription.rb +++ b/lib/custom_wizard/subscription.rb @@ -6,6 +6,23 @@ class CustomWizard::Subscription attr_accessor :authentication, :subscription + SUBSCRIPTION_LEVELS = { + standard: { + actions: ["send_message", "add_to_group", "watch_categories"], + custom_fields: { + klass: [], + type: ["json"], + }, + }, + business: { + actions: ["create_category", "create_group", "send_to_api"], + custom_fields: { + klass: ["group", "category"], + type: [], + }, + }, + } + def initialize @authentication = CustomWizard::Subscription::Authentication.new(get_authentication) @subscription = CustomWizard::Subscription::Subscription.new(get_subscription) @@ -27,6 +44,10 @@ class CustomWizard::Subscription "stripe" end + def type + @subscription.type + end + def client_name "custom-wizard" end @@ -35,6 +56,31 @@ class CustomWizard::Subscription "discourse-subscription-server:user_subscription" end + def requires_additional_subscription(kategory, sub_kategory) + case kategory + when "actions" + case self.type + when "business" + [] + when "standard" + SUBSCRIPTION_LEVELS[:business][kategory.to_sym] + else + SUBSCRIPTION_LEVELS[:standard][kategory.to_sym] + SUBSCRIPTION_LEVELS[:business][kategory.to_sym] + end + when "custom_fields" + case self.type + when "business" + [] + when "standard" + SUBSCRIPTION_LEVELS[:business][kategory.to_sym][sub_kategory.to_sym] + else + SUBSCRIPTION_LEVELS[:standard][kategory.to_sym][sub_kategory.to_sym] + SUBSCRIPTION_LEVELS[:business][kategory.to_sym][sub_kategory.to_sym] + end + else + [] + end + end + def update if @authentication.active? response = Excon.get( @@ -119,6 +165,14 @@ class CustomWizard::Subscription self.new.subscribed? end + def self.type + self.new.type + end + + def self.requires_additional_subscription(kategory, sub_kategory) + self.new.requires_additional_subscription(kategory, sub_kategory) + end + def self.authorized? self.new.authorized? end diff --git a/lib/custom_wizard/subscription/subscription.rb b/lib/custom_wizard/subscription/subscription.rb index 129f993a..0b3bb84d 100644 --- a/lib/custom_wizard/subscription/subscription.rb +++ b/lib/custom_wizard/subscription/subscription.rb @@ -13,7 +13,7 @@ class CustomWizard::Subscription::Subscription end def types - %w(community business) + %w(none standard business) end def active? diff --git a/spec/components/custom_wizard/action_spec.rb b/spec/components/custom_wizard/action_spec.rb index e34dd861..284f63d3 100644 --- a/spec/components/custom_wizard/action_spec.rb +++ b/spec/components/custom_wizard/action_spec.rb @@ -13,6 +13,9 @@ describe CustomWizard::Action do let(:add_to_group) { get_wizard_fixture("actions/add_to_group") } let(:send_message) { get_wizard_fixture("actions/send_message") } let(:send_message_multi) { get_wizard_fixture("actions/send_message_multi") } + let(:api_test_endpoint) { get_wizard_fixture("endpoints/test_endpoint") } + let(:api_test_endpoint_body) { get_wizard_fixture("endpoints/test_endpoint_body") } + let(:api_test_no_authorization) { get_wizard_fixture("api/no_authorization") } def update_template(template) CustomWizard::Template.save(template, skip_jobs: true) @@ -176,7 +179,7 @@ describe CustomWizard::Action do context "subscription actions" do before do - enable_subscription + enable_subscription("standard") end it '#send_message' do @@ -265,5 +268,51 @@ describe CustomWizard::Action do expect(group.users.first.username).to eq('angus') end + + it '#send_to_api successful' do + stub_request(:put, "https://myexternalapi.com/update"). + with( + body: "some_body", + headers: { + 'Host' => 'myexternalapi.com' + }). + to_return(status: 200, body: "success", headers: {}) + + new_api = CustomWizard::Api.new("my_api") + CustomWizard::Api.set("my_api", title: "Mocked external api") + CustomWizard::Api::Authorization.set("my_api", api_test_no_authorization) + CustomWizard::Api::Endpoint.new("my_api") + CustomWizard::Api::Endpoint.set("my_api", api_test_endpoint) + endpoint_id = CustomWizard::Api::Endpoint.list("my_api").first.id + + result = CustomWizard::Api::Endpoint.request("my_api", endpoint_id, "some_body") + log_entry = CustomWizard::Api::LogEntry.list("my_api").first + + expect(result).to eq('success') + expect(log_entry.status).to eq('SUCCESS') + end + + it '#send_to_api failure' do + stub_request(:put, "https://myexternalapi.com/update"). + with( + body: "some_body", + headers: { + 'Host' => 'myexternalapi.com' + }). + to_return(status: 500, body: "failure", headers: {}) + + new_api = CustomWizard::Api.new("my_api") + CustomWizard::Api.set("my_api", title: "Mocked external api") + CustomWizard::Api::Authorization.set("my_api", api_test_no_authorization) + CustomWizard::Api::Endpoint.new("my_api") + CustomWizard::Api::Endpoint.set("my_api", api_test_endpoint) + endpoint_id = CustomWizard::Api::Endpoint.list("my_api").first.id + + result = CustomWizard::Api::Endpoint.request("my_api", endpoint_id, "some_body") + log_entry = CustomWizard::Api::LogEntry.list("my_api").first + + expect(result).to eq({ error: "API request failed" }) + expect(log_entry.status).to eq('FAIL') + end end end diff --git a/spec/components/custom_wizard/builder_spec.rb b/spec/components/custom_wizard/builder_spec.rb index 28880409..f4a6d2fc 100644 --- a/spec/components/custom_wizard/builder_spec.rb +++ b/spec/components/custom_wizard/builder_spec.rb @@ -176,7 +176,7 @@ describe CustomWizard::Builder do context "restart is enabled" do before do - enable_subscription + enable_subscription("standard") @template[:restart_on_revisit] = true CustomWizard::Template.save(@template.as_json) end @@ -205,7 +205,7 @@ describe CustomWizard::Builder do context 'with required data' do before do - enable_subscription + enable_subscription("standard") @template[:steps][0][:required_data] = required_data_json['required_data'] @template[:steps][0][:required_data_message] = required_data_json['required_data_message'] CustomWizard::Template.save(@template.as_json) @@ -241,7 +241,7 @@ describe CustomWizard::Builder do context "with permitted params" do before do - enable_subscription + enable_subscription("standard") @template[:steps][0][:permitted_params] = permitted_param_json['permitted_params'] CustomWizard::Template.save(@template.as_json) end @@ -256,7 +256,7 @@ describe CustomWizard::Builder do context "with condition" do before do - enable_subscription + enable_subscription("standard") @template[:steps][0][:condition] = user_condition_json['condition'] CustomWizard::Template.save(@template.as_json) end @@ -295,7 +295,7 @@ describe CustomWizard::Builder do context "with condition" do before do - enable_subscription + enable_subscription("standard") @template[:steps][0][:fields][0][:condition] = user_condition_json['condition'] CustomWizard::Template.save(@template.as_json) end @@ -327,7 +327,7 @@ describe CustomWizard::Builder do context 'save submissions disabled' do before do - enable_subscription + enable_subscription("standard") @template[:save_submissions] = false CustomWizard::Template.save(@template.as_json) @wizard = CustomWizard::Builder.new(@template[:id], user).build diff --git a/spec/components/custom_wizard/custom_field_spec.rb b/spec/components/custom_wizard/custom_field_spec.rb index a30ec02b..fd0fa4fc 100644 --- a/spec/components/custom_wizard/custom_field_spec.rb +++ b/spec/components/custom_wizard/custom_field_spec.rb @@ -217,7 +217,7 @@ describe CustomWizard::CustomField do context "with a subscription" do before do - enable_subscription + enable_subscription("business") end it "saves subscription field types" do diff --git a/spec/components/custom_wizard/mapper_spec.rb b/spec/components/custom_wizard/mapper_spec.rb index 1ac945d0..71c1e7de 100644 --- a/spec/components/custom_wizard/mapper_spec.rb +++ b/spec/components/custom_wizard/mapper_spec.rb @@ -359,7 +359,7 @@ describe CustomWizard::Mapper do context "with a subscription" do before do - enable_subscription + enable_subscription("standard") end it "treats replaced values as string literals" do diff --git a/spec/components/custom_wizard/template_validator_spec.rb b/spec/components/custom_wizard/template_validator_spec.rb index e8b8af28..14214e4d 100644 --- a/spec/components/custom_wizard/template_validator_spec.rb +++ b/spec/components/custom_wizard/template_validator_spec.rb @@ -75,7 +75,7 @@ describe CustomWizard::TemplateValidator do context "with subscription" do before do - enable_subscription + enable_subscription("standard") end it "validates wizard attributes" do diff --git a/spec/extensions/custom_field_extensions_spec.rb b/spec/extensions/custom_field_extensions_spec.rb index 7bba27a0..3660644b 100644 --- a/spec/extensions/custom_field_extensions_spec.rb +++ b/spec/extensions/custom_field_extensions_spec.rb @@ -74,7 +74,7 @@ describe "custom field extensions" do context "subscription custom fields" do before do - enable_subscription + enable_subscription("business") subscription_custom_field_json['custom_fields'].each do |field_json| custom_field = CustomWizard::CustomField.new(nil, field_json) diff --git a/spec/fixtures/api/no_authorization.json b/spec/fixtures/api/no_authorization.json new file mode 100644 index 00000000..b1c6506e --- /dev/null +++ b/spec/fixtures/api/no_authorization.json @@ -0,0 +1,3 @@ +{ + "auth_type": "none" +} \ No newline at end of file diff --git a/spec/fixtures/endpoints/test_endpoint.json b/spec/fixtures/endpoints/test_endpoint.json new file mode 100644 index 00000000..ba979064 --- /dev/null +++ b/spec/fixtures/endpoints/test_endpoint.json @@ -0,0 +1,5 @@ +{ + "method": "PUT", + "url": "https://myexternalapi.com/update", + "success_codes": [200] +} \ No newline at end of file diff --git a/spec/fixtures/endpoints/test_endpoint_body.json b/spec/fixtures/endpoints/test_endpoint_body.json new file mode 100644 index 00000000..837e7c72 --- /dev/null +++ b/spec/fixtures/endpoints/test_endpoint_body.json @@ -0,0 +1,3 @@ +{ + "data": "some_data" +} \ No newline at end of file diff --git a/spec/plugin_helper.rb b/spec/plugin_helper.rb index d47c47c1..232a2411 100644 --- a/spec/plugin_helper.rb +++ b/spec/plugin_helper.rb @@ -15,6 +15,7 @@ require 'oj' Oj.default_options = Oj.default_options.merge(cache_str: -1) require 'rails_helper' +require 'webmock/rspec' def get_wizard_fixture(path) JSON.parse( @@ -28,8 +29,10 @@ def authenticate_subscription CustomWizard::Subscription::Authentication.any_instance.stubs(:active?).returns(true) end -def enable_subscription +def enable_subscription(type) + # CustomWizard::Subscription.new CustomWizard::Subscription.any_instance.stubs(:subscribed?).returns(true) + CustomWizard::Subscription.any_instance.stubs(:type).returns(type) end def disable_subscription diff --git a/spec/requests/custom_wizard/custom_field_extensions_spec.rb b/spec/requests/custom_wizard/custom_field_extensions_spec.rb index 775e3ee0..19a8417a 100644 --- a/spec/requests/custom_wizard/custom_field_extensions_spec.rb +++ b/spec/requests/custom_wizard/custom_field_extensions_spec.rb @@ -40,8 +40,7 @@ describe "custom field extensions" do context "with a subscription" do before do - enable_subscription - + enable_subscription("business") subscription_custom_field_json['custom_fields'].each do |field_json| custom_field = CustomWizard::CustomField.new(nil, field_json) custom_field.save diff --git a/spec/requests/custom_wizard/steps_controller_spec.rb b/spec/requests/custom_wizard/steps_controller_spec.rb index 8396135c..b55de419 100644 --- a/spec/requests/custom_wizard/steps_controller_spec.rb +++ b/spec/requests/custom_wizard/steps_controller_spec.rb @@ -121,7 +121,7 @@ describe CustomWizard::StepsController do context "subscription" do before do - enable_subscription + enable_subscription("standard") end it "raises an error when user cant see the step due to conditions" do diff --git a/spec/serializers/custom_wizard/subscription/subscription_serializer_spec.rb b/spec/serializers/custom_wizard/subscription/subscription_serializer_spec.rb index 63caf363..91dc59b1 100644 --- a/spec/serializers/custom_wizard/subscription/subscription_serializer_spec.rb +++ b/spec/serializers/custom_wizard/subscription/subscription_serializer_spec.rb @@ -4,11 +4,11 @@ require_relative '../../../plugin_helper' describe CustomWizard::Subscription::SubscriptionSerializer do it 'should return subscription attributes' do - sub = CustomWizard::Subscription::Subscription.new(OpenStruct.new(type: 'community', updated_at: Time.now)) + sub = CustomWizard::Subscription::Subscription.new(OpenStruct.new(type: 'standard', updated_at: Time.now)) serialized = described_class.new(sub, root: false).as_json expect(serialized[:active]).to eq(true) - expect(serialized[:type]).to eq('community') + expect(serialized[:type]).to eq('standard') expect(serialized[:updated_at]).to eq(sub.updated_at) end end diff --git a/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb b/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb index 0a9f7e89..f0128765 100644 --- a/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb +++ b/spec/serializers/custom_wizard/wizard_step_serializer_spec.rb @@ -34,7 +34,7 @@ describe CustomWizard::StepSerializer do context 'with required data' do before do - enable_subscription + enable_subscription("standard") wizard_template['steps'][0]['required_data'] = required_data_json['required_data'] wizard_template['steps'][0]['required_data_message'] = required_data_json['required_data_message'] CustomWizard::Template.save(wizard_template)