You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.5 KiB

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