Make rubocop happy.
This commit is contained in:
parent
9826f10a60
commit
c44ac56d32
|
@ -20,49 +20,49 @@ class DiscourseChat::Channel < DiscourseChat::PluginModel
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def init_data
|
def init_data
|
||||||
self.data = {} if self.data.nil?
|
self.data = {} if self.data.nil?
|
||||||
|
end
|
||||||
|
|
||||||
|
def destroy_rules
|
||||||
|
rules.destroy_all
|
||||||
|
end
|
||||||
|
|
||||||
|
def provider_valid?
|
||||||
|
if !DiscourseChat::Provider.provider_names.include?(provider)
|
||||||
|
errors.add(:provider, "#{provider} is not a valid provider")
|
||||||
|
end
|
||||||
|
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
|
||||||
|
|
||||||
|
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
|
end
|
||||||
|
|
||||||
def destroy_rules
|
check_unique = false
|
||||||
rules.destroy_all
|
matching_channels = DiscourseChat::Channel.with_provider(provider).where.not(id: id)
|
||||||
end
|
|
||||||
|
|
||||||
def provider_valid?
|
data.each do |key, value|
|
||||||
if !DiscourseChat::Provider.provider_names.include?(provider)
|
regex_string = params.find { |p| p[:key] == key }[:regex]
|
||||||
errors.add(:provider, "#{provider} is not a valid provider")
|
if !Regexp.new(regex_string).match(value)
|
||||||
|
errors.add(:data, "data.#{key} is invalid")
|
||||||
|
end
|
||||||
|
|
||||||
|
unique = params.find { |p| p[:key] == key }[:unique]
|
||||||
|
if unique
|
||||||
|
check_unique = true
|
||||||
|
matching_channels = matching_channels.with_data_value(key, value)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def data_valid?
|
if check_unique && matching_channels.exists?
|
||||||
# If provider is invalid, don't try and check data
|
errors.add(:data, "matches an existing channel")
|
||||||
return unless ::DiscourseChat::Provider.provider_names.include? provider
|
|
||||||
|
|
||||||
params = ::DiscourseChat::Provider.get_by_name(provider)::CHANNEL_PARAMETERS
|
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
check_unique = false
|
|
||||||
matching_channels = DiscourseChat::Channel.with_provider(provider).where.not(id: id)
|
|
||||||
|
|
||||||
data.each do |key, value|
|
|
||||||
regex_string = params.find { |p| p[:key] == key }[:regex]
|
|
||||||
if !Regexp.new(regex_string).match(value)
|
|
||||||
errors.add(:data, "data.#{key} is invalid")
|
|
||||||
end
|
|
||||||
|
|
||||||
unique = params.find { |p| p[:key] == key }[:unique]
|
|
||||||
if unique
|
|
||||||
check_unique = true
|
|
||||||
matching_channels = matching_channels.with_data_value(key, value)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if check_unique && matching_channels.exists?
|
|
||||||
errors.add(:data, "matches an existing channel")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,22 +18,22 @@ class DiscourseChat::PluginModel < PluginStoreRow
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def set_key
|
def set_key
|
||||||
self.key ||= self.class.alloc_key
|
self.key ||= self.class.alloc_key
|
||||||
end
|
end
|
||||||
|
|
||||||
def init_plugin_model
|
def init_plugin_model
|
||||||
self.type_name ||= 'JSON'
|
self.type_name ||= 'JSON'
|
||||||
self.plugin_name ||= PLUGIN_NAME
|
self.plugin_name ||= PLUGIN_NAME
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.alloc_key
|
def self.alloc_key
|
||||||
DistributedMutex.synchronize("#{self::PLUGIN_NAME}_#{self.key_prefix}_id") do
|
DistributedMutex.synchronize("#{self::PLUGIN_NAME}_#{self.key_prefix}_id") do
|
||||||
max_id = PluginStore.get(self::PLUGIN_NAME, "#{self.key_prefix}_id")
|
max_id = PluginStore.get(self::PLUGIN_NAME, "#{self.key_prefix}_id")
|
||||||
max_id = 1 unless max_id
|
max_id = 1 unless max_id
|
||||||
PluginStore.set(self::PLUGIN_NAME, "#{self.key_prefix}_id", max_id + 1)
|
PluginStore.set(self::PLUGIN_NAME, "#{self.key_prefix}_id", max_id + 1)
|
||||||
"#{self.key_prefix}#{max_id}"
|
"#{self.key_prefix}#{max_id}"
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -81,48 +81,48 @@ class DiscourseChat::Rule < DiscourseChat::PluginModel
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def channel_valid?
|
def channel_valid?
|
||||||
if !(DiscourseChat::Channel.where(id: channel_id).exists?)
|
if !(DiscourseChat::Channel.where(id: channel_id).exists?)
|
||||||
errors.add(:channel_id, "#{channel_id} is not a valid channel id")
|
errors.add(:channel_id, "#{channel_id} is not a valid channel id")
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def category_valid?
|
||||||
|
if type != 'normal' && !category_id.nil?
|
||||||
|
errors.add(:category_id, "cannot be specified for that type of rule")
|
||||||
end
|
end
|
||||||
|
|
||||||
def category_valid?
|
return unless type == 'normal'
|
||||||
if type != 'normal' && !category_id.nil?
|
|
||||||
errors.add(:category_id, "cannot be specified for that type of rule")
|
|
||||||
end
|
|
||||||
|
|
||||||
return unless type == 'normal'
|
if !(category_id.nil? || Category.where(id: category_id).exists?)
|
||||||
|
errors.add(:category_id, "#{category_id} is not a valid category id")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if !(category_id.nil? || Category.where(id: category_id).exists?)
|
def group_valid?
|
||||||
errors.add(:category_id, "#{category_id} is not a valid category id")
|
if type == 'normal' && !group_id.nil?
|
||||||
end
|
errors.add(:group_id, "cannot be specified for that type of rule")
|
||||||
end
|
end
|
||||||
|
|
||||||
def group_valid?
|
return if type == 'normal'
|
||||||
if type == 'normal' && !group_id.nil?
|
|
||||||
errors.add(:group_id, "cannot be specified for that type of rule")
|
|
||||||
end
|
|
||||||
|
|
||||||
return if type == 'normal'
|
if !Group.where(id: group_id).exists?
|
||||||
|
errors.add(:group_id, "#{group_id} is not a valid group id")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if !Group.where(id: group_id).exists?
|
def tags_valid?
|
||||||
errors.add(:group_id, "#{group_id} is not a valid group id")
|
return if tags.nil?
|
||||||
|
|
||||||
|
tags.each do |tag|
|
||||||
|
if !Tag.where(name: tag).exists?
|
||||||
|
errors.add(:tags, "#{tag} is not a valid tag")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def tags_valid?
|
def init_filter
|
||||||
return if tags.nil?
|
self.filter ||= 'watch'
|
||||||
|
self.type ||= 'normal'
|
||||||
tags.each do |tag|
|
end
|
||||||
if !Tag.where(name: tag).exists?
|
|
||||||
errors.add(:tags, "#{tag} is not a valid tag")
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def init_filter
|
|
||||||
self.filter ||= 'watch'
|
|
||||||
self.type ||= 'normal'
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -87,9 +87,9 @@ module DiscourseChat::Provider::SlackProvider
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def user
|
def user
|
||||||
return nil unless user_id = @raw["user"]
|
return nil unless user_id = @raw["user"]
|
||||||
@transcript.users.find { |u| u["id"] == user_id }
|
@transcript.users.find { |u| u["id"] == user_id }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,13 +37,13 @@ module DiscourseChat::Provider::SlackProvider
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def absolute_url(url)
|
def absolute_url(url)
|
||||||
uri = URI(url) rescue nil
|
uri = URI(url) rescue nil
|
||||||
|
|
||||||
return Discourse.current_hostname unless uri
|
return Discourse.current_hostname unless uri
|
||||||
uri.host = Discourse.current_hostname if !uri.host
|
uri.host = Discourse.current_hostname if !uri.host
|
||||||
uri.scheme = (SiteSetting.force_https ? 'https' : 'http') if !uri.scheme
|
uri.scheme = (SiteSetting.force_https ? 'https' : 'http') if !uri.scheme
|
||||||
uri.to_s
|
uri.to_s
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue