0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-25 02:30:28 +01:00
discourse-custom-wizard/lib/custom_wizard/subscription.rb

239 Zeilen
5,5 KiB
Ruby

2022-03-25 12:22:27 +01:00
# frozen_string_literal: true
2023-09-25 18:00:39 +02:00
require "discourse_subscription_client"
2021-09-24 11:58:42 +02:00
class CustomWizard::Subscription
2024-10-16 13:52:03 +02:00
PRODUCT_HIERARCHY = %w[community standard business]
def self.attributes
{
wizard: {
required: {
none: [],
2024-10-16 13:52:03 +02:00
standard: ["*"],
business: ["*"],
community: ["*"],
},
permitted: {
none: [],
2024-10-16 13:52:03 +02:00
standard: ["*"],
business: ["*"],
community: ["*", "!#{CustomWizard::Wizard::GUEST_GROUP_ID}"],
},
restart_on_revisit: {
none: [],
2024-10-16 13:52:03 +02:00
standard: ["*"],
business: ["*"],
community: ["*"],
},
2024-10-17 16:15:25 +02:00
after_time_groups: {
none: [],
standard: [],
business: ["*"],
community: [],
},
},
step: {
condition: {
none: [],
2024-10-16 13:52:03 +02:00
standard: ["*"],
business: ["*"],
community: ["*"],
},
required_data: {
none: [],
2024-10-16 13:52:03 +02:00
standard: ["*"],
business: ["*"],
community: ["*"],
},
permitted_params: {
none: [],
2024-10-16 13:52:03 +02:00
standard: ["*"],
business: ["*"],
community: ["*"],
},
},
field: {
condition: {
none: [],
2024-10-16 13:52:03 +02:00
standard: ["*"],
business: ["*"],
community: ["*"],
},
type: {
2024-10-16 13:52:03 +02:00
none: %w[text textarea text_only date time date_time number checkbox dropdown upload],
standard: ["*"],
business: ["*"],
community: ["*"],
},
realtime_validations: {
none: [],
2024-10-16 13:52:03 +02:00
standard: ["*"],
business: ["*"],
community: ["*"],
},
},
action: {
type: {
2024-10-16 13:52:03 +02:00
none: %w[create_topic update_profile open_composer route_to],
standard: %w[
create_topic
update_profile
open_composer
route_to
send_message
watch_categories
watch_tags
add_to_group
],
business: ["*"],
community: ["*"],
},
},
custom_field: {
klass: {
2024-10-16 13:52:03 +02:00
none: %w[topic post],
standard: %w[topic post],
business: ["*"],
community: ["*"],
},
type: {
2024-10-16 13:52:03 +02:00
none: %w[string boolean integer],
standard: %w[string boolean integer],
business: ["*"],
community: ["*"],
},
},
api: {
all: {
none: [],
standard: [],
2024-10-16 13:52:03 +02:00
business: ["*"],
community: ["*"],
},
},
}
end
2024-10-16 13:52:03 +02:00
attr_accessor :product_id, :product_slug
def initialize(update = false)
2024-10-16 13:52:03 +02:00
::DiscourseSubscriptionClient::Subscriptions.update if update
2023-09-25 18:00:39 +02:00
result = ::DiscourseSubscriptionClient.find_subscriptions("discourse-custom-wizard")
2023-09-24 11:03:38 +02:00
if result&.any?
2024-10-16 13:52:03 +02:00
ids_and_slugs =
result.subscriptions.map do |subscription|
{ id: subscription.product_id, slug: result.products[subscription.product_id] }
end
2023-09-24 11:03:38 +02:00
2024-10-16 13:52:03 +02:00
id_and_slug =
ids_and_slugs
.sort { |a, b| PRODUCT_HIERARCHY.index(b[:slug]) - PRODUCT_HIERARCHY.index(a[:slug]) }
.first
2023-09-24 11:03:38 +02:00
@product_id = id_and_slug[:id]
@product_slug = id_and_slug[:slug]
end
2023-06-29 10:26:39 +02:00
@product_slug ||= ENV["CUSTOM_WIZARD_PRODUCT_SLUG"]
end
def includes?(feature, attribute, value = nil)
attributes = self.class.attributes[feature]
## Attribute is not part of a subscription
return true unless attributes.present? && attributes.key?(attribute)
values = attributes[attribute][type]
## Subscription type does not support the attribute.
return false if values.blank?
## Value is an exception for the subscription type
if (exceptions = get_exceptions(values)).any?
value = mapped_output(value) if CustomWizard::Mapper.mapped_value?(value)
value = [*value].map(&:to_s)
return false if (exceptions & value).length > 0
end
## Subscription type supports all values of the attribute.
return true if values.include?("*")
## Subscription type supports some values of the attributes.
values.include?(value)
end
def type
return :none unless subscribed?
return :business if business?
2023-06-29 10:26:39 +02:00
return :standard if standard?
2023-09-24 13:20:57 +02:00
:community if community?
end
def subscribed?
standard? || business? || community?
end
def standard?
product_slug === "standard"
end
def business?
product_slug === "business"
end
def community?
product_slug === "community"
end
2023-11-17 10:10:46 +01:00
# TODO candidate for removal once code that depends on it externally is no longer used.
def self.client_installed?
2024-10-16 13:52:03 +02:00
defined?(DiscourseSubscriptionClient) == "constant" &&
DiscourseSubscriptionClient.class == Module
2023-11-17 10:10:46 +01:00
end
2021-08-10 11:00:42 +02:00
def self.subscribed?
new.subscribed?
2021-08-10 11:00:42 +02:00
end
2021-08-18 08:59:43 +02:00
def self.business?
new.business?
end
def self.community?
new.community?
end
def self.standard?
new.standard?
end
2021-09-01 04:19:00 +02:00
def self.type
new.type
2021-09-01 04:19:00 +02:00
end
2022-03-25 17:08:24 +01:00
def self.includes?(feature, attribute, value)
new.includes?(feature, attribute, value)
end
protected
def get_exceptions(values)
values.reduce([]) do |result, value|
result << value.split("!").last if value.start_with?("!")
result
end
end
def mapped_output(value)
2024-10-16 13:52:03 +02:00
value
.reduce([]) do |result, v|
## We can only validate mapped assignment values at the moment
result << v["output"] if v.is_a?(Hash) && v["type"] === "assignment"
result
end
.flatten
end
2021-09-07 14:11:50 +02:00
end