note_serializer.rb 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. # frozen_string_literal: true
  2. class ActivityPub::NoteSerializer < ActivityPub::Serializer
  3. context_extensions :atom_uri, :conversation, :sensitive, :voters_count
  4. attributes :id, :type, :summary,
  5. :in_reply_to, :published, :url,
  6. :attributed_to, :to, :cc, :sensitive,
  7. :atom_uri, :in_reply_to_atom_uri,
  8. :conversation
  9. attribute :content
  10. attribute :content_map, if: :language?
  11. has_many :media_attachments, key: :attachment
  12. has_many :virtual_tags, key: :tag
  13. has_one :replies, serializer: ActivityPub::CollectionSerializer, if: :local?
  14. has_many :poll_options, key: :one_of, if: :poll_and_not_multiple?
  15. has_many :poll_options, key: :any_of, if: :poll_and_multiple?
  16. attribute :end_time, if: :poll_and_expires?
  17. attribute :closed, if: :poll_and_expired?
  18. attribute :voters_count, if: :poll_and_voters_count?
  19. def id
  20. ActivityPub::TagManager.instance.uri_for(object)
  21. end
  22. def type
  23. object.preloadable_poll ? 'Question' : 'Note'
  24. end
  25. def summary
  26. object.spoiler_text.presence
  27. end
  28. def content
  29. Formatter.instance.format(object)
  30. end
  31. def content_map
  32. { object.language => Formatter.instance.format(object) }
  33. end
  34. def replies
  35. replies = object.self_replies(5).pluck(:id, :uri)
  36. last_id = replies.last&.first
  37. ActivityPub::CollectionPresenter.new(
  38. type: :unordered,
  39. id: ActivityPub::TagManager.instance.replies_uri_for(object),
  40. first: ActivityPub::CollectionPresenter.new(
  41. type: :unordered,
  42. part_of: ActivityPub::TagManager.instance.replies_uri_for(object),
  43. items: replies.map(&:second),
  44. next: last_id ? ActivityPub::TagManager.instance.replies_uri_for(object, page: true, min_id: last_id) : ActivityPub::TagManager.instance.replies_uri_for(object, page: true, only_other_accounts: true)
  45. )
  46. )
  47. end
  48. def language?
  49. object.language.present?
  50. end
  51. def in_reply_to
  52. return unless object.reply? && !object.thread.nil?
  53. if object.thread.uri.nil? || object.thread.uri.start_with?('http')
  54. ActivityPub::TagManager.instance.uri_for(object.thread)
  55. else
  56. object.thread.url
  57. end
  58. end
  59. def published
  60. object.created_at.iso8601
  61. end
  62. def url
  63. ActivityPub::TagManager.instance.url_for(object)
  64. end
  65. def attributed_to
  66. ActivityPub::TagManager.instance.uri_for(object.account)
  67. end
  68. def to
  69. ActivityPub::TagManager.instance.to(object)
  70. end
  71. def cc
  72. ActivityPub::TagManager.instance.cc(object)
  73. end
  74. def sensitive
  75. object.account.sensitized? || object.sensitive
  76. end
  77. def virtual_tags
  78. object.active_mentions.to_a.sort_by(&:id) + object.tags + object.emojis
  79. end
  80. def atom_uri
  81. return unless object.local?
  82. OStatus::TagManager.instance.uri_for(object)
  83. end
  84. def in_reply_to_atom_uri
  85. return unless object.reply? && !object.thread.nil?
  86. OStatus::TagManager.instance.uri_for(object.thread)
  87. end
  88. def conversation
  89. return if object.conversation.nil?
  90. if object.conversation.uri?
  91. object.conversation.uri
  92. else
  93. OStatus::TagManager.instance.unique_tag(object.conversation.created_at, object.conversation.id, 'Conversation')
  94. end
  95. end
  96. def local?
  97. object.account.local?
  98. end
  99. def poll_options
  100. object.preloadable_poll.loaded_options
  101. end
  102. def poll_and_multiple?
  103. object.preloadable_poll&.multiple?
  104. end
  105. def poll_and_not_multiple?
  106. object.preloadable_poll && !object.preloadable_poll.multiple?
  107. end
  108. def closed
  109. object.preloadable_poll.expires_at.iso8601
  110. end
  111. alias end_time closed
  112. def voters_count
  113. object.preloadable_poll.voters_count
  114. end
  115. def poll_and_expires?
  116. object.preloadable_poll&.expires_at&.present?
  117. end
  118. def poll_and_expired?
  119. object.preloadable_poll&.expired?
  120. end
  121. def poll_and_voters_count?
  122. object.preloadable_poll&.voters_count
  123. end
  124. class MediaAttachmentSerializer < ActivityPub::Serializer
  125. context_extensions :blurhash, :focal_point
  126. include RoutingHelper
  127. attributes :type, :media_type, :url, :name, :blurhash
  128. attribute :focal_point, if: :focal_point?
  129. attribute :width, if: :width?
  130. attribute :height, if: :height?
  131. has_one :icon, serializer: ActivityPub::ImageSerializer, if: :thumbnail?
  132. def type
  133. 'Document'
  134. end
  135. def name
  136. object.description
  137. end
  138. def media_type
  139. object.file_content_type
  140. end
  141. def url
  142. object.local? ? full_asset_url(object.file.url(:original, false)) : object.remote_url
  143. end
  144. def focal_point?
  145. object.file.meta.is_a?(Hash) && object.file.meta['focus'].is_a?(Hash)
  146. end
  147. def focal_point
  148. [object.file.meta['focus']['x'], object.file.meta['focus']['y']]
  149. end
  150. def icon
  151. object.thumbnail
  152. end
  153. def thumbnail?
  154. object.thumbnail.present?
  155. end
  156. def width?
  157. object.file.meta&.dig('original', 'width').present?
  158. end
  159. def height?
  160. object.file.meta&.dig('original', 'height').present?
  161. end
  162. def width
  163. object.file.meta.dig('original', 'width')
  164. end
  165. def height
  166. object.file.meta.dig('original', 'height')
  167. end
  168. end
  169. class MentionSerializer < ActivityPub::Serializer
  170. attributes :type, :href, :name
  171. def type
  172. 'Mention'
  173. end
  174. def href
  175. ActivityPub::TagManager.instance.uri_for(object.account)
  176. end
  177. def name
  178. "@#{object.account.acct}"
  179. end
  180. end
  181. class TagSerializer < ActivityPub::Serializer
  182. context_extensions :hashtag
  183. include RoutingHelper
  184. attributes :type, :href, :name
  185. def type
  186. 'Hashtag'
  187. end
  188. def href
  189. tag_url(object)
  190. end
  191. def name
  192. "##{object.name}"
  193. end
  194. end
  195. class CustomEmojiSerializer < ActivityPub::EmojiSerializer
  196. end
  197. class OptionSerializer < ActivityPub::Serializer
  198. class RepliesSerializer < ActivityPub::Serializer
  199. attributes :type, :total_items
  200. def type
  201. 'Collection'
  202. end
  203. def total_items
  204. object.votes_count
  205. end
  206. end
  207. attributes :type, :name
  208. has_one :replies, serializer: ActivityPub::NoteSerializer::OptionSerializer::RepliesSerializer
  209. def type
  210. 'Note'
  211. end
  212. def name
  213. object.title
  214. end
  215. def replies
  216. object
  217. end
  218. end
  219. end