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.

129 lines
2.3 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. has_many :media_attachments, key: :attachment
  7. has_many :virtual_tags, key: :tag
  8. attribute :atom_uri, key: '_:atomUri', if: :local?
  9. attribute :in_reply_to_atom_uri, key: '_:inReplyToAtomUri'
  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. ::TagManager.instance.uri_for(object)
  50. end
  51. def in_reply_to_atom_uri
  52. return unless object.reply?
  53. ::TagManager.instance.uri_for(object.thread)
  54. end
  55. def local?
  56. object.account.local?
  57. end
  58. class MediaAttachmentSerializer < ActiveModel::Serializer
  59. include RoutingHelper
  60. attributes :type, :media_type, :url
  61. def type
  62. 'Document'
  63. end
  64. def media_type
  65. object.file_content_type
  66. end
  67. def url
  68. object.local? ? full_asset_url(object.file.url(:original, false)) : object.remote_url
  69. end
  70. end
  71. class MentionSerializer < ActiveModel::Serializer
  72. attributes :type, :href, :name
  73. def type
  74. 'Mention'
  75. end
  76. def href
  77. ActivityPub::TagManager.instance.uri_for(object.account)
  78. end
  79. def name
  80. "@#{object.account.acct}"
  81. end
  82. end
  83. class TagSerializer < ActiveModel::Serializer
  84. include RoutingHelper
  85. attributes :type, :href, :name
  86. def type
  87. 'Hashtag'
  88. end
  89. def href
  90. tag_url(object)
  91. end
  92. def name
  93. "##{object.name}"
  94. end
  95. end
  96. end