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.

184 lines
3.8 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::NoteSerializer < ActiveModel::Serializer
  3. attributes :id, :type, :summary,
  4. :in_reply_to, :published, :url,
  5. :attributed_to, :to, :cc, :sensitive,
  6. :atom_uri, :in_reply_to_atom_uri,
  7. :conversation
  8. attribute :content
  9. attribute :content_map, if: :language?
  10. has_many :media_attachments, key: :attachment
  11. has_many :virtual_tags, key: :tag
  12. has_one :replies, serializer: ActivityPub::CollectionSerializer, if: :local?
  13. def id
  14. ActivityPub::TagManager.instance.uri_for(object)
  15. end
  16. def type
  17. 'Note'
  18. end
  19. def summary
  20. object.spoiler_text.presence
  21. end
  22. def content
  23. Formatter.instance.format(object)
  24. end
  25. def content_map
  26. { object.language => Formatter.instance.format(object) }
  27. end
  28. def replies
  29. replies = object.self_replies(5).pluck(:id, :uri)
  30. last_id = replies.last&.first
  31. ActivityPub::CollectionPresenter.new(
  32. type: :unordered,
  33. id: ActivityPub::TagManager.instance.replies_uri_for(object),
  34. first: ActivityPub::CollectionPresenter.new(
  35. type: :unordered,
  36. part_of: ActivityPub::TagManager.instance.replies_uri_for(object),
  37. items: replies.map(&:second),
  38. next: last_id ? ActivityPub::TagManager.instance.replies_uri_for(object, page: true, min_id: last_id) : nil
  39. )
  40. )
  41. end
  42. def language?
  43. object.language.present?
  44. end
  45. def in_reply_to
  46. return unless object.reply? && !object.thread.nil?
  47. if object.thread.uri.nil? || object.thread.uri.start_with?('http')
  48. ActivityPub::TagManager.instance.uri_for(object.thread)
  49. else
  50. object.thread.url
  51. end
  52. end
  53. def published
  54. object.created_at.iso8601
  55. end
  56. def url
  57. ActivityPub::TagManager.instance.url_for(object)
  58. end
  59. def attributed_to
  60. ActivityPub::TagManager.instance.uri_for(object.account)
  61. end
  62. def to
  63. ActivityPub::TagManager.instance.to(object)
  64. end
  65. def cc
  66. ActivityPub::TagManager.instance.cc(object)
  67. end
  68. def virtual_tags
  69. object.active_mentions.to_a.sort_by(&:id) + object.tags + object.emojis
  70. end
  71. def atom_uri
  72. return unless object.local?
  73. OStatus::TagManager.instance.uri_for(object)
  74. end
  75. def in_reply_to_atom_uri
  76. return unless object.reply? && !object.thread.nil?
  77. OStatus::TagManager.instance.uri_for(object.thread)
  78. end
  79. def conversation
  80. return if object.conversation.nil?
  81. if object.conversation.uri?
  82. object.conversation.uri
  83. else
  84. OStatus::TagManager.instance.unique_tag(object.conversation.created_at, object.conversation.id, 'Conversation')
  85. end
  86. end
  87. def local?
  88. object.account.local?
  89. end
  90. class MediaAttachmentSerializer < ActiveModel::Serializer
  91. include RoutingHelper
  92. attributes :type, :media_type, :url, :name
  93. attribute :focal_point, if: :focal_point?
  94. def type
  95. 'Document'
  96. end
  97. def name
  98. object.description
  99. end
  100. def media_type
  101. object.file_content_type
  102. end
  103. def url
  104. object.local? ? full_asset_url(object.file.url(:original, false)) : object.remote_url
  105. end
  106. def focal_point?
  107. object.file.meta.is_a?(Hash) && object.file.meta['focus'].is_a?(Hash)
  108. end
  109. def focal_point
  110. [object.file.meta['focus']['x'], object.file.meta['focus']['y']]
  111. end
  112. end
  113. class MentionSerializer < ActiveModel::Serializer
  114. attributes :type, :href, :name
  115. def type
  116. 'Mention'
  117. end
  118. def href
  119. ActivityPub::TagManager.instance.uri_for(object.account)
  120. end
  121. def name
  122. "@#{object.account.acct}"
  123. end
  124. end
  125. class TagSerializer < ActiveModel::Serializer
  126. include RoutingHelper
  127. attributes :type, :href, :name
  128. def type
  129. 'Hashtag'
  130. end
  131. def href
  132. tag_url(object)
  133. end
  134. def name
  135. "##{object.name}"
  136. end
  137. end
  138. class CustomEmojiSerializer < ActivityPub::EmojiSerializer
  139. end
  140. end