about summary refs log tree commit diff
path: root/app/serializers
diff options
context:
space:
mode:
authorkibigo! <marrus-sh@users.noreply.github.com>2017-07-12 02:03:17 -0700
committerkibigo! <marrus-sh@users.noreply.github.com>2017-07-12 02:03:17 -0700
commit79d898ae0ad8c0e66bd63ec3e0904e9e5e7894e8 (patch)
treeee8d832ed2f11e9afe62daf0e586a86004eb8d98 /app/serializers
parentbcf7ee48e94cd2e4d2de28e8854e7f0e2b5cad1f (diff)
parent056b5ed72f6d980bceeb49eb249b8365fe8fce66 (diff)
Merge upstream!! #64 <3 <3
Diffstat (limited to 'app/serializers')
-rw-r--r--app/serializers/initial_state_serializer.rb49
-rw-r--r--app/serializers/oembed_serializer.rb56
-rw-r--r--app/serializers/rest/account_serializer.rb33
-rw-r--r--app/serializers/rest/application_serializer.rb14
-rw-r--r--app/serializers/rest/context_serializer.rb6
-rw-r--r--app/serializers/rest/credential_account_serializer.rb14
-rw-r--r--app/serializers/rest/instance_serializer.rb30
-rw-r--r--app/serializers/rest/media_attachment_serializer.rb24
-rw-r--r--app/serializers/rest/notification_serializer.rb12
-rw-r--r--app/serializers/rest/preview_card_serializer.rb14
-rw-r--r--app/serializers/rest/relationship_serializer.rb30
-rw-r--r--app/serializers/rest/report_serializer.rb5
-rw-r--r--app/serializers/rest/search_serializer.rb12
-rw-r--r--app/serializers/rest/status_serializer.rb93
14 files changed, 392 insertions, 0 deletions
diff --git a/app/serializers/initial_state_serializer.rb b/app/serializers/initial_state_serializer.rb
new file mode 100644
index 000000000..6751c9411
--- /dev/null
+++ b/app/serializers/initial_state_serializer.rb
@@ -0,0 +1,49 @@
+# frozen_string_literal: true
+
+class InitialStateSerializer < ActiveModel::Serializer
+  attributes :meta, :compose, :accounts,
+             :media_attachments, :settings
+
+  def meta
+    store = {
+      streaming_api_base_url: Rails.configuration.x.streaming_api_base_url,
+      access_token: object.token,
+      locale: I18n.locale,
+      domain: Rails.configuration.x.local_domain,
+      admin: object.admin&.id,
+    }
+
+    if object.current_account
+      store[:me]             = object.current_account.id
+      store[:boost_modal]    = object.current_account.user.setting_boost_modal
+      store[:delete_modal]   = object.current_account.user.setting_delete_modal
+      store[:auto_play_gif]  = object.current_account.user.setting_auto_play_gif
+      store[:system_font_ui] = object.current_account.user.setting_system_font_ui
+    end
+
+    store
+  end
+
+  def compose
+    store = {}
+
+    if object.current_account
+      store[:me]                = object.current_account.id
+      store[:default_privacy]   = object.current_account.user.setting_default_privacy
+      store[:default_sensitive] = object.current_account.user.setting_default_sensitive
+    end
+
+    store
+  end
+
+  def accounts
+    store = {}
+    store[object.current_account.id] = ActiveModelSerializers::SerializableResource.new(object.current_account, serializer: REST::AccountSerializer) if object.current_account
+    store[object.admin.id]           = ActiveModelSerializers::SerializableResource.new(object.admin, serializer: REST::AccountSerializer) if object.admin
+    store
+  end
+
+  def media_attachments
+    { accept_content_types: MediaAttachment::IMAGE_MIME_TYPES + MediaAttachment::VIDEO_MIME_TYPES }
+  end
+end
diff --git a/app/serializers/oembed_serializer.rb b/app/serializers/oembed_serializer.rb
new file mode 100644
index 000000000..78376d253
--- /dev/null
+++ b/app/serializers/oembed_serializer.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+class OEmbedSerializer < ActiveModel::Serializer
+  include RoutingHelper
+  include ActionView::Helpers::TagHelper
+
+  attributes :type, :version, :title, :author_name,
+             :author_url, :provider_name, :provider_url,
+             :cache_age, :html, :width, :height
+
+  def type
+    'rich'
+  end
+
+  def version
+    '1.0'
+  end
+
+  def author_name
+    object.account.display_name.presence || object.account.username
+  end
+
+  def author_url
+    account_url(object.account)
+  end
+
+  def provider_name
+    Rails.configuration.x.local_domain
+  end
+
+  def provider_url
+    root_url
+  end
+
+  def cache_age
+    86_400
+  end
+
+  def html
+    tag :iframe,
+        src: embed_account_stream_entry_url(object.account, object),
+        style: 'width: 100%; overflow: hidden',
+        frameborder: '0',
+        scrolling: 'no',
+        width: width,
+        height: height
+  end
+
+  def width
+    instance_options[:width]
+  end
+
+  def height
+    instance_options[:height]
+  end
+end
diff --git a/app/serializers/rest/account_serializer.rb b/app/serializers/rest/account_serializer.rb
new file mode 100644
index 000000000..012a4fd18
--- /dev/null
+++ b/app/serializers/rest/account_serializer.rb
@@ -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
diff --git a/app/serializers/rest/application_serializer.rb b/app/serializers/rest/application_serializer.rb
new file mode 100644
index 000000000..868a62f1e
--- /dev/null
+++ b/app/serializers/rest/application_serializer.rb
@@ -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
diff --git a/app/serializers/rest/context_serializer.rb b/app/serializers/rest/context_serializer.rb
new file mode 100644
index 000000000..44515c85d
--- /dev/null
+++ b/app/serializers/rest/context_serializer.rb
@@ -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
diff --git a/app/serializers/rest/credential_account_serializer.rb b/app/serializers/rest/credential_account_serializer.rb
new file mode 100644
index 000000000..870d8b71f
--- /dev/null
+++ b/app/serializers/rest/credential_account_serializer.rb
@@ -0,0 +1,14 @@
+# frozen_string_literal: true
+
+class REST::CredentialAccountSerializer < REST::AccountSerializer
+  attributes :source
+
+  def source
+    user = object.user
+    {
+      privacy: user.setting_default_privacy,
+      sensitive: user.setting_default_sensitive,
+      note: object.note,
+    }
+  end
+end
diff --git a/app/serializers/rest/instance_serializer.rb b/app/serializers/rest/instance_serializer.rb
new file mode 100644
index 000000000..8e32f9cb3
--- /dev/null
+++ b/app/serializers/rest/instance_serializer.rb
@@ -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
diff --git a/app/serializers/rest/media_attachment_serializer.rb b/app/serializers/rest/media_attachment_serializer.rb
new file mode 100644
index 000000000..9055b8db4
--- /dev/null
+++ b/app/serializers/rest/media_attachment_serializer.rb
@@ -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
+    object.local? ? medium_url(object) : nil
+  end
+
+  def meta
+    object.file.meta
+  end
+end
diff --git a/app/serializers/rest/notification_serializer.rb b/app/serializers/rest/notification_serializer.rb
new file mode 100644
index 000000000..f95d099a3
--- /dev/null
+++ b/app/serializers/rest/notification_serializer.rb
@@ -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 :target_status, key: :status, if: :status_type?, serializer: REST::StatusSerializer
+
+  def status_type?
+    [:favourite, :reblog, :mention].include?(object.type)
+  end
+end
diff --git a/app/serializers/rest/preview_card_serializer.rb b/app/serializers/rest/preview_card_serializer.rb
new file mode 100644
index 000000000..9c460332c
--- /dev/null
+++ b/app/serializers/rest/preview_card_serializer.rb
@@ -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
diff --git a/app/serializers/rest/relationship_serializer.rb b/app/serializers/rest/relationship_serializer.rb
new file mode 100644
index 000000000..1d431aa1b
--- /dev/null
+++ b/app/serializers/rest/relationship_serializer.rb
@@ -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
diff --git a/app/serializers/rest/report_serializer.rb b/app/serializers/rest/report_serializer.rb
new file mode 100644
index 000000000..0c6bd6556
--- /dev/null
+++ b/app/serializers/rest/report_serializer.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+class REST::ReportSerializer < ActiveModel::Serializer
+  attributes :id, :action_taken
+end
diff --git a/app/serializers/rest/search_serializer.rb b/app/serializers/rest/search_serializer.rb
new file mode 100644
index 000000000..157f543ae
--- /dev/null
+++ b/app/serializers/rest/search_serializer.rb
@@ -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
diff --git a/app/serializers/rest/status_serializer.rb b/app/serializers/rest/status_serializer.rb
new file mode 100644
index 000000000..246b12a90
--- /dev/null
+++ b/app/serializers/rest/status_serializer.rb
@@ -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