Skip to content

Commit

Permalink
Added spam role (#20434)
Browse files Browse the repository at this point in the history
* Added spam role

* Fixed role spec
  • Loading branch information
lightalloy authored Dec 11, 2023
1 parent 8c9560e commit 1eeaa34
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/lib/constants/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Role
"Comment Suspended" => { name: "comment_suspended", resource_type: nil },
"Limited" => { name: "limited", resource_type: nil },
"Suspended" => { name: "suspended", resource_type: nil },
"Spam" => { name: "spam", resource_type: nil },
# This "role" is a weird amalgamation of multiple roles.
"Good standing" => :good_standing,
"Trusted" => { name: "trusted", resource_type: nil }
Expand Down
1 change: 1 addition & 0 deletions app/models/role.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Role < ApplicationRecord
super_admin
support_admin
suspended
spam
tag_moderator
tech_admin
trusted
Expand Down
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,7 @@ def authorizer
:super_admin?,
:support_admin?,
:suspended?,
:spam?,
:tag_moderator?,
:tech_admin?,
:trusted?,
Expand Down
4 changes: 4 additions & 0 deletions app/policies/authorizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ def suspended?
has_role?(:suspended)
end

def spam?
has_role?(:spam)
end

def tag_moderator?(tag: nil)
# Note a fan of "peeking" into the roles table, which in a way
# circumvents the rolify gem. But this was the past implementation.
Expand Down
9 changes: 8 additions & 1 deletion app/services/moderator/manage_activity_and_roles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def create_note(reason, content)
)
end

# rubocop:disable Metrics/CyclomaticComplexity
def handle_user_status(role, note)
case role
when "Admin"
Expand All @@ -69,6 +70,9 @@ def handle_user_status(role, note)
when "Suspended" || "Spammer"
user.add_role(:suspended)
remove_privileges
when "Spam"
user.add_role(:spam)
remove_privileges
when "Super Moderator"
assign_elevated_role_to_user(user, :super_moderator)
TagModerators::AddTrustedRole.call(user)
Expand All @@ -95,6 +99,7 @@ def handle_user_status(role, note)
end
create_note(role, note)
end
# rubocop:enable Metrics/CyclomaticComplexity

def assign_elevated_role_to_user(user, role)
check_super_admin
Expand Down Expand Up @@ -129,13 +134,15 @@ def regular_member

def warned
user.add_role(:warned)
user.remove_role(:suspended)
user.remove_role(:suspended) if user.suspended?
user.remove_role(:spam) if user.spam?
remove_privileges
end

def remove_negative_roles
user.remove_role(:limited) if user.limited?
user.remove_role(:suspended) if user.suspended?
user.remove_role(:spam) if user.spam?
user.remove_role(:warned) if user.warned?
user.remove_role(:comment_suspended) if user.comment_suspended?
end
Expand Down
1 change: 1 addition & 0 deletions config/locales/views/admin/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ en:
Comment Suspended: Comment Suspended
Limited: Limited
Suspended: Suspended
Spam: Spam
Good Standing: Good Standing
Good standing: Good standing
Trusted: Trusted
Expand Down
1 change: 1 addition & 0 deletions config/locales/views/admin/fr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ fr:
Comment Suspended: Comment Suspended
Limited: Limited
Suspended: Suspended
Spam: Spam
Good Standing: Good Standing
Good standing: Good standing
Trusted: Trusted
Expand Down
2 changes: 1 addition & 1 deletion spec/models/role_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
it "contains the correct values" do
expected_roles = %w[
admin codeland_admin comment_suspended limited podcast_admin
restricted_liquid_tag single_resource_admin super_admin support_admin suspended tag_moderator tech_admin
restricted_liquid_tag single_resource_admin super_admin support_admin suspended spam tag_moderator tech_admin
trusted warned creator super_moderator
]
expect(described_class::ROLES).to match_array(expected_roles)
Expand Down
27 changes: 27 additions & 0 deletions spec/requests/moderations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -278,5 +278,32 @@
end.to change(AuditLog, :count).by(1)
end
end

context "when adding the spam role to the user" do
it "creates a note on a user when note content is provided" do
note_content = "Spam acount"
expect do
patch "/admin/member_manager/users/#{article.user_id}/user_status",
params: { user: { user_status: "Spam", note_for_current_role: note_content } }
end.to change(Note, :count).by(1)
expect(Note.last.content).to eq(note_content)
end

it "creates a default note on a user when note content isn't provided" do
expected_note = "#{super_mod.username} updated #{article.user.username}"
expect do
patch "/admin/member_manager/users/#{article.user_id}/user_status",
params: { user: { user_status: "Spam" } }
end.to change(Note, :count).by(1)
expect(Note.last.content).to eq(expected_note)
end

it "creates an AuditLog for the action taken" do
expect do
patch "/admin/member_manager/users/#{article.user_id}/user_status",
params: { user: { user_status: "Spam", new_note: "Test note" } }
end.to change(AuditLog, :count).by(1)
end
end
end
end
21 changes: 19 additions & 2 deletions spec/services/moderator/manage_activity_and_roles_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def manage_roles_for(user, user_status:, note: "Test note", acting_as: admin)
end
end

context "when user is in spam role" do
before { user.add_role(:spam) }

it "adding #{status} also removes the spam role" do
expect(user.roles.pluck(:name)).to include("spam") # confirm assumptions
manage_roles_for user, user_status: status
expect(user.roles.pluck(:name)).not_to include("spam") # confirm assumptions
end
end

context "when user is in comment_suspended role" do
before { user.add_role(:comment_suspended) }

Expand Down Expand Up @@ -138,17 +148,24 @@ def manage_roles_for(user, user_status:, note: "Test note", acting_as: admin)
it_behaves_like "elevated role", "Tech Admin"

it_behaves_like "negative role", "Suspended"
it_behaves_like "negative role", "Spam"
it_behaves_like "negative role", "Limited"
it_behaves_like "negative role", "Warned"

context "when user is in suspended role" do
before { user.add_role(:suspended) }

it "adding warned removes the suspended role" do
user.add_role(:suspended)
expect(user.roles.pluck(:name)).to include("suspended") # confirm assumptions
manage_roles_for user, user_status: "Warned"
expect(user.roles.pluck(:name)).not_to include("suspended") # confirm assumptions
end

it "adding warned removes the spam role" do
user.add_role(:spam)
expect(user.roles.pluck(:name)).to include("spam")
manage_roles_for user, user_status: "Warned"
expect(user.roles.pluck(:name)).not_to include("spam")
end
end

it "updates user status to limited" do
Expand Down

0 comments on commit 1eeaa34

Please sign in to comment.