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.

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