decidim-cdtb 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d908d1cc4021da9869dd10cacb1fe06b360301d4e13f1a70a55fdc90a2712b6a
4
- data.tar.gz: e7a0c808803bbbd6dc75e5bdfaee7b3acfb1311c085f17eb41a9de32fc7795fa
3
+ metadata.gz: 515d740942f3089a3cb906fab890191dc4b19f97c98ccff17fac5aac2aa83d9e
4
+ data.tar.gz: 557fa8a269b25d0ac8b15c522d16072600c0b5a0d39fee2250cf9c80299486ab
5
5
  SHA512:
6
- metadata.gz: 20ddde6eddf59ab12b7ca8d6f489692a04183331f0c1ef496b199b4350cdd103d2b23ca94329199d36f2a9e7222ea0c3269da25648ec0acb47bdde6b38666b92
7
- data.tar.gz: 2dd41a8a1f79ffd4506c6b18d58393d3a88d8a021e0acf05cfbd4e4ac1dcc22250294c8de29d7168c2fce29bf2d2df71c0b9905414a3df1e28991aa665be8d36
6
+ metadata.gz: 519c7234a2cff3adba2732e07e4fb9c2aa111fb0803ac8cac8180a9683c501676bb2f0aa96059f1a5cedd063db98471f70e1321d33be0e8db4a9947341df3b6a
7
+ data.tar.gz: be0c056d4b91a543a528628b4f820fd06a7913899c12f3fa4e5db646ddbd1599970fcba41d67d583472b53abe2eef447e8b0d4aede8d83222617e3f3d80f9df8
data/.rubocop.yml CHANGED
@@ -11,7 +11,7 @@ AllCops:
11
11
 
12
12
  Metrics/BlockLength:
13
13
  Enabled: true
14
- Max: 30
14
+ Max: 50
15
15
  Exclude:
16
16
  - lib/tasks/*.rake
17
17
 
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.1.4] - 2024-01-30 (Peus grans com gegants)
4
+
5
+ - Add users spam detector task
6
+
3
7
  ## [0.1.3] - 2023-06-23 (Tan iguals com especials)
4
8
 
5
9
  - Validate migrations task
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- decidim-cdtb (0.1.3)
4
+ decidim-cdtb (0.1.4)
5
5
  decidim (>= 0.26.2)
6
6
  rails (>= 6)
7
7
  ruby-progressbar
@@ -775,4 +775,4 @@ DEPENDENCIES
775
775
  sqlite3
776
776
 
777
777
  BUNDLED WITH
778
- 2.3.6
778
+ 2.4.22
data/README.md CHANGED
@@ -76,6 +76,28 @@ To migrate from S3 to local storage, the identified steps will be:
76
76
  `bin/rake cache:clear`
77
77
  5. Restart the Rails server
78
78
 
79
+ ### Detect spam
80
+
81
+ To detect spam in Decidim.
82
+
83
+ #### Detect spam users
84
+ Detects users susceptible of being spammers. It can run on all organizations or be scoped to a single organization by passing the organization ID as the rake task parameter.
85
+
86
+ This rake task export a .csv with a list of all the searched users. A column indicates if each user is suspicious of being a spammer or not.
87
+ The columns in the CSV are: "ID, "Is suspicious?", "Name", "Email", "Nickname", "Personal URL", "About"
88
+
89
+ Examples:
90
+ `bin/rake cdtb:spam:users[org_id]` --> find users in organization with an id.
91
+ `bin/rake cdtb:spam:users` --> find all users in all organizations.
92
+
93
+ To set custom words in the rake, you can override it with an initalizer:
94
+
95
+ ```
96
+ Decidim::Cdtb.configure do |config|
97
+ config.spam_words = ENV["CDTB_SPAM_WORDS"]&.split(",")
98
+ end
99
+ ```
100
+
79
101
  ### Upgrades:
80
102
 
81
103
  #### Upgrade modules
@@ -10,6 +10,10 @@ module Decidim
10
10
  # Workaround for https://stackoverflow.com/questions/72970170/upgrading-to-rails-6-1-6-1-causes-psychdisallowedclass-tried-to-load-unspecif
11
11
  Rails.application.config.active_record.use_yaml_unsafe_load = true
12
12
  end
13
+
14
+ config.after_initialize do
15
+ Decidim::Cdtb.config.spam_regexp = Regexp.union(Decidim::Cdtb.config.spam_words)
16
+ end
13
17
  end
14
18
  end
15
19
  end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "csv"
4
+
5
+ module Decidim
6
+ module Cdtb
7
+ module Spam
8
+ # Detect spam behavior in users
9
+ #
10
+ class UserSpamDetector < ::Decidim::Cdtb::Task
11
+ # rubocop:disable Style/RedundantRegexpEscape
12
+ URL_REGEX = %r{(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|
13
+ www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|
14
+ (?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})}.freeze
15
+ # rubocop:enable Style/RedundantRegexpEscape
16
+
17
+ def initialize(organization = nil)
18
+ @organization = organization
19
+ progress_bar = { title: "Decidim::User" }
20
+ super("SPAM DETECTOR", progress_bar: progress_bar)
21
+ end
22
+
23
+ def prepare_execution(_ctx)
24
+ @users = if @organization.present?
25
+ Decidim::User.where(organization: @organization)
26
+ else
27
+ Decidim::User.all
28
+ end
29
+
30
+ @num_users = @users.count
31
+ log_task_info("Checking #{@num_users} users...")
32
+ end
33
+
34
+ def total_items
35
+ @num_users
36
+ end
37
+
38
+ def do_execution(context)
39
+ progress_bar = context[:progress_bar]
40
+
41
+ CSV.open("spam_users.csv", "w") do |csv|
42
+ csv_headers = ["ID", "Is suspicious?", "Name", "Email", "Nickname", "Personal URL", "About"]
43
+ csv << csv_headers
44
+
45
+ @users.find_each do |user|
46
+ suspicious = "NO"
47
+
48
+ if spam_user?(user)
49
+ suspicious = "YES"
50
+ @num_applied+= 1
51
+ end
52
+
53
+ csv << [user.id, suspicious, user.name, user.email, user.nickname, user.personal_url, user.about]
54
+
55
+ progress_bar.increment
56
+ end
57
+ end
58
+ end
59
+
60
+ def end_execution(_ctx)
61
+ if @num_applied.positive?
62
+ log_task_step("#{@num_applied} suspicious users")
63
+ log_task_step("Suspicious users list exported to spam_users.csv")
64
+ else
65
+ log_task_step("There are not suspicious users!!")
66
+ end
67
+ end
68
+
69
+ def spam_user?(user)
70
+ has_spam_word?(user) || has_spam_url?(user)
71
+ end
72
+
73
+ private
74
+
75
+ def has_spam_word?(user)
76
+ [user.name, user.about, user.nickname,
77
+ user.personal_url, user.about].compact.join("||").match?(Decidim::Cdtb.config.spam_regexp)
78
+ end
79
+
80
+ def has_spam_url?(user)
81
+ !!(user&.about =~ URL_REGEX || user.name =~ URL_REGEX)
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
@@ -4,6 +4,7 @@ require "decidim/cdtb/tasks_utils"
4
4
  require "decidim/cdtb/task"
5
5
  require "decidim/cdtb/fixes/nickname_fixer"
6
6
  require "decidim/cdtb/multitenants/org_by_host_like"
7
+ require "decidim/cdtb/spam/user_spam_detector"
7
8
  require "decidim/cdtb/storage/local_sharding"
8
9
  require "decidim/cdtb/storage/set_local_on_blobs"
9
10
  require "decidim/cdtb/upgrades/validate_migrations_task"
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Decidim
4
4
  module Cdtb
5
- VERSION = "0.1.3"
5
+ VERSION = "0.1.4"
6
6
  DECIDIM_MIN_VERSION = ">= 0.26.2"
7
7
  end
8
8
  end
data/lib/decidim/cdtb.rb CHANGED
@@ -5,7 +5,14 @@ require_relative "cdtb/engine"
5
5
  require_relative "cdtb/tasks"
6
6
 
7
7
  module Decidim
8
+ # Cdtb configuration
8
9
  module Cdtb
10
+ include ActiveSupport::Configurable
11
+
9
12
  class Error < StandardError; end
13
+
14
+ config_accessor :spam_words do
15
+ %w[viagra sex game free crypto crack xxx luck girls vip download]
16
+ end
10
17
  end
11
18
  end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ namespace :cdtb do
4
+ namespace :spam do
5
+ desc "Show a list with users suspected of spam"
6
+ task :users, %i[org_id] => :environment do |_task, args|
7
+ organization = args.org_id.present? ? Decidim::Organization.find(args.org_id) : nil
8
+
9
+ detector = ::Decidim::Cdtb::Spam::UserSpamDetector.new(organization)
10
+ detector.execute!
11
+ end
12
+ end
13
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: decidim-cdtb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oliver Valls
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-07-10 00:00:00.000000000 Z
11
+ date: 2024-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: decidim
@@ -105,6 +105,7 @@ files:
105
105
  - lib/decidim/cdtb/engine.rb
106
106
  - lib/decidim/cdtb/fixes/nickname_fixer.rb
107
107
  - lib/decidim/cdtb/multitenants/org_by_host_like.rb
108
+ - lib/decidim/cdtb/spam/user_spam_detector.rb
108
109
  - lib/decidim/cdtb/storage/local_sharding.rb
109
110
  - lib/decidim/cdtb/storage/set_local_on_blobs.rb
110
111
  - lib/decidim/cdtb/task.rb
@@ -120,6 +121,7 @@ files:
120
121
  - lib/tasks/anonymize.rake
121
122
  - lib/tasks/cdtb.rake
122
123
  - lib/tasks/multitenants.rake
124
+ - lib/tasks/spam.rake
123
125
  - lib/tasks/storage.rake
124
126
  - lib/tasks/upgrade.rake
125
127
  - sig/decidim/cdtb.rbs