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.

218 lines
4.2 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, :discoverable, :olm, :suspended
  7. attributes :id, :type, :following, :followers,
  8. :inbox, :outbox, :featured, :featured_tags,
  9. :preferred_username, :name, :summary,
  10. :url, :manually_approves_followers,
  11. :discoverable, :published
  12. has_one :public_key, serializer: ActivityPub::PublicKeySerializer
  13. has_many :virtual_tags, key: :tag
  14. has_many :virtual_attachments, key: :attachment
  15. attribute :devices, unless: :instance_actor?
  16. attribute :moved_to, if: :moved?
  17. attribute :also_known_as, if: :also_known_as?
  18. attribute :suspended, if: :suspended?
  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 :suspended?, :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. object.instance_actor? ? instance_actor_outbox_url : account_outbox_url(object)
  58. end
  59. def featured
  60. account_collection_url(object, :featured)
  61. end
  62. def featured_tags
  63. account_collection_url(object, :tags)
  64. end
  65. def endpoints
  66. object
  67. end
  68. def preferred_username
  69. object.username
  70. end
  71. def discoverable
  72. object.suspended? ? false : (object.discoverable || false)
  73. end
  74. def name
  75. object.suspended? ? '' : object.display_name
  76. end
  77. def summary
  78. object.suspended? ? '' : Formatter.instance.simplified_format(object)
  79. end
  80. def icon
  81. object.avatar
  82. end
  83. def image
  84. object.header
  85. end
  86. def public_key
  87. object
  88. end
  89. def suspended
  90. object.suspended?
  91. end
  92. def url
  93. object.instance_actor? ? about_more_url(instance_actor: true) : short_account_url(object)
  94. end
  95. def avatar_exists?
  96. !object.suspended? && object.avatar?
  97. end
  98. def header_exists?
  99. !object.suspended? && object.header?
  100. end
  101. def manually_approves_followers
  102. object.suspended? ? false : object.locked
  103. end
  104. def virtual_tags
  105. object.suspended? ? [] : (object.emojis + object.tags)
  106. end
  107. def virtual_attachments
  108. object.suspended? ? [] : object.fields
  109. end
  110. def moved_to
  111. ActivityPub::TagManager.instance.uri_for(object.moved_to_account)
  112. end
  113. def moved?
  114. !object.suspended? && object.moved?
  115. end
  116. def also_known_as?
  117. !object.suspended? && !object.also_known_as.empty?
  118. end
  119. def published
  120. object.created_at.midnight.iso8601
  121. end
  122. class CustomEmojiSerializer < ActivityPub::EmojiSerializer
  123. end
  124. class TagSerializer < ActivityPub::Serializer
  125. context_extensions :hashtag
  126. include RoutingHelper
  127. attributes :type, :href, :name
  128. def type
  129. 'Hashtag'
  130. end
  131. def href
  132. tag_url(object)
  133. end
  134. def name
  135. "##{object.name}"
  136. end
  137. end
  138. class Account::FieldSerializer < ActivityPub::Serializer
  139. attributes :type, :name, :value
  140. def type
  141. 'PropertyValue'
  142. end
  143. def value
  144. Formatter.instance.format_field(object.account, object.value)
  145. end
  146. end
  147. class AccountIdentityProofSerializer < ActivityPub::Serializer
  148. attributes :type, :name, :signature_algorithm, :signature_value
  149. def type
  150. 'IdentityProof'
  151. end
  152. def name
  153. object.provider_username
  154. end
  155. def signature_algorithm
  156. object.provider
  157. end
  158. def signature_value
  159. object.token
  160. end
  161. end
  162. end