status_serializer.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # frozen_string_literal: true
  2. class REST::StatusSerializer < ActiveModel::Serializer
  3. include FormattingHelper
  4. attributes :id, :created_at, :in_reply_to_id, :in_reply_to_account_id,
  5. :sensitive, :spoiler_text, :visibility, :language,
  6. :uri, :url, :replies_count, :reblogs_count,
  7. :favourites_count, :edited_at
  8. attribute :favourited, if: :current_user?
  9. attribute :reblogged, if: :current_user?
  10. attribute :muted, if: :current_user?
  11. attribute :bookmarked, if: :current_user?
  12. attribute :pinned, if: :pinnable?
  13. has_many :filtered, serializer: REST::FilterResultSerializer, if: :current_user?
  14. attribute :content, unless: :source_requested?
  15. attribute :text, if: :source_requested?
  16. belongs_to :reblog, serializer: REST::StatusSerializer
  17. belongs_to :application, if: :show_application?
  18. belongs_to :account, serializer: REST::AccountSerializer
  19. has_many :ordered_media_attachments, key: :media_attachments, serializer: REST::MediaAttachmentSerializer
  20. has_many :ordered_mentions, key: :mentions
  21. has_many :tags
  22. has_many :emojis, serializer: REST::CustomEmojiSerializer
  23. has_one :preview_card, key: :card, serializer: REST::PreviewCardSerializer
  24. has_one :preloadable_poll, key: :poll, serializer: REST::PollSerializer
  25. def id
  26. object.id.to_s
  27. end
  28. def in_reply_to_id
  29. object.in_reply_to_id&.to_s
  30. end
  31. def in_reply_to_account_id
  32. object.in_reply_to_account_id&.to_s
  33. end
  34. def current_user?
  35. !current_user.nil?
  36. end
  37. def show_application?
  38. object.account.user_shows_application? || (current_user? && current_user.account_id == object.account_id)
  39. end
  40. def visibility
  41. # This visibility is masked behind "private"
  42. # to avoid API changes because there are no
  43. # UX differences
  44. if object.limited_visibility?
  45. 'private'
  46. else
  47. object.visibility
  48. end
  49. end
  50. def sensitive
  51. if current_user? && current_user.account_id == object.account_id
  52. object.sensitive
  53. else
  54. object.account.sensitized? || object.sensitive
  55. end
  56. end
  57. def uri
  58. ActivityPub::TagManager.instance.uri_for(object)
  59. end
  60. def content
  61. status_content_format(object)
  62. end
  63. def url
  64. ActivityPub::TagManager.instance.url_for(object)
  65. end
  66. def reblogs_count
  67. relationships&.attributes_map&.dig(object.id, :reblogs_count) || object.reblogs_count
  68. end
  69. def favourites_count
  70. relationships&.attributes_map&.dig(object.id, :favourites_count) || object.favourites_count
  71. end
  72. def favourited
  73. if relationships
  74. relationships.favourites_map[object.id] || false
  75. else
  76. current_user.account.favourited?(object)
  77. end
  78. end
  79. def reblogged
  80. if relationships
  81. relationships.reblogs_map[object.id] || false
  82. else
  83. current_user.account.reblogged?(object)
  84. end
  85. end
  86. def muted
  87. if relationships
  88. relationships.mutes_map[object.conversation_id] || false
  89. else
  90. current_user.account.muting_conversation?(object.conversation)
  91. end
  92. end
  93. def bookmarked
  94. if relationships
  95. relationships.bookmarks_map[object.id] || false
  96. else
  97. current_user.account.bookmarked?(object)
  98. end
  99. end
  100. def pinned
  101. if relationships
  102. relationships.pins_map[object.id] || false
  103. else
  104. current_user.account.pinned?(object)
  105. end
  106. end
  107. def filtered
  108. if relationships
  109. relationships.filters_map[object.id] || []
  110. else
  111. current_user.account.status_matches_filters(object)
  112. end
  113. end
  114. def pinnable?
  115. current_user? &&
  116. current_user.account_id == object.account_id &&
  117. !object.reblog? &&
  118. %w(public unlisted private).include?(object.visibility)
  119. end
  120. def source_requested?
  121. instance_options[:source_requested]
  122. end
  123. def ordered_mentions
  124. object.active_mentions.to_a.sort_by(&:id)
  125. end
  126. private
  127. def relationships
  128. instance_options && instance_options[:relationships]
  129. end
  130. class ApplicationSerializer < ActiveModel::Serializer
  131. attributes :name, :website
  132. def website
  133. object.website.presence
  134. end
  135. end
  136. class MentionSerializer < ActiveModel::Serializer
  137. attributes :id, :username, :url, :acct
  138. def id
  139. object.account_id.to_s
  140. end
  141. def username
  142. object.account_username
  143. end
  144. def url
  145. ActivityPub::TagManager.instance.url_for(object.account)
  146. end
  147. def acct
  148. object.account.pretty_acct
  149. end
  150. end
  151. class TagSerializer < ActiveModel::Serializer
  152. include RoutingHelper
  153. attributes :name, :url
  154. def url
  155. tag_url(object)
  156. end
  157. end
  158. end