2019-05-12 22:37:49 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
class DiscourseChatIntegration::Channel < DiscourseChatIntegration::PluginModel
|
2017-07-13 08:32:11 -04:00
|
|
|
# Setup ActiveRecord::Store to use the JSON field to read/write these values
|
2022-12-29 07:31:05 -05:00
|
|
|
store :value, accessors: %i[provider error_key error_info data], coder: JSON
|
2017-07-13 11:09:34 -04:00
|
|
|
|
2017-09-28 04:32:38 -04:00
|
|
|
scope :with_provider, ->(provider) { where("value::json->>'provider'=?", provider) }
|
2022-12-29 07:31:05 -05:00
|
|
|
scope :with_data_value,
|
|
|
|
->(key, value) { where("(value::json->>'data')::json->>?=?", key.to_s, value.to_s) }
|
2017-07-13 15:50:45 -04:00
|
|
|
|
2017-09-28 04:32:38 -04:00
|
|
|
after_initialize :init_data
|
2017-07-18 11:42:05 -04:00
|
|
|
after_destroy :destroy_rules
|
|
|
|
|
2017-07-13 15:50:45 -04:00
|
|
|
validate :provider_valid?, :data_valid?
|
|
|
|
|
2017-10-03 03:35:27 -04:00
|
|
|
def self.key_prefix
|
2022-12-29 07:31:05 -05:00
|
|
|
"channel:".freeze
|
2017-10-03 03:35:27 -04:00
|
|
|
end
|
|
|
|
|
2017-09-28 04:32:38 -04:00
|
|
|
def rules
|
2021-07-13 15:36:16 -04:00
|
|
|
DiscourseChatIntegration::Rule.with_channel_id(id).order_by_precedence
|
2017-07-13 15:50:45 -04:00
|
|
|
end
|
|
|
|
|
2017-09-28 04:32:38 -04:00
|
|
|
private
|
2017-07-13 15:50:45 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
def init_data
|
|
|
|
self.data = {} if self.data.nil?
|
|
|
|
end
|
2017-07-13 11:09:34 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
def destroy_rules
|
|
|
|
rules.destroy_all
|
|
|
|
end
|
2017-07-28 10:47:46 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
def provider_valid?
|
2021-07-13 15:36:16 -04:00
|
|
|
if !DiscourseChatIntegration::Provider.provider_names.include?(provider)
|
2018-06-07 21:52:35 -04:00
|
|
|
errors.add(:provider, "#{provider} is not a valid provider")
|
2017-07-13 11:09:34 -04:00
|
|
|
end
|
2018-06-07 21:52:35 -04:00
|
|
|
end
|
2017-08-01 15:53:39 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
def data_valid?
|
|
|
|
# If provider is invalid, don't try and check data
|
2024-05-28 05:16:00 -04:00
|
|
|
return if ::DiscourseChatIntegration::Provider.provider_names.exclude? provider
|
2017-07-13 11:09:34 -04:00
|
|
|
|
2021-07-13 15:36:16 -04:00
|
|
|
params = ::DiscourseChatIntegration::Provider.get_by_name(provider)::CHANNEL_PARAMETERS
|
2017-07-13 11:09:34 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
unless params.map { |p| p[:key] }.sort == data.keys.sort
|
|
|
|
errors.add(:data, "data does not match the required structure for provider #{provider}")
|
|
|
|
return
|
|
|
|
end
|
2017-07-13 08:32:11 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
check_unique = false
|
2021-07-13 15:36:16 -04:00
|
|
|
matching_channels = DiscourseChatIntegration::Channel.with_provider(provider).where.not(id: id)
|
2017-09-28 04:32:38 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
data.each do |key, value|
|
|
|
|
regex_string = params.find { |p| p[:key] == key }[:regex]
|
2022-12-29 07:31:05 -05:00
|
|
|
errors.add(:data, "data.#{key} is invalid") if !Regexp.new(regex_string).match(value)
|
2017-07-13 17:50:14 -04:00
|
|
|
|
2018-06-07 21:52:35 -04:00
|
|
|
unique = params.find { |p| p[:key] == key }[:unique]
|
|
|
|
if unique
|
|
|
|
check_unique = true
|
|
|
|
matching_channels = matching_channels.with_data_value(key, value)
|
2017-09-28 04:32:38 -04:00
|
|
|
end
|
|
|
|
end
|
2018-06-07 21:52:35 -04:00
|
|
|
|
2022-12-29 07:31:05 -05:00
|
|
|
errors.add(:data, "matches an existing channel") if check_unique && matching_channels.exists?
|
2018-06-07 21:52:35 -04:00
|
|
|
end
|
2017-08-01 15:53:39 -04:00
|
|
|
end
|