account_serializer.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # frozen_string_literal: true
  2. class REST::AccountSerializer < ActiveModel::Serializer
  3. include RoutingHelper
  4. include FormattingHelper
  5. # Please update `app/javascript/mastodon/api_types/accounts.ts` when making changes to the attributes
  6. attributes :id, :username, :acct, :display_name, :locked, :bot, :discoverable, :indexable, :group, :created_at,
  7. :note, :url, :uri, :avatar, :avatar_static, :header, :header_static,
  8. :followers_count, :following_count, :statuses_count, :last_status_at, :hide_collections
  9. has_one :moved_to_account, key: :moved, serializer: REST::AccountSerializer, if: :moved_and_not_nested?
  10. has_many :emojis, serializer: REST::CustomEmojiSerializer
  11. attribute :suspended, if: :suspended?
  12. attribute :silenced, key: :limited, if: :silenced?
  13. attribute :noindex, if: :local?
  14. attribute :memorial, if: :memorial?
  15. class AccountDecorator < SimpleDelegator
  16. def self.model_name
  17. Account.model_name
  18. end
  19. def moved?
  20. false
  21. end
  22. end
  23. class RoleSerializer < ActiveModel::Serializer
  24. attributes :id, :name, :color
  25. def id
  26. object.id.to_s
  27. end
  28. end
  29. has_many :roles, serializer: RoleSerializer, if: :local?
  30. class FieldSerializer < ActiveModel::Serializer
  31. include FormattingHelper
  32. attributes :name, :value, :verified_at
  33. def value
  34. account_field_value_format(object)
  35. end
  36. end
  37. has_many :fields
  38. def id
  39. object.id.to_s
  40. end
  41. def acct
  42. object.pretty_acct
  43. end
  44. def note
  45. object.unavailable? ? '' : account_bio_format(object)
  46. end
  47. def url
  48. ActivityPub::TagManager.instance.url_for(object)
  49. end
  50. def uri
  51. ActivityPub::TagManager.instance.uri_for(object)
  52. end
  53. def avatar
  54. full_asset_url(object.unavailable? ? object.avatar.default_url : object.avatar_original_url)
  55. end
  56. def avatar_static
  57. full_asset_url(object.unavailable? ? object.avatar.default_url : object.avatar_static_url)
  58. end
  59. def header
  60. full_asset_url(object.unavailable? ? object.header.default_url : object.header_original_url)
  61. end
  62. def header_static
  63. full_asset_url(object.unavailable? ? object.header.default_url : object.header_static_url)
  64. end
  65. def created_at
  66. object.created_at.midnight.as_json
  67. end
  68. def last_status_at
  69. object.last_status_at&.to_date&.iso8601
  70. end
  71. def display_name
  72. object.unavailable? ? '' : object.display_name
  73. end
  74. def locked
  75. object.unavailable? ? false : object.locked
  76. end
  77. def bot
  78. object.unavailable? ? false : object.bot
  79. end
  80. def discoverable
  81. object.unavailable? ? false : object.discoverable
  82. end
  83. def indexable
  84. object.unavailable? ? false : object.indexable
  85. end
  86. def moved_to_account
  87. object.unavailable? ? nil : AccountDecorator.new(object.moved_to_account)
  88. end
  89. def emojis
  90. object.unavailable? ? [] : object.emojis
  91. end
  92. def fields
  93. object.unavailable? ? [] : object.fields
  94. end
  95. def suspended
  96. object.unavailable?
  97. end
  98. def silenced
  99. object.silenced?
  100. end
  101. def memorial
  102. object.memorial?
  103. end
  104. def roles
  105. if object.unavailable? || object.user.nil?
  106. []
  107. else
  108. [object.user.role].compact.filter(&:highlighted?)
  109. end
  110. end
  111. def noindex
  112. object.user_prefers_noindex?
  113. end
  114. delegate :suspended?, :silenced?, :local?, :memorial?, to: :object
  115. def moved_and_not_nested?
  116. object.moved?
  117. end
  118. end