diff --git a/app/controllers/api/v2_alpha/notifications_controller.rb b/app/controllers/api/v2_alpha/notifications_controller.rb index 83d40a0886..945d600e3d 100644 --- a/app/controllers/api/v2_alpha/notifications_controller.rb +++ b/app/controllers/api/v2_alpha/notifications_controller.rb @@ -31,13 +31,15 @@ class Api::V2Alpha::NotificationsController < Api::BaseController 'app.notification_grouping.status.unique_count' => statuses.uniq.size ) - render json: @grouped_notifications, each_serializer: REST::NotificationGroupSerializer, relationships: @relationships, group_metadata: @group_metadata + presenter = GroupedNotificationsPresenter.new(@grouped_notifications) + render json: presenter, serializer: REST::DedupNotificationGroupSerializer, relationships: @relationships, group_metadata: @group_metadata end end def show @notification = current_account.notifications.without_suspended.find_by!(group_key: params[:id]) - render json: NotificationGroup.from_notification(@notification), serializer: REST::NotificationGroupSerializer + presenter = GroupedNotificationsPresenter.new([NotificationGroup.from_notification(@notification)]) + render json: presenter, serializer: REST::DedupNotificationGroupSerializer end def clear diff --git a/app/serializers/rest/notification_group_serializer.rb b/app/serializers/rest/notification_group_serializer.rb deleted file mode 100644 index 749f717754..0000000000 --- a/app/serializers/rest/notification_group_serializer.rb +++ /dev/null @@ -1,51 +0,0 @@ -# frozen_string_literal: true - -class REST::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 - - attribute :page_min_id, if: :paginated? - attribute :page_max_id, if: :paginated? - attribute :latest_page_notification_at, if: :paginated? - - has_many :sample_accounts, serializer: REST::AccountSerializer - belongs_to :target_status, key: :status, if: :status_type?, serializer: REST::StatusSerializer - belongs_to :report, if: :report_type?, serializer: REST::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::AccountWarningSerializer - - def status_type? - [:favourite, :reblog, :status, :mention, :poll, :update].include?(object.type) - 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 diff --git a/spec/requests/api/v2_alpha/notifications_spec.rb b/spec/requests/api/v2_alpha/notifications_spec.rb index 104651ebe3..20c94fa9c4 100644 --- a/spec/requests/api/v2_alpha/notifications_spec.rb +++ b/spec/requests/api/v2_alpha/notifications_spec.rb @@ -58,7 +58,7 @@ RSpec.describe 'Notifications' do expect(response).to have_http_status(200) expect(body_json_types.uniq).to eq ['mention'] - expect(body_as_json[0][:page_min_id]).to_not be_nil + expect(body_as_json.dig(:notification_groups, 0, :page_min_id)).to_not be_nil end end @@ -84,7 +84,7 @@ RSpec.describe 'Notifications' do end def body_json_types - body_as_json.pluck(:type) + body_as_json[:notification_groups].pluck(:type) end end