adapter.rb 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # frozen_string_literal: true
  2. class ActivityPub::Adapter < ActiveModelSerializers::Adapter::Base
  3. NAMED_CONTEXT_MAP = {
  4. activitystreams: 'https://www.w3.org/ns/activitystreams',
  5. security: 'https://w3id.org/security/v1',
  6. }.freeze
  7. CONTEXT_EXTENSION_MAP = {
  8. manually_approves_followers: { 'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers' },
  9. sensitive: { 'sensitive' => 'as:sensitive' },
  10. hashtag: { 'Hashtag' => 'as:Hashtag' },
  11. moved_to: { 'movedTo' => { '@id' => 'as:movedTo', '@type' => '@id' } },
  12. also_known_as: { 'alsoKnownAs' => { '@id' => 'as:alsoKnownAs', '@type' => '@id' } },
  13. emoji: { 'toot' => 'http://joinmastodon.org/ns#', 'Emoji' => 'toot:Emoji' },
  14. featured: { 'toot' => 'http://joinmastodon.org/ns#', 'featured' => { '@id' => 'toot:featured', '@type' => '@id' } },
  15. property_value: { 'schema' => 'http://schema.org#', 'PropertyValue' => 'schema:PropertyValue', 'value' => 'schema:value' },
  16. atom_uri: { 'ostatus' => 'http://ostatus.org#', 'atomUri' => 'ostatus:atomUri' },
  17. conversation: { 'ostatus' => 'http://ostatus.org#', 'inReplyToAtomUri' => 'ostatus:inReplyToAtomUri', 'conversation' => 'ostatus:conversation' },
  18. focal_point: { 'toot' => 'http://joinmastodon.org/ns#', 'focalPoint' => { '@container' => '@list', '@id' => 'toot:focalPoint' } },
  19. identity_proof: { 'toot' => 'http://joinmastodon.org/ns#', 'IdentityProof' => 'toot:IdentityProof' },
  20. blurhash: { 'toot' => 'http://joinmastodon.org/ns#', 'blurhash' => 'toot:blurhash' },
  21. discoverable: { 'toot' => 'http://joinmastodon.org/ns#', 'discoverable' => 'toot:discoverable' },
  22. voters_count: { 'toot' => 'http://joinmastodon.org/ns#', 'votersCount' => 'toot:votersCount' },
  23. 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' },
  24. }.freeze
  25. def self.default_key_transform
  26. :camel_lower
  27. end
  28. def self.transform_key_casing!(value, _options)
  29. ActivityPub::CaseTransform.camel_lower(value)
  30. end
  31. def serializable_hash(options = nil)
  32. named_contexts = {}
  33. context_extensions = {}
  34. options = serialization_options(options)
  35. serialized_hash = serializer.serializable_hash(options.merge(named_contexts: named_contexts, context_extensions: context_extensions))
  36. serialized_hash = serialized_hash.select { |k, _| options[:fields].include?(k) } if options[:fields]
  37. serialized_hash = self.class.transform_key_casing!(serialized_hash, instance_options)
  38. { '@context' => serialized_context(named_contexts, context_extensions) }.merge(serialized_hash)
  39. end
  40. private
  41. def serialized_context(named_contexts_map, context_extensions_map)
  42. context_array = []
  43. named_contexts = [:activitystreams] + named_contexts_map.keys
  44. context_extensions = context_extensions_map.keys
  45. named_contexts.each do |key|
  46. context_array << NAMED_CONTEXT_MAP[key]
  47. end
  48. extensions = context_extensions.each_with_object({}) do |key, h|
  49. h.merge!(CONTEXT_EXTENSION_MAP[key])
  50. end
  51. context_array << extensions unless extensions.empty?
  52. if context_array.size == 1
  53. context_array.first
  54. else
  55. context_array
  56. end
  57. end
  58. end