0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-13 21:42:53 +01:00
discourse-custom-wizard/lib/custom_wizard/subscription.rb

241 Zeilen
5,5 KiB
Ruby

# frozen_string_literal: true
2021-09-24 11:58:42 +02:00
class CustomWizard::Subscription
2021-09-01 04:19:00 +02:00
include ActiveModel::Serialization
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
2021-09-24 11:58:42 +02:00
@authentication = CustomWizard::Subscription::Authentication.new(get_authentication)
@subscription = CustomWizard::Subscription::Subscription.new(get_subscription)
2021-09-01 04:19:00 +02:00
end
def authorized?
@authentication.active?
end
def subscribed?
2021-09-03 10:46:43 +02:00
@subscription.active?
end
2021-08-18 08:59:43 +02:00
def server
"test.thepavilion.io"
end
def subscription_type
"stripe"
end
2021-10-12 10:54:52 +02:00
def type
@subscription.type
end
2021-08-18 08:59:43 +02:00
def client_name
"custom-wizard"
end
2021-09-01 04:19:00 +02:00
def scope
"discourse-subscription-server:user_subscription"
end
2021-08-10 11:00:42 +02:00
def requires_additional_subscription(kategory, sub_kategory)
case kategory
when "actions"
case self.type
when "business"
2021-11-09 16:01:17 +01:00
[]
when "standard"
2021-11-09 16:01:17 +01:00
SUBSCRIPTION_LEVELS[:business][kategory.to_sym]
else
2021-11-09 16:01:17 +01:00
SUBSCRIPTION_LEVELS[:standard][kategory.to_sym] + SUBSCRIPTION_LEVELS[:business][kategory.to_sym]
end
when "custom_fields"
case self.type
when "business"
2021-11-09 16:01:17 +01:00
[]
when "standard"
2021-11-09 16:04:04 +01:00
SUBSCRIPTION_LEVELS[:business][kategory.to_sym][sub_kategory.to_sym]
else
2021-11-09 16:01:17 +01:00
SUBSCRIPTION_LEVELS[:standard][kategory.to_sym][sub_kategory.to_sym] + SUBSCRIPTION_LEVELS[:business][kategory.to_sym][sub_kategory.to_sym]
end
else
2021-11-09 16:01:17 +01:00
[]
end
end
2021-09-24 11:58:42 +02:00
def update
2021-08-18 08:59:43 +02:00
if @authentication.active?
response = Excon.get(
"https://#{server}/subscription-server/user-subscriptions/#{subscription_type}/#{client_name}",
headers: {
"User-Api-Key" => @authentication.api_key
}
)
if response.status == 200
begin
data = JSON.parse(response.body).deep_symbolize_keys
rescue JSON::ParserError
return false
end
2021-09-01 04:19:00 +02:00
return false unless data && data.is_a?(Hash)
subscriptions = data[:subscriptions]
2021-09-07 14:06:13 +02:00
if subscriptions.present? && type = subscriptions.first[:price_nickname]
2021-09-01 04:19:00 +02:00
@subscription = set_subscription(type)
return true
end
2021-08-18 08:59:43 +02:00
end
end
2021-09-07 14:06:13 +02:00
destroy_subscription
false
2021-08-18 08:59:43 +02:00
end
2021-09-01 04:19:00 +02:00
def destroy_subscription
2021-09-07 14:06:13 +02:00
if remove_subscription
2021-09-24 11:58:42 +02:00
@subscription = CustomWizard::Subscription::Subscription.new(get_subscription)
2021-09-07 14:06:13 +02:00
!@subscription.active?
else
false
end
2021-08-18 08:59:43 +02:00
end
2021-09-07 14:06:13 +02:00
def authentication_url(user_id, request_id)
2021-08-18 08:59:43 +02:00
keys = @authentication.generate_keys(user_id, request_id)
params = {
public_key: keys.public_key,
nonce: keys.nonce,
client_id: @authentication.client_id,
2021-09-24 11:58:42 +02:00
auth_redirect: "#{Discourse.base_url}/admin/wizards/subscription/authorize/callback",
2021-08-18 08:59:43 +02:00
application_name: SiteSetting.title,
2021-09-01 04:19:00 +02:00
scopes: scope
2021-08-18 08:59:43 +02:00
}
uri = URI.parse("https://#{server}/user-api-key/new")
uri.query = URI.encode_www_form(params)
uri.to_s
end
2021-09-01 04:19:00 +02:00
def authentication_response(request_id, payload)
2021-08-18 08:59:43 +02:00
data = @authentication.decrypt_payload(request_id, payload)
2021-09-07 14:06:13 +02:00
return false unless data.is_a?(Hash) && data[:key] && data[:user_id]
2021-08-18 08:59:43 +02:00
2021-09-01 04:19:00 +02:00
api_key = data[:key]
user_id = data[:user_id]
user = User.find(user_id)
2021-08-18 08:59:43 +02:00
2021-09-01 04:19:00 +02:00
if user&.admin
@authentication = set_authentication(api_key, user.id)
true
else
false
end
2021-08-18 08:59:43 +02:00
end
2021-09-01 04:19:00 +02:00
def destroy_authentication
2021-09-07 14:06:13 +02:00
if remove_authentication
2021-09-24 11:58:42 +02:00
@authentication = CustomWizard::Subscription::Authentication.new(get_authentication)
2021-09-07 14:06:13 +02:00
!@authentication.active?
else
false
end
2021-08-18 08:59:43 +02:00
end
2021-08-10 11:00:42 +02:00
def self.subscribed?
self.new.subscribed?
end
2021-08-18 08:59:43 +02:00
2021-10-12 10:54:52 +02:00
def self.type
self.new.type
end
def self.requires_additional_subscription(kategory, sub_kategory)
self.new.requires_additional_subscription(kategory, sub_kategory)
end
2021-09-07 14:06:13 +02:00
def self.authorized?
self.new.authorized?
end
2021-09-24 11:58:42 +02:00
def self.update
self.new.update
2021-09-07 14:06:13 +02:00
end
2021-08-18 08:59:43 +02:00
def self.namespace
2021-09-24 11:58:42 +02:00
"custom_wizard_subscription"
2021-08-18 08:59:43 +02:00
end
2021-09-01 04:19:00 +02:00
private
def subscription_db_key
"subscription"
end
def authentication_db_key
"authentication"
end
def get_subscription
raw = PluginStore.get(self.class.namespace, subscription_db_key)
if raw.present?
OpenStruct.new(
type: raw['type'],
updated_at: raw['updated_at']
)
end
end
def remove_subscription
PluginStore.remove(self.class.namespace, subscription_db_key)
end
def set_subscription(type)
2021-09-24 11:58:42 +02:00
PluginStore.set(CustomWizard::Subscription.namespace, subscription_db_key, type: type, updated_at: Time.now)
CustomWizard::Subscription::Subscription.new(get_subscription)
2021-09-01 04:19:00 +02:00
end
def get_authentication
raw = PluginStore.get(self.class.namespace, authentication_db_key)
OpenStruct.new(
key: raw && raw['key'],
auth_by: raw && raw['auth_by'],
auth_at: raw && raw['auth_at']
)
end
def set_authentication(key, user_id)
PluginStore.set(self.class.namespace, authentication_db_key,
key: key,
auth_by: user_id,
auth_at: Time.now
)
2021-09-24 11:58:42 +02:00
CustomWizard::Subscription::Authentication.new(get_authentication)
2021-09-01 04:19:00 +02:00
end
def remove_authentication
PluginStore.remove(self.class.namespace, authentication_db_key)
2021-09-07 14:06:13 +02:00
get_authentication
2021-09-01 04:19:00 +02:00
end
2021-09-07 14:11:50 +02:00
end