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.

138 lines
2.5 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::NoteSerializer < ActiveModel::Serializer
  3. attributes :id, :type, :summary, :content,
  4. :in_reply_to, :published, :url,
  5. :attributed_to, :to, :cc, :sensitive,
  6. :atom_uri, :in_reply_to_atom_uri,
  7. :conversation
  8. has_many :media_attachments, key: :attachment
  9. has_many :virtual_tags, key: :tag
  10. def id
  11. ActivityPub::TagManager.instance.uri_for(object)
  12. end
  13. def type
  14. 'Note'
  15. end
  16. def summary
  17. object.spoiler_text.presence
  18. end
  19. def content
  20. Formatter.instance.format(object)
  21. end
  22. def in_reply_to
  23. return unless object.reply?
  24. if object.thread.uri.nil? || object.thread.uri.start_with?('http')
  25. ActivityPub::TagManager.instance.uri_for(object.thread)
  26. else
  27. object.thread.url
  28. end
  29. end
  30. def published
  31. object.created_at.iso8601
  32. end
  33. def url
  34. ActivityPub::TagManager.instance.url_for(object)
  35. end
  36. def attributed_to
  37. ActivityPub::TagManager.instance.uri_for(object.account)
  38. end
  39. def to
  40. ActivityPub::TagManager.instance.to(object)
  41. end
  42. def cc
  43. ActivityPub::TagManager.instance.cc(object)
  44. end
  45. def virtual_tags
  46. object.mentions + object.tags
  47. end
  48. def atom_uri
  49. return unless object.local?
  50. ::TagManager.instance.uri_for(object)
  51. end
  52. def in_reply_to_atom_uri
  53. return unless object.reply?
  54. ::TagManager.instance.uri_for(object.thread)
  55. end
  56. def conversation
  57. if object.conversation.uri?
  58. object.conversation.uri
  59. else
  60. TagManager.instance.unique_tag(object.conversation.created_at, object.conversation.id, 'Conversation')
  61. end
  62. end
  63. def local?
  64. object.account.local?
  65. end
  66. class MediaAttachmentSerializer < ActiveModel::Serializer
  67. include RoutingHelper
  68. attributes :type, :media_type, :url
  69. def type
  70. 'Document'
  71. end
  72. def media_type
  73. object.file_content_type
  74. end
  75. def url
  76. object.local? ? full_asset_url(object.file.url(:original, false)) : object.remote_url
  77. end
  78. end
  79. class MentionSerializer < ActiveModel::Serializer
  80. attributes :type, :href, :name
  81. def type
  82. 'Mention'
  83. end
  84. def href
  85. ActivityPub::TagManager.instance.uri_for(object.account)
  86. end
  87. def name
  88. "@#{object.account.acct}"
  89. end
  90. end
  91. class TagSerializer < ActiveModel::Serializer
  92. include RoutingHelper
  93. attributes :type, :href, :name
  94. def type
  95. 'Hashtag'
  96. end
  97. def href
  98. tag_url(object)
  99. end
  100. def name
  101. "##{object.name}"
  102. end
  103. end
  104. end