The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

139 lines
3.1 KiB

  1. # frozen_string_literal: true
  2. class REST::StatusSerializer < ActiveModel::Serializer
  3. attributes :id, :created_at, :in_reply_to_id, :in_reply_to_account_id,
  4. :sensitive, :spoiler_text, :visibility, :language,
  5. :uri, :content, :url, :replies_count, :reblogs_count,
  6. :favourites_count
  7. attribute :favourited, if: :current_user?
  8. attribute :reblogged, if: :current_user?
  9. attribute :muted, if: :current_user?
  10. attribute :pinned, if: :pinnable?
  11. belongs_to :reblog, serializer: REST::StatusSerializer
  12. belongs_to :application
  13. belongs_to :account, serializer: REST::AccountSerializer
  14. has_many :media_attachments, serializer: REST::MediaAttachmentSerializer
  15. has_many :ordered_mentions, key: :mentions
  16. has_many :tags
  17. has_many :emojis, serializer: REST::CustomEmojiSerializer
  18. def id
  19. object.id.to_s
  20. end
  21. def in_reply_to_id
  22. object.in_reply_to_id&.to_s
  23. end
  24. def in_reply_to_account_id
  25. object.in_reply_to_account_id&.to_s
  26. end
  27. def current_user?
  28. !current_user.nil?
  29. end
  30. def visibility
  31. # This visibility is masked behind "private"
  32. # to avoid API changes because there are no
  33. # UX differences
  34. if object.limited_visibility?
  35. 'private'
  36. else
  37. object.visibility
  38. end
  39. end
  40. def uri
  41. OStatus::TagManager.instance.uri_for(object)
  42. end
  43. def content
  44. Formatter.instance.format(object)
  45. end
  46. def url
  47. TagManager.instance.url_for(object)
  48. end
  49. def favourited
  50. if instance_options && instance_options[:relationships]
  51. instance_options[:relationships].favourites_map[object.id] || false
  52. else
  53. current_user.account.favourited?(object)
  54. end
  55. end
  56. def reblogged
  57. if instance_options && instance_options[:relationships]
  58. instance_options[:relationships].reblogs_map[object.id] || false
  59. else
  60. current_user.account.reblogged?(object)
  61. end
  62. end
  63. def muted
  64. if instance_options && instance_options[:relationships]
  65. instance_options[:relationships].mutes_map[object.conversation_id] || false
  66. else
  67. current_user.account.muting_conversation?(object.conversation)
  68. end
  69. end
  70. def pinned
  71. if instance_options && instance_options[:relationships]
  72. instance_options[:relationships].pins_map[object.id] || false
  73. else
  74. current_user.account.pinned?(object)
  75. end
  76. end
  77. def pinnable?
  78. current_user? &&
  79. current_user.account_id == object.account_id &&
  80. !object.reblog? &&
  81. %w(public unlisted).include?(object.visibility)
  82. end
  83. def ordered_mentions
  84. object.active_mentions.to_a.sort_by(&:id)
  85. end
  86. class ApplicationSerializer < ActiveModel::Serializer
  87. attributes :name, :website
  88. end
  89. class MentionSerializer < ActiveModel::Serializer
  90. attributes :id, :username, :url, :acct
  91. def id
  92. object.account_id.to_s
  93. end
  94. def username
  95. object.account_username
  96. end
  97. def url
  98. TagManager.instance.url_for(object.account)
  99. end
  100. def acct
  101. object.account_acct
  102. end
  103. end
  104. class TagSerializer < ActiveModel::Serializer
  105. include RoutingHelper
  106. attributes :name, :url
  107. def url
  108. tag_url(object)
  109. end
  110. end
  111. end