status_serializer.rb 4.0 KB

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