2017-07-15 11:01:39 +10:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ActivityPub::Adapter < ActiveModelSerializers::Adapter::Base
|
2019-03-28 01:55:23 +11: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' },
|
2020-09-02 10:11:12 +10:00
|
|
|
featured: { 'toot' => 'http://joinmastodon.org/ns#', 'featured' => { '@id' => 'toot:featured', '@type' => '@id' }, 'featuredTags' => { '@id' => 'toot:featuredTags', '@type' => '@id' } },
|
2019-03-28 01:55:23 +11:00
|
|
|
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-04-27 11:24:09 +10:00
|
|
|
blurhash: { 'toot' => 'http://joinmastodon.org/ns#', 'blurhash' => 'toot:blurhash' },
|
2019-08-30 08:14:36 +10:00
|
|
|
discoverable: { 'toot' => 'http://joinmastodon.org/ns#', 'discoverable' => 'toot:discoverable' },
|
2019-09-30 06:58:01 +10:00
|
|
|
voters_count: { 'toot' => 'http://joinmastodon.org/ns#', 'votersCount' => 'toot:votersCount' },
|
2020-06-03 03:24:53 +10:00
|
|
|
olm: { 'toot' => 'http://joinmastodon.org/ns#', 'Device' => 'toot:Device', 'Ed25519Signature' => 'toot:Ed25519Signature', 'Ed25519Key' => 'toot:Ed25519Key', 'Curve25519Key' => 'toot:Curve25519Key', 'EncryptedMessage' => 'toot:EncryptedMessage', 'publicKeyBase64' => 'toot:publicKeyBase64', 'deviceId' => 'toot:deviceId', 'claim' => { '@type' => '@id', '@id' => 'toot:claim' }, 'fingerprintKey' => { '@type' => '@id', '@id' => 'toot:fingerprintKey' }, 'identityKey' => { '@type' => '@id', '@id' => 'toot:identityKey' }, 'devices' => { '@type' => '@id', '@id' => 'toot:devices' }, 'messageFranking' => 'toot:messageFranking', 'messageType' => 'toot:messageType', 'cipherText' => 'toot:cipherText' },
|
2020-11-08 10:28:39 +11:00
|
|
|
suspended: { 'toot' => 'http://joinmastodon.org/ns#', 'suspended' => 'toot:suspended' },
|
2017-09-02 22:01:23 +10:00
|
|
|
}.freeze
|
|
|
|
|
2017-07-15 11:01:39 +10:00
|
|
|
def self.default_key_transform
|
|
|
|
:camel_lower
|
|
|
|
end
|
|
|
|
|
2017-08-13 01:41:03 +10:00
|
|
|
def self.transform_key_casing!(value, _options)
|
|
|
|
ActivityPub::CaseTransform.camel_lower(value)
|
|
|
|
end
|
|
|
|
|
2017-07-15 11:01:39 +10:00
|
|
|
def serializable_hash(options = nil)
|
2019-09-04 06:52:32 +10:00
|
|
|
named_contexts = {}
|
|
|
|
context_extensions = {}
|
2019-12-03 04:25:43 +11:00
|
|
|
|
2019-03-28 01:55:23 +11:00
|
|
|
options = serialization_options(options)
|
2019-09-04 06:52:32 +10:00
|
|
|
serialized_hash = serializer.serializable_hash(options.merge(named_contexts: named_contexts, context_extensions: context_extensions))
|
2019-07-12 04:11:09 +10:00
|
|
|
serialized_hash = serialized_hash.select { |k, _| options[:fields].include?(k) } if options[:fields]
|
2019-03-28 01:55:23 +11:00
|
|
|
serialized_hash = self.class.transform_key_casing!(serialized_hash, instance_options)
|
|
|
|
|
2019-09-04 06:52:32 +10:00
|
|
|
{ '@context' => serialized_context(named_contexts, context_extensions) }.merge(serialized_hash)
|
2019-03-28 01:55:23 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-09-04 06:52:32 +10:00
|
|
|
def serialized_context(named_contexts_map, context_extensions_map)
|
2019-03-28 01:55:23 +11:00
|
|
|
context_array = []
|
|
|
|
|
2019-09-04 06:52:32 +10:00
|
|
|
named_contexts = [:activitystreams] + named_contexts_map.keys
|
|
|
|
context_extensions = context_extensions_map.keys
|
2019-03-28 01:55:23 +11:00
|
|
|
|
|
|
|
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 11:01:39 +10:00
|
|
|
end
|
|
|
|
end
|