account_serializer.rb 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 FieldSerializer < ActiveModel::Serializer
  22. include FormattingHelper
  23. attributes :name, :value, :verified_at
  24. def value
  25. account_field_value_format(object)
  26. end
  27. end
  28. has_many :fields
  29. def id
  30. object.id.to_s
  31. end
  32. def acct
  33. object.pretty_acct
  34. end
  35. def note
  36. object.suspended? ? '' : account_bio_format(object)
  37. end
  38. def url
  39. ActivityPub::TagManager.instance.url_for(object)
  40. end
  41. def avatar
  42. full_asset_url(object.suspended? ? object.avatar.default_url : object.avatar_original_url)
  43. end
  44. def avatar_static
  45. full_asset_url(object.suspended? ? object.avatar.default_url : object.avatar_static_url)
  46. end
  47. def header
  48. full_asset_url(object.suspended? ? object.header.default_url : object.header_original_url)
  49. end
  50. def header_static
  51. full_asset_url(object.suspended? ? object.header.default_url : object.header_static_url)
  52. end
  53. def created_at
  54. object.created_at.midnight.as_json
  55. end
  56. def last_status_at
  57. object.last_status_at&.to_date&.iso8601
  58. end
  59. def display_name
  60. object.suspended? ? '' : object.display_name
  61. end
  62. def locked
  63. object.suspended? ? false : object.locked
  64. end
  65. def bot
  66. object.suspended? ? false : object.bot
  67. end
  68. def discoverable
  69. object.suspended? ? false : object.discoverable
  70. end
  71. def moved_to_account
  72. object.suspended? ? nil : AccountDecorator.new(object.moved_to_account)
  73. end
  74. def emojis
  75. object.suspended? ? [] : object.emojis
  76. end
  77. def fields
  78. object.suspended? ? [] : object.fields
  79. end
  80. def suspended
  81. object.suspended?
  82. end
  83. def silenced
  84. object.silenced?
  85. end
  86. def noindex
  87. object.user_prefers_noindex?
  88. end
  89. delegate :suspended?, :silenced?, :local?, to: :object
  90. def moved_and_not_nested?
  91. object.moved?
  92. end
  93. end