diff --git a/Gemfile.lock b/Gemfile.lock index a5b34d1..1b627c0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,7 +1,7 @@ PATH remote: . specs: - decidim-spam_detection (3.0.0) + decidim-spam_detection (3.1.0) decidim-core (~> 0.26.0) GEM diff --git a/README.md b/README.md index 4d59461..75662f3 100644 --- a/README.md +++ b/README.md @@ -44,6 +44,9 @@ if you are using sidekiq scheduler you can use the following configuration: ### Further configuration list of env var, default value and their usage: ``` +ACTIVATE_SPAM_DETECTION_SERVICE: + default: false + usage: Activate the spam detection service if api url is set to default one SPAM_DETECTION_API_AUTH_TOKEN default_value: dummy usage: Token auth for authentication used by external service, ask us for more details diff --git a/app/services/decidim/spam_detection/mark_users_service.rb b/app/services/decidim/spam_detection/mark_users_service.rb index 6e61a8d..f0f947c 100644 --- a/app/services/decidim/spam_detection/mark_users_service.rb +++ b/app/services/decidim/spam_detection/mark_users_service.rb @@ -31,6 +31,8 @@ def initialize end def self.call + return unless Decidim::SpamDetection.service_activated? + new.ask_and_mark end diff --git a/lib/decidim/spam_detection.rb b/lib/decidim/spam_detection.rb index b8a43db..f5b54e4 100644 --- a/lib/decidim/spam_detection.rb +++ b/lib/decidim/spam_detection.rb @@ -8,6 +8,9 @@ module Decidim # This namespace holds the logic of the `SpamDetection` component. This component # allows users to create spam_detection in a participatory space. module SpamDetection + DEFAULT_URL = "http://localhost:8080/api" + include ActiveSupport::Configurable + autoload :Command, "decidim/spam_detection/command" autoload :CommandErrors, "decidim/spam_detection/command_errors" autoload :ApiProxy, "decidim/spam_detection/api_proxy" @@ -15,5 +18,33 @@ module SpamDetection autoload :ReportSpamUserCommand, "decidim/spam_detection/report_spam_user_command" autoload :BlockSpamUserCommand, "decidim/spam_detection/block_spam_user_command" autoload :SpamUserCommandAdapter, "decidim/spam_detection/spam_user_command_adapter" + + config_accessor :spam_detection_api_url do + ENV.fetch("SPAM_DETECTION_API_URL", DEFAULT_URL) + end + + config_accessor :spam_detection_api_auth_token do + ENV.fetch("SPAM_DETECTION_API_AUTH_TOKEN", "dummy") + end + + config_accessor :spam_detection_api_perform_block_user do + ENV.fetch("PERFORM_BLOCK_USER", "0") == "1" + end + + config_accessor :spam_detection_api_force_activate_service do + ENV.fetch("ACTIVATE_SPAM_DETECTION_SERVICE", "0") == "1" + end + + config_accessor :spam_detection_api_activate_service do + lambda do + return true unless Rails.env.production? + + spam_detection_api_force_activate_service || spam_detection_api_url != DEFAULT_URL + end + end + + def self.service_activated? + spam_detection_api_activate_service.call + end end end diff --git a/lib/decidim/spam_detection/api_proxy.rb b/lib/decidim/spam_detection/api_proxy.rb index 980c4ce..52b14ca 100644 --- a/lib/decidim/spam_detection/api_proxy.rb +++ b/lib/decidim/spam_detection/api_proxy.rb @@ -6,8 +6,8 @@ module Decidim module SpamDetection class ApiProxy - URL = URI(ENV.fetch("SPAM_DETECTION_API_URL", "http://localhost:8080/api")) - AUTH_TOKEN = ENV.fetch("SPAM_DETECTION_API_AUTH_TOKEN", "dummy") + URL = URI(Decidim::SpamDetection.spam_detection_api_url) + AUTH_TOKEN = Decidim::SpamDetection.spam_detection_api_auth_token def initialize(data_array, batch_size) @data_array = data_array diff --git a/lib/decidim/spam_detection/spam_user_command_adapter.rb b/lib/decidim/spam_detection/spam_user_command_adapter.rb index c0f3ea9..281e680 100644 --- a/lib/decidim/spam_detection/spam_user_command_adapter.rb +++ b/lib/decidim/spam_detection/spam_user_command_adapter.rb @@ -7,7 +7,7 @@ class SpamUserCommandAdapter SPAM_LEVEL = { very_sure: 0.99, probable: 0.7 }.freeze def self.perform_block_user? - ENV.fetch("PERFORM_BLOCK_USER", false) + Decidim::SpamDetection.spam_detection_api_perform_block_user end def initialize(probability_hash) diff --git a/lib/decidim/spam_detection/version.rb b/lib/decidim/spam_detection/version.rb index 098805b..6cc33f3 100644 --- a/lib/decidim/spam_detection/version.rb +++ b/lib/decidim/spam_detection/version.rb @@ -5,7 +5,7 @@ module Decidim # This holds the decidim-spam_detection version. module SpamDetection def self.version - "3.0.0" + "3.1.0" end def self.decidim_version diff --git a/spec/lib/decidim/spam_detection/configuration_spec.rb b/spec/lib/decidim/spam_detection/configuration_spec.rb new file mode 100644 index 0000000..992006c --- /dev/null +++ b/spec/lib/decidim/spam_detection/configuration_spec.rb @@ -0,0 +1,69 @@ +# frozen_string_literal: true + +require "spec_helper" + +module Decidim + module SpamDetection + describe "Configuration" do + let(:subject) { Decidim::SpamDetection } + + describe "spam_detection_api_url" do + it "returns the default value" do + expect(subject.spam_detection_api_url).to eq("http://localhost:8080/api") + end + end + + describe "spam_detection_api_auth_token" do + it "returns the default value" do + expect(subject.spam_detection_api_auth_token).to eq("dummy") + end + end + + describe "spam_detection_api_perform_block_user" do + it "returns the default value" do + expect(subject.spam_detection_api_perform_block_user).to eq(false) + end + end + + describe "spam_detection_api_force_activate_service" do + it "returns the default value" do + expect(subject.spam_detection_api_force_activate_service).to eq(false) + end + end + + describe "spam_detection_api_activate_service" do + it "returns the default value" do + expect(subject.spam_detection_api_activate_service.call).to eq(true) + end + + context "when force is set to true" do + before do + allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("production")) + allow(subject).to receive(:spam_detection_api_force_activate_service).and_return(true) + end + + it "returns true" do + expect(subject.spam_detection_api_activate_service.call).to eq(true) + end + end + + context "when url is not the default one" do + before do + allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("production")) + allow(subject).to receive(:spam_detection_api_url).and_return("http://other.org:8080/api") + end + + it "returns true" do + expect(subject.spam_detection_api_activate_service.call).to eq(true) + end + end + end + + describe ".service_activated?" do + it "returns true if the spam service is activated" do + expect(subject.service_activated?).to eq(true) + end + end + end + end +end