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.

80 lines
2.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. unfollow: 'http://ostatus.org/schema/1.0/unfollow',
  14. block: 'http://mastodon.social/schema/1.0/block',
  15. unblock: 'http://mastodon.social/schema/1.0/unblock',
  16. }.freeze
  17. TYPES = {
  18. activity: 'http://activitystrea.ms/schema/1.0/activity',
  19. note: 'http://activitystrea.ms/schema/1.0/note',
  20. comment: 'http://activitystrea.ms/schema/1.0/comment',
  21. person: 'http://activitystrea.ms/schema/1.0/person',
  22. collection: 'http://activitystrea.ms/schema/1.0/collection',
  23. group: 'http://activitystrea.ms/schema/1.0/group',
  24. }.freeze
  25. COLLECTIONS = {
  26. public: 'http://activityschema.org/collection/public',
  27. }.freeze
  28. XMLNS = 'http://www.w3.org/2005/Atom'
  29. MEDIA_XMLNS = 'http://purl.org/syndication/atommedia'
  30. AS_XMLNS = 'http://activitystrea.ms/spec/1.0/'
  31. THR_XMLNS = 'http://purl.org/syndication/thread/1.0'
  32. POCO_XMLNS = 'http://portablecontacts.net/spec/1.0'
  33. DFRN_XMLNS = 'http://purl.org/macgirvin/dfrn/1.0'
  34. OS_XMLNS = 'http://ostatus.org/schema/1.0'
  35. def unique_tag(date, id, type)
  36. "tag:#{Rails.configuration.x.local_domain},#{date.strftime('%Y-%m-%d')}:objectId=#{id}:objectType=#{type}"
  37. end
  38. def unique_tag_to_local_id(tag, expected_type)
  39. matches = Regexp.new("objectId=([\\d]+):objectType=#{expected_type}").match(tag)
  40. return matches[1] unless matches.nil?
  41. end
  42. def local_id?(id)
  43. id.start_with?("tag:#{Rails.configuration.x.local_domain}")
  44. end
  45. def local_domain?(domain)
  46. domain.nil? || domain.gsub(/[\/]/, '').casecmp(Rails.configuration.x.local_domain).zero?
  47. end
  48. def uri_for(target)
  49. return target.uri if target.respond_to?(:local?) && !target.local?
  50. case target.object_type
  51. when :person
  52. account_url(target)
  53. else
  54. unique_tag(target.stream_entry.created_at, target.stream_entry.activity_id, target.stream_entry.activity_type)
  55. end
  56. end
  57. def url_for(target)
  58. return target.url if target.respond_to?(:local?) && !target.local?
  59. case target.object_type
  60. when :person
  61. account_url(target)
  62. else
  63. account_stream_entry_url(target.account, target.stream_entry)
  64. end
  65. end
  66. end