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.

112 lines
3.7 KiB

  1. # frozen_string_literal: true
  2. require 'singleton'
  3. class TagManager
  4. include Singleton
  5. include RoutingHelper
  6. VERBS = {
  7. post: 'http://activitystrea.ms/schema/1.0/post',
  8. share: 'http://activitystrea.ms/schema/1.0/share',
  9. favorite: 'http://activitystrea.ms/schema/1.0/favorite',
  10. unfavorite: 'http://activitystrea.ms/schema/1.0/unfavorite',
  11. delete: 'http://activitystrea.ms/schema/1.0/delete',
  12. follow: 'http://activitystrea.ms/schema/1.0/follow',
  13. request_friend: 'http://activitystrea.ms/schema/1.0/request-friend',
  14. authorize: 'http://activitystrea.ms/schema/1.0/authorize',
  15. reject: 'http://activitystrea.ms/schema/1.0/reject',
  16. unfollow: 'http://ostatus.org/schema/1.0/unfollow',
  17. block: 'http://mastodon.social/schema/1.0/block',
  18. unblock: 'http://mastodon.social/schema/1.0/unblock',
  19. }.freeze
  20. TYPES = {
  21. activity: 'http://activitystrea.ms/schema/1.0/activity',
  22. note: 'http://activitystrea.ms/schema/1.0/note',
  23. comment: 'http://activitystrea.ms/schema/1.0/comment',
  24. person: 'http://activitystrea.ms/schema/1.0/person',
  25. collection: 'http://activitystrea.ms/schema/1.0/collection',
  26. group: 'http://activitystrea.ms/schema/1.0/group',
  27. }.freeze
  28. COLLECTIONS = {
  29. public: 'http://activityschema.org/collection/public',
  30. }.freeze
  31. XMLNS = 'http://www.w3.org/2005/Atom'
  32. MEDIA_XMLNS = 'http://purl.org/syndication/atommedia'
  33. AS_XMLNS = 'http://activitystrea.ms/spec/1.0/'
  34. THR_XMLNS = 'http://purl.org/syndication/thread/1.0'
  35. POCO_XMLNS = 'http://portablecontacts.net/spec/1.0'
  36. DFRN_XMLNS = 'http://purl.org/macgirvin/dfrn/1.0'
  37. OS_XMLNS = 'http://ostatus.org/schema/1.0'
  38. MTDN_XMLNS = 'http://mastodon.social/schema/1.0'
  39. def unique_tag(date, id, type)
  40. "tag:#{Rails.configuration.x.local_domain},#{date.strftime('%Y-%m-%d')}:objectId=#{id}:objectType=#{type}"
  41. end
  42. def unique_tag_to_local_id(tag, expected_type)
  43. matches = Regexp.new("objectId=([\\d]+):objectType=#{expected_type}").match(tag)
  44. return matches[1] unless matches.nil?
  45. end
  46. def local_id?(id)
  47. id.start_with?("tag:#{Rails.configuration.x.local_domain}")
  48. end
  49. def web_domain?(domain)
  50. domain.nil? || domain.gsub(/[\/]/, '').casecmp(Rails.configuration.x.web_domain).zero?
  51. end
  52. def local_domain?(domain)
  53. domain.nil? || domain.gsub(/[\/]/, '').casecmp(Rails.configuration.x.local_domain).zero?
  54. end
  55. def normalize_domain(domain)
  56. return if domain.nil?
  57. uri = Addressable::URI.new
  58. uri.host = domain.gsub(/[\/]/, '')
  59. uri.normalize.host
  60. end
  61. def same_acct?(canonical, needle)
  62. return true if canonical.casecmp(needle).zero?
  63. username, domain = needle.split('@')
  64. local_domain?(domain) && canonical.casecmp(username).zero?
  65. end
  66. def local_url?(url)
  67. uri = Addressable::URI.parse(url).normalize
  68. domain = uri.host + (uri.port ? ":#{uri.port}" : '')
  69. TagManager.instance.local_domain?(domain)
  70. end
  71. def uri_for(target)
  72. return target.uri if target.respond_to?(:local?) && !target.local?
  73. case target.object_type
  74. when :person
  75. account_url(target)
  76. when :note, :comment, :activity
  77. unique_tag(target.created_at, target.id, 'Status')
  78. else
  79. unique_tag(target.stream_entry.created_at, target.stream_entry.activity_id, target.stream_entry.activity_type)
  80. end
  81. end
  82. def url_for(target)
  83. return target.url if target.respond_to?(:local?) && !target.local?
  84. case target.object_type
  85. when :person
  86. short_account_url(target)
  87. when :note, :comment, :activity
  88. short_account_status_url(target.account, target)
  89. else
  90. account_stream_entry_url(target.account, target.stream_entry)
  91. end
  92. end
  93. end