[WiP] Introduce shallow serializers

This commit is contained in:
Claire 2024-07-30 13:36:01 +02:00
parent 2ce99c51dd
commit 662ef2b85b
16 changed files with 515 additions and 381 deletions

View file

@ -1,25 +1,8 @@
# frozen_string_literal: true
class REST::AccountSerializer < ActiveModel::Serializer
include RoutingHelper
include FormattingHelper
# Please update `app/javascript/mastodon/api_types/accounts.ts` when making changes to the attributes
attributes :id, :username, :acct, :display_name, :locked, :bot, :discoverable, :indexable, :group, :created_at,
:note, :url, :uri, :avatar, :avatar_static, :header, :header_static,
:followers_count, :following_count, :statuses_count, :last_status_at, :hide_collections
class REST::AccountSerializer < REST::BaseAccountSerializer
has_one :moved_to_account, key: :moved, serializer: REST::AccountSerializer, if: :moved_and_not_nested?
has_many :emojis, serializer: REST::CustomEmojiSerializer
attribute :suspended, if: :suspended?
attribute :silenced, key: :limited, if: :silenced?
attribute :noindex, if: :local?
attribute :memorial, if: :memorial?
class AccountDecorator < SimpleDelegator
def self.model_name
Account.model_name
@ -30,130 +13,10 @@ class REST::AccountSerializer < ActiveModel::Serializer
end
end
class RoleSerializer < ActiveModel::Serializer
attributes :id, :name, :color
def id
object.id.to_s
end
end
has_many :roles, serializer: RoleSerializer, if: :local?
class FieldSerializer < ActiveModel::Serializer
include FormattingHelper
attributes :name, :value, :verified_at
def value
account_field_value_format(object)
end
end
has_many :fields
def id
object.id.to_s
end
def acct
object.pretty_acct
end
def note
object.unavailable? ? '' : account_bio_format(object)
end
def url
ActivityPub::TagManager.instance.url_for(object)
end
def uri
ActivityPub::TagManager.instance.uri_for(object)
end
def avatar
full_asset_url(object.unavailable? ? object.avatar.default_url : object.avatar_original_url)
end
def avatar_static
full_asset_url(object.unavailable? ? object.avatar.default_url : object.avatar_static_url)
end
def header
full_asset_url(object.unavailable? ? object.header.default_url : object.header_original_url)
end
def header_static
full_asset_url(object.unavailable? ? object.header.default_url : object.header_static_url)
end
def created_at
object.created_at.midnight.as_json
end
def last_status_at
object.last_status_at&.to_date&.iso8601
end
def display_name
object.unavailable? ? '' : object.display_name
end
def locked
object.unavailable? ? false : object.locked
end
def bot
object.unavailable? ? false : object.bot
end
def discoverable
object.unavailable? ? false : object.discoverable
end
def indexable
object.unavailable? ? false : object.indexable
end
def moved_to_account
object.unavailable? ? nil : AccountDecorator.new(object.moved_to_account)
end
def emojis
object.unavailable? ? [] : object.emojis
end
def fields
object.unavailable? ? [] : object.fields
end
def suspended
object.unavailable?
end
def silenced
object.silenced?
end
def memorial
object.memorial?
end
def roles
if object.unavailable? || object.user.nil?
[]
else
[object.user.role].compact.filter(&:highlighted?)
end
end
def noindex
object.user_prefers_noindex?
end
delegate :suspended?, :silenced?, :local?, :memorial?, to: :object
def moved_and_not_nested?
object.moved?
end

View file

@ -1,16 +1,5 @@
# frozen_string_literal: true
class REST::AccountWarningSerializer < ActiveModel::Serializer
attributes :id, :action, :text, :status_ids, :created_at
class REST::AccountWarningSerializer < REST::BaseAccountWarningSerializer
has_one :target_account, serializer: REST::AccountSerializer
has_one :appeal, serializer: REST::AppealSerializer
def id
object.id.to_s
end
def status_ids
object&.status_ids&.map(&:to_s)
end
end

View file

@ -0,0 +1,140 @@
# frozen_string_literal: true
class REST::BaseAccountSerializer < ActiveModel::Serializer
include RoutingHelper
include FormattingHelper
# Please update `app/javascript/mastodon/api_types/accounts.ts` when making changes to the attributes
attributes :id, :username, :acct, :display_name, :locked, :bot, :discoverable, :indexable, :group, :created_at,
:note, :url, :uri, :avatar, :avatar_static, :header, :header_static,
:followers_count, :following_count, :statuses_count, :last_status_at, :hide_collections
has_many :emojis, serializer: REST::CustomEmojiSerializer
attribute :suspended, if: :suspended?
attribute :silenced, key: :limited, if: :silenced?
attribute :noindex, if: :local?
attribute :memorial, if: :memorial?
class RoleSerializer < ActiveModel::Serializer
attributes :id, :name, :color
def id
object.id.to_s
end
end
has_many :roles, serializer: RoleSerializer, if: :local?
class FieldSerializer < ActiveModel::Serializer
include FormattingHelper
attributes :name, :value, :verified_at
def value
account_field_value_format(object)
end
end
has_many :fields
def id
object.id.to_s
end
def acct
object.pretty_acct
end
def note
object.unavailable? ? '' : account_bio_format(object)
end
def url
ActivityPub::TagManager.instance.url_for(object)
end
def uri
ActivityPub::TagManager.instance.uri_for(object)
end
def avatar
full_asset_url(object.unavailable? ? object.avatar.default_url : object.avatar_original_url)
end
def avatar_static
full_asset_url(object.unavailable? ? object.avatar.default_url : object.avatar_static_url)
end
def header
full_asset_url(object.unavailable? ? object.header.default_url : object.header_original_url)
end
def header_static
full_asset_url(object.unavailable? ? object.header.default_url : object.header_static_url)
end
def created_at
object.created_at.midnight.as_json
end
def last_status_at
object.last_status_at&.to_date&.iso8601
end
def display_name
object.unavailable? ? '' : object.display_name
end
def locked
object.unavailable? ? false : object.locked
end
def bot
object.unavailable? ? false : object.bot
end
def discoverable
object.unavailable? ? false : object.discoverable
end
def indexable
object.unavailable? ? false : object.indexable
end
def emojis
object.unavailable? ? [] : object.emojis
end
def fields
object.unavailable? ? [] : object.fields
end
def suspended
object.unavailable?
end
def silenced
object.silenced?
end
def memorial
object.memorial?
end
def roles
if object.unavailable? || object.user.nil?
[]
else
[object.user.role].compact.filter(&:highlighted?)
end
end
def noindex
object.user_prefers_noindex?
end
delegate :suspended?, :silenced?, :local?, :memorial?, to: :object
end

View file

@ -0,0 +1,15 @@
# frozen_string_literal: true
class REST::BaseAccountWarningSerializer < ActiveModel::Serializer
attributes :id, :action, :text, :status_ids, :created_at
has_one :appeal, serializer: REST::AppealSerializer
def id
object.id.to_s
end
def status_ids
object&.status_ids&.map(&:to_s)
end
end

View file

@ -0,0 +1,22 @@
# frozen_string_literal: true
class REST::BasePreviewCardSerializer < ActiveModel::Serializer
include RoutingHelper
attributes :url, :title, :description, :language, :type,
:author_name, :author_url, :provider_name,
:provider_url, :html, :width, :height,
:image, :image_description, :embed_url, :blurhash, :published_at
def url
object.original_url.presence || object.url
end
def image
object.image? ? full_asset_url(object.image.url(:original)) : nil
end
def html
Sanitize.fragment(object.html, Sanitize::Config::MASTODON_OEMBED)
end
end

View file

@ -0,0 +1,18 @@
# frozen_string_literal: true
class REST::BaseReportSerializer < ActiveModel::Serializer
attributes :id, :action_taken, :action_taken_at, :category, :comment,
:forwarded, :created_at, :status_ids, :rule_ids
def id
object.id.to_s
end
def status_ids
object&.status_ids&.map(&:to_s)
end
def rule_ids
object&.rule_ids&.map(&:to_s)
end
end

View file

@ -0,0 +1,197 @@
# frozen_string_literal: true
class REST::BaseStatusSerializer < ActiveModel::Serializer
include FormattingHelper
# Please update `app/javascript/mastodon/api_types/statuses.ts` when making changes to the attributes
attributes :id, :created_at, :in_reply_to_id, :in_reply_to_account_id,
:sensitive, :spoiler_text, :visibility, :language,
:uri, :url, :replies_count, :reblogs_count,
:favourites_count, :edited_at
attribute :favourited, if: :current_user?
attribute :reblogged, if: :current_user?
attribute :muted, if: :current_user?
attribute :bookmarked, if: :current_user?
attribute :pinned, if: :pinnable?
has_many :filtered, serializer: REST::FilterResultSerializer, if: :current_user?
attribute :content, unless: :source_requested?
attribute :text, if: :source_requested?
belongs_to :application, if: :show_application?
has_many :ordered_media_attachments, key: :media_attachments, serializer: REST::MediaAttachmentSerializer
has_many :ordered_mentions, key: :mentions
has_many :tags
has_many :emojis, serializer: REST::CustomEmojiSerializer
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 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 sensitive
if current_user? && current_user.account_id == object.account_id
object.sensitive
else
object.account.sensitized? || object.sensitive
end
end
def uri
ActivityPub::TagManager.instance.uri_for(object)
end
def content
status_content_format(object)
end
def url
ActivityPub::TagManager.instance.url_for(object)
end
def reblogs_count
relationships&.attributes_map&.dig(object.id, :reblogs_count) || object.reblogs_count
end
def favourites_count
relationships&.attributes_map&.dig(object.id, :favourites_count) || object.favourites_count
end
def favourited
if relationships
relationships.favourites_map[object.id] || false
else
current_user.account.favourited?(object)
end
end
def reblogged
if relationships
relationships.reblogs_map[object.id] || false
else
current_user.account.reblogged?(object)
end
end
def muted
if relationships
relationships.mutes_map[object.conversation_id] || false
else
current_user.account.muting_conversation?(object.conversation)
end
end
def bookmarked
if relationships
relationships.bookmarks_map[object.id] || false
else
current_user.account.bookmarked?(object)
end
end
def pinned
if relationships
relationships.pins_map[object.id] || false
else
current_user.account.pinned?(object)
end
end
def filtered
if relationships
relationships.filters_map[object.id] || []
else
current_user.account.status_matches_filters(object)
end
end
def pinnable?
current_user? &&
current_user.account_id == object.account_id &&
!object.reblog? &&
%w(public unlisted private).include?(object.visibility)
end
def source_requested?
instance_options[:source_requested]
end
def ordered_mentions
object.active_mentions.to_a.sort_by(&:id)
end
private
def relationships
instance_options && instance_options[:relationships]
end
class ApplicationSerializer < ActiveModel::Serializer
attributes :name, :website
def website
object.website.presence
end
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
ActivityPub::TagManager.instance.url_for(object.account)
end
def acct
object.account.pretty_acct
end
end
class TagSerializer < ActiveModel::Serializer
include RoutingHelper
attributes :name, :url
def url
tag_url(object)
end
end
end

View file

@ -1,29 +1,10 @@
# frozen_string_literal: true
class REST::PreviewCardSerializer < ActiveModel::Serializer
class REST::PreviewCardSerializer < REST::BasePreviewCardSerializer
class AuthorSerializer < ActiveModel::Serializer
attributes :name, :url
has_one :account, serializer: REST::AccountSerializer
end
include RoutingHelper
attributes :url, :title, :description, :language, :type,
:author_name, :author_url, :provider_name,
:provider_url, :html, :width, :height,
:image, :image_description, :embed_url, :blurhash, :published_at
has_many :authors, serializer: AuthorSerializer
def url
object.original_url.presence || object.url
end
def image
object.image? ? full_asset_url(object.image.url(:original)) : nil
end
def html
Sanitize.fragment(object.html, Sanitize::Config::MASTODON_OEMBED)
end
end

View file

@ -1,20 +1,5 @@
# frozen_string_literal: true
class REST::ReportSerializer < ActiveModel::Serializer
attributes :id, :action_taken, :action_taken_at, :category, :comment,
:forwarded, :created_at, :status_ids, :rule_ids
class REST::ReportSerializer < REST::BaseReportSerializer
has_one :target_account, serializer: REST::AccountSerializer
def id
object.id.to_s
end
def status_ids
object&.status_ids&.map(&:to_s)
end
def rule_ids
object&.rule_ids&.map(&:to_s)
end
end

View file

@ -0,0 +1,11 @@
# frozen_string_literal: true
class REST::Shallow::AccountSerializer < REST::BaseAccountSerializer
attribute :moved_to_account_id, if: :moved?
def moved_to_account_id
object.unavailable? ? nil : object.moved_to_account_id&.to_s
end
delegate :moved?, to: :object
end

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
class REST::Shallow::AccountWarningSerializer < REST::BaseAccountWarningSerializer
attribute :target_account_id
def target_account_id
object.target_account_id&.to_s
end
end

View file

@ -0,0 +1,60 @@
# frozen_string_literal: true
class REST::Shallow::NotificationGroupSerializer < ActiveModel::Serializer
# Please update app/javascript/api_types/notification.ts when making changes to the attributes
attributes :group_key, :notifications_count, :type, :most_recent_notification_id
attributes :sample_account_ids
attribute :page_min_id, if: :paginated?
attribute :page_max_id, if: :paginated?
attribute :latest_page_notification_at, if: :paginated?
attribute :status_id, if: :status_type?
belongs_to :report, if: :report_type?, serializer: REST::Shallow::ReportSerializer
belongs_to :account_relationship_severance_event, key: :event, if: :relationship_severance_event?, serializer: REST::AccountRelationshipSeveranceEventSerializer
belongs_to :account_warning, key: :moderation_warning, if: :moderation_warning_event?, serializer: REST::Shallow::AccountWarningSerializer
def sample_account_ids
object.sample_accounts.pluck(:id).map(&:to_s)
end
def status_type?
[:favourite, :reblog, :status, :mention, :poll, :update].include?(object.type)
end
def status_id
object.target_status&.id&.to_s
end
def report_type?
object.type == :'admin.report'
end
def relationship_severance_event?
object.type == :severed_relationships
end
def moderation_warning_event?
object.type == :moderation_warning
end
def page_min_id
range = instance_options[:group_metadata][object.group_key]
range.present? ? range[:min_id].to_s : object.notification.id.to_s
end
def page_max_id
range = instance_options[:group_metadata][object.group_key]
range.present? ? range[:max_id].to_s : object.notification.id.to_s
end
def latest_page_notification_at
range = instance_options[:group_metadata][object.group_key]
range.present? ? range[:latest_notification_at] : object.notification.created_at
end
def paginated?
!instance_options[:group_metadata].nil?
end
end

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
class REST::Shallow::PreviewCardSerializer < REST::BasePreviewCardSerializer
class AuthorSerializer < ActiveModel::Serializer
attributes :name, :url, :account_id
def account_id
object.account_id&.to_s
end
end
has_many :authors, serializer: AuthorSerializer
end

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
class REST::Shallow::ReportSerializer < REST::BaseReportSerializer
attribute :target_account_id
def target_account_id
object.target_account_id&.to_s
end
end

View file

@ -0,0 +1,15 @@
# frozen_string_literal: true
class REST::Shallow::StatusSerializer < REST::BaseStatusSerializer
attributes :account_id, :reblog_id
has_one :preview_card, key: :card, serializer: REST::Shallow::PreviewCardSerializer
def account_id
object.account_id&.to_s
end
def reblog_id
object.reblog_of_id&.to_s
end
end

View file

@ -1,200 +1,7 @@
# frozen_string_literal: true
class REST::StatusSerializer < ActiveModel::Serializer
include FormattingHelper
# Please update `app/javascript/mastodon/api_types/statuses.ts` when making changes to the attributes
attributes :id, :created_at, :in_reply_to_id, :in_reply_to_account_id,
:sensitive, :spoiler_text, :visibility, :language,
:uri, :url, :replies_count, :reblogs_count,
:favourites_count, :edited_at
attribute :favourited, if: :current_user?
attribute :reblogged, if: :current_user?
attribute :muted, if: :current_user?
attribute :bookmarked, if: :current_user?
attribute :pinned, if: :pinnable?
has_many :filtered, serializer: REST::FilterResultSerializer, if: :current_user?
attribute :content, unless: :source_requested?
attribute :text, if: :source_requested?
belongs_to :reblog, serializer: REST::StatusSerializer
belongs_to :application, if: :show_application?
class REST::StatusSerializer < REST::BaseStatusSerializer
belongs_to :account, serializer: REST::AccountSerializer
has_many :ordered_media_attachments, key: :media_attachments, serializer: REST::MediaAttachmentSerializer
has_many :ordered_mentions, key: :mentions
has_many :tags
has_many :emojis, serializer: REST::CustomEmojiSerializer
belongs_to :reblog, serializer: REST::StatusSerializer
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 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 sensitive
if current_user? && current_user.account_id == object.account_id
object.sensitive
else
object.account.sensitized? || object.sensitive
end
end
def uri
ActivityPub::TagManager.instance.uri_for(object)
end
def content
status_content_format(object)
end
def url
ActivityPub::TagManager.instance.url_for(object)
end
def reblogs_count
relationships&.attributes_map&.dig(object.id, :reblogs_count) || object.reblogs_count
end
def favourites_count
relationships&.attributes_map&.dig(object.id, :favourites_count) || object.favourites_count
end
def favourited
if relationships
relationships.favourites_map[object.id] || false
else
current_user.account.favourited?(object)
end
end
def reblogged
if relationships
relationships.reblogs_map[object.id] || false
else
current_user.account.reblogged?(object)
end
end
def muted
if relationships
relationships.mutes_map[object.conversation_id] || false
else
current_user.account.muting_conversation?(object.conversation)
end
end
def bookmarked
if relationships
relationships.bookmarks_map[object.id] || false
else
current_user.account.bookmarked?(object)
end
end
def pinned
if relationships
relationships.pins_map[object.id] || false
else
current_user.account.pinned?(object)
end
end
def filtered
if relationships
relationships.filters_map[object.id] || []
else
current_user.account.status_matches_filters(object)
end
end
def pinnable?
current_user? &&
current_user.account_id == object.account_id &&
!object.reblog? &&
%w(public unlisted private).include?(object.visibility)
end
def source_requested?
instance_options[:source_requested]
end
def ordered_mentions
object.active_mentions.to_a.sort_by(&:id)
end
private
def relationships
instance_options && instance_options[:relationships]
end
class ApplicationSerializer < ActiveModel::Serializer
attributes :name, :website
def website
object.website.presence
end
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
ActivityPub::TagManager.instance.url_for(object.account)
end
def acct
object.account.pretty_acct
end
end
class TagSerializer < ActiveModel::Serializer
include RoutingHelper
attributes :name, :url
def url
tag_url(object)
end
end
end