Refactor JSON templates to be generated with ActiveModelSerializers instead of Rabl (#4090)
This commit is contained in:
33
app/serializers/rest/account_serializer.rb
Normal file
33
app/serializers/rest/account_serializer.rb
Normal file
@ -0,0 +1,33 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::AccountSerializer < ActiveModel::Serializer
|
||||
include RoutingHelper
|
||||
|
||||
attributes :id, :username, :acct, :display_name, :locked, :created_at,
|
||||
:note, :url, :avatar, :avatar_static, :header, :header_static,
|
||||
:followers_count, :following_count, :statuses_count
|
||||
|
||||
def note
|
||||
Formatter.instance.simplified_format(object)
|
||||
end
|
||||
|
||||
def url
|
||||
TagManager.instance.url_for(object)
|
||||
end
|
||||
|
||||
def avatar
|
||||
full_asset_url(object.avatar_original_url)
|
||||
end
|
||||
|
||||
def avatar_static
|
||||
full_asset_url(object.avatar_static_url)
|
||||
end
|
||||
|
||||
def header
|
||||
full_asset_url(object.header_original_url)
|
||||
end
|
||||
|
||||
def header_static
|
||||
full_asset_url(object.header_static_url)
|
||||
end
|
||||
end
|
14
app/serializers/rest/application_serializer.rb
Normal file
14
app/serializers/rest/application_serializer.rb
Normal file
@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::ApplicationSerializer < ActiveModel::Serializer
|
||||
attributes :id, :name, :website, :redirect_uri,
|
||||
:client_id, :client_secret
|
||||
|
||||
def client_id
|
||||
object.uid
|
||||
end
|
||||
|
||||
def client_secret
|
||||
object.secret
|
||||
end
|
||||
end
|
6
app/serializers/rest/context_serializer.rb
Normal file
6
app/serializers/rest/context_serializer.rb
Normal file
@ -0,0 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::ContextSerializer < ActiveModel::Serializer
|
||||
has_many :ancestors, serializer: REST::StatusSerializer
|
||||
has_many :descendants, serializer: REST::StatusSerializer
|
||||
end
|
30
app/serializers/rest/instance_serializer.rb
Normal file
30
app/serializers/rest/instance_serializer.rb
Normal file
@ -0,0 +1,30 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::InstanceSerializer < ActiveModel::Serializer
|
||||
attributes :uri, :title, :description, :email,
|
||||
:version, :urls
|
||||
|
||||
def uri
|
||||
Rails.configuration.x.local_domain
|
||||
end
|
||||
|
||||
def title
|
||||
Setting.site_title
|
||||
end
|
||||
|
||||
def description
|
||||
Setting.site_description
|
||||
end
|
||||
|
||||
def email
|
||||
Setting.site_contact_email
|
||||
end
|
||||
|
||||
def version
|
||||
Mastodon::Version.to_s
|
||||
end
|
||||
|
||||
def urls
|
||||
{ streaming_api: Rails.configuration.x.streaming_api_base_url }
|
||||
end
|
||||
end
|
24
app/serializers/rest/media_attachment_serializer.rb
Normal file
24
app/serializers/rest/media_attachment_serializer.rb
Normal file
@ -0,0 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::MediaAttachmentSerializer < ActiveModel::Serializer
|
||||
include RoutingHelper
|
||||
|
||||
attributes :id, :type, :url, :preview_url,
|
||||
:remote_url, :text_url, :meta
|
||||
|
||||
def url
|
||||
full_asset_url(object.file.url(:original))
|
||||
end
|
||||
|
||||
def preview_url
|
||||
full_asset_url(object.file.url(:small))
|
||||
end
|
||||
|
||||
def text_url
|
||||
medium_url(object.id)
|
||||
end
|
||||
|
||||
def meta
|
||||
object.file.meta
|
||||
end
|
||||
end
|
12
app/serializers/rest/notification_serializer.rb
Normal file
12
app/serializers/rest/notification_serializer.rb
Normal file
@ -0,0 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::NotificationSerializer < ActiveModel::Serializer
|
||||
attributes :id, :type, :created_at
|
||||
|
||||
belongs_to :from_account, key: :account, serializer: REST::AccountSerializer
|
||||
belongs_to :status, if: :status_type?, serializer: REST::StatusSerializer
|
||||
|
||||
def status_type?
|
||||
[:favourite, :reblog, :mention].include?(object.type)
|
||||
end
|
||||
end
|
14
app/serializers/rest/preview_card_serializer.rb
Normal file
14
app/serializers/rest/preview_card_serializer.rb
Normal file
@ -0,0 +1,14 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::PreviewCardSerializer < ActiveModel::Serializer
|
||||
include RoutingHelper
|
||||
|
||||
attributes :url, :title, :description, :type,
|
||||
:author_name, :author_url, :provider_name,
|
||||
:provider_url, :html, :width, :height,
|
||||
:image
|
||||
|
||||
def image
|
||||
object.image? ? full_asset_url(object.image.url(:original)) : nil
|
||||
end
|
||||
end
|
30
app/serializers/rest/relationship_serializer.rb
Normal file
30
app/serializers/rest/relationship_serializer.rb
Normal file
@ -0,0 +1,30 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::RelationshipSerializer < ActiveModel::Serializer
|
||||
attributes :id, :following, :followed_by, :blocking,
|
||||
:muting, :requested, :domain_blocking
|
||||
|
||||
def following
|
||||
instance_options[:relationships].following[object.id] || false
|
||||
end
|
||||
|
||||
def followed_by
|
||||
instance_options[:relationships].followed_by[object.id] || false
|
||||
end
|
||||
|
||||
def blocking
|
||||
instance_options[:relationships].blocking[object.id] || false
|
||||
end
|
||||
|
||||
def muting
|
||||
instance_options[:relationships].muting[object.id] || false
|
||||
end
|
||||
|
||||
def requested
|
||||
instance_options[:relationships].requested[object.id] || false
|
||||
end
|
||||
|
||||
def domain_blocking
|
||||
instance_options[:relationships].domain_blocking[object.id] || false
|
||||
end
|
||||
end
|
5
app/serializers/rest/report_serializer.rb
Normal file
5
app/serializers/rest/report_serializer.rb
Normal file
@ -0,0 +1,5 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::ReportSerializer < ActiveModel::Serializer
|
||||
attributes :id, :action_taken
|
||||
end
|
12
app/serializers/rest/search_serializer.rb
Normal file
12
app/serializers/rest/search_serializer.rb
Normal file
@ -0,0 +1,12 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class REST::SearchSerializer < ActiveModel::Serializer
|
||||
attributes :hashtags
|
||||
|
||||
has_many :accounts, serializer: REST::AccountSerializer
|
||||
has_many :statuses, serializer: REST::StatusSerializer
|
||||
|
||||
def hashtags
|
||||
object.hashtags.map(&:name)
|
||||
end
|
||||
end
|
93
app/serializers/rest/status_serializer.rb
Normal file
93
app/serializers/rest/status_serializer.rb
Normal file
@ -0,0 +1,93 @@
|
||||
# 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, :reblogs_count, :favourites_count
|
||||
|
||||
attribute :favourited, if: :current_user?
|
||||
attribute :reblogged, if: :current_user?
|
||||
attribute :muted, if: :current_user?
|
||||
|
||||
belongs_to :reblog, serializer: REST::StatusSerializer
|
||||
belongs_to :application
|
||||
belongs_to :account, serializer: REST::AccountSerializer
|
||||
|
||||
has_many :media_attachments, serializer: REST::MediaAttachmentSerializer
|
||||
has_many :mentions
|
||||
has_many :tags
|
||||
|
||||
def current_user?
|
||||
!current_user.nil?
|
||||
end
|
||||
|
||||
def uri
|
||||
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
|
||||
|
||||
class ApplicationSerializer < ActiveModel::Serializer
|
||||
attributes :name, :website
|
||||
end
|
||||
|
||||
class MentionSerializer < ActiveModel::Serializer
|
||||
attributes :id, :username, :url, :acct
|
||||
|
||||
def id
|
||||
object.account_id
|
||||
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
|
Reference in New Issue
Block a user