<commit_before># frozen_string_literal: true

class SuspendAccountService < BaseService
  include Payloadable

  def call(account)
    @account = account

    suspend!
    reject_remote_follows!
    distribute_update_actor!
    unmerge_from_home_timelines!
    unmerge_from_list_timelines!
    privatize_media_attachments!
  end

  private

  def suspend!
    @account.suspend! unless @account.suspended?
  end

  def reject_remote_follows!
    return if @account.local? || !@account.activitypub?

    # When suspending a remote account, the account obviously doesn't
    # actually become suspended on its origin server, i.e. unlike a
    # locally suspended account it continues to have access to its home
    # feed and other content. To prevent it from being able to continue
    # to access toots it would receive because it follows local accounts,
    # we have to force it to unfollow them. Unfortunately, there is no
    # counterpart to this operation, i.e. you can't then force a remote
    # account to re-follow you, so this part is not reversible.

    follows = Follow.where(account: @account).to_a

    ActivityPub::DeliveryWorker.push_bulk(follows) do |follow|
      [Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer)), follow.target_account_id, @account.inbox_url]
    end

    follows.in_batches.destroy_all
  end

  def distribute_update_actor!
    ActivityPub::UpdateDistributionWorker.perform_async(@account.id) if @account.local?
  end

  def unmerge_from_home_timelines!
    @account.followers_for_local_distribution.find_each do |follower|
      FeedManager.instance.unmerge_from_home(@account, follower)
    end
  end

  def unmerge_from_list_timelines!
    @account.lists_for_local_distribution.find_each do |list|
      FeedManager.instance.unmerge_from_list(@account, list)<commit_msg>Fix crash in SuspendAccountWorker (#15106)<commit_after># frozen_string_literal: true

class SuspendAccountService < BaseService
  include Payloadable

  def call(account)
    @account = account

    suspend!
    reject_remote_follows!
    distribute_update_actor!
    unmerge_from_home_timelines!
    unmerge_from_list_timelines!
    privatize_media_attachments!
  end

  private

  def suspend!
    @account.suspend! unless @account.suspended?
  end

  def reject_remote_follows!
    return if @account.local? || !@account.activitypub?

    # When suspending a remote account, the account obviously doesn't
    # actually become suspended on its origin server, i.e. unlike a
    # locally suspended account it continues to have access to its home
    # feed and other content. To prevent it from being able to continue
    # to access toots it would receive because it follows local accounts,
    # we have to force it to unfollow them. Unfortunately, there is no
    # counterpart to this operation, i.e. you can't then force a remote
    # account to re-follow you, so this part is not reversible.

    follows = Follow.where(account: @account).to_a

    ActivityPub::DeliveryWorker.push_bulk(follows) do |follow|
      [Oj.dump(serialize_payload(follow, ActivityPub::RejectFollowSerializer)), follow.target_account_id, @account.inbox_url]
    end

    follows.each(&:destroy)
  end

  def distribute_update_actor!
    ActivityPub::UpdateDistributionWorker.perform_async(@account.id) if @account.local?
  end

  def unmerge_from_home_timelines!
    @account.followers_for_local_distribution.find_each do |follower|
      FeedManager.instance.unmerge_from_home(@account, follower)
    end
  end

  def unmerge_from_list_timelines!
    @account.lists_for_local_distribution.find_each do |list|
      FeedManager.instance.unmerge_from_list(@account, list)
{'fraction_non_alphanumeric': 0.06829268292682927, 'fraction_numerical': 0.0013550135501355014, 'mean_word_length': 4.213276836158192, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7135772', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1067, 'n_words': 309}