actor_serializer.rb 4.4 KB

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