2022-01-19 22:37:27 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::Parser::StatusParser
|
|
|
|
include JsonLdHelper
|
|
|
|
|
2024-05-23 19:28:18 +02:00
|
|
|
NORMALIZED_LOCALE_NAMES = LanguagesHelper::SUPPORTED_LOCALES.keys.index_by(&:downcase).freeze
|
|
|
|
|
2022-01-19 22:37:27 +01:00
|
|
|
# @param [Hash] json
|
2024-01-25 11:37:09 +01:00
|
|
|
# @param [Hash] options
|
|
|
|
# @option options [String] :followers_collection
|
|
|
|
# @option options [Hash] :object
|
|
|
|
def initialize(json, **options)
|
|
|
|
@json = json
|
|
|
|
@object = options[:object] || json['object'] || json
|
|
|
|
@options = options
|
2022-01-19 22:37:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def uri
|
|
|
|
id = @object['id']
|
|
|
|
|
|
|
|
if id&.start_with?('bear:')
|
|
|
|
Addressable::URI.parse(id).query_values['u']
|
|
|
|
else
|
|
|
|
id
|
|
|
|
end
|
|
|
|
rescue Addressable::URI::InvalidURIError
|
|
|
|
id
|
|
|
|
end
|
|
|
|
|
|
|
|
def url
|
|
|
|
url_to_href(@object['url'], 'text/html') if @object['url'].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def text
|
|
|
|
if @object['content'].present?
|
|
|
|
@object['content']
|
|
|
|
elsif content_language_map?
|
|
|
|
@object['contentMap'].values.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def spoiler_text
|
|
|
|
if @object['summary'].present?
|
|
|
|
@object['summary']
|
|
|
|
elsif summary_language_map?
|
|
|
|
@object['summaryMap'].values.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def title
|
|
|
|
if @object['name'].present?
|
|
|
|
@object['name']
|
|
|
|
elsif name_language_map?
|
|
|
|
@object['nameMap'].values.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def created_at
|
2023-11-06 10:28:14 +01:00
|
|
|
datetime = @object['published']&.to_datetime
|
|
|
|
datetime if datetime.present? && (0..9999).cover?(datetime.year)
|
2022-01-19 22:37:27 +01:00
|
|
|
rescue ArgumentError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def edited_at
|
|
|
|
@object['updated']&.to_datetime
|
|
|
|
rescue ArgumentError
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def reply
|
|
|
|
@object['inReplyTo'].present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def sensitive
|
|
|
|
@object['sensitive']
|
|
|
|
end
|
|
|
|
|
|
|
|
def visibility
|
|
|
|
if audience_to.any? { |to| ActivityPub::TagManager.instance.public_collection?(to) }
|
|
|
|
:public
|
|
|
|
elsif audience_cc.any? { |cc| ActivityPub::TagManager.instance.public_collection?(cc) }
|
|
|
|
:unlisted
|
2024-01-25 11:37:09 +01:00
|
|
|
elsif audience_to.include?(@options[:followers_collection])
|
2022-01-19 22:37:27 +01:00
|
|
|
:private
|
2022-01-19 23:19:00 +01:00
|
|
|
elsif direct_message == false
|
|
|
|
:limited
|
2022-01-19 22:37:27 +01:00
|
|
|
else
|
|
|
|
:direct
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def language
|
2024-05-23 19:28:18 +02:00
|
|
|
lang = raw_language_code
|
|
|
|
lang.presence && NORMALIZED_LOCALE_NAMES.fetch(lang.downcase.to_sym, lang)
|
|
|
|
end
|
|
|
|
|
2024-10-28 05:55:18 +01:00
|
|
|
def favourites_count
|
|
|
|
@object.dig(:likes, :totalItems)
|
|
|
|
end
|
|
|
|
|
|
|
|
def reblogs_count
|
|
|
|
@object.dig(:shares, :totalItems)
|
|
|
|
end
|
|
|
|
|
2024-05-23 19:28:18 +02:00
|
|
|
private
|
|
|
|
|
|
|
|
def raw_language_code
|
2022-01-19 22:37:27 +01:00
|
|
|
if content_language_map?
|
|
|
|
@object['contentMap'].keys.first
|
|
|
|
elsif name_language_map?
|
|
|
|
@object['nameMap'].keys.first
|
|
|
|
elsif summary_language_map?
|
|
|
|
@object['summaryMap'].keys.first
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-19 23:19:00 +01:00
|
|
|
def direct_message
|
|
|
|
@object['directMessage']
|
|
|
|
end
|
|
|
|
|
2022-01-19 22:37:27 +01:00
|
|
|
def audience_to
|
|
|
|
as_array(@object['to'] || @json['to']).map { |x| value_or_id(x) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def audience_cc
|
|
|
|
as_array(@object['cc'] || @json['cc']).map { |x| value_or_id(x) }
|
|
|
|
end
|
|
|
|
|
|
|
|
def summary_language_map?
|
|
|
|
@object['summaryMap'].is_a?(Hash) && !@object['summaryMap'].empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def content_language_map?
|
|
|
|
@object['contentMap'].is_a?(Hash) && !@object['contentMap'].empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def name_language_map?
|
|
|
|
@object['nameMap'].is_a?(Hash) && !@object['nameMap'].empty?
|
|
|
|
end
|
|
|
|
end
|