1
0

context_helper.rb 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. indexable: { 'toot' => 'http://joinmastodon.org/ns#', 'indexable' => 'toot:indexable' },
  22. memorial: { 'toot' => 'http://joinmastodon.org/ns#', 'memorial' => 'toot:memorial' },
  23. voters_count: { 'toot' => 'http://joinmastodon.org/ns#', 'votersCount' => 'toot:votersCount' },
  24. olm: {
  25. 'toot' => 'http://joinmastodon.org/ns#',
  26. 'Device' => 'toot:Device',
  27. 'Ed25519Signature' => 'toot:Ed25519Signature',
  28. 'Ed25519Key' => 'toot:Ed25519Key',
  29. 'Curve25519Key' => 'toot:Curve25519Key',
  30. 'EncryptedMessage' => 'toot:EncryptedMessage',
  31. 'publicKeyBase64' => 'toot:publicKeyBase64',
  32. 'deviceId' => 'toot:deviceId',
  33. 'claim' => { '@type' => '@id', '@id' => 'toot:claim' },
  34. 'fingerprintKey' => { '@type' => '@id', '@id' => 'toot:fingerprintKey' },
  35. 'identityKey' => { '@type' => '@id', '@id' => 'toot:identityKey' },
  36. 'devices' => { '@type' => '@id', '@id' => 'toot:devices' },
  37. 'messageFranking' => 'toot:messageFranking',
  38. 'messageType' => 'toot:messageType',
  39. 'cipherText' => 'toot:cipherText',
  40. },
  41. suspended: { 'toot' => 'http://joinmastodon.org/ns#', 'suspended' => 'toot:suspended' },
  42. }.freeze
  43. def full_context
  44. serialized_context(NAMED_CONTEXT_MAP, CONTEXT_EXTENSION_MAP)
  45. end
  46. def serialized_context(named_contexts_map, context_extensions_map)
  47. named_contexts = named_contexts_map.keys
  48. context_extensions = context_extensions_map.keys
  49. context_array = named_contexts.map do |key|
  50. NAMED_CONTEXT_MAP[key]
  51. end
  52. extensions = context_extensions.each_with_object({}) do |key, h|
  53. h.merge!(CONTEXT_EXTENSION_MAP[key])
  54. end
  55. context_array << extensions unless extensions.empty?
  56. if context_array.size == 1
  57. context_array.first
  58. else
  59. context_array
  60. end
  61. end
  62. end