account_serializer.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # frozen_string_literal: true
  2. class REST::AccountSerializer < ActiveModel::Serializer
  3. include RoutingHelper
  4. attributes :id, :username, :acct, :display_name, :locked, :bot, :discoverable, :group, :created_at,
  5. :note, :url, :avatar, :avatar_static, :header, :header_static,
  6. :followers_count, :following_count, :statuses_count, :last_status_at
  7. has_one :moved_to_account, key: :moved, serializer: REST::AccountSerializer, if: :moved_and_not_nested?
  8. has_many :emojis, serializer: REST::CustomEmojiSerializer
  9. attribute :suspended, if: :suspended?
  10. class FieldSerializer < ActiveModel::Serializer
  11. attributes :name, :value, :verified_at
  12. def value
  13. Formatter.instance.format_field(object.account, object.value)
  14. end
  15. end
  16. has_many :fields
  17. def id
  18. object.id.to_s
  19. end
  20. def acct
  21. object.pretty_acct
  22. end
  23. def note
  24. object.suspended? ? '' : Formatter.instance.simplified_format(object)
  25. end
  26. def url
  27. ActivityPub::TagManager.instance.url_for(object)
  28. end
  29. def avatar
  30. full_asset_url(object.suspended? ? object.avatar.default_url : object.avatar_original_url)
  31. end
  32. def avatar_static
  33. full_asset_url(object.suspended? ? object.avatar.default_url : object.avatar_static_url)
  34. end
  35. def header
  36. full_asset_url(object.suspended? ? object.header.default_url : object.header_original_url)
  37. end
  38. def header_static
  39. full_asset_url(object.suspended? ? object.header.default_url : object.header_static_url)
  40. end
  41. def last_status_at
  42. object.last_status_at&.to_date&.iso8601
  43. end
  44. def display_name
  45. object.suspended? ? '' : object.display_name
  46. end
  47. def locked
  48. object.suspended? ? false : object.locked
  49. end
  50. def bot
  51. object.suspended? ? false : object.bot
  52. end
  53. def discoverable
  54. object.suspended? ? false : object.discoverable
  55. end
  56. def moved_to_account
  57. object.suspended? ? nil : object.moved_to_account
  58. end
  59. def emojis
  60. object.suspended? ? [] : object.emojis
  61. end
  62. def fields
  63. object.suspended? ? [] : object.fields
  64. end
  65. def suspended
  66. object.suspended?
  67. end
  68. delegate :suspended?, to: :object
  69. def moved_and_not_nested?
  70. object.moved? && object.moved_to_account.moved_to_account_id.nil?
  71. end
  72. end