Remove notifications related to spammer actions (after the role is assigned) (#20593)

This commit is contained in:
Anna Buianova 2024-02-07 20:36:19 +03:00 committed by GitHub
parent a900050fdc
commit b43cb38d40
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 91 additions and 0 deletions

View file

@ -33,6 +33,10 @@ module Moderator
remove_tag_moderator_role
end
def remove_notifications
Notifications::RemoveBySpammerWorker.perform_async(user.id)
end
def remove_mod_roles
@user.remove_role(:trusted)
@user.remove_role(:tag_moderator)
@ -73,6 +77,7 @@ module Moderator
when "Spam"
user.add_role(:spam)
remove_privileges
remove_notifications
when "Super Moderator"
assign_elevated_role_to_user(user, :super_moderator)
TagModerators::AddTrustedRole.call(user)

View file

@ -0,0 +1,25 @@
# remove notifications created for spammer actions:
# follow user, create comments, create articles
module Notifications
class RemoveBySpammer
def self.call(...)
new(...).call
end
def initialize(user)
@user = user
end
def call
return unless user
Notification.where(notifiable_type: "Follow", notifiable_id: user.follow_ids).delete_all
Notification.where(notifiable_type: "Comment", notifiable_id: user.comment_ids).delete_all
Notification.where(notifiable_type: "Article", action: "Published", notifiable_id: user.article_ids).delete_all
end
private
attr_reader :user
end
end

View file

@ -0,0 +1,14 @@
module Notifications
class RemoveBySpammerWorker
include Sidekiq::Job
sidekiq_options queue: :low_priority, retry: 10
def perform(user_id)
user = User.find_by(id: user_id)
return unless user
Notifications::RemoveBySpammer.call(user)
end
end
end

View file

@ -250,6 +250,29 @@ RSpec.describe Moderator::ManageActivityAndRoles, type: :service do
end
end
describe "removes notifications when adding the spam role" do
let(:nice_article) { create(:article, user: user) }
let(:spam_user) { create(:user) }
let(:spam_article) { create(:article, user: spam_user) }
let(:spam_follow) { create(:follow, follower: spam_user, followable: user) }
let(:spam_comment) { create(:comment, user: spam_user, commentable: nice_article) }
before do
create(:notification, notifiable: spam_comment, user: user)
create(:notification, notifiable: spam_article, action: "Published", user: user)
create(:notification, notifiable: spam_follow, user: user)
end
it "removes notifications related to the spammer" do
expect(Notification.count).to eq(3)
expect do
sidekiq_perform_enqueued_jobs do
manage_roles_for(spam_user, user_status: "Spam")
end
end.to change(Notification, :count).by(-3)
end
end
context "when not super admin" do
before do
admin.remove_role(:super_admin)

View file

@ -0,0 +1,24 @@
require "rails_helper"
RSpec.describe Notifications::RemoveBySpammerWorker, type: :woker do
include_examples "#enqueues_on_correct_queue", "low_priority", 1
describe "#perform" do
let(:worker) { subject }
let(:user) { create(:user) }
before do
allow(Notifications::RemoveBySpammer).to receive(:call)
end
it "calls the service" do
worker.perform(user.id)
expect(Notifications::RemoveBySpammer).to have_received(:call).with(user)
end
it "doesn't call the service with a non-existent user" do
worker.perform(-10)
expect(Notifications::RemoveBySpammer).not_to have_received(:call)
end
end
end