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.

122 lines
2.1 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. def id
  10. ActivityPub::TagManager.instance.uri_for(object)
  11. end
  12. def type
  13. 'Note'
  14. end
  15. def summary
  16. object.spoiler_text.presence
  17. end
  18. def content
  19. Formatter.instance.format(object)
  20. end
  21. def in_reply_to
  22. return unless object.reply?
  23. if object.thread.uri.nil? || object.thread.uri.start_with?('http')
  24. ActivityPub::TagManager.instance.uri_for(object.thread)
  25. else
  26. object.thread.url
  27. end
  28. end
  29. def published
  30. object.created_at.iso8601
  31. end
  32. def url
  33. ActivityPub::TagManager.instance.url_for(object)
  34. end
  35. def attributed_to
  36. ActivityPub::TagManager.instance.uri_for(object.account)
  37. end
  38. def to
  39. ActivityPub::TagManager.instance.to(object)
  40. end
  41. def cc
  42. ActivityPub::TagManager.instance.cc(object)
  43. end
  44. def virtual_tags
  45. object.mentions + object.tags
  46. end
  47. def atom_uri
  48. ::TagManager.instance.uri_for(object)
  49. end
  50. def local?
  51. object.account.local?
  52. end
  53. class MediaAttachmentSerializer < ActiveModel::Serializer
  54. include RoutingHelper
  55. attributes :type, :media_type, :url
  56. def type
  57. 'Document'
  58. end
  59. def media_type
  60. object.file_content_type
  61. end
  62. def url
  63. object.local? ? full_asset_url(object.file.url(:original, false)) : object.remote_url
  64. end
  65. end
  66. class MentionSerializer < ActiveModel::Serializer
  67. attributes :type, :href, :name
  68. def type
  69. 'Mention'
  70. end
  71. def href
  72. ActivityPub::TagManager.instance.uri_for(object.account)
  73. end
  74. def name
  75. "@#{object.account.acct}"
  76. end
  77. end
  78. class TagSerializer < ActiveModel::Serializer
  79. include RoutingHelper
  80. attributes :type, :href, :name
  81. def type
  82. 'Hashtag'
  83. end
  84. def href
  85. tag_url(object)
  86. end
  87. def name
  88. "##{object.name}"
  89. end
  90. end
  91. end