gab-social/app/serializers/rest/status_serializer.rb

178 lines
3.8 KiB
Ruby
Raw Normal View History

2019-07-02 08:10:25 +01:00
# frozen_string_literal: true
class REST::StatusSerializer < ActiveModel::Serializer
2019-09-17 15:22:02 +01:00
attributes :id, :created_at, :revised_at, :in_reply_to_id, :in_reply_to_account_id,
2019-07-02 08:10:25 +01:00
:sensitive, :spoiler_text, :visibility, :language,
2020-11-25 21:22:37 +00:00
:url, :replies_count, :reblogs_count, :pinnable, :pinnable_by_group,
:favourites_count, :quote_of_id, :expires_at, :has_quote, :bookmark_collection_id
2019-07-02 08:10:25 +01:00
attribute :favourited, if: :current_user?
attribute :reblogged, if: :current_user?
2020-11-15 18:48:32 +00:00
2019-07-02 08:10:25 +01:00
attribute :content, unless: :source_requested?
2020-04-22 06:00:11 +01:00
attribute :rich_content, unless: :source_requested?
2020-06-17 21:27:58 +01:00
attribute :plain_markdown, unless: :source_requested?
2019-07-02 08:10:25 +01:00
attribute :text, if: :source_requested?
belongs_to :reblog, serializer: REST::StatusSerializer
belongs_to :quote, serializer: REST::StatusSerializer
belongs_to :account, serializer: REST::AccountSerializer
belongs_to :group, serializer: REST::GroupSerializer
2019-07-02 08:10:25 +01:00
has_many :media_attachments, serializer: REST::MediaAttachmentSerializer
has_many :ordered_mentions, key: :mentions
has_many :tags
has_many :emojis, serializer: REST::CustomEmojiSerializer
has_one :preview_card, key: :card, serializer: REST::PreviewCardSerializer
2019-07-02 08:10:25 +01:00
has_one :preloadable_poll, key: :poll, serializer: REST::PollSerializer
def id
object.id.to_s
end
def in_reply_to_id
object.in_reply_to_id&.to_s
end
def in_reply_to_account_id
object.in_reply_to_account_id&.to_s
end
def quote_of_id
object.quote_of_id&.to_s
end
2019-07-02 08:10:25 +01:00
def current_user?
!current_user.nil?
end
def visibility
# This visibility is masked behind "private"
# to avoid API changes because there are no
# UX differences
if object.limited_visibility?
'private'
else
object.visibility
end
end
def uri
2020-11-15 18:48:32 +00:00
"/#{object.account.username}/posts/#{object.id}"
2019-07-02 08:10:25 +01:00
end
def content
2019-07-04 01:07:57 +01:00
Formatter.instance.format(object).strip
2019-07-02 08:10:25 +01:00
end
2020-04-22 06:00:11 +01:00
def rich_content
2020-06-12 17:01:54 +01:00
Formatter.instance.format(object, use_markdown: true).strip
2020-04-22 06:00:11 +01:00
end
2020-06-17 21:27:58 +01:00
def plain_markdown
object.markdown
end
2019-07-02 08:10:25 +01:00
def url
TagManager.instance.url_for(object)
end
def favourited
if instance_options && instance_options[:relationships]
instance_options[:relationships].favourites_map[object.id] || false
else
current_user.account.favourited?(object)
end
end
2020-03-13 18:44:44 +00:00
def favourites_count
if instance_options && instance_options[:unfavourite]
object.favourites_count - 1
else
object.favourites_count
end
end
2019-07-02 08:10:25 +01:00
def reblogged
if instance_options && instance_options[:relationships]
instance_options[:relationships].reblogs_map[object.id] || false
else
current_user.account.reblogged?(object)
end
end
def bookmarked
2020-11-25 21:22:37 +00:00
return
end
2019-07-02 08:10:25 +01:00
def pinned
2020-11-25 21:22:37 +00:00
return
2019-07-02 08:10:25 +01:00
end
2020-11-25 21:22:37 +00:00
def pinnable
2019-07-02 08:10:25 +01:00
current_user? &&
current_user.account_id == object.account_id &&
!object.reblog? &&
%w(public unlisted).include?(object.visibility)
end
def pinned_by_group
2020-11-25 21:22:37 +00:00
return
end
2020-11-25 21:22:37 +00:00
def pinnable_by_group
if object.group_id?
true
else
false
end
end
2019-07-02 08:10:25 +01:00
def source_requested?
instance_options[:source_requested]
end
def ordered_mentions
object.active_mentions.to_a.sort_by(&:id)
end
def bookmark_collection_id
instance_options[:bookmark_collection_id]
end
2019-07-02 08:10:25 +01:00
class ApplicationSerializer < ActiveModel::Serializer
attributes :name, :website
end
class MentionSerializer < ActiveModel::Serializer
attributes :id, :username, :url, :acct
def id
object.account_id.to_s
end
def username
object.account_username
end
def url
TagManager.instance.url_for(object.account)
end
def acct
object.account_acct
end
end
class TagSerializer < ActiveModel::Serializer
include RoutingHelper
attributes :name, :url
def url
2020-11-15 18:48:32 +00:00
"/tags/#{object.name}"
2019-07-02 08:10:25 +01:00
end
end
end