2017-07-15 03:01:39 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::Adapter < ActiveModelSerializers::Adapter::Base
|
2019-03-27 15:55:23 +01:00
|
|
|
NAMED_CONTEXT_MAP = {
|
|
|
|
activitystreams: 'https://www.w3.org/ns/activitystreams',
|
|
|
|
security: 'https://w3id.org/security/v1',
|
|
|
|
}.freeze
|
|
|
|
|
|
|
|
CONTEXT_EXTENSION_MAP = {
|
|
|
|
manually_approves_followers: { 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers' },
|
|
|
|
sensitive: { 'sensitive' => 'as:sensitive' },
|
|
|
|
hashtag: { 'Hashtag' => 'as:Hashtag' },
|
|
|
|
moved_to: { 'movedTo' => { '@id' => 'as:movedTo', '@type' => '@id' } },
|
|
|
|
also_known_as: { 'alsoKnownAs' => { '@id' => 'as:alsoKnownAs', '@type' => '@id' } },
|
|
|
|
emoji: { 'toot' => 'http://joinmastodon.org/ns#', 'Emoji' => 'toot:Emoji' },
|
|
|
|
featured: { 'toot' => 'http://joinmastodon.org/ns#', 'featured' => { '@id' => 'toot:featured', '@type' => '@id' } },
|
|
|
|
property_value: { 'schema' => 'http://schema.org#', 'PropertyValue' => 'schema:PropertyValue', 'value' => 'schema:value' },
|
|
|
|
atom_uri: { 'ostatus' => 'http://ostatus.org#', 'atomUri' => 'ostatus:atomUri' },
|
|
|
|
conversation: { 'ostatus' => 'http://ostatus.org#', 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri', 'conversation' => 'ostatus:conversation' },
|
|
|
|
focal_point: { 'toot' => 'http://joinmastodon.org/ns#', 'focalPoint' => { '@container' => '@list', '@id' => 'toot:focalPoint' } },
|
2019-03-30 02:12:06 +01:00
|
|
|
identity_proof: { 'toot' => 'http://joinmastodon.org/ns#', 'IdentityProof' => 'toot:IdentityProof' },
|
2019-04-27 03:24:09 +02:00
|
|
|
blurhash: { 'toot' => 'http://joinmastodon.org/ns#', 'blurhash' => 'toot:blurhash' },
|
2017-09-02 14:01:23 +02:00
|
|
|
}.freeze
|
|
|
|
|
2017-07-15 03:01:39 +02:00
|
|
|
def self.default_key_transform
|
|
|
|
:camel_lower
|
|
|
|
end
|
|
|
|
|
2017-08-12 17:41:03 +02:00
|
|
|
def self.transform_key_casing!(value, _options)
|
|
|
|
ActivityPub::CaseTransform.camel_lower(value)
|
|
|
|
end
|
|
|
|
|
2017-07-15 03:01:39 +02:00
|
|
|
def serializable_hash(options = nil)
|
2019-03-27 15:55:23 +01:00
|
|
|
options = serialization_options(options)
|
|
|
|
serialized_hash = serializer.serializable_hash(options)
|
2019-07-11 20:11:09 +02:00
|
|
|
serialized_hash = serialized_hash.select { |k, _| options[:fields].include?(k) } if options[:fields]
|
2019-03-27 15:55:23 +01:00
|
|
|
serialized_hash = self.class.transform_key_casing!(serialized_hash, instance_options)
|
|
|
|
|
|
|
|
{ '@context' => serialized_context }.merge(serialized_hash)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def serialized_context
|
|
|
|
context_array = []
|
|
|
|
|
|
|
|
serializer_options = serializer.send(:instance_options) || {}
|
|
|
|
named_contexts = [:activitystreams] + serializer._named_contexts.keys + serializer_options.fetch(:named_contexts, {}).keys
|
|
|
|
context_extensions = serializer._context_extensions.keys + serializer_options.fetch(:context_extensions, {}).keys
|
|
|
|
|
|
|
|
named_contexts.each do |key|
|
|
|
|
context_array << NAMED_CONTEXT_MAP[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
extensions = context_extensions.each_with_object({}) do |key, h|
|
|
|
|
h.merge!(CONTEXT_EXTENSION_MAP[key])
|
|
|
|
end
|
|
|
|
|
|
|
|
context_array << extensions unless extensions.empty?
|
|
|
|
|
|
|
|
if context_array.size == 1
|
|
|
|
context_array.first
|
|
|
|
else
|
|
|
|
context_array
|
|
|
|
end
|
2017-07-15 03:01:39 +02:00
|
|
|
end
|
|
|
|
end
|