status_serializer.rb 4.2 KB

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