account_serializer.rb 2.5 KB

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