chore: Add unread notification count API (#1646)
Co-authored-by: Sojan <sojan@pepalo.com>
This commit is contained in:
@@ -27,6 +27,11 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro
|
|||||||
render json: @notification
|
render json: @notification
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def unread_count
|
||||||
|
@unread_count = current_user.notifications.where(account_id: current_account.id, read_at: nil).count
|
||||||
|
render json: @unread_count
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def set_primary_actor
|
def set_primary_actor
|
||||||
|
|||||||
@@ -92,6 +92,7 @@ Rails.application.routes.draw do
|
|||||||
resources :notifications, only: [:index, :update] do
|
resources :notifications, only: [:index, :update] do
|
||||||
collection do
|
collection do
|
||||||
post :read_all
|
post :read_all
|
||||||
|
get :unread_count
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
resource :notification_settings, only: [:show, :update]
|
resource :notification_settings, only: [:show, :update]
|
||||||
|
|||||||
@@ -102,4 +102,29 @@ RSpec.describe 'Notifications API', type: :request do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'GET /api/v1/accounts/{account.id}/notifications/unread_count' do
|
||||||
|
context 'when it is an unauthenticated user' do
|
||||||
|
it 'returns unauthorized' do
|
||||||
|
get "/api/v1/accounts/#{account.id}/notifications/unread_count"
|
||||||
|
|
||||||
|
expect(response).to have_http_status(:unauthorized)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when it is an authenticated user' do
|
||||||
|
let(:admin) { create(:user, account: account, role: :administrator) }
|
||||||
|
|
||||||
|
it 'returns notifications unread count' do
|
||||||
|
2.times.each { create(:notification, account: account, user: admin) }
|
||||||
|
get "/api/v1/accounts/#{account.id}/notifications/unread_count",
|
||||||
|
headers: admin.create_new_auth_token,
|
||||||
|
as: :json
|
||||||
|
|
||||||
|
response_json = JSON.parse(response.body)
|
||||||
|
expect(response).to have_http_status(:success)
|
||||||
|
expect(response_json).to eq 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user