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.

94 lines
2.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. short_account_status_url(target.account, target)
  17. end
  18. end
  19. def uri_for(target)
  20. return target.uri if target.respond_to?(:local?) && !target.local?
  21. case target.object_type
  22. when :person
  23. account_url(target)
  24. when :note, :comment, :activity
  25. account_status_url(target.account, target)
  26. end
  27. end
  28. # Primary audience of a status
  29. # Public statuses go out to primarily the public collection
  30. # Unlisted and private statuses go out primarily to the followers collection
  31. # Others go out only to the people they mention
  32. def to(status)
  33. case status.visibility
  34. when 'public'
  35. [COLLECTIONS[:public]]
  36. when 'unlisted', 'private'
  37. [account_followers_url(status.account)]
  38. when 'direct'
  39. status.mentions.map { |mention| uri_for(mention.account) }
  40. end
  41. end
  42. # Secondary audience of a status
  43. # Public statuses go out to followers as well
  44. # Unlisted statuses go to the public as well
  45. # Both of those and private statuses also go to the people mentioned in them
  46. # Direct ones don't have a secondary audience
  47. def cc(status)
  48. cc = []
  49. case status.visibility
  50. when 'public'
  51. cc << account_followers_url(status.account)
  52. when 'unlisted'
  53. cc << COLLECTIONS[:public]
  54. end
  55. cc.concat(status.mentions.map { |mention| uri_for(mention.account) }) unless status.direct_visibility?
  56. cc
  57. end
  58. def local_uri?(uri)
  59. host = Addressable::URI.parse(uri).normalized_host
  60. ::TagManager.instance.local_domain?(host) || ::TagManager.instance.web_domain?(host)
  61. end
  62. def uri_to_local_id(uri, param = :id)
  63. path_params = Rails.application.routes.recognize_path(uri)
  64. path_params[param]
  65. end
  66. def uri_to_resource(uri, klass)
  67. if local_uri?(uri)
  68. case klass.name
  69. when 'Account'
  70. klass.find_local(uri_to_local_id(uri, :username))
  71. else
  72. klass.find_by(id: uri_to_local_id(uri))
  73. end
  74. else
  75. klass.find_by(uri: uri)
  76. end
  77. end
  78. end