mirror of
https://github.com/glitch-soc/mastodon.git
synced 2024-11-23 16:44:04 -05:00
Add endpoints for unread notifications count (#31191)
This commit is contained in:
parent
2ce99c51dd
commit
598ae4f2da
@ -30,10 +30,10 @@ class Api::BaseController < ApplicationController
|
|||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def limit_param(default_limit)
|
def limit_param(default_limit, max_limit = nil)
|
||||||
return default_limit unless params[:limit]
|
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
|
end
|
||||||
|
|
||||||
def params_slice(*keys)
|
def params_slice(*keys)
|
||||||
|
@ -7,6 +7,8 @@ class Api::V1::NotificationsController < Api::BaseController
|
|||||||
after_action :insert_pagination_headers, only: :index
|
after_action :insert_pagination_headers, only: :index
|
||||||
|
|
||||||
DEFAULT_NOTIFICATIONS_LIMIT = 40
|
DEFAULT_NOTIFICATIONS_LIMIT = 40
|
||||||
|
DEFAULT_NOTIFICATIONS_COUNT_LIMIT = 100
|
||||||
|
MAX_NOTIFICATIONS_COUNT_LIMIT = 1_000
|
||||||
|
|
||||||
def index
|
def index
|
||||||
with_read_replica do
|
with_read_replica do
|
||||||
@ -17,6 +19,14 @@ class Api::V1::NotificationsController < Api::BaseController
|
|||||||
render json: @notifications, each_serializer: REST::NotificationSerializer, relationships: @relationships
|
render json: @notifications, each_serializer: REST::NotificationSerializer, relationships: @relationships
|
||||||
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_by_min_id(limit, notification_marker&.last_read_id).count }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@notification = current_account.notifications.without_suspended.find(params[:id])
|
@notification = current_account.notifications.without_suspended.find(params[:id])
|
||||||
render json: @notification, serializer: REST::NotificationSerializer
|
render json: @notification, serializer: REST::NotificationSerializer
|
||||||
@ -54,6 +64,10 @@ class Api::V1::NotificationsController < Api::BaseController
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def notification_marker
|
||||||
|
current_user.markers.find_by(timeline: 'notifications')
|
||||||
|
end
|
||||||
|
|
||||||
def target_statuses_from_notifications
|
def target_statuses_from_notifications
|
||||||
@notifications.reject { |notification| notification.target_status.nil? }.map(&:target_status)
|
@notifications.reject { |notification| notification.target_status.nil? }.map(&:target_status)
|
||||||
end
|
end
|
||||||
|
@ -7,6 +7,8 @@ class Api::V2Alpha::NotificationsController < Api::BaseController
|
|||||||
after_action :insert_pagination_headers, only: :index
|
after_action :insert_pagination_headers, only: :index
|
||||||
|
|
||||||
DEFAULT_NOTIFICATIONS_LIMIT = 40
|
DEFAULT_NOTIFICATIONS_LIMIT = 40
|
||||||
|
DEFAULT_NOTIFICATIONS_COUNT_LIMIT = 100
|
||||||
|
MAX_NOTIFICATIONS_COUNT_LIMIT = 1_000
|
||||||
|
|
||||||
def index
|
def index
|
||||||
with_read_replica do
|
with_read_replica do
|
||||||
@ -35,6 +37,14 @@ class Api::V2Alpha::NotificationsController < Api::BaseController
|
|||||||
end
|
end
|
||||||
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
|
def show
|
||||||
@notification = current_account.notifications.without_suspended.find_by!(group_key: params[:id])
|
@notification = current_account.notifications.without_suspended.find_by!(group_key: params[:id])
|
||||||
render json: NotificationGroup.from_notification(@notification), serializer: REST::NotificationGroupSerializer
|
render json: NotificationGroup.from_notification(@notification), serializer: REST::NotificationGroupSerializer
|
||||||
@ -92,6 +102,10 @@ class Api::V2Alpha::NotificationsController < Api::BaseController
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def notification_marker
|
||||||
|
current_user.markers.find_by(timeline: 'notifications')
|
||||||
|
end
|
||||||
|
|
||||||
def target_statuses_from_notifications
|
def target_statuses_from_notifications
|
||||||
@notifications.filter_map(&:target_status)
|
@notifications.filter_map(&:target_status)
|
||||||
end
|
end
|
||||||
|
@ -167,6 +167,7 @@ namespace :api, format: false do
|
|||||||
resources :notifications, only: [:index, :show] do
|
resources :notifications, only: [:index, :show] do
|
||||||
collection do
|
collection do
|
||||||
post :clear
|
post :clear
|
||||||
|
get :unread_count
|
||||||
end
|
end
|
||||||
|
|
||||||
member do
|
member do
|
||||||
@ -336,6 +337,7 @@ namespace :api, format: false do
|
|||||||
resources :notifications, only: [:index, :show] do
|
resources :notifications, only: [:index, :show] do
|
||||||
collection do
|
collection do
|
||||||
post :clear
|
post :clear
|
||||||
|
get :unread_count
|
||||||
end
|
end
|
||||||
|
|
||||||
member do
|
member do
|
||||||
|
@ -8,6 +8,83 @@ RSpec.describe 'Notifications' do
|
|||||||
let(:scopes) { 'read:notifications write:notifications' }
|
let(:scopes) { 'read:notifications write:notifications' }
|
||||||
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
|
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
|
||||||
|
|
||||||
|
describe 'GET /api/v1/notifications/unread_count', :inline_jobs do
|
||||||
|
subject do
|
||||||
|
get '/api/v1/notifications/unread_count', headers: headers, params: params
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:params) { {} }
|
||||||
|
|
||||||
|
before do
|
||||||
|
first_status = PostStatusService.new.call(user.account, text: 'Test')
|
||||||
|
ReblogService.new.call(Fabricate(:account), first_status)
|
||||||
|
PostStatusService.new.call(Fabricate(:account), text: 'Hello @alice')
|
||||||
|
FavouriteService.new.call(Fabricate(:account), first_status)
|
||||||
|
FavouriteService.new.call(Fabricate(:account), first_status)
|
||||||
|
FollowService.new.call(Fabricate(:account), user.account)
|
||||||
|
end
|
||||||
|
|
||||||
|
it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
|
||||||
|
|
||||||
|
context 'with no options' do
|
||||||
|
it 'returns expected notifications count' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(response).to have_http_status(200)
|
||||||
|
expect(body_as_json[:count]).to eq 5
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with a read marker' do
|
||||||
|
before do
|
||||||
|
id = user.account.notifications.browserable.order(id: :desc).offset(2).first.id
|
||||||
|
user.markers.create!(timeline: 'notifications', last_read_id: id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns expected notifications count' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(response).to have_http_status(200)
|
||||||
|
expect(body_as_json[:count]).to eq 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with exclude_types param' do
|
||||||
|
let(:params) { { exclude_types: %w(mention) } }
|
||||||
|
|
||||||
|
it 'returns expected notifications count' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(response).to have_http_status(200)
|
||||||
|
expect(body_as_json[:count]).to eq 4
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with a user-provided limit' do
|
||||||
|
let(:params) { { limit: 2 } }
|
||||||
|
|
||||||
|
it 'returns a capped value' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(response).to have_http_status(200)
|
||||||
|
expect(body_as_json[:count]).to eq 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when there are more notifications than the limit' do
|
||||||
|
before do
|
||||||
|
stub_const('Api::V1::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT', 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns a capped value' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(response).to have_http_status(200)
|
||||||
|
expect(body_as_json[:count]).to eq Api::V1::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'GET /api/v1/notifications', :inline_jobs do
|
describe 'GET /api/v1/notifications', :inline_jobs do
|
||||||
subject do
|
subject do
|
||||||
get '/api/v1/notifications', headers: headers, params: params
|
get '/api/v1/notifications', headers: headers, params: params
|
||||||
|
@ -8,6 +8,83 @@ RSpec.describe 'Notifications' do
|
|||||||
let(:scopes) { 'read:notifications write:notifications' }
|
let(:scopes) { 'read:notifications write:notifications' }
|
||||||
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
|
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
|
||||||
|
|
||||||
|
describe 'GET /api/v2_alpha/notifications/unread_count', :inline_jobs do
|
||||||
|
subject do
|
||||||
|
get '/api/v2_alpha/notifications/unread_count', headers: headers, params: params
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:params) { {} }
|
||||||
|
|
||||||
|
before do
|
||||||
|
first_status = PostStatusService.new.call(user.account, text: 'Test')
|
||||||
|
ReblogService.new.call(Fabricate(:account), first_status)
|
||||||
|
PostStatusService.new.call(Fabricate(:account), text: 'Hello @alice')
|
||||||
|
FavouriteService.new.call(Fabricate(:account), first_status)
|
||||||
|
FavouriteService.new.call(Fabricate(:account), first_status)
|
||||||
|
FollowService.new.call(Fabricate(:account), user.account)
|
||||||
|
end
|
||||||
|
|
||||||
|
it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
|
||||||
|
|
||||||
|
context 'with no options' do
|
||||||
|
it 'returns expected notifications count' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(response).to have_http_status(200)
|
||||||
|
expect(body_as_json[:count]).to eq 4
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with a read marker' do
|
||||||
|
before do
|
||||||
|
id = user.account.notifications.browserable.order(id: :desc).offset(2).first.id
|
||||||
|
user.markers.create!(timeline: 'notifications', last_read_id: id)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns expected notifications count' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(response).to have_http_status(200)
|
||||||
|
expect(body_as_json[:count]).to eq 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with exclude_types param' do
|
||||||
|
let(:params) { { exclude_types: %w(mention) } }
|
||||||
|
|
||||||
|
it 'returns expected notifications count' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(response).to have_http_status(200)
|
||||||
|
expect(body_as_json[:count]).to eq 3
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'with a user-provided limit' do
|
||||||
|
let(:params) { { limit: 2 } }
|
||||||
|
|
||||||
|
it 'returns a capped value' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(response).to have_http_status(200)
|
||||||
|
expect(body_as_json[:count]).to eq 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when there are more notifications than the limit' do
|
||||||
|
before do
|
||||||
|
stub_const('Api::V2Alpha::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT', 2)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns a capped value' do
|
||||||
|
subject
|
||||||
|
|
||||||
|
expect(response).to have_http_status(200)
|
||||||
|
expect(body_as_json[:count]).to eq Api::V2Alpha::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe 'GET /api/v2_alpha/notifications', :inline_jobs do
|
describe 'GET /api/v2_alpha/notifications', :inline_jobs do
|
||||||
subject do
|
subject do
|
||||||
get '/api/v2_alpha/notifications', headers: headers, params: params
|
get '/api/v2_alpha/notifications', headers: headers, params: params
|
||||||
|
Loading…
Reference in New Issue
Block a user