The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

240 lines
4.9 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::NoteSerializer < ActivityPub::Serializer
  3. context_extensions :atom_uri, :sensitive,
  4. :hashtag, :emoji, :focal_point, :blurhash
  5. attributes :id, :type, :summary,
  6. :in_reply_to, :published, :url,
  7. :attributed_to, :to, :cc, :sensitive,
  8. :atom_uri, :in_reply_to_atom_uri
  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) : nil
  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 local?
  85. object.account.local?
  86. end
  87. def poll_options
  88. object.preloadable_poll.loaded_options
  89. end
  90. def poll_and_multiple?
  91. object.preloadable_poll&.multiple?
  92. end
  93. def poll_and_not_multiple?
  94. object.preloadable_poll && !object.preloadable_poll.multiple?
  95. end
  96. def closed
  97. object.preloadable_poll.expires_at.iso8601
  98. end
  99. alias end_time closed
  100. def poll_and_expires?
  101. object.preloadable_poll&.expires_at&.present?
  102. end
  103. def poll_and_expired?
  104. object.preloadable_poll&.expired?
  105. end
  106. class MediaAttachmentSerializer < ActivityPub::Serializer
  107. include RoutingHelper
  108. attributes :type, :media_type, :url, :name, :blurhash
  109. attribute :focal_point, if: :focal_point?
  110. def type
  111. 'Document'
  112. end
  113. def name
  114. object.description
  115. end
  116. def media_type
  117. object.file_content_type
  118. end
  119. def url
  120. object.local? ? full_asset_url(object.file.url(:original, false)) : object.remote_url
  121. end
  122. def focal_point?
  123. object.file.meta.is_a?(Hash) && object.file.meta['focus'].is_a?(Hash)
  124. end
  125. def focal_point
  126. [object.file.meta['focus']['x'], object.file.meta['focus']['y']]
  127. end
  128. end
  129. class MentionSerializer < ActivityPub::Serializer
  130. attributes :type, :href, :name
  131. def type
  132. 'Mention'
  133. end
  134. def href
  135. ActivityPub::TagManager.instance.uri_for(object.account)
  136. end
  137. def name
  138. "@#{object.account.acct}"
  139. end
  140. end
  141. class TagSerializer < ActivityPub::Serializer
  142. include RoutingHelper
  143. attributes :type, :href, :name
  144. def type
  145. 'Hashtag'
  146. end
  147. def href
  148. tag_url(object)
  149. end
  150. def name
  151. "##{object.name}"
  152. end
  153. end
  154. class CustomEmojiSerializer < ActivityPub::EmojiSerializer
  155. end
  156. class OptionSerializer < ActivityPub::Serializer
  157. class RepliesSerializer < ActivityPub::Serializer
  158. attributes :type, :total_items
  159. def type
  160. 'Collection'
  161. end
  162. def total_items
  163. object.votes_count
  164. end
  165. end
  166. attributes :type, :name
  167. has_one :replies, serializer: ActivityPub::NoteSerializer::OptionSerializer::RepliesSerializer
  168. def type
  169. 'Note'
  170. end
  171. def name
  172. object.title
  173. end
  174. def replies
  175. object
  176. end
  177. end
  178. end