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
|
2023-02-23 19:24:11 +01:00
|
|
|
PRODUCT_HIERARCHY = %w[
|
|
|
|
community
|
|
|
|
standard
|
|
|
|
business
|
|
|
|
]
|
2022-03-25 12:18:54 +01:00
|
|
|
|
|
|
|
def self.attributes
|
|
|
|
{
|
|
|
|
wizard: {
|
|
|
|
required: {
|
|
|
|
none: [],
|
|
|
|
standard: ['*'],
|
2022-09-23 15:52:05 +02:00
|
|
|
business: ['*'],
|
|
|
|
community: ['*']
|
2022-03-25 12:18:54 +01:00
|
|
|
},
|
|
|
|
permitted: {
|
|
|
|
none: [],
|
|
|
|
standard: ['*'],
|
2022-09-23 15:52:05 +02:00
|
|
|
business: ['*'],
|
2023-02-08 13:32:24 +01:00
|
|
|
community: ['*', "!#{CustomWizard::Wizard::GUEST_GROUP_ID}"]
|
2022-10-26 09:40:02 +02:00
|
|
|
},
|
|
|
|
restart_on_revisit: {
|
|
|
|
none: [],
|
|
|
|
standard: ['*'],
|
|
|
|
business: ['*'],
|
|
|
|
community: ['*']
|
2022-03-25 12:18:54 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
step: {
|
|
|
|
condition: {
|
|
|
|
none: [],
|
|
|
|
standard: ['*'],
|
2022-09-23 15:52:05 +02:00
|
|
|
business: ['*'],
|
|
|
|
community: ['*']
|
2022-03-25 12:18:54 +01:00
|
|
|
},
|
|
|
|
required_data: {
|
|
|
|
none: [],
|
|
|
|
standard: ['*'],
|
2022-09-23 15:52:05 +02:00
|
|
|
business: ['*'],
|
|
|
|
community: ['*']
|
2022-03-25 12:18:54 +01:00
|
|
|
},
|
|
|
|
permitted_params: {
|
|
|
|
none: [],
|
|
|
|
standard: ['*'],
|
2022-09-23 15:52:05 +02:00
|
|
|
business: ['*'],
|
|
|
|
community: ['*']
|
2022-03-25 12:18:54 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
field: {
|
|
|
|
condition: {
|
|
|
|
none: [],
|
|
|
|
standard: ['*'],
|
2022-09-23 15:52:05 +02:00
|
|
|
business: ['*'],
|
|
|
|
community: ['*']
|
2022-03-25 12:18:54 +01:00
|
|
|
},
|
|
|
|
type: {
|
2022-03-25 17:08:24 +01:00
|
|
|
none: ['text', 'textarea', 'text_only', 'date', 'time', 'date_time', 'number', 'checkbox', 'dropdown', 'upload'],
|
2022-03-25 12:18:54 +01:00
|
|
|
standard: ['*'],
|
2022-09-23 15:52:05 +02:00
|
|
|
business: ['*'],
|
|
|
|
community: ['*']
|
2022-03-25 12:18:54 +01:00
|
|
|
},
|
|
|
|
realtime_validations: {
|
|
|
|
none: [],
|
|
|
|
standard: ['*'],
|
2022-09-23 15:52:05 +02:00
|
|
|
business: ['*'],
|
|
|
|
community: ['*']
|
2022-03-25 12:18:54 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
action: {
|
|
|
|
type: {
|
2022-03-25 17:08:24 +01:00
|
|
|
none: ['create_topic', 'update_profile', 'open_composer', 'route_to'],
|
2022-12-23 17:28:09 +01:00
|
|
|
standard: ['create_topic', 'update_profile', 'open_composer', 'route_to', 'send_message', 'watch_categories', 'watch_tags', 'add_to_group'],
|
2022-09-23 15:52:05 +02:00
|
|
|
business: ['*'],
|
|
|
|
community: ['*']
|
2022-03-25 12:18:54 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
custom_field: {
|
|
|
|
klass: {
|
|
|
|
none: ['topic', 'post'],
|
|
|
|
standard: ['topic', 'post'],
|
2022-09-23 15:52:05 +02:00
|
|
|
business: ['*'],
|
|
|
|
community: ['*']
|
2022-03-25 12:18:54 +01:00
|
|
|
},
|
|
|
|
type: {
|
|
|
|
none: ['string', 'boolean', 'integer'],
|
|
|
|
standard: ['string', 'boolean', 'integer'],
|
2022-09-23 15:52:05 +02:00
|
|
|
business: ['*'],
|
|
|
|
community: ['*']
|
2022-03-25 12:18:54 +01:00
|
|
|
}
|
2022-11-01 13:38:56 +01:00
|
|
|
},
|
|
|
|
api: {
|
|
|
|
all: {
|
|
|
|
none: [],
|
|
|
|
standard: [],
|
|
|
|
business: ['*'],
|
|
|
|
community: ['*']
|
|
|
|
}
|
2022-03-25 12:18:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2023-02-23 19:24:11 +01:00
|
|
|
attr_accessor :product_id,
|
|
|
|
:product_slug
|
|
|
|
|
2023-11-15 16:36:21 +01:00
|
|
|
def initialize(update = false)
|
|
|
|
if update
|
|
|
|
::DiscourseSubscriptionClient::Subscriptions.update
|
|
|
|
end
|
|
|
|
|
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?
|
|
|
|
ids_and_slugs = result.subscriptions.map do |subscription|
|
|
|
|
{
|
|
|
|
id: subscription.product_id,
|
|
|
|
slug: result.products[subscription.product_id]
|
|
|
|
}
|
2023-02-23 19:24:11 +01:00
|
|
|
end
|
2023-09-24 11:03:38 +02:00
|
|
|
|
|
|
|
id_and_slug = ids_and_slugs.sort do |a, b|
|
|
|
|
PRODUCT_HIERARCHY.index(b[:slug]) - PRODUCT_HIERARCHY.index(a[:slug])
|
|
|
|
end.first
|
|
|
|
|
|
|
|
@product_id = id_and_slug[:id]
|
|
|
|
@product_slug = id_and_slug[:slug]
|
2023-02-23 19:24:11 +01:00
|
|
|
end
|
2023-06-29 10:26:39 +02:00
|
|
|
|
|
|
|
@product_slug ||= ENV["CUSTOM_WIZARD_PRODUCT_SLUG"]
|
2022-03-25 12:18:54 +01:00
|
|
|
end
|
|
|
|
|
2022-11-01 13:38:56 +01:00
|
|
|
def includes?(feature, attribute, value = nil)
|
2022-03-25 12:18:54 +01:00
|
|
|
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?
|
|
|
|
|
2023-02-08 13:32:24 +01:00
|
|
|
## 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
|
|
|
|
|
2022-03-25 12:18:54 +01:00
|
|
|
## Subscription type supports all values of the attribute.
|
2023-02-08 13:32:24 +01:00
|
|
|
return true if values.include?("*")
|
2022-03-25 12:18:54 +01:00
|
|
|
|
|
|
|
## 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?
|
2022-03-25 12:18:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def subscribed?
|
2022-09-23 15:52:05 +02:00
|
|
|
standard? || business? || community?
|
2022-03-25 12:18:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def standard?
|
2023-02-23 19:24:11 +01:00
|
|
|
product_slug === "standard"
|
2022-03-25 12:18:54 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def business?
|
2023-02-23 19:24:11 +01:00
|
|
|
product_slug === "business"
|
2022-03-25 12:18:54 +01:00
|
|
|
end
|
|
|
|
|
2022-09-23 15:52:05 +02:00
|
|
|
def community?
|
2023-02-23 19:24:11 +01:00
|
|
|
product_slug === "community"
|
2022-03-25 12:18:54 +01:00
|
|
|
end
|
|
|
|
|
2021-08-10 11:00:42 +02:00
|
|
|
def self.subscribed?
|
2022-03-25 12:18:54 +01:00
|
|
|
new.subscribed?
|
2021-08-10 11:00:42 +02:00
|
|
|
end
|
2021-08-18 08:59:43 +02:00
|
|
|
|
2022-03-25 12:18:54 +01:00
|
|
|
def self.business?
|
|
|
|
new.business?
|
|
|
|
end
|
|
|
|
|
2022-09-23 15:52:05 +02:00
|
|
|
def self.community?
|
|
|
|
new.community?
|
|
|
|
end
|
|
|
|
|
2022-03-25 12:18:54 +01:00
|
|
|
def self.standard?
|
|
|
|
new.standard?
|
|
|
|
end
|
2021-09-01 04:19:00 +02:00
|
|
|
|
2022-03-25 12:18:54 +01: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
|
2023-02-08 13:32:24 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
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
|