about summary refs log tree commit diff
path: root/app/services/activitypub/fetch_replies_service.rb
blob: 64a7dc9de93844c9fbdf13dca43a59b383843de1 (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
# frozen_string_literal: true

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

    fetch_collection_items(collection, **options)
    return if collection.is_a?(String) && collection == @account.outbox_url

    fetch_collection_items(@account.outbox_url, **options) unless @account.silenced?
  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