From b99e1b03de2841c7302e2c13c5e433faa3dd4245 Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Mon, 23 Jan 2023 12:25:48 +0100 Subject: [PATCH 1/3] notifications: only update all new notifications --- app/views/layouts/notifications.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/layouts/notifications.html.haml b/app/views/layouts/notifications.html.haml index d7c87e7e..dceb6d47 100644 --- a/app/views/layouts/notifications.html.haml +++ b/app/views/layouts/notifications.html.haml @@ -8,5 +8,5 @@ .d-block.d-sm-none= render "shared/links" :ruby - Notification.for(current_user).update_all(new: false) + Notification.for(current_user).where(new: true).update_all(new: false) parent_layout 'base' From 58705fffba417db7210c36500487ab7b448c3196 Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Tue, 24 Jan 2023 15:54:41 +0100 Subject: [PATCH 2/3] mark notifications as "read" in the controller and when you see them this makes it behave a bit more like the inbox --- app/controllers/notifications_controller.rb | 7 +++++++ app/views/layouts/notifications.html.haml | 1 - spec/controllers/notifications_controller_spec.rb | 4 ++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 6f6f0e91..0655c3c3 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -3,6 +3,8 @@ class NotificationsController < ApplicationController before_action :authenticate_user! + after_action :mark_notifications_as_read, only: %i[index] + TYPE_MAPPINGS = { "answer" => Notification::QuestionAnswered.name, "comment" => Notification::Commented.name, @@ -25,6 +27,11 @@ class NotificationsController < ApplicationController 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) cursor_params = { last_id: last_id, size: size }.compact diff --git a/app/views/layouts/notifications.html.haml b/app/views/layouts/notifications.html.haml index dceb6d47..08d8ad1d 100644 --- a/app/views/layouts/notifications.html.haml +++ b/app/views/layouts/notifications.html.haml @@ -8,5 +8,4 @@ .d-block.d-sm-none= render "shared/links" :ruby - Notification.for(current_user).where(new: true).update_all(new: false) parent_layout 'base' diff --git a/spec/controllers/notifications_controller_spec.rb b/spec/controllers/notifications_controller_spec.rb index f89db5fb..bcae9a8d 100644 --- a/spec/controllers/notifications_controller_spec.rb +++ b/spec/controllers/notifications_controller_spec.rb @@ -33,5 +33,9 @@ describe NotificationsController do expect(response).to render_template(:index) expect(controller.instance_variable_get(:@notifications)).to have_attributes(size: 2) 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 From 8cd0d481c8ca1d9fcf51a589905658c1e62b77be Mon Sep 17 00:00:00 2001 From: Georg Gadinger Date: Tue, 24 Jan 2023 15:57:34 +0100 Subject: [PATCH 3/3] bruh --- app/controllers/notifications_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 0655c3c3..4f2dfdeb 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -29,7 +29,7 @@ class NotificationsController < ApplicationController def mark_notifications_as_read # using .dup to not modify @notifications -- useful in tests - @notifications&.dup&.update_all(new: false) + @notifications&.dup&.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations end def cursored_notifications_for(type:, last_id:, size: nil)