Ensure counters are up to date when rendering inbox/notifications views

This commit is contained in:
Karina Kwiatek 2023-06-16 18:19:31 +02:00
parent ece64669a1
commit 5a3f65e39a
2 changed files with 9 additions and 15 deletions

View File

@ -3,8 +3,6 @@
class InboxController < ApplicationController class InboxController < ApplicationController
before_action :authenticate_user! before_action :authenticate_user!
after_action :mark_inbox_entries_as_read, only: %i[show]
def show # rubocop:disable Metrics/MethodLength def show # rubocop:disable Metrics/MethodLength
find_author find_author
find_inbox_entries find_inbox_entries
@ -19,14 +17,11 @@ class InboxController < ApplicationController
@delete_id = find_delete_id @delete_id = find_delete_id
@disabled = true if @inbox.empty? @disabled = true if @inbox.empty?
respond_to do |format| mark_inbox_entries_as_read
format.html { render "show" }
format.turbo_stream do
render "show", layout: false, status: :see_other
# rubocop disabled as just flipping a flag doesn't need to have validations to be run respond_to do |format|
@inbox.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations format.html
end format.turbo_stream
end end
end end
@ -85,8 +80,8 @@ class InboxController < ApplicationController
# rubocop:disable Rails/SkipsModelValidations # rubocop:disable Rails/SkipsModelValidations
def mark_inbox_entries_as_read def mark_inbox_entries_as_read
# using .dup to not modify @inbox -- useful in tests # using .dup to not modify @inbox -- useful in tests
@inbox&.dup&.update_all(new: false) updated = @inbox&.dup&.update_all(new: false)
current_user.touch(:inbox_updated_at) current_user.touch(:inbox_updated_at) if updated.positive?
end end
# rubocop:enable Rails/SkipsModelValidations # rubocop:enable Rails/SkipsModelValidations

View File

@ -3,8 +3,6 @@
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,
@ -18,6 +16,7 @@ class NotificationsController < ApplicationController
@notifications = cursored_notifications_for(type: @type, last_id: params[:last_id]) @notifications = cursored_notifications_for(type: @type, last_id: params[:last_id])
paginate_notifications paginate_notifications
@counters = count_unread_by_type @counters = count_unread_by_type
mark_notifications_as_read
respond_to do |format| respond_to do |format|
format.html format.html
@ -52,8 +51,8 @@ class NotificationsController < ApplicationController
# rubocop:disable Rails/SkipsModelValidations # rubocop:disable Rails/SkipsModelValidations
def mark_notifications_as_read def mark_notifications_as_read
# using .dup to not modify @notifications -- useful in tests # using .dup to not modify @notifications -- useful in tests
@notifications&.dup&.update_all(new: false) updated = @notifications&.dup&.update_all(new: false)
current_user.touch(:notifications_updated_at) current_user.touch(:notifications_updated_at) if updated.positive?
end end
# rubocop:enable Rails/SkipsModelValidations # rubocop:enable Rails/SkipsModelValidations