Skip to content

Commit 4ece130

Browse files
authored
Merge pull request #1758 from codidact/art/1729/spam-accounts
Spam account automatic de-privileging
2 parents 35b5554 + a10434a commit 4ece130

8 files changed

Lines changed: 86 additions & 1 deletion

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class PotentialSpamProfilesJob < ApplicationJob
2+
queue_as :default
3+
4+
def perform
5+
sql = File.read(Rails.root.join('db/scripts/potential_spam_profiles.sql'))
6+
user_ids = ActiveRecord::Base.connection.execute(sql).to_a.flatten
7+
users = User.where(id: user_ids)
8+
9+
ability_ids = Ability.unscoped.where(internal_id: 'unrestricted').map(&:id)
10+
11+
users.each do |user|
12+
cu_ids = user.community_users.map(&:id)
13+
UserAbility.where(community_user_id: cu_ids, ability_id: ability_ids)
14+
.update_all(is_suspended: true, suspension_message: 'This ability has been automatically suspended.')
15+
end
16+
end
17+
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
select u.id
2+
from users u
3+
inner join community_users cu on u.id = cu.user_id
4+
inner join communities c on cu.community_id = c.id
5+
left join posts p on p.user_id = u.id
6+
left join comments cm on cm.user_id = u.id
7+
left join votes v on v.user_id = u.id
8+
left join flags f on f.user_id = u.id
9+
where u.profile_markdown is not null
10+
and u.profile like '%href="%'
11+
and u.created_at >= date_sub(current_timestamp, interval 25 hour)
12+
and u.deleted = false
13+
and u.email not like '%localhost'
14+
group by u.id
15+
having sum(cu.reputation) = 1
16+
and count(distinct p.id) = 0
17+
and count(distinct cm.id) = 0
18+
and count(distinct v.id) = 0
19+
and count(distinct f.id) = 0
20+
order by u.id

scripts/run_spam_cleanup.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
CleanUpSpammyUsersJob.perform_later
2+
PotentialSpamProfilesJob.perform_later

test/fixtures/community_users.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,10 @@ sample_enabled_2fa:
9898
is_admin: false
9999
is_moderator: false
100100
reputation: 1
101+
102+
sample_spammer:
103+
user: spammer
104+
community: sample
105+
is_admin: false
106+
is_moderator: false
107+
reputation: 1

test/fixtures/user_abilities.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,11 @@ e2_ep_susp:
7272
is_suspended: true
7373
suspension_end: ~
7474
suspension_message: go away
75+
76+
sp_eo:
77+
community_user: sample_spammer
78+
ability: everyone
79+
80+
sp_ur:
81+
community_user: sample_spammer
82+
ability: unrestricted

test/fixtures/users.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,18 @@ enabled_2fa:
148148
enabled_2fa: true
149149
two_factor_token: WT65ANYXBB2SBR7III7IVWNJDS4PQF2T
150150
backup_2fa_code: M8lENyehyCvo9F9MbyTl1aOL
151+
152+
spammer:
153+
email: spammer@example.com
154+
encrypted_password: '$2a$11$roUHXKxecjyQ72Qn7DWs3.9eRCCoRn176kX/UNb/xiue3aGqf7xEW'
155+
profile: >
156+
Find the best spam on the network <a href="https://example.com">here</a>!
157+
The yummiest spam you've ever tasted.
158+
profile_markdown: >
159+
Find the best spam on the network [here](https://example.com)!
160+
The yummiest spam you've ever tasted.
161+
sign_in_count: 1
162+
username: BestSpamOnTheNet
163+
is_global_admin: false
164+
is_global_moderator: false
165+
confirmed_at: 2020-01-01T00:00:00.000000Z
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require 'test_helper'
2+
3+
class PotentialSpamProfilesJobTest < ActiveJob::TestCase
4+
test 'should run job successfully' do
5+
unrestricted_id = abilities(:unrestricted).internal_id
6+
7+
assert users(:spammer).community_user.ability?(unrestricted_id)
8+
9+
perform_enqueued_jobs do
10+
PotentialSpamProfilesJob.perform_later
11+
end
12+
13+
assert_performed_jobs 1
14+
assert_not users(:spammer).community_user.ability?(unrestricted_id)
15+
end
16+
end

test/models/user_test.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ class UserTest < ActiveSupport::TestCase
205205
communities.each do |community|
206206
CommunityUser.unscoped.undeleted.where(community_id: community.id).each do |cu|
207207
unless cu.user.deleted
208-
assert_equal cu.user.ability_on?(community.id, everyone.internal_id), true
208+
assert cu.user.ability_on?(community.id, everyone.internal_id),
209+
"Expected user '#{cu.user.username}' to have the 'everyone' ability"
209210
end
210211
end
211212
end

0 commit comments

Comments
 (0)