Fix “Remove all followers from the selected domains” being more destructive than it claims (#23805)
This commit is contained in:
		@@ -60,8 +60,8 @@ class RelationshipsController < ApplicationController
 | 
			
		||||
      'unfollow'
 | 
			
		||||
    elsif params[:remove_from_followers]
 | 
			
		||||
      'remove_from_followers'
 | 
			
		||||
    elsif params[:block_domains]
 | 
			
		||||
      'block_domains'
 | 
			
		||||
    elsif params[:block_domains] || params[:remove_domains_from_followers]
 | 
			
		||||
      'remove_domains_from_followers'
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -17,8 +17,8 @@ class Form::AccountBatch
 | 
			
		||||
      unfollow!
 | 
			
		||||
    when 'remove_from_followers'
 | 
			
		||||
      remove_from_followers!
 | 
			
		||||
    when 'block_domains'
 | 
			
		||||
      block_domains!
 | 
			
		||||
    when 'remove_domains_from_followers'
 | 
			
		||||
      remove_domains_from_followers!
 | 
			
		||||
    when 'approve'
 | 
			
		||||
      approve!
 | 
			
		||||
    when 'reject'
 | 
			
		||||
@@ -50,10 +50,8 @@ class Form::AccountBatch
 | 
			
		||||
    RemoveFromFollowersService.new.call(current_account, account_ids)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def block_domains!
 | 
			
		||||
    AfterAccountDomainBlockWorker.push_bulk(account_domains) do |domain|
 | 
			
		||||
      [current_account.id, domain]
 | 
			
		||||
    end
 | 
			
		||||
  def remove_domains_from_followers!
 | 
			
		||||
    RemoveDomainsFromFollowersService.new.call(current_account, account_domains)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def account_domains
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										23
									
								
								app/services/remove_domains_from_followers_service.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								app/services/remove_domains_from_followers_service.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,23 @@
 | 
			
		||||
# frozen_string_literal: true
 | 
			
		||||
 | 
			
		||||
class RemoveDomainsFromFollowersService < BaseService
 | 
			
		||||
  include Payloadable
 | 
			
		||||
 | 
			
		||||
  def call(source_account, target_domains)
 | 
			
		||||
    source_account.passive_relationships.where(account_id: Account.where(domain: target_domains)).find_each do |follow|
 | 
			
		||||
      follow.destroy
 | 
			
		||||
 | 
			
		||||
      create_notification(follow) if source_account.local? && !follow.account.local? && follow.account.activitypub?
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  private
 | 
			
		||||
 | 
			
		||||
  def create_notification(follow)
 | 
			
		||||
    ActivityPub::DeliveryWorker.perform_async(build_json(follow), follow.target_account_id, follow.account.inbox_url)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def build_json(follow)
 | 
			
		||||
    Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer))
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@@ -48,7 +48,7 @@
 | 
			
		||||
 | 
			
		||||
        = f.button safe_join([fa_icon('trash'), t('relationships.remove_selected_followers')]), name: :remove_from_followers, class: 'table-action-link', type: :submit, data: { confirm: t('relationships.confirm_remove_selected_followers') } unless following_relationship?
 | 
			
		||||
 | 
			
		||||
        = f.button safe_join([fa_icon('trash'), t('relationships.remove_selected_domains')]), name: :block_domains, class: 'table-action-link', type: :submit, data: { confirm: t('admin.reports.are_you_sure') } if followed_by_relationship?
 | 
			
		||||
        = f.button safe_join([fa_icon('trash'), t('relationships.remove_selected_domains')]), name: :remove_domains_from_followers, class: 'table-action-link', type: :submit, data: { confirm: t('admin.reports.are_you_sure') } if followed_by_relationship?
 | 
			
		||||
    .batch-table__body
 | 
			
		||||
      - if @accounts.empty?
 | 
			
		||||
        = nothing_here 'nothing-here--under-tabs'
 | 
			
		||||
 
 | 
			
		||||
@@ -58,7 +58,7 @@ describe RelationshipsController do
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    context 'when select parameter is provided' do
 | 
			
		||||
      subject { patch :update, params: { form_account_batch: { account_ids: [poopfeast.id] }, block_domains: '' } }
 | 
			
		||||
      subject { patch :update, params: { form_account_batch: { account_ids: [poopfeast.id] }, remove_domains_from_followers: '' } }
 | 
			
		||||
 | 
			
		||||
      it 'soft-blocks followers from selected domains' do
 | 
			
		||||
        poopfeast.follow!(user.account)
 | 
			
		||||
@@ -69,6 +69,15 @@ describe RelationshipsController do
 | 
			
		||||
        expect(poopfeast.following?(user.account)).to be false
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'does not unfollow users from selected domains' do
 | 
			
		||||
        user.account.follow!(poopfeast)
 | 
			
		||||
 | 
			
		||||
        sign_in user, scope: :user
 | 
			
		||||
        subject
 | 
			
		||||
 | 
			
		||||
        expect(user.account.following?(poopfeast)).to be true
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      include_examples 'authenticate user'
 | 
			
		||||
      include_examples 'redirects back to followers page'
 | 
			
		||||
    end
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user