status_parser.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. # frozen_string_literal: true
  2. class ActivityPub::Parser::StatusParser
  3. include JsonLdHelper
  4. # @param [Hash] json
  5. # @param [Hash] magic_values
  6. # @option magic_values [String] :followers_collection
  7. def initialize(json, magic_values = {})
  8. @json = json
  9. @object = json['object'] || json
  10. @magic_values = magic_values
  11. end
  12. def uri
  13. id = @object['id']
  14. if id&.start_with?('bear:')
  15. Addressable::URI.parse(id).query_values['u']
  16. else
  17. id
  18. end
  19. rescue Addressable::URI::InvalidURIError
  20. id
  21. end
  22. def url
  23. url_to_href(@object['url'], 'text/html') if @object['url'].present?
  24. end
  25. def text
  26. if @object['content'].present?
  27. @object['content']
  28. elsif content_language_map?
  29. @object['contentMap'].values.first
  30. end
  31. end
  32. def spoiler_text
  33. if @object['summary'].present?
  34. @object['summary']
  35. elsif summary_language_map?
  36. @object['summaryMap'].values.first
  37. end
  38. end
  39. def title
  40. if @object['name'].present?
  41. @object['name']
  42. elsif name_language_map?
  43. @object['nameMap'].values.first
  44. end
  45. end
  46. def created_at
  47. datetime = @object['published']&.to_datetime
  48. datetime if datetime.present? && (0..9999).cover?(datetime.year)
  49. rescue ArgumentError
  50. nil
  51. end
  52. def edited_at
  53. @object['updated']&.to_datetime
  54. rescue ArgumentError
  55. nil
  56. end
  57. def reply
  58. @object['inReplyTo'].present?
  59. end
  60. def sensitive
  61. @object['sensitive']
  62. end
  63. def visibility
  64. if audience_to.any? { |to| ActivityPub::TagManager.instance.public_collection?(to) }
  65. :public
  66. elsif audience_cc.any? { |cc| ActivityPub::TagManager.instance.public_collection?(cc) }
  67. :unlisted
  68. elsif audience_to.include?(@magic_values[:followers_collection])
  69. :private
  70. else
  71. :direct
  72. end
  73. end
  74. def language
  75. if content_language_map?
  76. @object['contentMap'].keys.first
  77. elsif name_language_map?
  78. @object['nameMap'].keys.first
  79. elsif summary_language_map?
  80. @object['summaryMap'].keys.first
  81. end
  82. end
  83. private
  84. def audience_to
  85. as_array(@object['to'] || @json['to']).map { |x| value_or_id(x) }
  86. end
  87. def audience_cc
  88. as_array(@object['cc'] || @json['cc']).map { |x| value_or_id(x) }
  89. end
  90. def summary_language_map?
  91. @object['summaryMap'].is_a?(Hash) && !@object['summaryMap'].empty?
  92. end
  93. def content_language_map?
  94. @object['contentMap'].is_a?(Hash) && !@object['contentMap'].empty?
  95. end
  96. def name_language_map?
  97. @object['nameMap'].is_a?(Hash) && !@object['nameMap'].empty?
  98. end
  99. end