From 88874389d2d80e638954e28c4f4145e7a3cdd105 Mon Sep 17 00:00:00 2001 From: Mark VanLandingham Date: Wed, 1 Nov 2023 10:06:33 -0500 Subject: [PATCH] FIX: Send push notifications for category/tag watching notifications (#24196) Problem and solution are outlined here on Meta - https://meta.discourse.org/t/watching-a-category-does-not-cause-push-notifications/282794 --- app/services/post_alerter.rb | 1 + spec/services/post_alerter_spec.rb | 57 +++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 9 deletions(-) diff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rb index 75aa3644a81..871cff0526f 100644 --- a/app/services/post_alerter.rb +++ b/app/services/post_alerter.rb @@ -392,6 +392,7 @@ class PostAlerter private_message group_mentioned watching_first_post + watching_category_or_tag event_reminder event_invitation ].map { |t| Notification.types[t] } diff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rb index 18f7295f1b3..9656cdc3d13 100644 --- a/spec/services/post_alerter_spec.rb +++ b/spec/services/post_alerter_spec.rb @@ -59,6 +59,18 @@ RSpec.describe PostAlerter do PostAlerter.post_created(post) end + def setup_push_notification_subscription_for(user:) + 2.times do |i| + UserApiKey.create!( + user_id: user.id, + client_id: "xxx#{i}", + application_name: "iPhone#{i}", + scopes: ["notifications"].map { |name| UserApiKeyScope.new(name: name) }, + push_url: "https://site2.com/push", + ) + end + end + context "with private message" do it "notifies for pms correctly" do pm = Fabricate(:topic, archetype: "private_message", category_id: nil) @@ -1172,15 +1184,7 @@ RSpec.describe PostAlerter do before do SiteSetting.allowed_user_api_push_urls = "https://site.com/push|https://site2.com/push" - 2.times do |i| - UserApiKey.create!( - user_id: evil_trout.id, - client_id: "xxx#{i}", - application_name: "iPhone#{i}", - scopes: ["notifications"].map { |name| UserApiKeyScope.new(name: name) }, - push_url: "https://site2.com/push", - ) - end + setup_push_notification_subscription_for(user: evil_trout) end describe "DiscoursePluginRegistry#push_notification_filters" do @@ -1506,6 +1510,24 @@ RSpec.describe PostAlerter do params: [[user], post], ) end + + it "sends a push notification when user has a push subscription" do + setup_push_notification_subscription_for(user: user) + + level = CategoryUser.notification_levels[:watching_first_post] + CategoryUser.set_notification_level_for_category(user, level, category.id) + events = + DiscourseEvent.track_events(:push_notification) do + PostAlerter.new.after_save_post(post, true) + end + + expect( + events.detect do |e| + e[:params][0] == user && + e[:params][1][:notification_type] == Notification.types[:watching_first_post] + end, + ).to be_present + end end context "with replies" do @@ -1828,6 +1850,23 @@ RSpec.describe PostAlerter do notification_data = JSON.parse(notification.data) expect(notification_data["display_username"]).to eq(I18n.t("embed.replies", count: 2)) end + + it "sends a push notification when user has a push subscription" do + setup_push_notification_subscription_for(user: user) + + topic = Fabricate(:topic, category: category) + post = Fabricate(:post, topic: topic) + level = CategoryUser.notification_levels[:watching] + CategoryUser.set_notification_level_for_category(user, level, category.id) + events = DiscourseEvent.track_events(:push_notification) { PostAlerter.post_created(post) } + + expect( + events.detect do |e| + e[:params][0] == user && + e[:params][1][:notification_type] == Notification.types[:watching_category_or_tag] + end, + ).to be_present + end end end