mark notifications as "read" in the controller and when you see them

this makes it behave a bit more like the inbox
This commit is contained in:
Georg Gadinger 2023-01-24 15:54:41 +01:00
parent b99e1b03de
commit 58705fffba
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)
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).where(new: true).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