fetch_featured_collection_service.rb 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # frozen_string_literal: true
  2. class ActivityPub::FetchFeaturedCollectionService < BaseService
  3. include JsonLdHelper
  4. def call(account)
  5. return if account.featured_collection_url.blank? || account.suspended? || account.local?
  6. @account = account
  7. @json = fetch_resource(@account.featured_collection_url, true)
  8. return unless supported_context?
  9. case @json['type']
  10. when 'Collection', 'CollectionPage'
  11. process_items @json['items']
  12. when 'OrderedCollection', 'OrderedCollectionPage'
  13. process_items @json['orderedItems']
  14. end
  15. end
  16. private
  17. def process_items(items)
  18. status_ids = items.map { |item| value_or_id(item) }
  19. .reject { |uri| ActivityPub::TagManager.instance.local_uri?(uri) }
  20. .map { |uri| ActivityPub::FetchRemoteStatusService.new.call(uri) }
  21. .compact
  22. .select { |status| status.account_id == @account.id }
  23. .map(&:id)
  24. to_remove = []
  25. to_add = status_ids
  26. StatusPin.where(account: @account).pluck(:status_id).each do |status_id|
  27. if status_ids.include?(status_id)
  28. to_add.delete(status_id)
  29. else
  30. to_remove << status_id
  31. end
  32. end
  33. StatusPin.where(account: @account, status_id: to_remove).delete_all unless to_remove.empty?
  34. to_add.each do |status_id|
  35. StatusPin.create!(account: @account, status_id: status_id)
  36. end
  37. end
  38. def supported_context?
  39. super(@json)
  40. end
  41. end