diff --git a/app/controllers/api/v1/accounts/notifications_controller.rb b/app/controllers/api/v1/accounts/notifications_controller.rb index 0d8cf6a47..40ed65f5f 100644 --- a/app/controllers/api/v1/accounts/notifications_controller.rb +++ b/app/controllers/api/v1/accounts/notifications_controller.rb @@ -4,12 +4,11 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro before_action :fetch_notification, only: [:update, :destroy, :snooze] 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 - @count = notifications.count - @notifications = notifications.page(@current_page).per(RESULTS_PER_PAGE) + @notifications = notification_finder.perform + @count = @notifications.count end def read_all @@ -57,11 +56,7 @@ class Api::V1::Accounts::NotificationsController < Api::V1::Accounts::BaseContro @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) + def notification_finder + @notification_finder ||= NotificationFinder.new(Current.user, Current.account, params) end end diff --git a/app/finders/notification_finder.rb b/app/finders/notification_finder.rb new file mode 100644 index 000000000..741d7c53d --- /dev/null +++ b/app/finders/notification_finder.rb @@ -0,0 +1,40 @@ +class NotificationFinder + attr_reader :current_user, :current_account, :params + + RESULTS_PER_PAGE = 15 + + def initialize(current_user, current_account, params) + @current_user = current_user + @current_account = current_account + @params = params + end + + def perform + set_up + notifications + end + + private + + def set_up + find_all_notifications + filter_by_status if params[:status] == 'snoozed' + end + + def find_all_notifications + @notifications = current_user.notifications.where(account_id: @current_account.id) + end + + def filter_by_status + @notifications = @notifications.where('snoozed_until > ?', DateTime.now.utc) + @notifications + end + + def current_page + params[:page] || 1 + end + + def notifications + @notifications.page(current_page).per(RESULTS_PER_PAGE).order(updated_at: :desc) + end +end diff --git a/spec/finders/notification_finder_spec.rb b/spec/finders/notification_finder_spec.rb new file mode 100644 index 000000000..abee7f202 --- /dev/null +++ b/spec/finders/notification_finder_spec.rb @@ -0,0 +1,34 @@ +require 'rails_helper' + +describe NotificationFinder do + subject(:notification_finder) { described_class.new(user, account, params) } + + let!(:account) { create(:account) } + let!(:user) { create(:user, account: account) } + + before do + create(:notification, account: account, user: user) + create(:notification, account: account, user: user) + create(:notification, account: account, user: user, snoozed_until: DateTime.now.utc + 1.day) + end + + describe '#perform' do + context 'with snoozed status' do + let(:params) { { status: 'snoozed' } } + + it 'filter notifications by status' do + result = notification_finder.perform + expect(result.length).to be 1 + end + end + + context 'without snoozed status' do + let(:params) { { status: 'open' } } + + it 'returns all notifications' do + result = notification_finder.perform + expect(result.length).to be 3 + end + end + end +end