Skip to content

Commit 5dddb80

Browse files
committed
Action Cable channels merged
Also, proactively send notification counts on reconnection
1 parent 44ac24c commit 5dddb80

6 files changed

Lines changed: 31 additions & 30 deletions

File tree

app/channels/notification_counts_channel.rb

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
class WebNotificationsChannel < ApplicationCable::Channel
22
def subscribed
3+
# Open streaming
34
stream_for current_user
5+
6+
# Proactively send count on subscription
7+
transmit({
8+
kind: 'notification_count',
9+
count: current_user.unseen_notifications.count
10+
})
411
end
512
end

app/javascript/channels/notification_counts_channel.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

app/javascript/channels/web_notifications_channel.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@ const wn_struct = {
66
},
77

88
received(data) {
9+
switch (data["kind"]) {
10+
case "web_notification":
11+
this.web_notification(data);
12+
break;
13+
case "notification_count":
14+
this.notification_count(data);
15+
break;
16+
}
17+
},
18+
19+
// Web Notifications (individual event)
20+
web_notification(data) {
921
if (Notification.permission === "granted") {
1022
this.browser_notification(data);
1123
}
@@ -33,6 +45,13 @@ const wn_struct = {
3345

3446
toast_notification(data) {
3547
new_toast("", data["title"], ROOT_PATH + "alerts/" + data["alert"]);
48+
},
49+
50+
// Notification Count (aggregated)
51+
notification_count(data) {
52+
var badge = $("#notification-count-badge");
53+
badge.html(data["count"] == 0 ? "" : data["count"]);
54+
badge.data("count", data["count"]);
3655
}
3756
};
3857

app/models/notification.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def send_email_notification
2525
def broadcast_web_notification
2626
WebNotificationsChannel.broadcast_to(
2727
user,
28+
kind: 'web_notification',
2829
title: title || action,
2930
tag: '%s:%s' % [notifiable.class.to_s, notifiable.id],
3031
alert: id
@@ -34,8 +35,10 @@ def broadcast_web_notification
3435
end
3536

3637
def broadcast_notification_count
37-
NotificationCountsChannel.broadcast_to(
38-
user, count: user.unseen_notifications.count
38+
WebNotificationsChannel.broadcast_to(
39+
user,
40+
kind: 'notification_count',
41+
count: user.unseen_notifications.count
3942
)
4043
rescue => e
4144
Rails.logger.error e

test/channels/notification_counts_channel_test.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)