闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
2.5 KiB

  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. def current_user?
  17. !current_user.nil?
  18. end
  19. def uri
  20. TagManager.instance.uri_for(object)
  21. end
  22. def content
  23. Formatter.instance.format(object)
  24. end
  25. def url
  26. TagManager.instance.url_for(object)
  27. end
  28. def favourited
  29. if instance_options && instance_options[:relationships]
  30. instance_options[:relationships].favourites_map[object.id] || false
  31. else
  32. current_user.account.favourited?(object)
  33. end
  34. end
  35. def reblogged
  36. if instance_options && instance_options[:relationships]
  37. instance_options[:relationships].reblogs_map[object.id] || false
  38. else
  39. current_user.account.reblogged?(object)
  40. end
  41. end
  42. def muted
  43. if instance_options && instance_options[:relationships]
  44. instance_options[:relationships].mutes_map[object.conversation_id] || false
  45. else
  46. current_user.account.muting_conversation?(object.conversation)
  47. end
  48. end
  49. def pinned
  50. if instance_options && instance_options[:relationships]
  51. instance_options[:relationships].pins_map[object.id] || false
  52. else
  53. current_user.account.pinned?(object)
  54. end
  55. end
  56. def pinnable?
  57. current_user? &&
  58. current_user.account_id == object.account_id &&
  59. !object.reblog? &&
  60. %w(public unlisted).include?(object.visibility)
  61. end
  62. class ApplicationSerializer < ActiveModel::Serializer
  63. attributes :name, :website
  64. end
  65. class MentionSerializer < ActiveModel::Serializer
  66. attributes :id, :username, :url, :acct
  67. def id
  68. object.account_id
  69. end
  70. def username
  71. object.account_username
  72. end
  73. def url
  74. TagManager.instance.url_for(object.account)
  75. end
  76. def acct
  77. object.account_acct
  78. end
  79. end
  80. class TagSerializer < ActiveModel::Serializer
  81. include RoutingHelper
  82. attributes :name, :url
  83. def url
  84. tag_url(object)
  85. end
  86. end
  87. end