You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
3.0 KiB

  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. }.freeze
  24. def self.default_key_transform
  25. :camel_lower
  26. end
  27. def self.transform_key_casing!(value, _options)
  28. ActivityPub::CaseTransform.camel_lower(value)
  29. end
  30. def serializable_hash(options = nil)
  31. named_contexts = {}
  32. context_extensions = {}
  33. options = serialization_options(options)
  34. serialized_hash = serializer.serializable_hash(options.merge(named_contexts: named_contexts, context_extensions: context_extensions))
  35. serialized_hash = serialized_hash.select { |k, _| options[:fields].include?(k) } if options[:fields]
  36. serialized_hash = self.class.transform_key_casing!(serialized_hash, instance_options)
  37. { '@context' => serialized_context(named_contexts, context_extensions) }.merge(serialized_hash)
  38. end
  39. private
  40. def serialized_context(named_contexts_map, context_extensions_map)
  41. context_array = []
  42. named_contexts = [:activitystreams] + named_contexts_map.keys
  43. context_extensions = context_extensions_map.keys
  44. named_contexts.each do |key|
  45. context_array << NAMED_CONTEXT_MAP[key]
  46. end
  47. extensions = context_extensions.each_with_object({}) do |key, h|
  48. h.merge!(CONTEXT_EXTENSION_MAP[key])
  49. end
  50. context_array << extensions unless extensions.empty?
  51. if context_array.size == 1
  52. context_array.first
  53. else
  54. context_array
  55. end
  56. end
  57. end