0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-10 12:22:54 +01:00
discourse-custom-wizard/lib/custom_wizard/notice/connection_error.rb

78 Zeilen
1,8 KiB
Ruby

2021-10-05 14:54:06 +02:00
# frozen_string_literal: true
class CustomWizard::Notice::ConnectionError
2021-11-01 14:52:29 +01:00
attr_reader :archetype
2021-10-05 14:54:06 +02:00
2021-11-01 14:52:29 +01:00
def initialize(archetype)
@archetype = archetype
2021-10-05 14:54:06 +02:00
end
def create!
2021-11-01 14:52:29 +01:00
if attrs = current_error
2021-11-02 08:29:31 +01:00
key = "#{archetype.to_s}_error_#{attrs[:id]}"
attrs[:updated_at] = Time.now
attrs[:count] = attrs[:count].to_i + 1
2021-10-05 14:54:06 +02:00
else
2021-11-01 14:52:29 +01:00
domain = CustomWizard::Notice.send("#{archetype.to_s}_domain")
id = SecureRandom.hex(8)
2021-10-05 14:54:06 +02:00
attrs = {
2021-11-01 14:52:29 +01:00
id: id,
2021-10-05 14:54:06 +02:00
message: I18n.t("wizard.notice.connection_error", domain: domain),
2021-11-01 14:52:29 +01:00
archetype: CustomWizard::Notice.archetypes[archetype.to_sym],
2021-10-05 14:54:06 +02:00
created_at: Time.now,
count: 1
}
2021-11-01 14:52:29 +01:00
key = "#{archetype.to_s}_error_#{id}"
2021-10-05 14:54:06 +02:00
end
2021-11-01 14:52:29 +01:00
PluginStore.set(namespace, key, attrs)
2021-11-02 08:29:31 +01:00
@current_error = nil
2021-10-05 14:54:06 +02:00
end
def expire!
2021-11-01 14:52:29 +01:00
if query = current_error(query_only: true)
record = query.first
error = JSON.parse(record.value)
error['expired_at'] = Time.now
record.value = error.to_json
record.save
2021-10-05 14:54:06 +02:00
end
end
def plugin_status_limit
5
end
2021-11-01 14:52:29 +01:00
def subscription_message_limit
2021-10-05 14:54:06 +02:00
10
end
def limit
2021-11-01 14:52:29 +01:00
self.send("#{archetype.to_s}_limit")
2021-10-05 14:54:06 +02:00
end
def reached_limit?
2021-11-01 14:52:29 +01:00
return false unless current_error.present?
2021-11-02 08:29:31 +01:00
current_error[:count].to_i >= limit
2021-10-05 14:54:06 +02:00
end
def namespace
"#{CustomWizard::PLUGIN_NAME}_notice_connection"
end
2021-11-01 14:52:29 +01:00
def current_error(query_only: false)
@current_error ||= begin
2021-10-05 14:54:06 +02:00
query = PluginStoreRow.where(plugin_name: namespace)
2021-11-02 08:29:31 +01:00
query = query.where("(value::json->>'archetype')::integer = ?", CustomWizard::Notice.archetypes[archetype.to_sym])
2021-11-01 14:52:29 +01:00
query = query.where("(value::json->>'expired_at') IS NULL")
2021-11-02 08:29:31 +01:00
2021-11-01 14:52:29 +01:00
return nil if !query.exists?
return query if query_only
2021-11-02 08:29:31 +01:00
JSON.parse(query.first.value).deep_symbolize_keys
2021-10-05 14:54:06 +02:00
end
end
end