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.

144 lines
4.7 KiB

  1. # frozen_string_literal: true
  2. require 'singleton'
  3. class ActivityPub::TagManager
  4. include Singleton
  5. include RoutingHelper
  6. CONTEXT = 'https://www.w3.org/ns/activitystreams'
  7. COLLECTIONS = {
  8. public: 'https://www.w3.org/ns/activitystreams#Public',
  9. }.freeze
  10. def url_for(target)
  11. return target.url if target.respond_to?(:local?) && !target.local?
  12. case target.object_type
  13. when :person
  14. target.instance_actor? ? about_more_url(instance_actor: true) : short_account_url(target)
  15. when :note, :comment, :activity
  16. return activity_account_status_url(target.account, target) if target.reblog?
  17. short_account_status_url(target.account, target)
  18. end
  19. end
  20. def uri_for(target)
  21. return target.uri if target.respond_to?(:local?) && !target.local?
  22. case target.object_type
  23. when :person
  24. target.instance_actor? ? instance_actor_url : account_url(target)
  25. when :note, :comment, :activity
  26. return activity_account_status_url(target.account, target) if target.reblog?
  27. account_status_url(target.account, target)
  28. when :emoji
  29. emoji_url(target)
  30. end
  31. end
  32. def generate_uri_for(_target)
  33. URI.join(root_url, 'payloads', SecureRandom.uuid)
  34. end
  35. def activity_uri_for(target)
  36. raise ArgumentError, 'target must be a local activity' unless %i(note comment activity).include?(target.object_type) && target.local?
  37. activity_account_status_url(target.account, target)
  38. end
  39. def replies_uri_for(target, page_params = nil)
  40. raise ArgumentError, 'target must be a local activity' unless %i(note comment activity).include?(target.object_type) && target.local?
  41. account_status_replies_url(target.account, target, page_params)
  42. end
  43. # Primary audience of a status
  44. # Public statuses go out to primarily the public collection
  45. # Unlisted and private statuses go out primarily to the followers collection
  46. # Others go out only to the people they mention
  47. def to(status)
  48. case status.visibility
  49. when 'public'
  50. [COLLECTIONS[:public]]
  51. when 'unlisted', 'private'
  52. [account_followers_url(status.account)]
  53. when 'direct', 'limited'
  54. if status.account.silenced?
  55. # Only notify followers if the account is locally silenced
  56. account_ids = status.active_mentions.pluck(:account_id)
  57. to = status.account.followers.where(id: account_ids).map { |account| uri_for(account) }
  58. to.concat(FollowRequest.where(target_account_id: status.account_id, account_id: account_ids).map { |request| uri_for(request.account) })
  59. else
  60. status.active_mentions.map { |mention| uri_for(mention.account) }
  61. end
  62. end
  63. end
  64. # Secondary audience of a status
  65. # Public statuses go out to followers as well
  66. # Unlisted statuses go to the public as well
  67. # Both of those and private statuses also go to the people mentioned in them
  68. # Direct ones don't have a secondary audience
  69. def cc(status)
  70. cc = []
  71. cc << uri_for(status.reblog.account) if status.reblog?
  72. case status.visibility
  73. when 'public'
  74. cc << account_followers_url(status.account)
  75. when 'unlisted'
  76. cc << COLLECTIONS[:public]
  77. end
  78. unless status.direct_visibility? || status.limited_visibility?
  79. if status.account.silenced?
  80. # Only notify followers if the account is locally silenced
  81. account_ids = status.active_mentions.pluck(:account_id)
  82. cc.concat(status.account.followers.where(id: account_ids).map { |account| uri_for(account) })
  83. cc.concat(FollowRequest.where(target_account_id: status.account_id, account_id: account_ids).map { |request| uri_for(request.account) })
  84. else
  85. cc.concat(status.active_mentions.map { |mention| uri_for(mention.account) })
  86. end
  87. end
  88. cc
  89. end
  90. def local_uri?(uri)
  91. return false if uri.nil?
  92. uri = Addressable::URI.parse(uri)
  93. host = uri.normalized_host
  94. host = "#{host}:#{uri.port}" if uri.port
  95. !host.nil? && (::TagManager.instance.local_domain?(host) || ::TagManager.instance.web_domain?(host))
  96. end
  97. def uri_to_local_id(uri, param = :id)
  98. path_params = Rails.application.routes.recognize_path(uri)
  99. path_params[:username] = Rails.configuration.x.local_domain if path_params[:controller] == 'instance_actors'
  100. path_params[param]
  101. end
  102. def uri_to_resource(uri, klass)
  103. return if uri.nil?
  104. if local_uri?(uri)
  105. case klass.name
  106. when 'Account'
  107. klass.find_local(uri_to_local_id(uri, :username))
  108. else
  109. StatusFinder.new(uri).status
  110. end
  111. elsif OStatus::TagManager.instance.local_id?(uri)
  112. klass.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, klass.to_s))
  113. else
  114. klass.find_by(uri: uri.split('#').first)
  115. end
  116. rescue ActiveRecord::RecordNotFound
  117. nil
  118. end
  119. end