2022-01-20 08:37:27 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::Parser::MediaAttachmentParser
|
|
|
|
include JsonLdHelper
|
|
|
|
|
|
|
|
def initialize(json)
|
|
|
|
@json = json
|
|
|
|
end
|
|
|
|
|
|
|
|
# @param [MediaAttachment] previous_record
|
|
|
|
def significantly_changes?(previous_record)
|
|
|
|
remote_url != previous_record.remote_url ||
|
|
|
|
thumbnail_remote_url != previous_record.thumbnail_remote_url ||
|
|
|
|
description != previous_record.description
|
|
|
|
end
|
|
|
|
|
|
|
|
def remote_url
|
|
|
|
Addressable::URI.parse(@json['url'])&.normalize&.to_s
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def thumbnail_remote_url
|
|
|
|
Addressable::URI.parse(@json['icon'].is_a?(Hash) ? @json['icon']['url'] : @json['icon'])&.normalize&.to_s
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def description
|
2022-03-26 10:38:44 +11:00
|
|
|
str = @json['summary'].presence || @json['name'].presence
|
|
|
|
str = str.strip[0...MediaAttachment::MAX_DESCRIPTION_LENGTH] if str.present?
|
|
|
|
str
|
2022-01-20 08:37:27 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def focus
|
|
|
|
@json['focalPoint']
|
|
|
|
end
|
|
|
|
|
|
|
|
def blurhash
|
|
|
|
supported_blurhash? ? @json['blurhash'] : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_content_type
|
|
|
|
@json['mediaType']
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def supported_blurhash?
|
|
|
|
components = begin
|
|
|
|
blurhash = @json['blurhash']
|
|
|
|
|
2022-06-24 07:10:03 +10:00
|
|
|
if blurhash.present? && /^[\w#$%*+,-.:;=?@\[\]^{|}~]+$/.match?(blurhash)
|
2022-01-20 08:37:27 +11:00
|
|
|
Blurhash.components(blurhash)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
components.present? && components.none? { |comp| comp > 5 }
|
|
|
|
end
|
|
|
|
end
|