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.

167 lines
3.2 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::NoteSerializer < ActiveModel::Serializer
  3. attributes :id, :type, :summary,
  4. :in_reply_to, :published, :url,
  5. :attributed_to, :to, :cc, :sensitive,
  6. :atom_uri, :in_reply_to_atom_uri,
  7. :conversation
  8. attribute :content
  9. attribute :content_map, if: :language?
  10. has_many :media_attachments, key: :attachment
  11. has_many :virtual_tags, key: :tag
  12. def id
  13. ActivityPub::TagManager.instance.uri_for(object)
  14. end
  15. def type
  16. 'Note'
  17. end
  18. def summary
  19. object.spoiler_text.presence
  20. end
  21. def content
  22. Formatter.instance.format(object)
  23. end
  24. def content_map
  25. { object.language => Formatter.instance.format(object) }
  26. end
  27. def language?
  28. object.language.present?
  29. end
  30. def in_reply_to
  31. return unless object.reply? && !object.thread.nil?
  32. if object.thread.uri.nil? || object.thread.uri.start_with?('http')
  33. ActivityPub::TagManager.instance.uri_for(object.thread)
  34. else
  35. object.thread.url
  36. end
  37. end
  38. def published
  39. object.created_at.iso8601
  40. end
  41. def url
  42. ActivityPub::TagManager.instance.url_for(object)
  43. end
  44. def attributed_to
  45. ActivityPub::TagManager.instance.uri_for(object.account)
  46. end
  47. def to
  48. ActivityPub::TagManager.instance.to(object)
  49. end
  50. def cc
  51. ActivityPub::TagManager.instance.cc(object)
  52. end
  53. def virtual_tags
  54. object.mentions.to_a.sort_by(&:id) + object.tags + object.emojis
  55. end
  56. def atom_uri
  57. return unless object.local?
  58. OStatus::TagManager.instance.uri_for(object)
  59. end
  60. def in_reply_to_atom_uri
  61. return unless object.reply? && !object.thread.nil?
  62. OStatus::TagManager.instance.uri_for(object.thread)
  63. end
  64. def conversation
  65. return if object.conversation.nil?
  66. if object.conversation.uri?
  67. object.conversation.uri
  68. else
  69. OStatus::TagManager.instance.unique_tag(object.conversation.created_at, object.conversation.id, 'Conversation')
  70. end
  71. end
  72. def local?
  73. object.account.local?
  74. end
  75. class MediaAttachmentSerializer < ActiveModel::Serializer
  76. include RoutingHelper
  77. attributes :type, :media_type, :url, :name
  78. attribute :focal_point, if: :focal_point?
  79. def type
  80. 'Document'
  81. end
  82. def name
  83. object.description
  84. end
  85. def media_type
  86. object.file_content_type
  87. end
  88. def url
  89. object.local? ? full_asset_url(object.file.url(:original, false)) : object.remote_url
  90. end
  91. def focal_point?
  92. object.file.meta.is_a?(Hash) && object.file.meta['focus'].is_a?(Hash)
  93. end
  94. def focal_point
  95. [object.file.meta['focus']['x'], object.file.meta['focus']['y']]
  96. end
  97. end
  98. class MentionSerializer < ActiveModel::Serializer
  99. attributes :type, :href, :name
  100. def type
  101. 'Mention'
  102. end
  103. def href
  104. ActivityPub::TagManager.instance.uri_for(object.account)
  105. end
  106. def name
  107. "@#{object.account.acct}"
  108. end
  109. end
  110. class TagSerializer < ActiveModel::Serializer
  111. include RoutingHelper
  112. attributes :type, :href, :name
  113. def type
  114. 'Hashtag'
  115. end
  116. def href
  117. tag_url(object)
  118. end
  119. def name
  120. "##{object.name}"
  121. end
  122. end
  123. class CustomEmojiSerializer < ActivityPub::EmojiSerializer
  124. end
  125. end