From bb2d77b4a07e95e7d96df080f2d6994c1422b0c0 Mon Sep 17 00:00:00 2001 From: Claire Date: Thu, 20 Jun 2024 17:54:50 +0200 Subject: [PATCH] Change `/api/v2_alpha/notifications` to only return historical data in pages (#30781) --- app/controllers/api/v2_alpha/notifications_controller.rb | 2 +- app/models/notification_group.rb | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v2_alpha/notifications_controller.rb b/app/controllers/api/v2_alpha/notifications_controller.rb index 19d3ac9018f..edba23ab4a8 100644 --- a/app/controllers/api/v2_alpha/notifications_controller.rb +++ b/app/controllers/api/v2_alpha/notifications_controller.rb @@ -15,7 +15,7 @@ class Api::V2Alpha::NotificationsController < Api::BaseController @relationships = StatusRelationshipsPresenter.new(target_statuses_from_notifications, current_user&.account_id) end - render json: @notifications.map { |notification| NotificationGroup.from_notification(notification) }, each_serializer: REST::NotificationGroupSerializer, relationships: @relationships, group_metadata: @group_metadata + render json: @notifications.map { |notification| NotificationGroup.from_notification(notification, max_id: @group_metadata.dig(notification.group_key, :max_id)) }, each_serializer: REST::NotificationGroupSerializer, relationships: @relationships, group_metadata: @group_metadata end def show diff --git a/app/models/notification_group.rb b/app/models/notification_group.rb index 093fdbe33ce..b1cbd7c19ab 100644 --- a/app/models/notification_group.rb +++ b/app/models/notification_group.rb @@ -3,13 +3,16 @@ class NotificationGroup < ActiveModelSerializers::Model attributes :group_key, :sample_accounts, :notifications_count, :notification, :most_recent_notification_id - def self.from_notification(notification) + def self.from_notification(notification, max_id: nil) if notification.group_key.present? # TODO: caching and preloading - most_recent_notifications = notification.account.notifications.where(group_key: notification.group_key).order(id: :desc).take(3) + scope = notification.account.notifications.where(group_key: notification.group_key) + scope = scope.where(id: ..max_id) if max_id.present? + + most_recent_notifications = scope.order(id: :desc).take(3) most_recent_id = most_recent_notifications.first.id sample_accounts = most_recent_notifications.map(&:from_account) - notifications_count = notification.account.notifications.where(group_key: notification.group_key).count + notifications_count = scope.count else most_recent_id = notification.id sample_accounts = [notification.from_account]