Add endpoints for unread notifications count (#31191)
This commit is contained in:
parent
2ce99c51dd
commit
598ae4f2da
6 changed files with 186 additions and 2 deletions
|
@ -30,10 +30,10 @@ class Api::BaseController < ApplicationController
|
|||
|
||||
protected
|
||||
|
||||
def limit_param(default_limit)
|
||||
def limit_param(default_limit, max_limit = nil)
|
||||
return default_limit unless params[:limit]
|
||||
|
||||
[params[:limit].to_i.abs, default_limit * 2].min
|
||||
[params[:limit].to_i.abs, max_limit || (default_limit * 2)].min
|
||||
end
|
||||
|
||||
def params_slice(*keys)
|
||||
|
|
|
@ -7,6 +7,8 @@ class Api::V1::NotificationsController < Api::BaseController
|
|||
after_action :insert_pagination_headers, only: :index
|
||||
|
||||
DEFAULT_NOTIFICATIONS_LIMIT = 40
|
||||
DEFAULT_NOTIFICATIONS_COUNT_LIMIT = 100
|
||||
MAX_NOTIFICATIONS_COUNT_LIMIT = 1_000
|
||||
|
||||
def index
|
||||
with_read_replica do
|
||||
|
@ -17,6 +19,14 @@ class Api::V1::NotificationsController < Api::BaseController
|
|||
render json: @notifications, each_serializer: REST::NotificationSerializer, relationships: @relationships
|
||||
end
|
||||
|
||||
def unread_count
|
||||
limit = limit_param(DEFAULT_NOTIFICATIONS_COUNT_LIMIT, MAX_NOTIFICATIONS_COUNT_LIMIT)
|
||||
|
||||
with_read_replica do
|
||||
render json: { count: browserable_account_notifications.paginate_by_min_id(limit, notification_marker&.last_read_id).count }
|
||||
end
|
||||
end
|
||||
|
||||
def show
|
||||
@notification = current_account.notifications.without_suspended.find(params[:id])
|
||||
render json: @notification, serializer: REST::NotificationSerializer
|
||||
|
@ -54,6 +64,10 @@ class Api::V1::NotificationsController < Api::BaseController
|
|||
)
|
||||
end
|
||||
|
||||
def notification_marker
|
||||
current_user.markers.find_by(timeline: 'notifications')
|
||||
end
|
||||
|
||||
def target_statuses_from_notifications
|
||||
@notifications.reject { |notification| notification.target_status.nil? }.map(&:target_status)
|
||||
end
|
||||
|
|
|
@ -7,6 +7,8 @@ class Api::V2Alpha::NotificationsController < Api::BaseController
|
|||
after_action :insert_pagination_headers, only: :index
|
||||
|
||||
DEFAULT_NOTIFICATIONS_LIMIT = 40
|
||||
DEFAULT_NOTIFICATIONS_COUNT_LIMIT = 100
|
||||
MAX_NOTIFICATIONS_COUNT_LIMIT = 1_000
|
||||
|
||||
def index
|
||||
with_read_replica do
|
||||
|
@ -35,6 +37,14 @@ class Api::V2Alpha::NotificationsController < Api::BaseController
|
|||
end
|
||||
end
|
||||
|
||||
def unread_count
|
||||
limit = limit_param(DEFAULT_NOTIFICATIONS_COUNT_LIMIT, MAX_NOTIFICATIONS_COUNT_LIMIT)
|
||||
|
||||
with_read_replica do
|
||||
render json: { count: browserable_account_notifications.paginate_groups_by_min_id(limit, min_id: notification_marker&.last_read_id).count }
|
||||
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
|
||||
|
@ -92,6 +102,10 @@ class Api::V2Alpha::NotificationsController < Api::BaseController
|
|||
)
|
||||
end
|
||||
|
||||
def notification_marker
|
||||
current_user.markers.find_by(timeline: 'notifications')
|
||||
end
|
||||
|
||||
def target_statuses_from_notifications
|
||||
@notifications.filter_map(&:target_status)
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue