status_serializer.rb 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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, :content, :url, :reblogs_count, :favourites_count
  6. attribute :favourited, if: :current_user?
  7. attribute :reblogged, if: :current_user?
  8. attribute :muted, if: :current_user?
  9. attribute :pinned, if: :pinnable?
  10. belongs_to :reblog, serializer: REST::StatusSerializer
  11. belongs_to :application
  12. belongs_to :account, serializer: REST::AccountSerializer
  13. has_many :media_attachments, serializer: REST::MediaAttachmentSerializer
  14. has_many :mentions
  15. has_many :tags
  16. has_many :emojis, serializer: REST::CustomEmojiSerializer
  17. def id
  18. object.id.to_s
  19. end
  20. def in_reply_to_id
  21. object.in_reply_to_id&.to_s
  22. end
  23. def in_reply_to_account_id
  24. object.in_reply_to_account_id&.to_s
  25. end
  26. def current_user?
  27. !current_user.nil?
  28. end
  29. def uri
  30. OStatus::TagManager.instance.uri_for(object)
  31. end
  32. def content
  33. Formatter.instance.format(object)
  34. end
  35. def url
  36. TagManager.instance.url_for(object)
  37. end
  38. def favourited
  39. if instance_options && instance_options[:relationships]
  40. instance_options[:relationships].favourites_map[object.id] || false
  41. else
  42. current_user.account.favourited?(object)
  43. end
  44. end
  45. def reblogged
  46. if instance_options && instance_options[:relationships]
  47. instance_options[:relationships].reblogs_map[object.id] || false
  48. else
  49. current_user.account.reblogged?(object)
  50. end
  51. end
  52. def muted
  53. if instance_options && instance_options[:relationships]
  54. instance_options[:relationships].mutes_map[object.conversation_id] || false
  55. else
  56. current_user.account.muting_conversation?(object.conversation)
  57. end
  58. end
  59. def pinned
  60. if instance_options && instance_options[:relationships]
  61. instance_options[:relationships].pins_map[object.id] || false
  62. else
  63. current_user.account.pinned?(object)
  64. end
  65. end
  66. def pinnable?
  67. current_user? &&
  68. current_user.account_id == object.account_id &&
  69. !object.reblog? &&
  70. %w(public unlisted).include?(object.visibility)
  71. end
  72. class ApplicationSerializer < ActiveModel::Serializer
  73. attributes :name, :website
  74. end
  75. class MentionSerializer < ActiveModel::Serializer
  76. attributes :id, :username, :url, :acct
  77. def id
  78. object.account_id.to_s
  79. end
  80. def username
  81. object.account_username
  82. end
  83. def url
  84. TagManager.instance.url_for(object.account)
  85. end
  86. def acct
  87. object.account_acct
  88. end
  89. end
  90. class TagSerializer < ActiveModel::Serializer
  91. include RoutingHelper
  92. attributes :name, :url
  93. def url
  94. tag_url(object)
  95. end
  96. end
  97. end