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.

119 lines
3.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. 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 :follow
  24. account_follow_url(target.account.username, target)
  25. when :person
  26. account_url(target)
  27. when :note, :comment, :activity
  28. return activity_account_status_url(target.account, target) if target.reblog?
  29. account_status_url(target.account, target)
  30. when :emoji
  31. emoji_url(target)
  32. end
  33. end
  34. def activity_uri_for(target)
  35. raise ArgumentError, 'target must be a local activity' unless %i(note comment activity).include?(target.object_type) && target.local?
  36. activity_account_status_url(target.account, target)
  37. end
  38. # Primary audience of a status
  39. # Public statuses go out to primarily the public collection
  40. # Unlisted and private statuses go out primarily to the followers collection
  41. # Others go out only to the people they mention
  42. def to(status)
  43. case status.visibility
  44. when 'public'
  45. [COLLECTIONS[:public]]
  46. when 'unlisted', 'private'
  47. [account_followers_url(status.account)]
  48. when 'direct'
  49. status.mentions.map { |mention| uri_for(mention.account) }
  50. end
  51. end
  52. # Secondary audience of a status
  53. # Public statuses go out to followers as well
  54. # Unlisted statuses go to the public as well
  55. # Both of those and private statuses also go to the people mentioned in them
  56. # Direct ones don't have a secondary audience
  57. def cc(status)
  58. cc = []
  59. case status.visibility
  60. when 'public'
  61. cc << account_followers_url(status.account)
  62. when 'unlisted'
  63. cc << COLLECTIONS[:public]
  64. end
  65. cc.concat(status.mentions.map { |mention| uri_for(mention.account) }) unless status.direct_visibility?
  66. cc
  67. end
  68. def local_uri?(uri)
  69. uri = Addressable::URI.parse(uri)
  70. host = uri.normalized_host
  71. host = "#{host}:#{uri.port}" if uri.port
  72. !host.nil? && (::TagManager.instance.local_domain?(host) || ::TagManager.instance.web_domain?(host))
  73. end
  74. def uri_to_local_id(uri, param = :id)
  75. path_params = Rails.application.routes.recognize_path(uri)
  76. path_params[param]
  77. end
  78. def uri_to_resource(uri, klass)
  79. if local_uri?(uri)
  80. case klass.name
  81. when 'Account'
  82. klass.find_local(uri_to_local_id(uri, :username))
  83. when 'FollowRequest'
  84. params = Rails.application.routes.recognize_path(uri)
  85. klass.joins(:account).find_by!(
  86. accounts: { domain: nil, username: params[:account_username] },
  87. id: params[:id]
  88. )
  89. else
  90. StatusFinder.new(uri).status
  91. end
  92. elsif OStatus::TagManager.instance.local_id?(uri)
  93. klass.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, klass.to_s))
  94. else
  95. klass.find_by(uri: uri.split('#').first)
  96. end
  97. rescue ActiveRecord::RecordNotFound
  98. nil
  99. end
  100. end