DEV: Add push notification filtering to MessageBus alerts (#25965)

This commit is contained in:
Mark VanLandingham 2024-02-29 12:49:46 -06:00 committed by GitHub
parent f0baa0ddfe
commit 6c2c690479
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 49 additions and 14 deletions

View File

@ -38,6 +38,12 @@ class PostAlerter
DiscourseEvent.trigger(:pre_notification_alert, user, payload)
if user.allow_live_notifications?
send_notification =
DiscoursePluginRegistry.push_notification_filters.all? do |filter|
filter.call(user, payload)
end
if send_notification
payload =
DiscoursePluginRegistry.apply_modifier(
:post_alerter_live_notification_payload,
@ -46,6 +52,7 @@ class PostAlerter
)
MessageBus.publish("/notification-alert/#{user.id}", payload, user_ids: [user.id])
end
end
push_notification(user, payload)

View File

@ -76,7 +76,17 @@ module Jobs
}
if membership.desktop_notifications_always? && !membership.muted?
::MessageBus.publish("/chat/notification-alert/#{user.id}", payload, user_ids: [user.id])
send_notification =
DiscoursePluginRegistry.push_notification_filters.all? do |filter|
filter.call(user, payload)
end
if send_notification
::MessageBus.publish(
"/chat/notification-alert/#{user.id}",
payload,
user_ids: [user.id],
)
end
end
if membership.mobile_notifications_always? && !membership.muted?

View File

@ -85,6 +85,16 @@ RSpec.describe Jobs::Chat::NotifyWatching do
end
end
context "with push_notification_filter registered to block push notifications" do
after { DiscoursePluginRegistry.reset_register!(:push_notification_filters) }
it "doesn't send notification alert via MessageBus" do
Plugin::Instance.new.register_push_notification_filter { |user, payload| false }
expect(notification_messages_for(user2)).to be_empty
end
end
context "when the channel is muted via membership preferences" do
before { membership2.update!(muted: true) }

View File

@ -1188,21 +1188,29 @@ RSpec.describe PostAlerter do
end
describe "DiscoursePluginRegistry#push_notification_filters" do
after { DiscoursePluginRegistry.reset_register!(:push_notification_filters) }
it "sends push notifications when all filters pass" do
evil_trout.update!(last_seen_at: 10.minutes.ago)
Plugin::Instance.new.register_push_notification_filter { |user, payload| true }
alerts =
MessageBus.track_publish("/notification-alert/#{evil_trout.id}") do
expect { mention_post }.to change { Jobs::PushNotification.jobs.count }.by(1)
DiscoursePluginRegistry.reset!
end
expect(alerts).not_to be_empty
end
it "does not send push notifications when a filters returns false" do
Plugin::Instance.new.register_push_notification_filter { |user, payload| false }
alerts =
MessageBus.track_publish("/notification-alert/#{evil_trout.id}") do
expect { mention_post }.not_to change { Jobs::PushNotification.jobs.count }
end
events = DiscourseEvent.track_events { mention_post }
expect(events.find { |event| event[:event_name] == :push_notification }).not_to be_present
DiscoursePluginRegistry.reset!
expect(alerts).to be_empty
end
end