Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/deactivated by default #41

Merged
merged 6 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions app/services/decidim/spam_detection/mark_users_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ def initialize
end

def self.call
return unless Decidim::SpamDetection.service_activated?

new.ask_and_mark
end

Expand Down
31 changes: 31 additions & 0 deletions lib/decidim/spam_detection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,43 @@ 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"
autoload :AbstractSpamUserCommand, "decidim/spam_detection/abstract_spam_user_command"
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
4 changes: 2 additions & 2 deletions lib/decidim/spam_detection/api_proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/decidim/spam_detection/spam_user_command_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion lib/decidim/spam_detection/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
69 changes: 69 additions & 0 deletions spec/lib/decidim/spam_detection/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -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