context_helper.rb 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. suspended: { 'toot' => 'http://joinmastodon.org/ns#', 'suspended' => 'toot:suspended' },
  25. attribution_domains: { 'toot' => 'http://joinmastodon.org/ns#', 'attributionDomains' => { '@id' => 'toot:attributionDomains', '@type' => '@id' } },
  26. }.freeze
  27. def full_context
  28. serialized_context(NAMED_CONTEXT_MAP, CONTEXT_EXTENSION_MAP)
  29. end
  30. def serialized_context(named_contexts_map, context_extensions_map)
  31. named_contexts = named_contexts_map.keys
  32. context_extensions = context_extensions_map.keys
  33. context_array = named_contexts.map do |key|
  34. NAMED_CONTEXT_MAP[key]
  35. end
  36. extensions = context_extensions.each_with_object({}) do |key, h|
  37. h.merge!(CONTEXT_EXTENSION_MAP[key])
  38. end
  39. context_array << extensions unless extensions.empty?
  40. if context_array.size == 1
  41. context_array.first
  42. else
  43. context_array
  44. end
  45. end
  46. end