Prevent 𝑛+1 for notification type counters

This commit is contained in:
Karina Kwiatek 2023-02-02 00:55:31 +01:00
parent da711dcfd4
commit 81a6c6ac55
2 changed files with 8 additions and 5 deletions

View File

@ -18,6 +18,9 @@ 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])
@notifications_last_id = @notifications.map(&:id).min @notifications_last_id = @notifications.map(&:id).min
@more_data_available = !cursored_notifications_for(type: @type, last_id: @notifications_last_id, size: 1).count.zero? @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| respond_to do |format|
format.html format.html

View File

@ -9,18 +9,18 @@
.list-group .list-group
= list_group_item t('.answer'), = list_group_item t('.answer'),
notifications_path('answer'), notifications_path('answer'),
badge: Notification.for(current_user).where(target_type: 'Answer', new: true).count badge: @counters['Answer']
= list_group_item t('.smile'), = list_group_item t('.smile'),
notifications_path('smile'), notifications_path('smile'),
badge: Notification.for(current_user).where(target_type: 'Smile', new: true).count badge: @counters['Smile']
= list_group_item t('.comment'), = list_group_item t('.comment'),
notifications_path('comment'), notifications_path('comment'),
badge: Notification.for(current_user).where(target_type: 'Comment', new: true).count badge: @counters['Comment']
= list_group_item t('.commentsmile'), = list_group_item t('.commentsmile'),
notifications_path('commentsmile'), notifications_path('commentsmile'),
badge: Notification.for(current_user).where(target_type: 'CommentSmile', new: true).count badge: @counters['CommentSmile']
= list_group_item t('.relationship'), = list_group_item t('.relationship'),
notifications_path('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' .d-none.d-sm-block= render 'shared/links'