1
0
Fork 0

IMPROVE: improve how subscription type checks are handled

Dieser Commit ist enthalten in:
Angus McLeod 2022-02-14 17:32:17 +11:00
Ursprung 4605b23585
Commit 7de00ca040
9 geänderte Dateien mit 111 neuen und 193 gelöschten Zeilen

Datei anzeigen

@ -2,29 +2,7 @@ import Component from "@ember/component";
import discourseComputed, { observes } from "discourse-common/utils/decorators"; import discourseComputed, { observes } from "discourse-common/utils/decorators";
import { alias, equal, or } from "@ember/object/computed"; import { alias, equal, or } from "@ember/object/computed";
import I18n from "I18n"; import I18n from "I18n";
import { generateSubscriptionContent } from "../lib/wizard";
import wizardSchema, {
requiringAdditionalSubscription,
subscriptionLevel,
} from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema";
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;
}, []);
};
export default Component.extend({ export default Component.extend({
tagName: "tr", tagName: "tr",
@ -60,12 +38,12 @@ export default Component.extend({
@discourseComputed("subscription") @discourseComputed("subscription")
customFieldTypes(subscription) { customFieldTypes(subscription) {
return generateContent("type", subscription); return generateSubscriptionContent("custom_fields", "type", subscription);
}, },
@discourseComputed("subscription") @discourseComputed("subscription")
customFieldKlasses(subscription) { customFieldKlasses(subscription) {
return generateContent("klass", subscription); return generateSubscriptionContent("custom_fields", "klass", subscription);
}, },
@observes("field.klass") @observes("field.klass")

Datei anzeigen

@ -1,10 +1,10 @@
import { default as discourseComputed } from "discourse-common/utils/decorators"; import { default as discourseComputed } from "discourse-common/utils/decorators";
import wizardSchema, {
requiringAdditionalSubscription,
subscriptionLevel,
} from "discourse/plugins/discourse-custom-wizard/discourse/lib/wizard-schema";
import { empty, equal, or } from "@ember/object/computed"; import { empty, equal, or } from "@ember/object/computed";
import { notificationLevels, selectKitContent } from "../lib/wizard"; import {
notificationLevels,
selectKitContent,
generateSubscriptionContent
} from "../lib/wizard";
import { computed } from "@ember/object"; import { computed } from "@ember/object";
import UndoChanges from "../mixins/undo-changes"; import UndoChanges from "../mixins/undo-changes";
import Component from "@ember/component"; import Component from "@ember/component";
@ -99,20 +99,6 @@ export default Component.extend(UndoChanges, {
@discourseComputed("subscription") @discourseComputed("subscription")
actionTypes(subscription) { actionTypes(subscription) {
let unsubscribedActions = requiringAdditionalSubscription( return generateSubscriptionContent("action", "types", subscription);
subscription,
"actions",
""
);
return Object.keys(wizardSchema.action.types).reduce((result, type) => {
let disabled = unsubscribedActions.includes(type);
result.push({
id: type,
name: I18n.t(`admin.wizard.action.${type}.label`),
subscription: subscriptionLevel(type, "actions", ""),
disabled,
});
return result;
}, []);
}, },
}); });

Datei anzeigen

@ -203,107 +203,35 @@ const custom_field = {
type: ["string", "boolean", "integer", "json"], 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 = { const wizardSchema = {
wizard, wizard,
step, step,
field, field,
custom_field, custom_field,
action, action
subscription_levels,
}; };
export function requiringAdditionalSubscription( export function hasRequiredSubscription(currentSubscriptionType, featureSubscriptionType) {
currentSubscription, const types = wizardSchema.subscription.types;
category, return types.indexOf(currentSubscriptionType) >= types.indexOf(featureSubscriptionType);
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) { export function subscriptionType(feature, attribute, value) {
case "business": let attributes = wizardSchema.subscription.features[feature];
return [];
case "standard": if (!attributes || !attributes[attribute] || !attributes[attribute][value]) {
return subscription_levels["business"][category][subCategory]; return wizardSchema.subscription_types[0];
default: } else {
return subscription_levels["standard"][category][subCategory].concat( return attributes[attribute][value];
subscription_levels["business"][category][subCategory]
);
}
default:
return [];
} }
} }
export function subscriptionLevel(type, category, subCategory) { export function buildSchema(model) {
switch (category) { wizardSchema.subscription = {};
case "actions": wizardSchema.subscription.features = model.subscription_features;
if (subscription_levels["business"].actions.includes(type)) { wizardSchema.subscription.types = model.subscription_types;
return "business"; wizardSchema.field.types = model.field_types;
} else { wizardSchema.field.validations = model.realtime_validations;
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;
}
export function buildFieldValidations(validations) {
wizardSchema.field.validations = validations;
} }
const siteSettings = getOwner(this).lookup("site-settings:main"); const siteSettings = getOwner(this).lookup("site-settings:main");

Datei anzeigen

@ -1,5 +1,8 @@
import EmberObject from "@ember/object"; import EmberObject from "@ember/object";
import wizardSchema from "./wizard-schema"; import wizardSchema, {
hasRequiredSubscription,
subscriptionType
} from "./wizard-schema";
function selectKitContent(content) { function selectKitContent(content) {
return content.map((i) => ({ id: i, name: i })); return content.map((i) => ({ id: i, name: i }));
@ -110,6 +113,26 @@ function wizardFieldList(steps = [], opts = {}) {
}, []); }, []);
} }
function buildSubscriptionContent(feature, attribute, currentSubscription) {
let attributes = wizardSchema[feature];
let values = attributes[attribute];
if (typeof values === 'object') {
values = Object.keys(values):
}
return values.map((value) => {
let subscriptionType = subscriptionType(feature, attribute, value);
return {
id: value,
name: I18n.t(`admin.wizard.${feature}.${attribute}.${value}`),
subscription: subscriptionType,
disabled: hasRequiredSubscription(currentSubscription, subscriptionType)
}
});
}
export { export {
selectKitContent, selectKitContent,
generateName, generateName,
@ -121,4 +144,5 @@ export {
notificationLevels, notificationLevels,
wizardFieldList, wizardFieldList,
sentenceCase, sentenceCase,
buildSubscriptionContent
}; };

Datei anzeigen

@ -1,5 +1,5 @@
import DiscourseRoute from "discourse/routes/discourse"; import DiscourseRoute from "discourse/routes/discourse";
import { buildFieldTypes, buildFieldValidations } from "../lib/wizard-schema"; import { buildSchema } from "../lib/wizard-schema";
import EmberObject, { set } from "@ember/object"; import EmberObject, { set } from "@ember/object";
import { A } from "@ember/array"; import { A } from "@ember/array";
import { all } from "rsvp"; import { all } from "rsvp";
@ -11,8 +11,7 @@ export default DiscourseRoute.extend({
}, },
afterModel(model) { afterModel(model) {
buildFieldTypes(model.field_types); buildSchema(model)
buildFieldValidations(model.realtime_validations);
return all([ return all([
this._getThemes(model), this._getThemes(model),

Datei anzeigen

@ -12,7 +12,9 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController
realtime_validations: CustomWizard::RealtimeValidation.types, realtime_validations: CustomWizard::RealtimeValidation.types,
custom_fields: custom_field_list, custom_fields: custom_field_list,
subscribed: CustomWizard::Subscription.subscribed?, subscribed: CustomWizard::Subscription.subscribed?,
subscription: CustomWizard::Subscription.type subscription: CustomWizard::Subscription.type,
subscription_features: CustomWizard::Subscription::Subscription::FEATURES,
subscription_types: CustomWizard::Subscription::Subscription.types
) )
end end

Datei anzeigen

@ -84,7 +84,7 @@ class ::CustomWizard::CustomField
next next
end end
if attr == 'klass' && @subscription.requires_additional_subscription("custom_fields", "klass").include?(value) if attr == 'klass' && @subscription.subscription.can_use_feature?("custom_fields", "klass", value)
add_error(I18n.t("wizard.custom_field.error.subscription_type", type: value)) add_error(I18n.t("wizard.custom_field.error.subscription_type", type: value))
end end
@ -99,7 +99,7 @@ class ::CustomWizard::CustomField
add_error(I18n.t("#{i18n_key}.unsupported_type", type: value)) add_error(I18n.t("#{i18n_key}.unsupported_type", type: value))
end end
if attr == 'type' && @subscription.requires_additional_subscription("custom_fields", "type").include?(value) if attr == 'type' && @subscription.subscription.can_use_feature?("custom_fields", "type", value)
add_error(I18n.t("wizard.custom_field.error.subscription_type", type: value)) add_error(I18n.t("wizard.custom_field.error.subscription_type", type: value))
end end

Datei anzeigen

@ -6,23 +6,6 @@ class CustomWizard::Subscription
attr_accessor :authentication, attr_accessor :authentication,
:subscription :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 def initialize
@authentication = CustomWizard::Subscription::Authentication.new(get_authentication) @authentication = CustomWizard::Subscription::Authentication.new(get_authentication)
@subscription = CustomWizard::Subscription::Subscription.new(get_subscription) @subscription = CustomWizard::Subscription::Subscription.new(get_subscription)
@ -56,31 +39,6 @@ class CustomWizard::Subscription
"discourse-subscription-server:user_subscription" "discourse-subscription-server:user_subscription"
end 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 def update
if @authentication.active? if @authentication.active?
response = Excon.get( response = Excon.get(
@ -169,10 +127,6 @@ class CustomWizard::Subscription
self.new.type self.new.type
end end
def self.requires_additional_subscription(kategory, sub_kategory)
self.new.requires_additional_subscription(kategory, sub_kategory)
end
def self.authorized? def self.authorized?
self.new.authorized? self.new.authorized?
end end

Datei anzeigen

@ -5,6 +5,40 @@ class CustomWizard::Subscription::Subscription
attr_reader :type, attr_reader :type,
:updated_at :updated_at
NONE ||= "none"
STANDARD ||= "standard"
BUSINESS ||= "business"
FEATURES ||= {
actions: {
type: {
create_topic: NONE,
update_profile: NONE,
open_composer: NONE,
route_to: NONE,
send_message: STANDARD,
watch_categories: STANDARD,
add_to_group: STANDARD,
send_to_api: BUSINESS,
create_category: BUSINESS,
create_group: BUSINESS
}
},
custom_fields: {
klass: {
topic: NONE,
post: NONE,
group: BUSINESS,
category: BUSINESS
},
type: {
string: NONE,
boolean: NONE,
integer: NONE,
json: STANDARD
}
}
}
def initialize(subscription) def initialize(subscription)
if subscription if subscription
@type = subscription.type @type = subscription.type
@ -12,11 +46,24 @@ class CustomWizard::Subscription::Subscription
end end
end end
def types
%w(none standard business)
end
def active? def active?
types.include?(type) && updated_at.to_datetime > (Time.zone.now - 2.hours).to_datetime types.include?(type) && updated_at.to_datetime > (Time.zone.now - 2.hours).to_datetime
end end
def can_use_feature?(feature, attribute, value)
feature_type = FEATURES.dig(*[feature.to_sym, attribute.to_sym, value.to_sym])
!feature_type || has_required_type?(feature_type)
end
def has_required_type?(t)
t && type_index(t) >= type_index(type)
end
def type_index(t)
self.class.types.index(t)
end
def self.types
[NONE, STANDARD, BUSINESS]
end
end end