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.

162 lines
5.4 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).each_with_object([]) do |account, result|
  58. result << uri_for(account)
  59. result << account.followers_url if account.group?
  60. end
  61. to.concat(FollowRequest.where(target_account_id: status.account_id, account_id: account_ids).each_with_object([]) do |request, result|
  62. result << uri_for(request.account)
  63. result << request.account.followers_url if request.account.group?
  64. end)
  65. else
  66. status.active_mentions.each_with_object([]) do |mention, result|
  67. result << uri_for(mention.account)
  68. result << mention.account.followers_url if mention.account.group?
  69. end
  70. end
  71. end
  72. end
  73. # Secondary audience of a status
  74. # Public statuses go out to followers as well
  75. # Unlisted statuses go to the public as well
  76. # Both of those and private statuses also go to the people mentioned in them
  77. # Direct ones don't have a secondary audience
  78. def cc(status)
  79. cc = []
  80. cc << uri_for(status.reblog.account) if status.reblog?
  81. case status.visibility
  82. when 'public'
  83. cc << account_followers_url(status.account)
  84. when 'unlisted'
  85. cc << COLLECTIONS[:public]
  86. end
  87. unless status.direct_visibility? || status.limited_visibility?
  88. if status.account.silenced?
  89. # Only notify followers if the account is locally silenced
  90. account_ids = status.active_mentions.pluck(:account_id)
  91. cc.concat(status.account.followers.where(id: account_ids).each_with_object([]) do |account, result|
  92. result << uri_for(account)
  93. result << account.followers_url if account.group?
  94. end)
  95. cc.concat(FollowRequest.where(target_account_id: status.account_id, account_id: account_ids).each_with_object([]) do |request, result|
  96. result << uri_for(request.account)
  97. result << request.account.followers_url if request.account.group?
  98. end)
  99. else
  100. cc.concat(status.active_mentions.each_with_object([]) do |mention, result|
  101. result << uri_for(mention.account)
  102. result << mention.account.followers_url if mention.account.group?
  103. end)
  104. end
  105. end
  106. cc
  107. end
  108. def local_uri?(uri)
  109. return false if uri.nil?
  110. uri = Addressable::URI.parse(uri)
  111. host = uri.normalized_host
  112. host = "#{host}:#{uri.port}" if uri.port
  113. !host.nil? && (::TagManager.instance.local_domain?(host) || ::TagManager.instance.web_domain?(host))
  114. end
  115. def uri_to_local_id(uri, param = :id)
  116. path_params = Rails.application.routes.recognize_path(uri)
  117. path_params[:username] = Rails.configuration.x.local_domain if path_params[:controller] == 'instance_actors'
  118. path_params[param]
  119. end
  120. def uri_to_resource(uri, klass)
  121. return if uri.nil?
  122. if local_uri?(uri)
  123. case klass.name
  124. when 'Account'
  125. klass.find_local(uri_to_local_id(uri, :username))
  126. else
  127. StatusFinder.new(uri).status
  128. end
  129. elsif OStatus::TagManager.instance.local_id?(uri)
  130. klass.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, klass.to_s))
  131. else
  132. klass.find_by(uri: uri.split('#').first)
  133. end
  134. rescue ActiveRecord::RecordNotFound
  135. nil
  136. end
  137. end