status_serializer.rb 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 favourited
  67. if instance_options && instance_options[:relationships]
  68. instance_options[:relationships].favourites_map[object.id] || false
  69. else
  70. current_user.account.favourited?(object)
  71. end
  72. end
  73. def reblogged
  74. if instance_options && instance_options[:relationships]
  75. instance_options[:relationships].reblogs_map[object.id] || false
  76. else
  77. current_user.account.reblogged?(object)
  78. end
  79. end
  80. def muted
  81. if instance_options && instance_options[:relationships]
  82. instance_options[:relationships].mutes_map[object.conversation_id] || false
  83. else
  84. current_user.account.muting_conversation?(object.conversation)
  85. end
  86. end
  87. def bookmarked
  88. if instance_options && instance_options[:relationships]
  89. instance_options[:relationships].bookmarks_map[object.id] || false
  90. else
  91. current_user.account.bookmarked?(object)
  92. end
  93. end
  94. def pinned
  95. if instance_options && instance_options[:relationships]
  96. instance_options[:relationships].pins_map[object.id] || false
  97. else
  98. current_user.account.pinned?(object)
  99. end
  100. end
  101. def filtered
  102. if instance_options && instance_options[:relationships]
  103. instance_options[:relationships].filters_map[object.id] || []
  104. else
  105. current_user.account.status_matches_filters(object)
  106. end
  107. end
  108. def pinnable?
  109. current_user? &&
  110. current_user.account_id == object.account_id &&
  111. !object.reblog? &&
  112. %w(public unlisted private).include?(object.visibility)
  113. end
  114. def source_requested?
  115. instance_options[:source_requested]
  116. end
  117. def ordered_mentions
  118. object.active_mentions.to_a.sort_by(&:id)
  119. end
  120. class ApplicationSerializer < ActiveModel::Serializer
  121. attributes :name, :website
  122. def website
  123. object.website.presence
  124. end
  125. end
  126. class MentionSerializer < ActiveModel::Serializer
  127. attributes :id, :username, :url, :acct
  128. def id
  129. object.account_id.to_s
  130. end
  131. def username
  132. object.account_username
  133. end
  134. def url
  135. ActivityPub::TagManager.instance.url_for(object.account)
  136. end
  137. def acct
  138. object.account.pretty_acct
  139. end
  140. end
  141. class TagSerializer < ActiveModel::Serializer
  142. include RoutingHelper
  143. attributes :name, :url
  144. def url
  145. tag_url(object)
  146. end
  147. end
  148. end