From 5e4508ebe1f81e321977b0eb56f059b1cbf8ca89 Mon Sep 17 00:00:00 2001 From: Jess Lee Date: Wed, 9 Jan 2019 15:12:38 -0500 Subject: [PATCH] Remove callbacks from banisher + specs! (#1501) * wip remove callbacks from banisher * remove pry --- app/models/user.rb | 12 +-- app/services/moderator/banisher.rb | 92 ++++++++++++------- .../internal_users_controller_spec.rb | 92 ++++++++++--------- 3 files changed, 110 insertions(+), 86 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 2687a948b..4e0e801f5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -388,6 +388,12 @@ class User < ApplicationRecord ProfileImage.new(self).get(90) end + def remove_from_algolia_index + remove_from_index! + index = Algolia::Index.new("searchables_#{Rails.env}") + index.delay.delete_object("users-#{id}") + end + private def send_welcome_notification @@ -556,12 +562,6 @@ class User < ApplicationRecord score.to_i end - def remove_from_algolia_index - remove_from_index! - index = Algolia::Index.new("searchables_#{Rails.env}") - index.delay.delete_object("users-#{id}") - end - def destroy_empty_dm_channels return if chat_channels.empty? || chat_channels.where(channel_type: "direct").empty? diff --git a/app/services/moderator/banisher.rb b/app/services/moderator/banisher.rb index e6863b980..b51ccb8c0 100644 --- a/app/services/moderator/banisher.rb +++ b/app/services/moderator/banisher.rb @@ -11,55 +11,77 @@ module Moderator @admin = admin end - def banish - return unless user.comments.where("created_at < ?", 150.days.ago).empty? - + def reassign_and_bust_username 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.update_columns(name: new_name, username: new_username) + CacheBuster.new.bust("/#{user.old_username}") + user.update_columns(old_username: nil) + end + + def remove_profile_info + user.update_columns(twitter_username: "", github_username: "", website_url: "", summary: "", location: "", education: "", employer_name: "", employer_url: "", employment_title: "", mostly_work_with: "", currently_learning: "", currently_hacking_on: "", available_for: "") + + user.update_columns(email_public: false, facebook_url: nil, dribbble_url: nil, medium_url: nil, stackoverflow_url: nil, behance_url: nil, linkedin_url: nil, gitlab_url: nil, mastodon_url: nil) + + user.update_columns(remote_profile_image_url: "https://thepracticaldev.s3.amazonaws.com/i/99mvlsfu5tfj9m7ku25d.png") if Rails.env.production? + end + + def add_banned_role user.add_role :banned unless user.notes.where(reason: "banned").any? user.notes. create!(reason: "banned", content: "spam account", author: admin) end + end + + def delete_reactions + user.reactions.each &:delete + end + + def delete_comments + return unless user.comments.count.positive? + user.comments.each do |comment| - comment.reactions.each { |rxn| rxn.delay.destroy! } - comment.delay.destroy! + comment.reactions.each &:delete + CacheBuster.new.bust_comment(comment.commentable, user.username) + comment.delete end - user.follows.each { |follow| follow.delay.destroy! } - user.articles.each { |article| article.delay.destroy! } - user.remove_from_index! + end + + def delete_follows + user.follows.each &:delete + end + + def delete_articles + user.articles.each do |article| + article.reactions.each &:delete + article.comments.each do |comment| + comment.reactions.each &:delete + CacheBuster.new.bust_comment(comment.commentable, comment.user.username) + comment.delete + end + CacheBuster.new.bust_article(article) + article.remove_algolia_index + article.delete + end + end + + def banish + # return unless user.comments.where("created_at < ?", 150.days.ago).empty? + reassign_and_bust_username + remove_profile_info + add_banned_role + delete_reactions + delete_comments + delete_articles + delete_follows + user.remove_from_algolia_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 94b10dc94..45d679a36 100644 --- a/spec/controllers/internal_users_controller_spec.rb +++ b/spec/controllers/internal_users_controller_spec.rb @@ -3,14 +3,19 @@ require "rails_helper" RSpec.describe "internal/users", type: :request do context "when banishing user" do let(:user) { create(:user) } - let(:article) { create(:article, user: user) } + let(:user2) { create(:user) } + let(:user3) { create(:user) } let(:super_admin) { create(:user, :super_admin) } - let(:article2) { create(:article, user: super_admin) } + let(:article) { create(:article, user: user) } + let(:article2) { create(:article, user: user2) } before do sign_in super_admin + user + user2 Delayed::Worker.delay_jobs = true - Delayed::Worker.destroy_failed_jobs = false + dependents_for_offending_user_article + offender_activity_on_other_content end after do @@ -23,58 +28,55 @@ RSpec.describe "internal/users", type: :request do user.reload end - def create_dependents - # Article 1 - create(:reaction, reactable: article, reactable_type: "Article", user: user) - parent_comment = create(:comment, commentable_type: "Article", commentable: article, user: user) - create(:comment, commentable_type: "Article", commentable: article, user: user, parent: parent_comment) + def dependents_for_offending_user_article + # create user2 comment on offending user article + comment = create(:comment, commentable_type: "Article", commentable: article, user: user2) + # create user3 reaction to user2 comment + create(:reaction, reactable: comment, reactable_type: "Comment", user: user3) + # create user3 comment response to user2 comment + comment2 = create(:comment, commentable_type: "Article", commentable: article, user: user3, ancestry: comment.id) + # create user2 reaction to user3 comment response + create(:reaction, reactable: comment2, reactable_type: "Comment", user: user2) + # create user3 reaction to offending article + create(:reaction, reactable: article, reactable_type: "Article", user: user3, category: "like") + Delayed::Worker.new(quiet: false).work_off + end - # Article 2 written by super_admin + def offender_activity_on_other_content + # offender reacts to user2 article create(:reaction, reactable: article2, reactable_type: "Article", user: user) - parent_comment2 = create(:comment, commentable_type: "Article", commentable: article2, user: user) - create(:comment, commentable_type: "Article", commentable: article2, user: super_admin, parent: parent_comment2) - - user.follow(super_admin) - - Delayed::Worker.new(quiet: true).work_off + # offender comments on user2 article + comment = create(:comment, commentable_type: "Article", commentable: article2, user: user) + # user3 reacts to offender comment + create(:reaction, reactable: comment, reactable_type: "Comment", user: user3) + Delayed::Worker.new(quiet: false).work_off end - 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 + it "reassigns username and removes profile info" do + user.currently_hacking_on = "currently hackin on !!!!!!!!!!!!" + user.save banish_user + expect(user.currently_hacking_on).to eq("") 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) - Delayed::Worker.new(quiet: true).work_off - end - - it "works" do - banish_user - expect(Delayed::Job.where("failed_at IS NOT NULL").count).to eq(0) - expect(user.old_username).to eq(nil) - expect(user.twitter_username).to eq("") - end + it "adds banned role" do + banish_user + expect(user.roles.last.name).to eq("banned") + expect(Note.count).to eq(1) end - xcontext "when offender has rich activity " do - it "works" do - create_dependents - banish_user - # failed = Delayed::Job.where("failed_at IS NOT NULL").first - # failed = YAML.load(failed.handler) if failed - expect(Delayed::Job.where("failed_at IS NOT NULL").count).to eq(0) - expect(user.old_username).to eq(nil) - expect(user.twitter_username).to eq("") - end + it "deletes user content" do + banish_user + expect(user.reactions.count).to eq(0) + expect(user.comments.count).to eq(0) + expect(user.articles.count).to eq(0) + end + + it "removes all follow relationships" do + user.follow(user2) + banish_user + expect(user.follows.count).to eq(0) end end end