diff --git a/.github/stale.yml b/.github/stale.yml index 2d184a347..b0bc84233 100644 --- a/.github/stale.yml +++ b/.github/stale.yml @@ -14,7 +14,7 @@ exemptLabels: exemptProjects: false # Set to true to ignore issues in a milestone (defaults to false) -exemptMilestones: false +exemptMilestones: true # Label to use when marking as stale staleLabel: stale diff --git a/app/controllers/internal/users_controller.rb b/app/controllers/internal/users_controller.rb index 4e3e45c8a..7c5779479 100644 --- a/app/controllers/internal/users_controller.rb +++ b/app/controllers/internal/users_controller.rb @@ -96,62 +96,14 @@ class Internal::UsersController < Internal::ApplicationController def banish @user = User.find(params[:id]) - Moderator::Banisher.call(admin: current_user, offender: @user) + begin + Moderator::Banisher.call(admin: current_user, offender: @user) + rescue StandardError => e + flash[:error] = e.message + end redirect_to "/internal/users/#{@user.id}/edit" end - def strip_user(user) - return unless user.comments.where("created_at < ?", 150.days.ago).empty? - new_name = "spam_#{rand(10000)}" - new_username = "spam_#{rand(10000)}" - if User.find_by(name: new_name) || User.find_by(username: new_username) - new_name = "spam_#{rand(10000)}" - new_username = "spam_#{rand(10000)}" - end - user.name = new_name - user.username = new_username - user.twitter_username = "" - user.github_username = "" - user.website_url = "" - user.summary = "" - user.location = "" - user.remote_profile_image_url = "https://thepracticaldev.s3.amazonaws.com/i/99mvlsfu5tfj9m7ku25d.png" if Rails.env.production? - user.education = "" - user.employer_name = "" - user.employer_url = "" - user.employment_title = "" - user.mostly_work_with = "" - user.currently_learning = "" - user.currently_hacking_on = "" - user.available_for = "" - user.email_public = false - user.facebook_url = nil - user.dribbble_url = nil - user.medium_url = nil - user.stackoverflow_url = nil - user.behance_url = nil - user.linkedin_url = nil - user.gitlab_url = nil - user.mastodon_url = nil - user.add_role :banned - unless user.notes.where(reason: "banned").any? - user.notes. - create!(reason: "banned", content: "spam account", author_id: current_user.id) - end - user.comments.each do |comment| - comment.reactions.each { |rxn| rxn.delay.destroy! } - comment.delay.destroy! - end - user.follows.each { |follow| follow.delay.destroy! } - user.articles.each { |article| article.delay.destroy! } - user.remove_from_index! - user.save! - CacheBuster.new.bust("/#{user.old_username}") - user.update!(old_username: nil) - rescue StandardError => e - flash[:error] = e.message - end - private def user_params diff --git a/app/services/moderator/banisher.rb b/app/services/moderator/banisher.rb index 0fb4d259a..b06d77552 100644 --- a/app/services/moderator/banisher.rb +++ b/app/services/moderator/banisher.rb @@ -12,53 +12,7 @@ module Moderator end def banish - # return unless user.comments.where("created_at < ?", 7.days.ago).empty? - ban_offender - strip_user_profile - destroy_dependents - user.remove_from_index! - end - - def destroy_dependents - destroy_reactions - destroy_comments - destroy_articles - destroy_follows - end - - def ban_offender - user.add_role :banned - unless user.notes.where(reason: "banned").any? - user.notes. - create!(reason: "banned", content: "spam account", author_id: admin.id) - end - end - - def destroy_follows - user.follows.destroy_all - end - handle_asynchronously :destroy_follows - - def destroy_reactions - user.reactions.destroy_all - end - - def destroy_comments - user.comments.destroy_all - end - handle_asynchronously :destroy_comments - - def destroy_articles - user.articles.destroy_all - end - handle_asynchronously :destroy_articles - - def bust_user_cache(old_username) - CacheBuster.new.bust("/#{old_username}") - end - - def strip_user_profile - bust_user_cache(user.username) + return unless user.comments.where("created_at < ?", 150.days.ago).empty? new_name = "spam_#{rand(10000)}" new_username = "spam_#{rand(10000)}" if User.find_by(name: new_name) || User.find_by(username: new_username) @@ -72,6 +26,7 @@ module Moderator user.website_url = "" user.summary = "" user.location = "" + user.remote_profile_image_url = "https://thepracticaldev.s3.amazonaws.com/i/99mvlsfu5tfj9m7ku25d.png" if Rails.env.production? user.education = "" user.employer_name = "" user.employer_url = "" @@ -87,8 +42,23 @@ module Moderator user.stackoverflow_url = nil user.behance_url = nil user.linkedin_url = nil - user.save - user.update_columns(old_username: nil) + user.gitlab_url = nil + user.mastodon_url = nil + user.add_role :banned + unless user.notes.where(reason: "banned").any? + user.notes. + create!(reason: "banned", content: "spam account", author: admin) + end + user.comments.each do |comment| + comment.reactions.each { |rxn| rxn.delay.destroy! } + comment.delay.destroy! + end + user.follows.each { |follow| follow.delay.destroy! } + user.articles.each { |article| article.delay.destroy! } + user.remove_from_index! + user.save! + CacheBuster.new.bust("/#{user.old_username}") + user.update!(old_username: nil) end end end diff --git a/spec/controllers/internal_users_controller_spec.rb b/spec/controllers/internal_users_controller_spec.rb index 74a2e9a8d..2cff05638 100644 --- a/spec/controllers/internal_users_controller_spec.rb +++ b/spec/controllers/internal_users_controller_spec.rb @@ -19,7 +19,7 @@ RSpec.describe "internal/users", type: :request do def banish_user post "/internal/users/#{user.id}/banish" - Delayed::Worker.new(quiet: false).work_off + Delayed::Worker.new(quiet: true).work_off user.reload end @@ -39,7 +39,18 @@ RSpec.describe "internal/users", type: :request do Delayed::Worker.new(quiet: true).work_off end - context "when offender is a spam user" do + it "works" do + create(:reaction, reactable: article, reactable_type: "Article", user: user) + create(:comment, commentable_type: "Article", commentable: article, user: user) + Delayed::Worker.new(quiet: true).work_off + banish_user + expect(user.username).to include("spam_") + expect(Article.count).to eq(0) + expect(Comment.count).to eq(0) + expect(Reaction.count).to eq(0) + end + + xcontext "when offender is a spam user" do before do create(:reaction, reactable: article, reactable_type: "Article", user: user) create(:comment, commentable_type: "Article", commentable: article, user: user)