about summary refs log tree commit diff
path: root/app/services/activitypub/fetch_replies_service.rb
blob: e113e4937b696f6afc3d06e5576c89f3e3069d88 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frozen_string_literal: true

class ActivityPub::FetchRepliesService < BaseService
  def call(parent_status, collection, **options)
    @account = parent_status.account
    return if @account.suspended?

    fetch_collection_items(collection, **options)
    return if (collection.is_a?(String) && collection == @account.outbox_url) || @account.local? || @account.silenced? || @account.passive_relationships.exists? || !@account.active_relationships.exists?

    fetch_collection_items(@account.outbox_url, **options)
  rescue ActiveRecord::RecordNotFound
    nil
  end

  private

  def fetch_collection_items(collection, **options)
    ActivityPub::FetchCollectionItemsService.new.call(
      collection,
      @account,
      page_limit: 1,
      item_limit: 20,
      **options
    )
  rescue Mastodon::RaceConditionError, Mastodon::UnexpectedResponseError
    collection_uri = collection.is_a?(Hash) ? collection['id'] : collection
    return unless collection_uri.present? && collection_uri.is_a?(String)

    ActivityPub::FetchRepliesWorker.perform_async(@account.id, collection_uri)
  end
end