tag_manager.rb 3.1 KB

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