diff --git a/app/models/channel.rb b/app/models/channel.rb index c43200a..007d5ef 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -20,49 +20,49 @@ class DiscourseChat::Channel < DiscourseChat::PluginModel private - def init_data - self.data = {} if self.data.nil? + def init_data + 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 - def destroy_rules - rules.destroy_all - end + check_unique = false + matching_channels = DiscourseChat::Channel.with_provider(provider).where.not(id: id) - def provider_valid? - if !DiscourseChat::Provider.provider_names.include?(provider) - errors.add(:provider, "#{provider} is not a valid provider") + 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 - 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 - - 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 + if check_unique && matching_channels.exists? + errors.add(:data, "matches an existing channel") end + end end diff --git a/app/models/plugin_model.rb b/app/models/plugin_model.rb index b275579..e2d9702 100644 --- a/app/models/plugin_model.rb +++ b/app/models/plugin_model.rb @@ -18,22 +18,22 @@ class DiscourseChat::PluginModel < PluginStoreRow private - def set_key - self.key ||= self.class.alloc_key - end + def set_key + self.key ||= self.class.alloc_key + end - def init_plugin_model - self.type_name ||= 'JSON' - self.plugin_name ||= PLUGIN_NAME - end + def init_plugin_model + self.type_name ||= 'JSON' + self.plugin_name ||= PLUGIN_NAME + end - def self.alloc_key - DistributedMutex.synchronize("#{self::PLUGIN_NAME}_#{self.key_prefix}_id") do - max_id = PluginStore.get(self::PLUGIN_NAME, "#{self.key_prefix}_id") - max_id = 1 unless max_id - PluginStore.set(self::PLUGIN_NAME, "#{self.key_prefix}_id", max_id + 1) - "#{self.key_prefix}#{max_id}" - end + def self.alloc_key + DistributedMutex.synchronize("#{self::PLUGIN_NAME}_#{self.key_prefix}_id") do + max_id = PluginStore.get(self::PLUGIN_NAME, "#{self.key_prefix}_id") + max_id = 1 unless max_id + PluginStore.set(self::PLUGIN_NAME, "#{self.key_prefix}_id", max_id + 1) + "#{self.key_prefix}#{max_id}" end + end end diff --git a/app/models/rule.rb b/app/models/rule.rb index 3e50a9b..32454c2 100644 --- a/app/models/rule.rb +++ b/app/models/rule.rb @@ -81,48 +81,48 @@ class DiscourseChat::Rule < DiscourseChat::PluginModel private - def channel_valid? - if !(DiscourseChat::Channel.where(id: channel_id).exists?) - errors.add(:channel_id, "#{channel_id} is not a valid channel id") - end + def channel_valid? + if !(DiscourseChat::Channel.where(id: channel_id).exists?) + errors.add(:channel_id, "#{channel_id} is not a valid channel id") + end + end + + def category_valid? + if type != 'normal' && !category_id.nil? + errors.add(:category_id, "cannot be specified for that type of rule") end - def category_valid? - if type != 'normal' && !category_id.nil? - errors.add(:category_id, "cannot be specified for that type of rule") - end + return unless type == 'normal' - 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?) - errors.add(:category_id, "#{category_id} is not a valid category id") - end + def group_valid? + if type == 'normal' && !group_id.nil? + errors.add(:group_id, "cannot be specified for that type of rule") end - def group_valid? - if type == 'normal' && !group_id.nil? - errors.add(:group_id, "cannot be specified for that type of rule") - end + return if type == 'normal' - 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? - errors.add(:group_id, "#{group_id} is not a valid group id") + def tags_valid? + 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 - def tags_valid? - 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 - - def init_filter - self.filter ||= 'watch' - self.type ||= 'normal' - end + def init_filter + self.filter ||= 'watch' + self.type ||= 'normal' + end end diff --git a/lib/discourse_chat/provider/slack/slack_message.rb b/lib/discourse_chat/provider/slack/slack_message.rb index b2a80c0..a5ca70e 100644 --- a/lib/discourse_chat/provider/slack/slack_message.rb +++ b/lib/discourse_chat/provider/slack/slack_message.rb @@ -87,9 +87,9 @@ module DiscourseChat::Provider::SlackProvider private - def user - return nil unless user_id = @raw["user"] - @transcript.users.find { |u| u["id"] == user_id } - end + def user + return nil unless user_id = @raw["user"] + @transcript.users.find { |u| u["id"] == user_id } + end end end diff --git a/lib/discourse_chat/provider/slack/slack_message_formatter.rb b/lib/discourse_chat/provider/slack/slack_message_formatter.rb index e1bcbae..55e68a7 100644 --- a/lib/discourse_chat/provider/slack/slack_message_formatter.rb +++ b/lib/discourse_chat/provider/slack/slack_message_formatter.rb @@ -37,13 +37,13 @@ module DiscourseChat::Provider::SlackProvider private - def absolute_url(url) - uri = URI(url) rescue nil + def absolute_url(url) + uri = URI(url) rescue nil - return Discourse.current_hostname unless uri - uri.host = Discourse.current_hostname if !uri.host - uri.scheme = (SiteSetting.force_https ? 'https' : 'http') if !uri.scheme - uri.to_s - end + return Discourse.current_hostname unless uri + uri.host = Discourse.current_hostname if !uri.host + uri.scheme = (SiteSetting.force_https ? 'https' : 'http') if !uri.scheme + uri.to_s + end end end