tag_manager.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 :person
  24. 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. # Primary audience of a status
  40. # Public statuses go out to primarily the public collection
  41. # Unlisted and private statuses go out primarily to the followers collection
  42. # Others go out only to the people they mention
  43. def to(status)
  44. case status.visibility
  45. when 'public'
  46. [COLLECTIONS[:public]]
  47. when 'unlisted', 'private'
  48. [account_followers_url(status.account)]
  49. when 'direct', 'limited'
  50. status.active_mentions.map { |mention| uri_for(mention.account) }
  51. end
  52. end
  53. # Secondary audience of a status
  54. # Public statuses go out to followers as well
  55. # Unlisted statuses go to the public as well
  56. # Both of those and private statuses also go to the people mentioned in them
  57. # Direct ones don't have a secondary audience
  58. def cc(status)
  59. cc = []
  60. cc << uri_for(status.reblog.account) if status.reblog?
  61. case status.visibility
  62. when 'public'
  63. cc << account_followers_url(status.account)
  64. when 'unlisted'
  65. cc << COLLECTIONS[:public]
  66. end
  67. cc.concat(status.active_mentions.map { |mention| uri_for(mention.account) }) unless status.direct_visibility? || status.limited_visibility?
  68. cc
  69. end
  70. def local_uri?(uri)
  71. return false if uri.nil?
  72. uri = Addressable::URI.parse(uri)
  73. host = uri.normalized_host
  74. host = "#{host}:#{uri.port}" if uri.port
  75. !host.nil? && (::TagManager.instance.local_domain?(host) || ::TagManager.instance.web_domain?(host))
  76. end
  77. def uri_to_local_id(uri, param = :id)
  78. path_params = Rails.application.routes.recognize_path(uri)
  79. path_params[param]
  80. end
  81. def uri_to_resource(uri, klass)
  82. return if uri.nil?
  83. if local_uri?(uri)
  84. case klass.name
  85. when 'Account'
  86. klass.find_local(uri_to_local_id(uri, :username))
  87. else
  88. StatusFinder.new(uri).status
  89. end
  90. elsif OStatus::TagManager.instance.local_id?(uri)
  91. klass.find_by(id: OStatus::TagManager.instance.unique_tag_to_local_id(uri, klass.to_s))
  92. else
  93. klass.find_by(uri: uri.split('#').first)
  94. end
  95. rescue ActiveRecord::RecordNotFound
  96. nil
  97. end
  98. end