actor_serializer.rb 4.4 KB

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