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