actor_serializer.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # frozen_string_literal: true
  2. class ActivityPub::ActorSerializer < ActivityPub::Serializer
  3. include RoutingHelper
  4. include FormattingHelper
  5. context :security
  6. context_extensions :manually_approves_followers, :featured, :also_known_as,
  7. :moved_to, :property_value, :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? ? '' : account_bio_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
  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. tag_url(object)
  134. end
  135. def name
  136. "##{object.name}"
  137. end
  138. end
  139. class Account::FieldSerializer < ActivityPub::Serializer
  140. include FormattingHelper
  141. attributes :type, :name, :value
  142. def type
  143. 'PropertyValue'
  144. end
  145. def value
  146. account_field_value_format(object)
  147. end
  148. end
  149. class AccountIdentityProofSerializer < ActivityPub::Serializer
  150. attributes :type, :name, :signature_algorithm, :signature_value
  151. def type
  152. 'IdentityProof'
  153. end
  154. def name
  155. object.provider_username
  156. end
  157. def signature_algorithm
  158. object.provider
  159. end
  160. def signature_value
  161. object.token
  162. end
  163. end
  164. end