logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

fetch_featured_collection_service.rb (1464B)


  1. # frozen_string_literal: true
  2. class ActivityPub::FetchFeaturedCollectionService < BaseService
  3. include JsonLdHelper
  4. def call(account)
  5. @account = account
  6. @json = fetch_resource(@account.featured_collection_url, true)
  7. return unless supported_context?
  8. return if @account.suspended? || @account.local?
  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