Skip to content

Commit 22990f4

Browse files
committed
Move unread notification count from webui to notification controller
1 parent 0465fa3 commit 22990f4

File tree

8 files changed

+21
-21
lines changed

8 files changed

+21
-21
lines changed

src/api/app/controllers/webui/users/notifications_controller.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ class Webui::Users::NotificationsController < Webui::WebuiController
3232
before_action :set_ordered_notifications, only: :index
3333
before_action :paginate_notifications, only: :index
3434

35-
skip_before_action :set_unread_notifications_count, only: :update
36-
3735
def index; end
3836

3937
def update
@@ -44,7 +42,6 @@ def update
4442
# rubocop:enable Rails/SkipsModelValidations
4543

4644
# manually update the count and the filtered subset after the update
47-
set_unread_notifications_count # before_action filter method defined in the Webui controller
4845
filter_notifications
4946
set_preloaded_notifications
5047
set_ordered_notifications
@@ -55,7 +52,7 @@ def update
5552
format.js do
5653
render partial: 'update', locals: {
5754
notifications: @notifications,
58-
unread_notifications_count: @unread_notifications_count,
55+
unread_notifications_count: unread_notifications.count,
5956
selected_filter: @selected_filter,
6057
total_count_notifications: @notifications.count,
6158
user: User.session
@@ -73,7 +70,7 @@ def autocomplete_projects
7370
def count_for_notification_types
7471
counted_notifications = {}
7572
# notifiable_type: 'Report', 'WorkflowRun', 'Decision', 'Comment', 'BsRequest', 'Group'
76-
counted_notifiable_types = @notifications.unread.group(:notifiable_type).count
73+
counted_notifiable_types = unread_notifications.group(:notifiable_type).count
7774

7875
NOTIFICATION_TYPES_KEY_MAP.each do |notifications_key, notification_types_key|
7976
counted_notifications[notifications_key] = counted_notifiable_types[notification_types_key] || 0
@@ -87,6 +84,7 @@ def count_for_event_types
8784

8885
# event_type: 'Event::RelationshipCreate', 'Event::RelationshipDelete', 'Event::BuildFail',
8986
counted_event_types = @notifications.unread.group(:event_type).count
87+
9088
EVENT_TYPES_KEY_MAP.each do |notifications_key, event_types_key|
9189
counted_notifications[notifications_key] = counted_event_types[event_types_key] || 0
9290
end
@@ -101,18 +99,26 @@ def count_for_notification_kinds
10199
when 'all'
102100
count = @notifications.count
103101
when 'unread'
104-
count = @unread_notifications_count # Variable set in the Webui controller
102+
count = unread_notifications.count
105103
when 'incoming_requests'
106-
count = @notifications.unread.for_incoming_requests(User.session).count
104+
count = unread_notifications.for_incoming_requests(User.session).count
107105
when 'outgoing_requests'
108-
count = @notifications.unread.for_outgoing_requests(User.session).count
106+
count = unread_notifications.for_outgoing_requests(User.session).count
109107
end
110108

111109
render partial: 'counter', locals: { id: "count_#{params[:notification_kind]}", count: count }
112110
end
113111

112+
def count_for_unread
113+
render partial: 'unread_counter', locals: { count: unread_notifications.count }
114+
end
115+
114116
private
115117

118+
def unread_notifications
119+
@notifications.unread
120+
end
121+
116122
def set_filter_kind
117123
@filter_kind = Array(params[:kind].presence || 'all')
118124
@filter_kind.reject! { |kind| ALLOWED_FILTERS.exclude?(kind) }

src/api/app/controllers/webui/webui_controller.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class Webui::WebuiController < ActionController::Base
2424
before_action :current_announcement, unless: -> { request.xhr? }
2525
before_action :fetch_watchlist_items
2626
before_action :set_paper_trail_whodunnit
27-
before_action :set_unread_notifications_count, unless: -> { request.xhr? }
2827

2928
# :notice and :alert are default, we add :success and :error
3029
add_flash_types :success, :error
@@ -184,10 +183,6 @@ def fetch_watchlist_items
184183
end
185184
end
186185

187-
def set_unread_notifications_count
188-
@unread_notifications_count = User.session ? User.session.unread_notifications_count : 0
189-
end
190-
191186
def add_arrays(arr1, arr2)
192187
# we assert that both have the same size
193188
ret = []

src/api/app/models/user.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -741,10 +741,6 @@ def tasks
741741
end
742742
end
743743

744-
def unread_notifications_count
745-
notifications.for_web.unread.size
746-
end
747-
748744
def update_globalroles(global_roles)
749745
roles.replace(global_roles + roles.where(global: false))
750746
end

src/api/app/views/layouts/webui/_bottom_navigation.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
%span.d-block Watchlist
88
%li.nav-item.border-start.border-gray-500
99
= link_to(my_notifications_path, id: 'bottom-notifications-counter', class: 'nav-link text-light px-1 py-2', alt: 'Notifications') do
10-
= render partial: 'layouts/webui/unread_notifications_counter', locals: { unread_notifications_count: unread_notifications_count }
10+
= render partial: 'layouts/webui/unread_notifications_counter'
1111
- if content_for?(:actions)
1212
%li.nav-item.border-start.border-gray-500
1313
= link_to('javascript:void(0)', class: 'nav-link px-1 py-2 text-light', alt: 'Actions', data: { toggle: 'actions' }) do

src/api/app/views/layouts/webui/_top_navigation.html.haml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
%span.d-block Watchlist
1212
.toggler.text-center.justify-content-center.nav-item
1313
= link_to(my_notifications_path, id: 'top-notifications-counter', class: 'nav-link p-0 w-100', alt: 'Notifications') do
14-
= render partial: 'layouts/webui/unread_notifications_counter', locals: { unread_notifications_count: unread_notifications_count }
14+
= render partial: 'layouts/webui/unread_notifications_counter'
1515
.toggler.text-center.justify-content-center.nav-item.dropdown
1616
= link_to('#', class: 'nav-link dropdown-toggle', id: 'top-navigation-profile-dropdown', role: 'button',
1717
'data-bs-toggle': 'dropdown', aria: { haspopup: true, expanded: false }) do
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
.notifications-counter
22
%i.fas.fa-bell
3-
- if defined?(unread_notifications_count) && unread_notifications_count&.positive?
4-
%span.badge.text-bg-primary.align-text-top= unread_notifications_count
3+
= turbo_frame_tag 'unread_notification_count', loading: 'lazy', src: count_for_unread_my_notifications_path do
4+
%i.fas.fa-spinner.fa-spin
55
%span.d-block Notifications
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
= turbo_frame_tag 'unread_notification_count' do
2+
%span.badge.text-bg-primary.align-text-top= count

src/api/config/routes/webui.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@
383383
get :count_for_notification_types
384384
get :count_for_event_types
385385
get :count_for_notification_kinds
386+
get :count_for_unread
386387
end
387388
end
388389

0 commit comments

Comments
 (0)