闭社主体 forked from https://github.com/tootsuite/mastodon
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.

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