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.

68 lines
1.8 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. RedisLock.acquire(lock_options) do |lock|
  6. if lock.acquired?
  7. original_status = status_from_object
  8. return reject_payload! if original_status.nil? || !announceable?(original_status)
  9. @status = Status.find_by(account: @account, reblog: original_status)
  10. return @status unless @status.nil?
  11. @status = Status.create!(
  12. account: @account,
  13. reblog: original_status,
  14. uri: @json['id'],
  15. created_at: @json['published'],
  16. override_timestamps: @options[:override_timestamps],
  17. visibility: visibility_from_audience
  18. )
  19. distribute(@status)
  20. else
  21. raise Mastodon::RaceConditionError
  22. end
  23. end
  24. @status
  25. end
  26. private
  27. def visibility_from_audience
  28. if equals_or_includes?(@json['to'], ActivityPub::TagManager::COLLECTIONS[:public])
  29. :public
  30. elsif equals_or_includes?(@json['cc'], ActivityPub::TagManager::COLLECTIONS[:public])
  31. :unlisted
  32. elsif equals_or_includes?(@json['to'], @account.followers_url)
  33. :private
  34. else
  35. :direct
  36. end
  37. end
  38. def announceable?(status)
  39. status.account_id == @account.id || status.distributable?
  40. end
  41. def related_to_local_activity?
  42. followed_by_local_accounts? || requested_through_relay? || reblog_of_local_status?
  43. end
  44. def requested_through_relay?
  45. super || Relay.find_by(inbox_url: @account.inbox_url)&.enabled?
  46. end
  47. def reblog_of_local_status?
  48. status_from_uri(object_uri)&.account&.local?
  49. end
  50. def lock_options
  51. { redis: Redis.current, key: "announce:#{@object['id']}" }
  52. end
  53. end