logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

tag_manager.rb (3234B)


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