status_serializer.rb 4.6 KB

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