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.

58 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::Parser::MediaAttachmentParser
  3. include JsonLdHelper
  4. def initialize(json)
  5. @json = json
  6. end
  7. # @param [MediaAttachment] previous_record
  8. def significantly_changes?(previous_record)
  9. remote_url != previous_record.remote_url ||
  10. thumbnail_remote_url != previous_record.thumbnail_remote_url ||
  11. description != previous_record.description
  12. end
  13. def remote_url
  14. Addressable::URI.parse(@json['url'])&.normalize&.to_s
  15. rescue Addressable::URI::InvalidURIError
  16. nil
  17. end
  18. def thumbnail_remote_url
  19. Addressable::URI.parse(@json['icon'].is_a?(Hash) ? @json['icon']['url'] : @json['icon'])&.normalize&.to_s
  20. rescue Addressable::URI::InvalidURIError
  21. nil
  22. end
  23. def description
  24. str = @json['summary'].presence || @json['name'].presence
  25. str = str.strip[0...MediaAttachment::MAX_DESCRIPTION_LENGTH] if str.present?
  26. str
  27. end
  28. def focus
  29. @json['focalPoint']
  30. end
  31. def blurhash
  32. supported_blurhash? ? @json['blurhash'] : nil
  33. end
  34. def file_content_type
  35. @json['mediaType']
  36. end
  37. private
  38. def supported_blurhash?
  39. components = begin
  40. blurhash = @json['blurhash']
  41. Blurhash.components(blurhash) if blurhash.present? && /^[\w#$%*+,-.:;=?@\[\]^{|}~]+$/.match?(blurhash)
  42. end
  43. components.present? && components.none? { |comp| comp > 5 }
  44. end
  45. end