FEATURE: Blocking is optional when deleting an user (#25)

FEATURE: Blocking is optional when deleting an user (#25)

  • FEATURE: Blocking is optional when deleting an user

  • Rename shared examples

diff --git a/models/reviewable_akismet_post.rb b/models/reviewable_akismet_post.rb
index c6624c3..c857fe8 100644
--- a/models/reviewable_akismet_post.rb
+++ b/models/reviewable_akismet_post.rb
@@ -19,7 +19,12 @@ class ReviewableAkismetPost < Reviewable
     end
 
     if guardian.can_delete_user?(target_created_by)
-      build_action(actions, :confirm_delete, icon: 'trash-alt', bundle: agree, confirm: true)
+      # TODO: Remove after the 2.8 release
+      if respond_to?(:delete_user_actions)
+        delete_user_actions(actions)
+      else
+        build_action(actions, :confirm_delete, icon: 'trash-alt', bundle: agree, confirm: true)
+      end
     end
 
     build_action(actions, :not_spam, icon: 'thumbs-down')
@@ -63,18 +68,26 @@ class ReviewableAkismetPost < Reviewable
     successful_transition :ignored, :ignored
   end
 
-  def perform_confirm_delete(performed_by, _args)
+  def perform_delete_user(performed_by, args)
     if Guardian.new(performed_by).can_delete_user?(target_created_by)
       bouncer.submit_feedback(post, 'spam')
       log_confirmation(performed_by, 'confirmed_spam_deleted')
 
       PostDestroyer.new(performed_by, post).destroy unless post.deleted_at?
-      UserDestroyer.new(performed_by).destroy(target_created_by, user_deletion_opts(performed_by))
+
+      opts = user_deletion_opts(performed_by, args)
+      UserDestroyer.new(performed_by).destroy(target_created_by, opts)
     end
 
     successful_transition :deleted, :agreed
   end
 
+  def perform_delete_user_block(performed_by, args)
+    perform_delete_user(performed_by, args.merge(block_email: true, block_ip: true))
+  end
+  alias :perform_confirm_delete :perform_delete_user_block
+  # TODO: Remove after the 2.8 release
+
   private
 
   def bouncer
@@ -97,15 +110,15 @@ class ReviewableAkismetPost < Reviewable
     end
   end
 
-  def user_deletion_opts(performed_by)
-    base = {
+  def user_deletion_opts(performed_by, args)
+    {
       context: I18n.t('akismet.delete_reason', performed_by: performed_by.username),
-      delete_posts: true
+      delete_posts: true,
+      block_urls: true,
+      delete_as_spammer: true,
+      block_email: !!args[:block_email],
+      block_ip: !!args[:block_ip]
     }
-
-    base.tap do |b|
-      b.merge!(block_email: true, block_ip: true) if Rails.env.production?
-    end
   end
 
   def log_confirmation(performed_by, custom_type)
diff --git a/models/reviewable_akismet_user.rb b/models/reviewable_akismet_user.rb
index 852bd56..b906b7a 100644
--- a/models/reviewable_akismet_user.rb
+++ b/models/reviewable_akismet_user.rb
@@ -9,11 +9,16 @@ class ReviewableAkismetUser < Reviewable
     build_action(actions, :not_spam, icon: 'thumbs-up')
 
     if guardian.is_staff?
-      build_action(
-        actions,
-        :reject_spam_user_delete, icon: 'trash-alt',
-                                  confirm: true, button_class: "btn-danger"
-      )
+      # TODO: Remove after the 2.8 release
+      if respond_to?(:delete_user_actions)
+        delete_user_actions(actions)
+      else
+        build_action(
+          actions,
+          :reject_spam_user_delete, icon: 'trash-alt',
+                                    confirm: true, button_class: "btn-danger"
+        )
+      end
     end
   end
 
@@ -27,7 +32,7 @@ class ReviewableAkismetUser < Reviewable
     successful_transition :rejected, :disagreed
   end
 
-  def perform_reject_spam_user_delete(performed_by, _args)
+  def perform_delete_user(performed_by, args)
     if target && Guardian.new(performed_by).can_delete_user?(target)
       log_confirmation(performed_by, 'confirmed_spam_deleted')
       bouncer.submit_feedback(target, 'spam')
@@ -35,12 +40,20 @@ class ReviewableAkismetUser < Reviewable
         :confirm_akismet_flagged_posts,
         user_id: target.id, performed_by_id: performed_by.id
       )
-      UserDestroyer.new(performed_by).destroy(target, user_deletion_opts(performed_by))
+
+      opts = user_deletion_opts(performed_by, args)
+      UserDestroyer.new(performed_by).destroy(target, opts)
     end
 
     successful_transition :deleted, :agreed
   end
 
+  def perform_delete_user_block(performed_by, args)
+    perform_delete_user(performed_by, args.merge(block_ip: true, block_email: true))
+  end
+  alias :perform_reject_spam_user_delete :perform_delete_user_block
+  # TODO: Remove after the 2.8 release
+
   private
 
   def bouncer
@@ -62,17 +75,15 @@ class ReviewableAkismetUser < Reviewable
     end
   end
 
-  def user_deletion_opts(performed_by)
-    base = {
+  def user_deletion_opts(performed_by, args)
+    {
       context: I18n.t('akismet.delete_reason', performed_by: performed_by.username),
       delete_posts: true,
       delete_as_spammer: true,
-      quiet: true
+      quiet: true,
+      block_ip: !!args[:block_ip],
+      block_email: !!args[:block_email]
     }
-
-    base.tap do |b|
-      b.merge!(block_email: true, block_ip: true) if Rails.env.production?
-    end
   end
 
   def log_confirmation(performed_by, custom_type)
diff --git a/spec/models/reviewable_akismet_post_spec.rb b/spec/models/reviewable_akismet_post_spec.rb
index 0e25105..151f37b 100644
--- a/spec/models/reviewable_akismet_post_spec.rb
+++ b/spec/models/reviewable_akismet_post_spec.rb
@@ -41,13 +41,14 @@ describe 'ReviewableAkismetPost' do
       expect(actions.has?(:ignore)).to be true
     end
 
-    it 'Adds the confirm delete action' do
+    it 'Adds the delete and delete + block actions' do
       admin = Fabricate(:admin)
       guardian = Guardian.new(admin)
 
       actions = reviewable_actions(guardian)
 
-      expect(actions.has?(:confirm_delete)).to be true
+      expect(actions.has?(:delete_user)).to be true
+      expect(actions.has?(:delete_user_block)).to be true
     end
 
     it 'Excludes the confirm delete action when the user is not an staff member' do
@@ -73,7 +74,7 @@ describe 'ReviewableAkismetPost' do
       PostDestroyer.new(admin, post).destroy
     end
 
-    shared_examples 'It logs actions in the staff actions logger' do
+    shared_examples 'a staff action logger' do
       it 'Creates a UserHistory that reflects the action taken' do
         reviewable.perform admin, action
 
@@ -98,7 +99,7 @@ describe 'ReviewableAkismetPost' do
       end
     end
 
-    shared_examples 'it submits feedback to Akismet' do
+    shared_examples 'an Akismet feedback submission' do
       it 'queues a job to submit feedback' do
         expect {
           reviewable.perform admin, action
@@ -111,8 +112,8 @@ describe 'ReviewableAkismetPost' do
       let(:action_name) { 'confirmed_spam' }
       let(:flag_stat_status) { :agreed }
 
-      it_behaves_like 'It logs actions in the staff actions logger'
-      it_behaves_like 'it submits feedback to Akismet'
+      it_behaves_like 'a staff action logger'
+      it_behaves_like 'an Akismet feedback submission'
 
       it 'Confirms spam and reviewable status is changed to approved' do
         result = reviewable.perform admin, action
@@ -126,8 +127,8 @@ describe 'ReviewableAkismetPost' do
       let(:action_name) { 'confirmed_ham' }
       let(:flag_stat_status) { :disagreed }
 
-      it_behaves_like 'It logs actions in the staff actions logger'
-      it_behaves_like 'it submits feedback to Akismet'
+      it_behaves_like 'a staff action logger'
+      it_behaves_like 'an Akismet feedback submission'
 
       it 'Set post as clear and reviewable status is changed to rejected' do
         result = reviewable.perform admin, action
@@ -190,7 +191,7 @@ describe 'ReviewableAkismetPost' do
       let(:action_name) { 'ignored' }
       let(:flag_stat_status) { :ignored }
 
-      it_behaves_like 'It logs actions in the staff actions logger'
+      it_behaves_like 'a staff action logger'
 
       it 'Set post as dismissed and reviewable status is changed to ignored' do
         result = reviewable.perform admin, action
@@ -199,13 +200,34 @@ describe 'ReviewableAkismetPost' do
       end
     end
 

[... diff too long, it was truncated ...]

GitHub sha: db31769604ec8f1a2bc21250be02553490dd1ce7

This commit appears in #25 which was approved by eviltrout. It was merged by romanrizzi.