From 2d5aa9d3bd4720c1504f9122fabed8ba5def679b Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Wed, 13 Jan 2021 03:34:27 -0800 Subject: [PATCH] chore: Add count and current page in notification meta details (#1619) --- .../api/v1/accounts/notifications_controller.rb | 12 +++++++++++- .../v1/accounts/notifications/index.json.jbuilder | 2 ++ .../api/v1/accounts/notifications_controller_spec.rb | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/accounts/notifications_controller.rb b/app/controllers/api/v1/accounts/notifications_controller.rb index 8ae1629ca..4a29f31c7 100644 --- a/app/controllers/api/v1/accounts/notifications_controller.rb +++ b/app/controllers/api/v1/accounts/notifications_controller.rb @@ -3,10 +3,12 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro before_action :fetch_notification, only: [:update] before_action :set_primary_actor, only: [:read_all] + before_action :set_current_page, only: [:index] def index @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 def read_all @@ -37,4 +39,12 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro def fetch_notification @notification = current_user.notifications.find(params[:id]) 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 diff --git a/app/views/api/v1/accounts/notifications/index.json.jbuilder b/app/views/api/v1/accounts/notifications/index.json.jbuilder index b69d869e6..8897baa7f 100644 --- a/app/views/api/v1/accounts/notifications/index.json.jbuilder +++ b/app/views/api/v1/accounts/notifications/index.json.jbuilder @@ -1,6 +1,8 @@ json.data do json.meta do json.unread_count @unread_count + json.count @count + json.current_page @current_page end json.payload do diff --git a/spec/controllers/api/v1/accounts/notifications_controller_spec.rb b/spec/controllers/api/v1/accounts/notifications_controller_spec.rb index 8f61cd803..fb92da8ad 100644 --- a/spec/controllers/api/v1/accounts/notifications_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/notifications_controller_spec.rb @@ -26,6 +26,7 @@ RSpec.describe 'Notifications API', type: :request do expect(response).to have_http_status(:success) expect(response.body).to include(notification1.notification_type) expect(response_json['data']['meta']['unread_count']).to eq 2 + expect(response_json['data']['meta']['count']).to eq 2 # notification appear in descending order expect(response_json['data']['payload'].first['id']).to eq notification2.id end