mastodon/app/serializers/rest/status_serializer.rb
Thibaut Girka dcded13a99 Merge branch 'master' into glitch-soc/merge-upstream
Conflicts:
- .github/ISSUE_TEMPLATE/bug_report.md
  Took our version.
- CONTRIBUTING.md
  Updated the embedded copy of upstream's version.
- README.md
  Took our version.
- app/policies/status_policy.rb
  Not a real conflict, took code from both.
- app/views/layouts/embedded.html.haml
  Added upstream's changes (dns-prefetch) and fixed
  `%body.embed`
- app/views/settings/preferences/show.html.haml
  Reverted some of upstream changes, as we have a
  page dedicated for flavours and skins.
- config/initializers/content_security_policy.rb
  Kept our version of the CSP.
- config/initializers/doorkeeper.rb
  Not a real conflict, took code from both.
2018-10-22 17:51:38 +02:00

148 lines
3.4 KiB
Ruby

# frozen_string_literal: true
class REST::StatusSerializer < ActiveModel::Serializer
attributes :id, :created_at, :in_reply_to_id, :in_reply_to_account_id,
:sensitive, :spoiler_text, :visibility, :language,
:uri, :content, :url, :replies_count, :reblogs_count,
:favourites_count
attribute :favourited, if: :current_user?
attribute :reblogged, if: :current_user?
attribute :muted, if: :current_user?
attribute :bookmarked, if: :current_user?
attribute :pinned, if: :pinnable?
belongs_to :reblog, serializer: REST::StatusSerializer
belongs_to :application
belongs_to :account, serializer: REST::AccountSerializer
has_many :media_attachments, serializer: REST::MediaAttachmentSerializer
has_many :ordered_mentions, key: :mentions
has_many :tags
has_many :emojis, serializer: REST::CustomEmojiSerializer
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 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
OStatus::TagManager.instance.uri_for(object)
end
def content
Formatter.instance.format(object)
end
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
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[:bookmarks]
instance_options[:bookmarks].bookmarks_map[object.id] || false
else
current_user.account.bookmarked?(object)
end
end
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 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