chore: Add count and current page in notification meta details (#1619)

This commit is contained in:
Muhsin Keloth 2021-01-13 03:34:27 -08:00 committed by GitHub
parent 8c66dbd342
commit 2d5aa9d3bd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 1 deletions

View file

@ -3,10 +3,12 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro
before_action :fetch_notification, only: [:update] before_action :fetch_notification, only: [:update]
before_action :set_primary_actor, only: [:read_all] before_action :set_primary_actor, only: [:read_all]
before_action :set_current_page, only: [:index]
def index def index
@unread_count = current_user.notifications.where(account_id: current_account.id, read_at: nil).count @unread_count = current_user.notifications.where(account_id: current_account.id, read_at: nil).count
@notifications = current_user.notifications.where(account_id: current_account.id).page params[:page] @count = notifications.count
@notifications = notifications.page @current_page
end end
def read_all def read_all
@ -37,4 +39,12 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro
def fetch_notification def fetch_notification
@notification = current_user.notifications.find(params[:id]) @notification = current_user.notifications.find(params[:id])
end end
def set_current_page
@current_page = params[:page] || 1
end
def notifications
@notifications ||= current_user.notifications.where(account_id: current_account.id)
end
end end

View file

@ -1,6 +1,8 @@
json.data do json.data do
json.meta do json.meta do
json.unread_count @unread_count json.unread_count @unread_count
json.count @count
json.current_page @current_page
end end
json.payload do json.payload do

View file

@ -26,6 +26,7 @@ RSpec.describe 'Notifications API', type: :request do
expect(response).to have_http_status(:success) expect(response).to have_http_status(:success)
expect(response.body).to include(notification1.notification_type) expect(response.body).to include(notification1.notification_type)
expect(response_json['data']['meta']['unread_count']).to eq 2 expect(response_json['data']['meta']['unread_count']).to eq 2
expect(response_json['data']['meta']['count']).to eq 2
# notification appear in descending order # notification appear in descending order
expect(response_json['data']['payload'].first['id']).to eq notification2.id expect(response_json['data']['payload'].first['id']).to eq notification2.id
end end