chore: Add count and current page in notification meta details (#1619)
This commit is contained in:
parent
8c66dbd342
commit
2d5aa9d3bd
3 changed files with 14 additions and 1 deletions
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue