context_helper.rb 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # frozen_string_literal: true
  2. module ContextHelper
  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' }, 'featuredTags' => { '@id' => 'toot:featuredTags', '@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. blurhash: { 'toot' => 'http://joinmastodon.org/ns#', 'blurhash' => 'toot:blurhash' },
  20. discoverable: { 'toot' => 'http://joinmastodon.org/ns#', 'discoverable' => 'toot:discoverable' },
  21. voters_count: { 'toot' => 'http://joinmastodon.org/ns#', 'votersCount' => 'toot:votersCount' },
  22. olm: {
  23. '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',
  24. 'claim' => { '@type' => '@id', '@id' => 'toot:claim' },
  25. 'fingerprintKey' => { '@type' => '@id', '@id' => 'toot:fingerprintKey' },
  26. 'identityKey' => { '@type' => '@id', '@id' => 'toot:identityKey' },
  27. 'devices' => { '@type' => '@id', '@id' => 'toot:devices' },
  28. 'messageFranking' => 'toot:messageFranking', 'messageType' => 'toot:messageType', 'cipherText' => 'toot:cipherText'
  29. },
  30. suspended: { 'toot' => 'http://joinmastodon.org/ns#', 'suspended' => 'toot:suspended' },
  31. }.freeze
  32. def full_context
  33. serialized_context(NAMED_CONTEXT_MAP, CONTEXT_EXTENSION_MAP)
  34. end
  35. def serialized_context(named_contexts_map, context_extensions_map)
  36. context_array = []
  37. named_contexts = named_contexts_map.keys
  38. context_extensions = context_extensions_map.keys
  39. named_contexts.each do |key|
  40. context_array << NAMED_CONTEXT_MAP[key]
  41. end
  42. extensions = context_extensions.each_with_object({}) do |key, h|
  43. h.merge!(CONTEXT_EXTENSION_MAP[key])
  44. end
  45. context_array << extensions unless extensions.empty?
  46. if context_array.size == 1
  47. context_array.first
  48. else
  49. context_array
  50. end
  51. end
  52. end