Correct rubocop offenses

This commit is contained in:
David Taylor 2017-08-01 20:53:39 +01:00
parent 4c379876b6
commit 4f9ad4efef
43 changed files with 488 additions and 514 deletions

View File

@ -37,7 +37,7 @@ class DiscourseChat::ChatController < ApplicationController
render json: { errors: [e.message] }, status: 422
rescue DiscourseChat::ProviderError => e
Rails.logger.error("Test provider failed #{e.info}")
if e.info.key?(:error_key) and !e.info[:error_key].nil?
if e.info.key?(:error_key) && !e.info[:error_key].nil?
render json: { error_key: e.info[:error_key] }, status: 422
else
render json: { errors: [e.message] }, status: 422
@ -63,7 +63,7 @@ class DiscourseChat::ChatController < ApplicationController
begin
providers = ::DiscourseChat::Provider.enabled_providers.map { |x| x::PROVIDER_NAME }
if not defined? params[:channel] and defined? params[:channel][:provider]
if (not defined? params[:channel]) && defined? params[:channel][:provider]
raise Discourse::InvalidParameters, 'Provider is not valid'
end

View File

@ -110,7 +110,7 @@ module DiscourseChat
category: category_name
)
if SiteSetting.tagging_enabled and not rule.tags.nil?
if SiteSetting.tagging_enabled && (not rule.tags.nil?)
text << I18n.t("chat_integration.provider.#{provider}.status.rule_string_tags_suffix", tags: rule.tags.join(', '))
end
@ -129,7 +129,7 @@ module DiscourseChat
def self.delete_by_index(channel, index)
rules = channel.rules.order_by_precedence
return false if index < 1 or index > rules.size
return false if index < (1) || index > (rules.size)
return :deleted if rules[index - 1].destroy
end
@ -192,11 +192,10 @@ module DiscourseChat
def self.save_transcript(transcript)
secret = SecureRandom.hex
redis_key = "chat_integration:transcript:" + secret
$redis.set(redis_key, transcript, {:ex => 3600}) # Expire in 1 hour
$redis.set(redis_key, transcript, ex: 3600) # Expire in 1 hour
return secret
end
end
end

View File

@ -11,11 +11,11 @@ class DiscourseChat::Rule < DiscourseChat::PluginModel
self.type ||= 'normal'
end
validates :filter, :inclusion => { :in => %w(watch follow mute),
:message => "%{value} is not a valid filter" }
validates :filter, inclusion: { in: %w(watch follow mute),
message: "%{value} is not a valid filter" }
validates :type, :inclusion => { :in => %w(normal group_message group_mention),
:message => "%{value} is not a valid filter" }
validates :type, inclusion: { in: %w(normal group_message group_mention),
message: "%{value} is not a valid filter" }
validate :channel_valid?, :category_valid?, :group_valid?, :tags_valid?
@ -34,7 +34,7 @@ class DiscourseChat::Rule < DiscourseChat::PluginModel
return unless type == 'normal'
# Validate category
if not (category_id.nil? or Category.where(id: category_id).exists?)
if not (category_id.nil? || Category.where(id: category_id).exists?)
errors.add(:category_id, "#{category_id} is not a valid category id")
end
end
@ -64,7 +64,7 @@ class DiscourseChat::Rule < DiscourseChat::PluginModel
# We never want an empty array, set it to nil instead
def tags=(array)
if array.nil? or array.empty?
if array.nil? || array.empty?
super(nil)
else
super(array)
@ -74,7 +74,7 @@ class DiscourseChat::Rule < DiscourseChat::PluginModel
# These are only allowed to be integers
%w(channel_id category_id group_id).each do |name|
define_method "#{name}=" do |val|
if val.nil? or val.blank?
if val.nil? || val.blank?
super(nil)
else
super(val.to_i)

View File

@ -46,7 +46,7 @@ module DiscourseChat
if SiteSetting.tagging_enabled
topic_tags = topic.tags.present? ? topic.tags.pluck(:name) : []
matching_rules = matching_rules.select do |rule|
next true if rule.tags.nil? or rule.tags.empty? # Filter has no tags specified
next true if rule.tags.nil? || rule.tags.empty? # Filter has no tags specified
any_tags_match = !((rule.tags & topic_tags).empty?)
next any_tags_match # If any tags match, keep this filter, otherwise throw away
end
@ -84,7 +84,7 @@ module DiscourseChat
provider.trigger_notification(post, channel)
channel.update_attribute('error_key', nil) if channel.error_key
rescue => e
if e.class == DiscourseChat::ProviderError and e.info.key?(:error_key) and !e.info[:error_key].nil?
if e.class == (DiscourseChat::ProviderError) && e.info.key?(:error_key) && !e.info[:error_key].nil?
channel.update_attribute('error_key', e.info[:error_key])
else
channel.update_attribute('error_key', 'chat_integration.channel_exception')
@ -104,6 +104,5 @@ module DiscourseChat
end
end
end

View File

@ -71,11 +71,11 @@ module DiscourseChat
engines = []
DiscourseChat::Provider.providers.each do |provider|
engine = provider.constants.select do |constant|
constant.to_s =~ /Engine$/ and not constant.to_s == "HookEngine"
constant.to_s =~ (/Engine$/) && (not constant.to_s == "HookEngine")
end.map(&provider.method(:const_get)).first
if engine
engines.push({engine: engine, name: provider::PROVIDER_NAME})
engines.push(engine: engine, name: provider::PROVIDER_NAME)
end
end

View File

@ -13,9 +13,7 @@ module DiscourseChat
event_type = 'm.room.message'
uid = Time.now.to_i
url_params = URI.encode_www_form({
access_token: SiteSetting.chat_integration_matrix_access_token
})
url_params = URI.encode_www_form(access_token: SiteSetting.chat_integration_matrix_access_token)
url = "#{homeserver}/_matrix/client/r0/rooms/#{CGI::escape(room_id)}/send/#{event_type}/#{uid}"
@ -42,18 +40,14 @@ module DiscourseChat
message = {
msgtype: 'm.notice',
body: I18n.t('chat_integration.provider.matrix.text_message', {
user: display_name,
body: I18n.t('chat_integration.provider.matrix.text_message', user: display_name,
post_url: post.full_url,
title: post.topic.title
}),
title: post.topic.title),
format: 'org.matrix.custom.html',
formatted_body: I18n.t('chat_integration.provider.matrix.formatted_message', {
user: display_name,
formatted_body: I18n.t('chat_integration.provider.matrix.formatted_message', user: display_name,
post_url: post.full_url,
title: post.topic.title,
excerpt: post.excerpt(SiteSetting.chat_integration_discord_excerpt_length, text_entities: true, strip_links: true, remap_emoji: true),
})
excerpt: post.excerpt(SiteSetting.chat_integration_discord_excerpt_length, text_entities: true, strip_links: true, remap_emoji: true))
}

View File

@ -66,6 +66,3 @@ module DiscourseChat::Provider::MattermostProvider
end
end

View File

@ -68,7 +68,7 @@ module DiscourseChat::Provider::SlackProvider
# Load the user data (we need this to change user IDs into usernames)
req = Net::HTTP::Post.new(URI('https://slack.com/api/users.list'))
req.set_form_data({token: SiteSetting.chat_integration_slack_access_token})
req.set_form_data(token: SiteSetting.chat_integration_slack_access_token)
response = http.request(req)
return error_text unless response.kind_of? Net::HTTPSuccess
json = JSON.parse(response.body)
@ -185,6 +185,3 @@ module DiscourseChat::Provider::SlackProvider
end
end

View File

@ -4,7 +4,7 @@ class ChatIntegrationSlackEnabledSettingValidator
end
def valid_value?(val)
return true if val == 'f' or val == false
return true if val == ('f') || val == (false)
return false if SiteSetting.chat_integration_slack_outbound_webhook_url.blank? && SiteSetting.chat_integration_slack_access_token.blank?
true
end

View File

@ -105,7 +105,7 @@ module DiscourseChat::Provider::SlackProvider
json = JSON.parse(response.body)
unless json["ok"] == true
if json.key?("error") and (json["error"] == 'channel_not_found' or json["error"] == 'is_archived')
if json.key?("error") && (json["error"] == ('channel_not_found') || json["error"] == ('is_archived'))
error_key = 'chat_integration.provider.slack.errors.channel_not_found'
else
error_key = json.to_s
@ -127,7 +127,7 @@ module DiscourseChat::Provider::SlackProvider
unless response.kind_of? Net::HTTPSuccess
if response.code.to_s == '403'
error_key = 'chat_integration.provider.slack.errors.action_prohibited'
elsif response.body == 'channel_not_found' or response.body == 'channel_is_archived'
elsif response.body == ('channel_not_found') || response.body == ('channel_is_archived')
error_key = 'chat_integration.provider.slack.errors.channel_not_found'
else
error_key = nil
@ -135,7 +135,6 @@ module DiscourseChat::Provider::SlackProvider
raise ::DiscourseChat::ProviderError.new info: { error_key: error_key, request: req.body, response_code: response.code, response_body: response.body }
end
end
def self.trigger_notification(post, channel)

View File

@ -27,7 +27,7 @@ module DiscourseChat::Provider::TelegramProvider
DiscourseChat::Provider::TelegramProvider.sendMessage(message)
elsif params.key?('channel_post') and params['channel_post']['text'].include? '/getchatid'
elsif params.key?('channel_post') && params['channel_post']['text'].include?('/getchatid')
chat_id = params['channel_post']['chat']['id']
message_text = I18n.t(

View File

@ -2,7 +2,7 @@ DiscourseEvent.on(:site_setting_saved) do |sitesetting|
isEnabledSetting = sitesetting.name == 'chat_integration_telegram_enabled'
isAccessToken = sitesetting.name == 'chat_integration_telegram_access_token'
if (isEnabledSetting or isAccessToken)
if (isEnabledSetting || isAccessToken)
enabled = isEnabledSetting ? sitesetting.value == 't' : SiteSetting.chat_integration_telegram_enabled
if enabled
Scheduler::Defer.later("Setup Telegram Webhook") do

View File

@ -174,7 +174,7 @@ describe 'Chat Controller', type: :request do
expect(response).to be_success
channel = DiscourseChat::Channel.all.first
expect(channel.data).to eq({"val" => "something-else"})
expect(channel.data).to eq("val" => "something-else")
end
it 'should fail for invalid params' do

View File

@ -225,7 +225,6 @@ RSpec.describe DiscourseChat::Manager do
expect(DiscourseChat::Helper.delete_by_index(chan1, 1)).to eq(:deleted)
end
end
describe '.smart_create_rule' do

View File

@ -18,7 +18,7 @@ RSpec.describe DiscourseChat::Provider::DiscordProvider do
it 'includes the protocol in the avatar URL' do
stub1 = stub_request(:post, 'https://discordapp.com/api/webhooks/1234/abcd?wait=true')
.with(body: hash_including({embeds:[hash_including({author:hash_including({url:/^https?:\/\//})})]}))
.with(body: hash_including(embeds: [hash_including(author: hash_including(url: /^https?:\/\//))]))
.to_return(status: 200)
described_class.trigger_notification(post, chan1)
expect(stub1).to have_been_requested.once

View File

@ -5,13 +5,10 @@ RSpec.describe DiscourseChat::Channel do
include_context "dummy provider"
include_context "validated dummy provider"
it 'should save and load successfully' do
expect(DiscourseChat::Channel.all.length).to eq(0)
chan = DiscourseChat::Channel.create({
provider:"dummy",
})
chan = DiscourseChat::Channel.create(provider: "dummy")
expect(DiscourseChat::Channel.all.length).to eq(1)
@ -51,7 +48,7 @@ RSpec.describe DiscourseChat::Channel do
end
it 'can find its own rules' do
channel = DiscourseChat::Channel.create({provider:'dummy'})
channel = DiscourseChat::Channel.create(provider: 'dummy')
expect(channel.rules.size).to eq(0)
DiscourseChat::Rule.create(channel: channel)
DiscourseChat::Rule.create(channel: channel)
@ -59,7 +56,7 @@ RSpec.describe DiscourseChat::Channel do
end
it 'destroys its rules on destroy' do
channel = DiscourseChat::Channel.create({provider:'dummy'})
channel = DiscourseChat::Channel.create(provider: 'dummy')
expect(channel.rules.size).to eq(0)
rule1 = DiscourseChat::Rule.create(channel: channel)
rule2 = DiscourseChat::Rule.create(channel: channel)
@ -106,6 +103,5 @@ RSpec.describe DiscourseChat::Channel do
expect(channel2.valid?).to eq(true)
end
end
end

View File

@ -28,12 +28,10 @@ RSpec.describe DiscourseChat::Rule do
it 'should save and load successfully' do
expect(DiscourseChat::Rule.all.length).to eq(0)
rule = DiscourseChat::Rule.create({
channel: channel,
rule = DiscourseChat::Rule.create(channel: channel,
category_id: category.id,
tags: [tag1.name, tag2.name],
filter: 'watch'
})
filter: 'watch')
expect(DiscourseChat::Rule.all.length).to eq(1)
@ -48,11 +46,9 @@ RSpec.describe DiscourseChat::Rule do
describe 'general operations' do
before do
rule = DiscourseChat::Rule.create({
channel: channel,
rule = DiscourseChat::Rule.create(channel: channel,
category_id: category.id,
tags: [tag1.name, tag2.name]
})
tags: [tag1.name, tag2.name])
end
it 'can be modified' do
@ -76,10 +72,10 @@ RSpec.describe DiscourseChat::Rule do
end
it 'can delete all' do
DiscourseChat::Rule.create({channel:channel})
DiscourseChat::Rule.create({channel:channel})
DiscourseChat::Rule.create({channel:channel})
DiscourseChat::Rule.create({channel:channel})
DiscourseChat::Rule.create(channel: channel)
DiscourseChat::Rule.create(channel: channel)
DiscourseChat::Rule.create(channel: channel)
DiscourseChat::Rule.create(channel: channel)
expect(DiscourseChat::Rule.all.length).to eq(5)
@ -154,11 +150,9 @@ RSpec.describe DiscourseChat::Rule do
describe 'validations' do
let(:rule) do
DiscourseChat::Rule.create({
filter: 'watch',
DiscourseChat::Rule.create(filter: 'watch',
channel: channel,
category_id: category.id,
})
category_id: category.id)
end
it 'validates channel correctly' do