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

203 lines
4.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,
:uri, :url, :replies_count, :reblogs_count,
:favourites_count, :quote_of_id, :expires_at, :has_quote
2019-07-02 08:10:25 +01:00
attribute :favourited, if: :current_user?
attribute :reblogged, if: :current_user?
attribute :muted, if: :current_user?
attribute :bookmarked, if: :current_user?
2019-07-02 08:10:25 +01:00
attribute :pinned, if: :pinnable?
attribute :pinned_by_group, if: :pinnable_by_group?
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
2019-07-02 08:10:25 +01:00
belongs_to :application, if: :show_application?
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
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 show_application?
object.account.user_shows_application? || (current_user? && current_user.account_id == object.account_id)
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
OStatus::TagManager.instance.uri_for(object)
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 muted
if instance_options && instance_options[:relationships]
instance_options[:relationships].mutes_map[object.conversation_id] || false
else
current_user.account.muting_conversation?(object.conversation)
end
end
def bookmarked
if instance_options && instance_options[:relationships]
instance_options[:relationships].bookmarks_map[object.id] || false
else
current_user.account.bookmarked?(object)
end
end
2019-07-02 08:10:25 +01:00
def pinned
if instance_options && instance_options[:relationships]
instance_options[:relationships].pins_map[object.id] || false
else
current_user.account.pinned?(object)
end
end
def pinnable?
current_user? &&
current_user.account_id == object.account_id &&
!object.reblog? &&
%w(public unlisted).include?(object.visibility)
end
def pinned_by_group
if instance_options && instance_options[:relationships]
instance_options[:relationships].group_pins_map[object.id] || false
else
false
end
end
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
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
tag_url(object)
end
end
end