The code powering m.abunchtell.com https://m.abunchtell.com
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

110 rader
3.6 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. return nil unless local_id?(tag)
  44. matches = Regexp.new("objectId=([\\d]+):objectType=#{expected_type}").match(tag)
  45. return matches[1] unless matches.nil?
  46. end
  47. def local_id?(id)
  48. id.start_with?("tag:#{Rails.configuration.x.local_domain}")
  49. end
  50. def web_domain?(domain)
  51. domain.nil? || domain.gsub(/[\/]/, '').casecmp(Rails.configuration.x.web_domain).zero?
  52. end
  53. def local_domain?(domain)
  54. domain.nil? || domain.gsub(/[\/]/, '').casecmp(Rails.configuration.x.local_domain).zero?
  55. end
  56. def normalize_domain(domain)
  57. return if domain.nil?
  58. uri = Addressable::URI.new
  59. uri.host = domain.gsub(/[\/]/, '')
  60. uri.normalized_host
  61. end
  62. def same_acct?(canonical, needle)
  63. return true if canonical.casecmp(needle).zero?
  64. username, domain = needle.split('@')
  65. local_domain?(domain) && canonical.casecmp(username).zero?
  66. end
  67. def local_url?(url)
  68. uri = Addressable::URI.parse(url).normalize
  69. domain = uri.host + (uri.port ? ":#{uri.port}" : '')
  70. TagManager.instance.local_domain?(domain)
  71. end
  72. def uri_for(target)
  73. return target.uri if target.respond_to?(:local?) && !target.local?
  74. case target.object_type
  75. when :person
  76. account_url(target)
  77. when :note, :comment, :activity
  78. unique_tag(target.created_at, target.id, 'Status')
  79. end
  80. end
  81. def url_for(target)
  82. return target.url if target.respond_to?(:local?) && !target.local?
  83. case target.object_type
  84. when :person
  85. short_account_url(target)
  86. when :note, :comment, :activity
  87. short_account_status_url(target.account, target)
  88. end
  89. end
  90. end