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.

104 lines
3.5 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 same_acct?(canonical, needle)
  56. return true if canonical.casecmp(needle).zero?
  57. username, domain = needle.split('@')
  58. local_domain?(domain) && canonical.casecmp(username).zero?
  59. end
  60. def local_url?(url)
  61. uri = Addressable::URI.parse(url)
  62. domain = uri.host + (uri.port ? ":#{uri.port}" : '')
  63. TagManager.instance.local_domain?(domain)
  64. end
  65. def uri_for(target)
  66. return target.uri if target.respond_to?(:local?) && !target.local?
  67. case target.object_type
  68. when :person
  69. account_url(target)
  70. when :note, :comment, :activity
  71. unique_tag(target.created_at, target.id, 'Status')
  72. else
  73. unique_tag(target.stream_entry.created_at, target.stream_entry.activity_id, target.stream_entry.activity_type)
  74. end
  75. end
  76. def url_for(target)
  77. return target.url if target.respond_to?(:local?) && !target.local?
  78. case target.object_type
  79. when :person
  80. short_account_url(target)
  81. when :note, :comment, :activity
  82. short_account_status_url(target.account, target)
  83. else
  84. account_stream_entry_url(target.account, target.stream_entry)
  85. end
  86. end
  87. end