From 81a6c6ac55aa40b36f32244efc5a2d50ad404df7 Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Thu, 2 Feb 2023 00:55:31 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Prevent=20=F0=9D=91=9B+1=20for=20notificati?= =?UTF-8?q?on=20type=20counters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/notifications_controller.rb | 3 +++ app/views/tabs/_notifications.html.haml | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 4f2dfdeb..8572f8ff 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -18,6 +18,9 @@ class NotificationsController < ApplicationController @notifications = cursored_notifications_for(type: @type, last_id: params[:last_id]) @notifications_last_id = @notifications.map(&:id).min @more_data_available = !cursored_notifications_for(type: @type, last_id: @notifications_last_id, size: 1).count.zero? + @counters = Notification.where(recipient: current_user, new: true) + .group(:target_type) + .count(:target_type) respond_to do |format| format.html diff --git a/app/views/tabs/_notifications.html.haml b/app/views/tabs/_notifications.html.haml index ad4d7a63..98ba302a 100644 --- a/app/views/tabs/_notifications.html.haml +++ b/app/views/tabs/_notifications.html.haml @@ -9,18 +9,18 @@ .list-group = list_group_item t('.answer'), notifications_path('answer'), - badge: Notification.for(current_user).where(target_type: 'Answer', new: true).count + badge: @counters['Answer'] = list_group_item t('.smile'), notifications_path('smile'), - badge: Notification.for(current_user).where(target_type: 'Smile', new: true).count + badge: @counters['Smile'] = list_group_item t('.comment'), notifications_path('comment'), - badge: Notification.for(current_user).where(target_type: 'Comment', new: true).count + badge: @counters['Comment'] = list_group_item t('.commentsmile'), notifications_path('commentsmile'), - badge: Notification.for(current_user).where(target_type: 'CommentSmile', new: true).count + badge: @counters['CommentSmile'] = list_group_item t('.relationship'), notifications_path('relationship'), - badge: Notification.for(current_user).where(target_type: 'Relationship', new: true).count + badge: @counters['Relationship'] .d-none.d-sm-block= render 'shared/links' From 80e312eb3b140392f0fa351add29baf02073baba Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Thu, 2 Feb 2023 10:38:41 +0100 Subject: [PATCH 2/3] Appease the dog overlords --- app/controllers/notifications_controller.rb | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/app/controllers/notifications_controller.rb b/app/controllers/notifications_controller.rb index 8572f8ff..860dd3ec 100644 --- a/app/controllers/notifications_controller.rb +++ b/app/controllers/notifications_controller.rb @@ -16,11 +16,8 @@ class NotificationsController < ApplicationController def index @type = TYPE_MAPPINGS[params[:type]] || params[:type] @notifications = cursored_notifications_for(type: @type, last_id: params[:last_id]) - @notifications_last_id = @notifications.map(&:id).min - @more_data_available = !cursored_notifications_for(type: @type, last_id: @notifications_last_id, size: 1).count.zero? - @counters = Notification.where(recipient: current_user, new: true) - .group(:target_type) - .count(:target_type) + paginate_notifications + @counters = count_unread_by_type respond_to do |format| format.html @@ -30,6 +27,17 @@ class NotificationsController < ApplicationController private + def paginate_notifications + @notifications_last_id = @notifications.map(&:id).min + @more_data_available = !cursored_notifications_for(type: @type, last_id: @notifications_last_id, size: 1).count.zero? + end + + def count_unread_by_type + Notification.where(recipient: current_user, new: true) + .group(:target_type) + .count(:target_type) + end + def mark_notifications_as_read # using .dup to not modify @notifications -- useful in tests @notifications&.dup&.update_all(new: false) # rubocop:disable Rails/SkipsModelValidations From 40f7f8f525b67c5ffc7ebb9478736f08970b606a Mon Sep 17 00:00:00 2001 From: Karina Kwiatek Date: Thu, 2 Feb 2023 11:03:27 +0100 Subject: [PATCH 3/3] Eager load notification targets --- app/models/notification.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/models/notification.rb b/app/models/notification.rb index 0bf19ff7..60890d17 100644 --- a/app/models/notification.rb +++ b/app/models/notification.rb @@ -11,11 +11,11 @@ class Notification < ApplicationRecord define_cursor_paginator :cursored_for_type, :for_type def for(recipient, **kwargs) - where(kwargs.merge!(recipient:)).order(:created_at).reverse_order + where(kwargs.merge!(recipient:)).includes(:target).order(:created_at).reverse_order end def for_type(recipient, type, **kwargs) - where(kwargs.merge!(recipient:)).where(type:).order(:created_at).reverse_order + where(kwargs.merge!(recipient:)).includes(:target).where(type:).order(:created_at).reverse_order end def notify(recipient, target)