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.

277 lines
5.8 KiB

  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. has_one :icon, serializer: ActivityPub::ImageSerializer, if: :thumbnail?
  130. def type
  131. 'Document'
  132. end
  133. def name
  134. object.description
  135. end
  136. def media_type
  137. object.file_content_type
  138. end
  139. def url
  140. object.local? ? full_asset_url(object.file.url(:original, false)) : object.remote_url
  141. end
  142. def focal_point?
  143. object.file.meta.is_a?(Hash) && object.file.meta['focus'].is_a?(Hash)
  144. end
  145. def focal_point
  146. [object.file.meta['focus']['x'], object.file.meta['focus']['y']]
  147. end
  148. def icon
  149. object.thumbnail
  150. end
  151. def thumbnail?
  152. object.thumbnail.present?
  153. end
  154. end
  155. class MentionSerializer < ActivityPub::Serializer
  156. attributes :type, :href, :name
  157. def type
  158. 'Mention'
  159. end
  160. def href
  161. ActivityPub::TagManager.instance.uri_for(object.account)
  162. end
  163. def name
  164. "@#{object.account.acct}"
  165. end
  166. end
  167. class TagSerializer < ActivityPub::Serializer
  168. context_extensions :hashtag
  169. include RoutingHelper
  170. attributes :type, :href, :name
  171. def type
  172. 'Hashtag'
  173. end
  174. def href
  175. tag_url(object)
  176. end
  177. def name
  178. "##{object.name}"
  179. end
  180. end
  181. class CustomEmojiSerializer < ActivityPub::EmojiSerializer
  182. end
  183. class OptionSerializer < ActivityPub::Serializer
  184. class RepliesSerializer < ActivityPub::Serializer
  185. attributes :type, :total_items
  186. def type
  187. 'Collection'
  188. end
  189. def total_items
  190. object.votes_count
  191. end
  192. end
  193. attributes :type, :name
  194. has_one :replies, serializer: ActivityPub::NoteSerializer::OptionSerializer::RepliesSerializer
  195. def type
  196. 'Note'
  197. end
  198. def name
  199. object.title
  200. end
  201. def replies
  202. object
  203. end
  204. end
  205. end