account_serializer.rb 2.9 KB

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