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.

57 lines
1.5 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::Activity::Announce < ActivityPub::Activity
  3. def perform
  4. return reject_payload! if delete_arrived_first?(@json['id']) || !related_to_local_activity?
  5. original_status = status_from_object
  6. return reject_payload! if original_status.nil? || !announceable?(original_status)
  7. status = Status.find_by(account: @account, reblog: original_status)
  8. return status unless status.nil?
  9. status = Status.create!(
  10. account: @account,
  11. reblog: original_status,
  12. uri: @json['id'],
  13. created_at: @json['published'],
  14. override_timestamps: @options[:override_timestamps],
  15. visibility: visibility_from_audience
  16. )
  17. distribute(status)
  18. status
  19. end
  20. private
  21. def visibility_from_audience
  22. if equals_or_includes?(@json['to'], ActivityPub::TagManager::COLLECTIONS[:public])
  23. :public
  24. elsif equals_or_includes?(@json['cc'], ActivityPub::TagManager::COLLECTIONS[:public])
  25. :unlisted
  26. elsif equals_or_includes?(@json['to'], @account.followers_url)
  27. :private
  28. else
  29. :direct
  30. end
  31. end
  32. def announceable?(status)
  33. status.account_id == @account.id || status.distributable?
  34. end
  35. def related_to_local_activity?
  36. followed_by_local_accounts? || requested_through_relay? || reblog_of_local_status?
  37. end
  38. def requested_through_relay?
  39. super || Relay.find_by(inbox_url: @account.inbox_url)&.enabled?
  40. end
  41. def reblog_of_local_status?
  42. status_from_uri(object_uri)&.account&.local?
  43. end
  44. end