闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

198 lines
3.6 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::ActorSerializer < ActivityPub::Serializer
  3. include RoutingHelper
  4. context :security
  5. context_extensions :manually_approves_followers, :featured, :also_known_as,
  6. :moved_to, :property_value, :identity_proof,
  7. :discoverable, :olm
  8. attributes :id, :type, :following, :followers,
  9. :inbox, :outbox, :featured,
  10. :preferred_username, :name, :summary,
  11. :url, :manually_approves_followers,
  12. :discoverable
  13. has_one :public_key, serializer: ActivityPub::PublicKeySerializer
  14. has_many :virtual_tags, key: :tag
  15. has_many :virtual_attachments, key: :attachment
  16. attribute :devices, unless: :instance_actor?
  17. attribute :moved_to, if: :moved?
  18. attribute :also_known_as, if: :also_known_as?
  19. class EndpointsSerializer < ActivityPub::Serializer
  20. include RoutingHelper
  21. attributes :shared_inbox
  22. def shared_inbox
  23. inbox_url
  24. end
  25. end
  26. has_one :endpoints, serializer: EndpointsSerializer
  27. has_one :icon, serializer: ActivityPub::ImageSerializer, if: :avatar_exists?
  28. has_one :image, serializer: ActivityPub::ImageSerializer, if: :header_exists?
  29. delegate :moved?, :instance_actor?, to: :object
  30. def id
  31. object.instance_actor? ? instance_actor_url : account_url(object)
  32. end
  33. def type
  34. if object.instance_actor?
  35. 'Application'
  36. elsif object.bot?
  37. 'Service'
  38. elsif object.group?
  39. 'Group'
  40. else
  41. 'Person'
  42. end
  43. end
  44. def following
  45. account_following_index_url(object)
  46. end
  47. def followers
  48. account_followers_url(object)
  49. end
  50. def inbox
  51. object.instance_actor? ? instance_actor_inbox_url : account_inbox_url(object)
  52. end
  53. def devices
  54. account_collection_url(object, :devices)
  55. end
  56. def outbox
  57. account_outbox_url(object)
  58. end
  59. def featured
  60. account_collection_url(object, :featured)
  61. end
  62. def endpoints
  63. object
  64. end
  65. def preferred_username
  66. object.username
  67. end
  68. def name
  69. object.display_name
  70. end
  71. def summary
  72. Formatter.instance.simplified_format(object)
  73. end
  74. def icon
  75. object.avatar
  76. end
  77. def image
  78. object.header
  79. end
  80. def public_key
  81. object
  82. end
  83. def url
  84. object.instance_actor? ? about_more_url(instance_actor: true) : short_account_url(object)
  85. end
  86. def avatar_exists?
  87. object.avatar?
  88. end
  89. def header_exists?
  90. object.header?
  91. end
  92. def manually_approves_followers
  93. object.locked
  94. end
  95. def virtual_tags
  96. object.emojis + object.tags
  97. end
  98. def virtual_attachments
  99. object.fields + object.identity_proofs.active
  100. end
  101. def moved_to
  102. ActivityPub::TagManager.instance.uri_for(object.moved_to_account)
  103. end
  104. def also_known_as?
  105. !object.also_known_as.empty?
  106. end
  107. class CustomEmojiSerializer < ActivityPub::EmojiSerializer
  108. end
  109. class TagSerializer < ActivityPub::Serializer
  110. context_extensions :hashtag
  111. include RoutingHelper
  112. attributes :type, :href, :name
  113. def type
  114. 'Hashtag'
  115. end
  116. def href
  117. explore_hashtag_url(object)
  118. end
  119. def name
  120. "##{object.name}"
  121. end
  122. end
  123. class Account::FieldSerializer < ActivityPub::Serializer
  124. attributes :type, :name, :value
  125. def type
  126. 'PropertyValue'
  127. end
  128. def value
  129. Formatter.instance.format_field(object.account, object.value)
  130. end
  131. end
  132. class AccountIdentityProofSerializer < ActivityPub::Serializer
  133. attributes :type, :name, :signature_algorithm, :signature_value
  134. def type
  135. 'IdentityProof'
  136. end
  137. def name
  138. object.provider_username
  139. end
  140. def signature_algorithm
  141. object.provider
  142. end
  143. def signature_value
  144. object.token
  145. end
  146. end
  147. end