A platform for community discussion. Free, open, simple.
DEV: Add :push_notification event and deprecate :post_notification_alert (#22917)
Sérgio Saquetim03690ccccf71
diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rbindex b0cb9310a2e..75aa3644a81 100644--- a/app/services/post_alerter.rb+++ b/app/services/post_alerter.rb@@ -42,6 +42,8 @@ class PostAlerterendpush_notification(user, payload)++ # deprecated. use push_notification insteadDiscourseEvent.trigger(:post_notification_alert, user, payload)endend@@ -80,6 +82,8 @@ class PostAlerterJobs.enqueue(:push_notification, clients: clients, payload: payload, user_id: user.id)endend++ DiscourseEvent.trigger(:push_notification, user, payload)enddef initialize(default_opts = {})diff --git a/lib/discourse_event.rb b/lib/discourse_event.rbindex 64a467bd77c..49051462cfe 100644--- a/lib/discourse_event.rb+++ b/lib/discourse_event.rb@@ -13,13 +13,23 @@ class DiscourseEventenddef self.on(event_name, &block)- if event_name == :user_badge_removed+ case event_name+ when :user_badge_removedDiscourse.deprecate("The :user_badge_removed event is deprecated. Please use :user_badge_revoked instead",since: "3.1.0.beta5",drop_from: "3.2.0.beta1",output_in_test: true,)+ when :post_notification_alert+ Discourse.deprecate(+ "The :post_notification_alert event is deprecated. Please use :push_notification instead",+ since: "3.2.0.beta1",+ drop_from: "3.3.0.beta1",+ output_in_test: true,+ )+ else+ # ignoreendevents[event_name] << blockdiff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rbindex 9cfb9c1350f..fe403d31d4a 100644--- a/spec/services/post_alerter_spec.rb+++ b/spec/services/post_alerter_spec.rb@@ -1194,13 +1194,30 @@ RSpec.describe PostAlerter doit "does not send push notifications when a filters returns false" doPlugin::Instance.new.register_push_notification_filter { |user, payload| false }expect { mention_post }.not_to change { Jobs::PushNotification.jobs.count }++ events = DiscourseEvent.track_events { mention_post }+ expect(events.find { |event| event[:event_name] == :push_notification }).not_to be_present+DiscoursePluginRegistry.reset!endend+ it "triggers the push notification event" do+ events = DiscourseEvent.track_events { mention_post }++ push_notification_event = events.find { |event| event[:event_name] == :push_notification }+ expect(push_notification_event).to be_present+ expect(push_notification_event[:params][0].username).to eq("eviltrout")+ expect(push_notification_event[:params][1][:username]).to eq(user.username)+ expect(push_notification_event[:params][1][:excerpt]).to eq("Hello @eviltrout ❤")+ end+it "pushes nothing to suspended users" doevil_trout.update_columns(suspended_till: 1.year.from_now)expect { mention_post }.to_not change { Jobs::PushNotification.jobs.count }++ events = DiscourseEvent.track_events { mention_post }+ expect(events.find { |event| event[:event_name] == :push_notification }).not_to be_presentendit "pushes nothing when the user is in 'do not disturb'" do@@ -1212,6 +1229,9 @@ RSpec.describe PostAlerter do)expect { mention_post }.to_not change { Jobs::PushNotification.jobs.count }++ events = DiscourseEvent.track_events { mention_post }+ expect(events.find { |event| event[:event_name] == :push_notification }).not_to be_presentendit "correctly pushes notifications if configured correctly" do@@ -1413,7 +1433,11 @@ RSpec.describe PostAlerter doendend- expect(events.size).to eq(2)+ expect(events.map { |event| event[:event_name] }).to include(+ :pre_notification_alert,+ :push_notification,+ :post_notification_alert,+ )expect(messages.size).to eq(0)expect(Jobs::PushNotification.jobs.size).to eq(1)end