about summary refs log tree commit diff
path: root/app/serializers
diff options
context:
space:
mode:
Diffstat (limited to 'app/serializers')
-rw-r--r--app/serializers/activitypub/actor_serializer.rb2
-rw-r--r--app/serializers/activitypub/block_serializer.rb2
-rw-r--r--app/serializers/activitypub/follow_serializer.rb2
-rw-r--r--app/serializers/initial_state_serializer.rb8
-rw-r--r--app/serializers/rest/account_serializer.rb3
-rw-r--r--app/serializers/rest/tag_serializer.rb11
-rw-r--r--app/serializers/rest/v2/search_serializer.rb7
-rw-r--r--app/serializers/rest/web_push_subscription_serializer.rb13
-rw-r--r--app/serializers/web/notification_serializer.rb164
9 files changed, 55 insertions, 157 deletions
diff --git a/app/serializers/activitypub/actor_serializer.rb b/app/serializers/activitypub/actor_serializer.rb
index fcf3bdf17..41c9aa44e 100644
--- a/app/serializers/activitypub/actor_serializer.rb
+++ b/app/serializers/activitypub/actor_serializer.rb
@@ -37,7 +37,7 @@ class ActivityPub::ActorSerializer < ActiveModel::Serializer
   end
 
   def type
-    'Person'
+    object.bot? ? 'Service' : 'Person'
   end
 
   def following
diff --git a/app/serializers/activitypub/block_serializer.rb b/app/serializers/activitypub/block_serializer.rb
index b3bd9f868..624ce2fce 100644
--- a/app/serializers/activitypub/block_serializer.rb
+++ b/app/serializers/activitypub/block_serializer.rb
@@ -5,7 +5,7 @@ class ActivityPub::BlockSerializer < ActiveModel::Serializer
   attribute :virtual_object, key: :object
 
   def id
-    [ActivityPub::TagManager.instance.uri_for(object.account), '#blocks/', object.id].join
+    ActivityPub::TagManager.instance.uri_for(object) || [ActivityPub::TagManager.instance.uri_for(object.account), '#blocks/', object.id].join
   end
 
   def type
diff --git a/app/serializers/activitypub/follow_serializer.rb b/app/serializers/activitypub/follow_serializer.rb
index 86c9992fe..bb204ee8f 100644
--- a/app/serializers/activitypub/follow_serializer.rb
+++ b/app/serializers/activitypub/follow_serializer.rb
@@ -5,7 +5,7 @@ class ActivityPub::FollowSerializer < ActiveModel::Serializer
   attribute :virtual_object, key: :object
 
   def id
-    [ActivityPub::TagManager.instance.uri_for(object.account), '#follows/', object.id].join
+    ActivityPub::TagManager.instance.uri_for(object) || [ActivityPub::TagManager.instance.uri_for(object.account), '#follows/', object.id].join
   end
 
   def type
diff --git a/app/serializers/initial_state_serializer.rb b/app/serializers/initial_state_serializer.rb
index 1d17e2b0a..4f2f4e38a 100644
--- a/app/serializers/initial_state_serializer.rb
+++ b/app/serializers/initial_state_serializer.rb
@@ -2,19 +2,15 @@
 
 class InitialStateSerializer < ActiveModel::Serializer
   attributes :meta, :compose, :accounts,
-             :media_attachments, :settings, :push_subscription,
+             :media_attachments, :settings,
              :max_toot_chars
 
-  has_many :custom_emojis, serializer: REST::CustomEmojiSerializer
+  has_one :push_subscription, serializer: REST::WebPushSubscriptionSerializer
 
   def max_toot_chars
     StatusLengthValidator::MAX_CHARS
   end
 
-  def custom_emojis
-    CustomEmoji.local.where(disabled: false)
-  end
-
   def meta
     store = {
       streaming_api_base_url: Rails.configuration.x.streaming_api_base_url,
diff --git a/app/serializers/rest/account_serializer.rb b/app/serializers/rest/account_serializer.rb
index 863238eb7..6adcd7039 100644
--- a/app/serializers/rest/account_serializer.rb
+++ b/app/serializers/rest/account_serializer.rb
@@ -3,11 +3,12 @@
 class REST::AccountSerializer < ActiveModel::Serializer
   include RoutingHelper
 
-  attributes :id, :username, :acct, :display_name, :locked, :created_at,
+  attributes :id, :username, :acct, :display_name, :locked, :bot, :created_at,
              :note, :url, :avatar, :avatar_static, :header, :header_static,
              :followers_count, :following_count, :statuses_count
 
   has_one :moved_to_account, key: :moved, serializer: REST::AccountSerializer, if: :moved_and_not_nested?
+  has_many :emojis, serializer: REST::CustomEmojiSerializer
 
   class FieldSerializer < ActiveModel::Serializer
     attributes :name, :value
diff --git a/app/serializers/rest/tag_serializer.rb b/app/serializers/rest/tag_serializer.rb
new file mode 100644
index 000000000..74aa571a4
--- /dev/null
+++ b/app/serializers/rest/tag_serializer.rb
@@ -0,0 +1,11 @@
+# frozen_string_literal: true
+
+class REST::TagSerializer < ActiveModel::Serializer
+  include RoutingHelper
+
+  attributes :name, :url, :history
+
+  def url
+    tag_url(object)
+  end
+end
diff --git a/app/serializers/rest/v2/search_serializer.rb b/app/serializers/rest/v2/search_serializer.rb
new file mode 100644
index 000000000..cdb6b3a53
--- /dev/null
+++ b/app/serializers/rest/v2/search_serializer.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+class REST::V2::SearchSerializer < ActiveModel::Serializer
+  has_many :accounts, serializer: REST::AccountSerializer
+  has_many :statuses, serializer: REST::StatusSerializer
+  has_many :hashtags, serializer: REST::TagSerializer
+end
diff --git a/app/serializers/rest/web_push_subscription_serializer.rb b/app/serializers/rest/web_push_subscription_serializer.rb
new file mode 100644
index 000000000..7fd952a56
--- /dev/null
+++ b/app/serializers/rest/web_push_subscription_serializer.rb
@@ -0,0 +1,13 @@
+# frozen_string_literal: true
+
+class REST::WebPushSubscriptionSerializer < ActiveModel::Serializer
+  attributes :id, :endpoint, :alerts, :server_key
+
+  def alerts
+    object.data&.dig('alerts') || {}
+  end
+
+  def server_key
+    Rails.configuration.x.vapid_public_key
+  end
+end
diff --git a/app/serializers/web/notification_serializer.rb b/app/serializers/web/notification_serializer.rb
index e5524fe7a..43ba4d92a 100644
--- a/app/serializers/web/notification_serializer.rb
+++ b/app/serializers/web/notification_serializer.rb
@@ -2,168 +2,38 @@
 
 class Web::NotificationSerializer < ActiveModel::Serializer
   include RoutingHelper
-  include StreamEntriesHelper
+  include ActionView::Helpers::TextHelper
+  include ActionView::Helpers::SanitizeHelper
 
-  class DataSerializer < ActiveModel::Serializer
-    include RoutingHelper
-    include StreamEntriesHelper
-    include ActionView::Helpers::SanitizeHelper
+  attributes :access_token, :preferred_locale, :notification_id,
+             :notification_type, :icon, :title, :body
 
-    attributes :content, :nsfw, :url, :actions,
-               :access_token, :message, :dir
-
-    def content
-      decoder.decode(strip_tags(body))
-    end
-
-    def dir
-      rtl?(body) ? 'rtl' : 'ltr'
-    end
-
-    def nsfw
-      return if object.target_status.nil?
-      object.target_status.spoiler_text.presence
-    end
-
-    def url
-      case object.type
-      when :mention
-        web_url("statuses/#{object.target_status.id}")
-      when :follow
-        web_url("accounts/#{object.from_account.id}")
-      when :favourite
-        web_url("statuses/#{object.target_status.id}")
-      when :reblog
-        web_url("statuses/#{object.target_status.id}")
-      end
-    end
-
-    def actions
-      return @actions if defined?(@actions)
-
-      @actions = []
-
-      if object.type == :mention
-        @actions << expand_action if collapsed?
-        @actions << favourite_action
-        @actions << reblog_action if rebloggable?
-      end
-
-      @actions
-    end
-
-    def access_token
-      return if actions.empty?
-      current_push_subscription.access_token
-    end
-
-    def message
-      I18n.t('push_notifications.group.title')
-    end
-
-    private
-
-    def body
-      case object.type
-      when :mention
-        object.target_status.text
-      when :follow
-        object.from_account.note
-      when :favourite
-        object.target_status.text
-      when :reblog
-        object.target_status.text
-      end
-    end
-
-    def decoder
-      @decoder ||= HTMLEntities.new
-    end
-
-    def expand_action
-      {
-        title: I18n.t('push_notifications.mention.action_expand'),
-        icon: full_asset_url('web-push-icon_expand.png', skip_pipeline: true),
-        todo: 'expand',
-        action: 'expand',
-      }
-    end
-
-    def favourite_action
-      {
-        title: I18n.t('push_notifications.mention.action_favourite'),
-        icon: full_asset_url('web-push-icon_favourite.png', skip_pipeline: true),
-        todo: 'request',
-        method: 'POST',
-        action: "/api/v1/statuses/#{object.target_status.id}/favourite",
-      }
-    end
-
-    def reblog_action
-      {
-        title: I18n.t('push_notifications.mention.action_boost'),
-        icon: full_asset_url('web-push-icon_reblog.png', skip_pipeline: true),
-        todo: 'request',
-        method: 'POST',
-        action: "/api/v1/statuses/#{object.target_status.id}/reblog",
-      }
-    end
-
-    def collapsed?
-      !object.target_status.nil? && (object.target_status.sensitive? || object.target_status.spoiler_text.present?)
-    end
-
-    def rebloggable?
-      !object.target_status.nil? && !object.target_status.hidden?
-    end
+  def access_token
+    current_push_subscription.associated_access_token
   end
 
-  attributes :title, :image, :badge, :tag,
-             :timestamp, :icon
-
-  has_one :data, serializer: DataSerializer
-
-  def title
-    case object.type
-    when :mention
-      I18n.t('push_notifications.mention.title', name: name)
-    when :follow
-      I18n.t('push_notifications.follow.title', name: name)
-    when :favourite
-      I18n.t('push_notifications.favourite.title', name: name)
-    when :reblog
-      I18n.t('push_notifications.reblog.title', name: name)
-    end
+  def preferred_locale
+    current_push_subscription.associated_user&.locale || I18n.default_locale
   end
 
-  def image
-    return if object.target_status.nil? || object.target_status.media_attachments.empty?
-    full_asset_url(object.target_status.media_attachments.first.file.url(:small))
-  end
-
-  def badge
-    full_asset_url('badge.png', skip_pipeline: true)
-  end
-
-  def tag
+  def notification_id
     object.id
   end
 
-  def timestamp
-    object.created_at
+  def notification_type
+    object.type
   end
 
   def icon
-    object.from_account.avatar_static_url
+    full_asset_url(object.from_account.avatar_static_url)
   end
 
-  def data
-    object
+  def title
+    I18n.t("notification_mailer.#{object.type}.subject", name: object.from_account.display_name.presence || object.from_account.username)
   end
 
-  private
-
-  def name
-    display_name(object.from_account)
+  def body
+    str = truncate(strip_tags(object.target_status&.spoiler_text&.presence || object.target_status&.text || object.from_account.note), length: 140)
+    HTMLEntities.new.decode(str.to_str) # Do not encode entities, since this value will not be used in HTML
   end
 end