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.

253 lines
5.3 KiB

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