Merge pull request #994 from Retrospring/improve-notifications-update-query

notifications: only update all new notifications
This commit is contained in:
Georg Gadinger 2023-01-26 08:52:28 +01:00 committed by GitHub
commit 93a0371476
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 1 deletions

View File

@ -3,6 +3,8 @@
class NotificationsController < ApplicationController class NotificationsController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
after_action :mark_notifications_as_read, only: %i[index]
TYPE_MAPPINGS = { TYPE_MAPPINGS = {
"answer" => Notification::QuestionAnswered.name, "answer" => Notification::QuestionAnswered.name,
"comment" => Notification::Commented.name, "comment" => Notification::Commented.name,
@ -25,6 +27,11 @@ class NotificationsController < ApplicationController
private private
def mark_notifications_as_read
# using .dup to not modify @notifications -- useful in tests
@notifications&.dup&.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations
end
def cursored_notifications_for(type:, last_id:, size: nil) def cursored_notifications_for(type:, last_id:, size: nil)
cursor_params = { last_id: last_id, size: size }.compact cursor_params = { last_id: last_id, size: size }.compact

View File

@ -8,5 +8,4 @@
.d-block.d-sm-none= render "shared/links" .d-block.d-sm-none= render "shared/links"
:ruby :ruby
Notification.for(current_user).update_all(new: false)
parent_layout 'base' parent_layout 'base'

View File

@ -33,5 +33,9 @@ describe NotificationsController do
expect(response).to render_template(:index) expect(response).to render_template(:index)
expect(controller.instance_variable_get(:@notifications)).to have_attributes(size: 2) expect(controller.instance_variable_get(:@notifications)).to have_attributes(size: 2)
end end
it "marks notifications as read" do
expect { subject }.to change { Notification.for(user).where(new: true).count }.from(2).to(0)
end
end end
end end