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.
 
 
 
 

251 lines
5.2 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::NoteSerializer < ActivityPub::Serializer
  3. context_extensions :atom_uri, :conversation, :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. :conversation
  10. attribute :content
  11. attribute :content_map, if: :language?
  12. has_many :media_attachments, key: :attachment
  13. has_many :virtual_tags, key: :tag
  14. has_one :replies, serializer: ActivityPub::CollectionSerializer, if: :local?
  15. has_many :poll_options, key: :one_of, if: :poll_and_not_multiple?
  16. has_many :poll_options, key: :any_of, if: :poll_and_multiple?
  17. attribute :end_time, if: :poll_and_expires?
  18. attribute :closed, if: :poll_and_expired?
  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) : nil
  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 virtual_tags
  75. object.active_mentions.to_a.sort_by(&:id) + object.tags + object.emojis
  76. end
  77. def atom_uri
  78. return unless object.local?
  79. OStatus::TagManager.instance.uri_for(object)
  80. end
  81. def in_reply_to_atom_uri
  82. return unless object.reply? && !object.thread.nil?
  83. OStatus::TagManager.instance.uri_for(object.thread)
  84. end
  85. def conversation
  86. return if object.conversation.nil?
  87. if object.conversation.uri?
  88. object.conversation.uri
  89. else
  90. OStatus::TagManager.instance.unique_tag(object.conversation.created_at, object.conversation.id, 'Conversation')
  91. end
  92. end
  93. def local?
  94. object.account.local?
  95. end
  96. def poll_options
  97. object.preloadable_poll.loaded_options
  98. end
  99. def poll_and_multiple?
  100. object.preloadable_poll&.multiple?
  101. end
  102. def poll_and_not_multiple?
  103. object.preloadable_poll && !object.preloadable_poll.multiple?
  104. end
  105. def closed
  106. object.preloadable_poll.expires_at.iso8601
  107. end
  108. alias end_time closed
  109. def poll_and_expires?
  110. object.preloadable_poll&.expires_at&.present?
  111. end
  112. def poll_and_expired?
  113. object.preloadable_poll&.expired?
  114. end
  115. class MediaAttachmentSerializer < ActivityPub::Serializer
  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. include RoutingHelper
  152. attributes :type, :href, :name
  153. def type
  154. 'Hashtag'
  155. end
  156. def href
  157. tag_url(object)
  158. end
  159. def name
  160. "##{object.name}"
  161. end
  162. end
  163. class CustomEmojiSerializer < ActivityPub::EmojiSerializer
  164. end
  165. class OptionSerializer < ActivityPub::Serializer
  166. class RepliesSerializer < ActivityPub::Serializer
  167. attributes :type, :total_items
  168. def type
  169. 'Collection'
  170. end
  171. def total_items
  172. object.votes_count
  173. end
  174. end
  175. attributes :type, :name
  176. has_one :replies, serializer: ActivityPub::NoteSerializer::OptionSerializer::RepliesSerializer
  177. def type
  178. 'Note'
  179. end
  180. def name
  181. object.title
  182. end
  183. def replies
  184. object
  185. end
  186. end
  187. end