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

Datei anzeigen

@ -1,10 +1,10 @@
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 { notificationLevels, selectKitContent } from "../lib/wizard";
import {
notificationLevels,
selectKitContent,
generateSubscriptionContent
} from "../lib/wizard";
import { computed } from "@ember/object";
import UndoChanges from "../mixins/undo-changes";
import Component from "@ember/component";
@ -99,20 +99,6 @@ export default Component.extend(UndoChanges, {
@discourseComputed("subscription")
actionTypes(subscription) {
let unsubscribedActions = requiringAdditionalSubscription(
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;
}, []);
return generateSubscriptionContent("action", "types", subscription);
},
});

Datei anzeigen

@ -203,107 +203,35 @@ const custom_field = {
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,
action
};
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 hasRequiredSubscription(currentSubscriptionType, featureSubscriptionType) {
const types = wizardSchema.subscription.types;
return types.indexOf(currentSubscriptionType) >= types.indexOf(featureSubscriptionType);
}
export function subscriptionType(feature, attribute, value) {
let attributes = wizardSchema.subscription.features[feature];
if (!attributes || !attributes[attribute] || !attributes[attribute][value]) {
return wizardSchema.subscription_types[0];
} else {
return attributes[attribute][value];
}
}
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;
}
export function buildFieldValidations(validations) {
wizardSchema.field.validations = validations;
export function buildSchema(model) {
wizardSchema.subscription = {};
wizardSchema.subscription.features = model.subscription_features;
wizardSchema.subscription.types = model.subscription_types;
wizardSchema.field.types = model.field_types;
wizardSchema.field.validations = model.realtime_validations;
}
const siteSettings = getOwner(this).lookup("site-settings:main");

Datei anzeigen

@ -1,5 +1,8 @@
import EmberObject from "@ember/object";
import wizardSchema from "./wizard-schema";
import wizardSchema, {
hasRequiredSubscription,
subscriptionType
} from "./wizard-schema";
function selectKitContent(content) {
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 {
selectKitContent,
generateName,
@ -121,4 +144,5 @@ export {
notificationLevels,
wizardFieldList,
sentenceCase,
buildSubscriptionContent
};

Datei anzeigen

@ -1,5 +1,5 @@
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 { A } from "@ember/array";
import { all } from "rsvp";
@ -11,8 +11,7 @@ export default DiscourseRoute.extend({
},
afterModel(model) {
buildFieldTypes(model.field_types);
buildFieldValidations(model.realtime_validations);
buildSchema(model)
return all([
this._getThemes(model),

Datei anzeigen

@ -12,7 +12,9 @@ class CustomWizard::AdminWizardController < CustomWizard::AdminController
realtime_validations: CustomWizard::RealtimeValidation.types,
custom_fields: custom_field_list,
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

Datei anzeigen

@ -84,7 +84,7 @@ class ::CustomWizard::CustomField
next
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))
end
@ -99,7 +99,7 @@ class ::CustomWizard::CustomField
add_error(I18n.t("#{i18n_key}.unsupported_type", type: value))
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))
end

Datei anzeigen

@ -6,23 +6,6 @@ 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)
@ -56,31 +39,6 @@ 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(
@ -169,10 +127,6 @@ class CustomWizard::Subscription
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

Datei anzeigen

@ -5,6 +5,40 @@ class CustomWizard::Subscription::Subscription
attr_reader :type,
: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)
if subscription
@type = subscription.type
@ -12,11 +46,24 @@ class CustomWizard::Subscription::Subscription
end
end
def types
%w(none standard business)
end
def active?
types.include?(type) && updated_at.to_datetime > (Time.zone.now - 2.hours).to_datetime
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