A platform for community discussion. Free, open, simple.
DEV: Centralize push notification delivery into a single job (#39207)
Rafael dos Santos Silva75718576c606
diff --git a/app/jobs/regular/deliver_push_notification.rb b/app/jobs/regular/deliver_push_notification.rbnew file mode 100644index 00000000000..d75a4a2eb18--- /dev/null+++ b/app/jobs/regular/deliver_push_notification.rb@@ -0,0 +1,55 @@+# frozen_string_literal: true++module Jobs+ class DeliverPushNotification < ::Jobs::Base+ def execute(args)+ user = User.find_by(id: args[:user_id])+ push_window = SiteSetting.push_notification_time_window_mins+ if !user ||+ (+ !args[:bypass_time_window] && push_window > 0 &&+ user.seen_since?(push_window.minutes.ago)+ )+ return+ end++ payload = args[:payload].with_indifferent_access++ if SiteSetting.content_localization_enabled+ I18n.with_locale(user.effective_locale) { localize_content!(payload) }+ end++ PushNotificationPusher.push(user, payload.deep_dup) if user.push_subscriptions.exists?+ HubPushNotificationPusher.push(user, payload.deep_dup)+ end++ private++ def localize_content!(payload)+ locale = I18n.locale.to_s.sub("-", "_")++ if (topic_id = payload[:topic_id])+ topic_localization =+ TopicLocalization.find_by(topic_id: topic_id, locale: locale) ||+ TopicLocalization.matching_locale(locale).find_by(topic_id: topic_id)+ payload[:topic_title] = topic_localization.title if topic_localization+ end++ if (post_id = payload[:post_id])+ post_localization =+ PostLocalization.find_by(post_id: post_id, locale: locale) ||+ PostLocalization.matching_locale(locale).find_by(post_id: post_id)+ if post_localization+ payload[:excerpt] = Post.excerpt(+ post_localization.cooked,+ 400,+ text_entities: true,+ strip_links: true,+ remap_emoji: true,+ plain_hashtags: true,+ )+ end+ end+ end+ end+enddiff --git a/app/jobs/regular/push_notification.rb b/app/jobs/regular/push_notification.rbindex 029a3154356..1d5c940af1a 100644--- a/app/jobs/regular/push_notification.rb+++ b/app/jobs/regular/push_notification.rb@@ -1,57 +1,11 @@# frozen_string_literal: truemodule Jobs+ # @deprecated Use Jobs::DeliverPushNotification instead.+ # Kept for backward compatibility with in-flight Sidekiq jobs.class PushNotification < ::Jobs::Basedef execute(args)- user = User.find_by(id: args["user_id"])- push_window = SiteSetting.push_notification_time_window_mins- return if !user || (push_window > 0 && user.seen_since?(push_window.minutes.ago))-- notification = args["payload"]- notification["url"] = UrlHelper.absolute_without_cdn(- Discourse.base_path + notification["post_url"],- )- notification.delete("post_url")-- payload = {- secret_key: SiteSetting.push_api_secret_key,- url: Discourse.base_url,- title: SiteSetting.title,- description: SiteSetting.site_description,- }-- clients = args["clients"]- clients- .group_by { |r| r[1] }- .each do |push_url, group|- notifications = group.map { |client_id, _| notification.merge(client_id: client_id) }-- next if push_url.blank?-- uri = URI.parse(push_url)-- http = FinalDestination::HTTP.new(uri.host, uri.port)- http.use_ssl = uri.scheme == "https"-- request =- FinalDestination::HTTP::Post.new(- uri.request_uri,- { "Content-Type" => "application/json" },- )- request.body = payload.merge(notifications: notifications).to_json-- begin- response = http.request(request)-- if response.code.to_i != 200- Rails.logger.warn(- "Failed to push a notification to #{push_url} Status: #{response.code}: #{response.body}",- )- end- rescue => e- Rails.logger.error("An error occurred while pushing a notification: #{e.message}")- end- end+ Jobs::DeliverPushNotification.new.execute(args)endendenddiff --git a/app/jobs/regular/send_push_notification.rb b/app/jobs/regular/send_push_notification.rbindex 236e1a27bb1..fb69f9bd108 100644--- a/app/jobs/regular/send_push_notification.rb+++ b/app/jobs/regular/send_push_notification.rb@@ -1,19 +1,11 @@# frozen_string_literal: truemodule Jobs+ # @deprecated Use Jobs::DeliverPushNotification instead.+ # Kept for backward compatibility with in-flight Sidekiq jobs.class SendPushNotification < ::Jobs::Basedef execute(args)- user = User.find_by(id: args[:user_id])- push_window = SiteSetting.push_notification_time_window_mins- if !user ||- (- !args[:bypass_time_window] && push_window > 0 &&- user.seen_since?(push_window.minutes.ago)- )- return- end-- PushNotificationPusher.push(user, args[:payload])+ Jobs::DeliverPushNotification.new.execute(args)endendenddiff --git a/app/models/user_api_key.rb b/app/models/user_api_key.rbindex dbb89a634de..013d882a2ce 100644--- a/app/models/user_api_key.rb+++ b/app/models/user_api_key.rb@@ -66,6 +66,21 @@ class UserApiKey < ActiveRecord::BaseSiteSetting.allowed_user_api_push_urls.include?(push_url)end+ def self.push_clients_for(user)+ return [] if SiteSetting.allow_user_api_key_scopes.split("|").exclude?("push")+ return [] if SiteSetting.allowed_user_api_push_urls.blank?++ user+ .user_api_keys+ .joins(:scopes, :client)+ .where("user_api_key_scopes.name IN ('push', 'notifications')")+ .where("push_url IS NOT NULL AND push_url <> ''")+ .where("position(push_url IN ?) > 0", SiteSetting.allowed_user_api_push_urls)+ .where("revoked_at IS NULL")+ .order("user_api_key_clients.client_id ASC")+ .pluck("user_api_key_clients.client_id, user_api_keys.push_url")+ end+def allow?(env)scopes.any? { |s| s.permits?(env) } || is_revoke_self_request?(env)enddiff --git a/app/services/hub_push_notification_pusher.rb b/app/services/hub_push_notification_pusher.rbnew file mode 100644index 00000000000..d8f25239fb1--- /dev/null+++ b/app/services/hub_push_notification_pusher.rb@@ -0,0 +1,53 @@+# frozen_string_literal: true++class HubPushNotificationPusher+ def self.push(user, payload)+ clients = UserApiKey.push_clients_for(user)+ return if clients.empty?++ notification = payload.dup+ notification["url"] = UrlHelper.absolute_without_cdn(+ Discourse.base_path + notification["post_url"],+ )+ notification.delete("post_url")++ hub_payload = {+ secret_key: SiteSetting.push_api_secret_key,+ url: Discourse.base_url,+ title: SiteSetting.title,+ description: SiteSetting.site_description,+ }++ clients+ .group_by { |r| r[1] }+ .each do |push_url, group|+ notifications = group.map { |client_id, _| notification.merge(client_id: client_id) }++ next if push_url.blank?++ uri = URI.parse(push_url)++ http = FinalDestination::HTTP.new(uri.host, uri.port)+ http.use_ssl = uri.scheme == "https"++ request =+ FinalDestination::HTTP::Post.new(+ uri.request_uri,+ { "Content-Type" => "application/json" },+ )+ request.body = hub_payload.merge(notifications: notifications).to_json++ begin+ response = http.request(request)++ if response.code.to_i != 200+ Rails.logger.warn(+ "Failed to push a notification to #{push_url} Status: #{response.code}: #{response.body}",+ )+ end+ rescue => e+ Rails.logger.error("An error occurred while pushing a notification: #{e.message}")+ end+ end+ end+enddiff --git a/app/services/post_alerter.rb b/app/services/post_alerter.rbindex 59fd9619a97..76eecb1e4a5 100644--- a/app/services/post_alerter.rb+++ b/app/services/post_alerter.rb@@ -84,45 +84,17 @@ class PostAlerterreturnend+ return unless user.push_subscriptions.exists? || UserApiKey.push_clients_for(user).any?+push_window = SiteSetting.push_notification_time_window_minsif push_window > 0 && user.seen_since?(push_window.minutes.ago)delay = (push_window - (Time.now - user.last_seen_at) / 60)end- if user.push_subscriptions.exists?- if delay.present?- Jobs.enqueue_in(delay.minutes, :send_push_notification, user_id: user.id, payload: payload)- else- Jobs.enqueue(:send_push_notification, user_id: user.id, payload: payload)- end- end-- if SiteSetting.allow_user_api_key_scopes.split("|").include?("push") &&- SiteSetting.allowed_user_api_push_urls.present?- clients =- user- .user_api_keys- .joins(:scopes, :client)- .where("user_api_key_scopes.name IN ('push', 'notifications')")- .where("push_url IS NOT NULL AND push_url <> ''")- .where("position(push_url IN ?) > 0", SiteSetting.allowed_user_api_push_urls)- .where("revoked_at IS NULL")- .order("user_api_key_clients.client_id ASC")- .pluck("user_api_key_clients.client_id, user_api_keys.push_url")-- return if clients.length == 0-- if delay.present?- Jobs.enqueue_in(- delay.minutes,- :push_notification,- clients: clients,- payload: payload,- user_id: user.id,- )- else- Jobs.enqueue(:push_notification, clients: clients, payload: payload, user_id: user.id)- end+ if delay.present?+ Jobs.enqueue_in(delay.minutes, :deliver_push_notification, user_id: user.id, payload: payload)+ else+ Jobs.enqueue(:deliver_push_notification, user_id: user.id, payload: payload)endenddiff --git a/app/services/push_notification_pusher.rb b/app/services/push_notification_pusher.rbindex c2080edf883..fd21a785117 100644--- a/app/services/push_notification_pusher.rb+++ b/app/services/push_notification_pusher.rb@@ -7,8 +7,6 @@ class PushNotificationPusherdef self.push(user, payload)message = nilI18n.with_locale(user.effective_locale) do- localize_content!(payload) if SiteSetting.content_localization_enabled-notification_icon_name = Notification.types[payload[:notification_type]]if !File.exist?(File.expand_path(@@ -184,34 +182,6 @@ class PushNotificationPusherendend- def self.localize_content!(payload)- locale = I18n.locale.to_s.sub("-", "_")-- if (topic_id = payload[:topic_id])- topic_localization =- TopicLocalization.find_by(topic_id: topic_id, locale: locale) ||- TopicLocalization.matching_locale(locale).find_by(topic_id: topic_id)- payload[:topic_title] = topic_localization.title if topic_localization- end-- if (post_id = payload[:post_id])- post_localization =- PostLocalization.find_by(post_id: post_id, locale: locale) ||- PostLocalization.matching_locale(locale).find_by(post_id: post_id)- if post_localization- payload[:excerpt] = Post.excerpt(- post_localization.cooked,- 400,- text_entities: true,- strip_links: true,- remap_emoji: true,- plain_hashtags: true,- )- end- end- end-private_class_method :send_notificationprivate_class_method :handle_generic_error- private_class_method :localize_content!enddiff --git a/spec/jobs/deliver_push_notification_spec.rb b/spec/jobs/deliver_push_notification_spec.rbnew file mode 100644index 00000000000..963879f5a58--- /dev/null+++ b/spec/jobs/deliver_push_notification_spec.rb@@ -0,0 +1,190 @@+# frozen_string_literal: true++RSpec.describe Jobs::DeliverPushNotification do+ fab!(:user)+ fab!(:post)++ let(:payload) do+ {+ "notification_type" => 1,+ "post_url" => "/t/#{post.topic_id}/#{post.post_number}",+ "excerpt" => "Hello you",+ }+ end++ before do+ freeze_time+ SiteSetting.push_notification_time_window_mins = 5+ end++ describe "time window gate" do+ before { Fabricate(:push_subscription, user: user) }++ it "does not deliver when user is missing" do+ PushNotificationPusher.expects(:push).never+ HubPushNotificationPusher.expects(:push).never+ Jobs::DeliverPushNotification.new.execute(user_id: -999, payload: payload)+ end++ it "does not deliver when user was recently seen" do+ user.update!(last_seen_at: 1.minute.ago)+ PushNotificationPusher.expects(:push).never+ HubPushNotificationPusher.expects(:push).never+ Jobs::DeliverPushNotification.new.execute(user_id: user.id, payload: payload)+ end++ it "delivers when user is offline" do+ user.update!(last_seen_at: 10.minutes.ago)+ PushNotificationPusher.expects(:push).with(user, payload)+ HubPushNotificationPusher.expects(:push).with(user, payload)+ Jobs::DeliverPushNotification.new.execute(user_id: user.id, payload: payload)+ end++ it "delivers when bypass_time_window is true despite user being active" do+ user.update!(last_seen_at: 1.minute.ago)+ PushNotificationPusher.expects(:push).with(user, payload)+ HubPushNotificationPusher.expects(:push).with(user, payload)++ Jobs::DeliverPushNotification.new.execute(+ user_id: user.id,+ bypass_time_window: true,+ payload: payload,+ )+ end+ end++ describe "web push delivery" do+ before { user.update!(last_seen_at: 10.minutes.ago) }++ it "calls PushNotificationPusher when user has push subscriptions" do+ Fabricate(:push_subscription, user: user)+ PushNotificationPusher.expects(:push).with(user, payload).once+ Jobs::DeliverPushNotification.new.execute(user_id: user.id, payload: payload)+ end++ it "does not call PushNotificationPusher when user has no push subscriptions" do+ PushNotificationPusher.expects(:push).never+ Jobs::DeliverPushNotification.new.execute(user_id: user.id, payload: payload)+ end+ end++ describe "hub push delivery" do+ before { user.update!(last_seen_at: 10.minutes.ago) }++ it "calls HubPushNotificationPusher" do+ HubPushNotificationPusher.expects(:push).with(user, payload).once+ Jobs::DeliverPushNotification.new.execute(user_id: user.id, payload: payload)+ end+ end++ describe "both mechanisms" do+ it "delivers via both pushers for the same user" do+ user.update!(last_seen_at: 10.minutes.ago)+ Fabricate(:push_subscription, user: user)++ PushNotificationPusher.expects(:push).with(user, payload).once+ HubPushNotificationPusher.expects(:push).with(user, payload).once++ Jobs::DeliverPushNotification.new.execute(user_id: user.id, payload: payload)+ end+ end++ describe "content localization" do+ fab!(:topic) { Fabricate(:topic, title: "Original topic title") }+ fab!(:localized_post) { Fabricate(:post, topic: topic, raw: "Original post content") }++ let(:localizable_payload) do+ {+ "notification_type" => 1,+ "post_url" => "/t/#{topic.id}/#{localized_post.post_number}",+ "topic_title" => topic.title,+ "topic_id" => topic.id,+ "post_id" => localized_post.id,+ "excerpt" => "Original post content",+ "username" => "system",+ "post_number" => localized_post.post_number,+ }+ end++ before do+ SiteSetting.content_localization_enabled = true+ SiteSetting.allow_user_locale = true+ user.update!(last_seen_at: 10.minutes.ago, locale: "ja")+ end++ it "localizes payload before delivering to web push" do+ Fabricate(:push_subscription, user: user)+ Fabricate(+ :topic_localization,+ topic: topic,+ locale: "ja",+ title: "ローカライズされたトピック",+ fancy_title: "ローカライズされたトピック",+ )+ Fabricate(+ :post_localization,+ post: localized_post,+ locale: "ja",+ raw: "ローカライズされた投稿",+ cooked: "<p>ローカライズされた投稿</p>",+ )++ PushNotificationPusher+ .expects(:push)+ .with { |u, p| p[:topic_title] == "ローカライズされたトピック" && p[:excerpt] == "ローカライズされた投稿" }+ .once++ Jobs::DeliverPushNotification.new.execute(user_id: user.id, payload: localizable_payload)+ end++ it "localizes payload before delivering to hub push" do+ SiteSetting.allowed_user_api_push_urls = "https://hub.example.com/push"+ client = Fabricate(:user_api_key_client)+ Fabricate(+ :user_api_key,+ user: user,+ scopes: ["notifications"].map { |name| UserApiKeyScope.new(name: name) },+ push_url: "https://hub.example.com/push",+ user_api_key_client_id: client.id,+ )+ Fabricate(+ :topic_localization,+ topic: topic,+ locale: "ja",+ title: "ローカライズされたトピック",+ fancy_title: "ローカライズされたトピック",+ )++ body = nil+ stub_request(:post, "https://hub.example.com/push").to_return do |request|+ body = JSON.parse(request.body)+ { status: 200 }+ end++ Jobs::DeliverPushNotification.new.execute(user_id: user.id, payload: localizable_payload)++ expect(body["notifications"].first["topic_title"]).to eq("ローカライズされたトピック")+ end++ it "does not localize when content_localization_enabled is false" do+ SiteSetting.content_localization_enabled = false+ Fabricate(:push_subscription, user: user)+ Fabricate(+ :topic_localization,+ topic: topic,+ locale: "ja",+ title: "ローカライズされたトピック",+ fancy_title: "ローカライズされたトピック",+ )++ PushNotificationPusher+ .expects(:push)+ .with do |u, p|+ p[:topic_title] == "Original topic title" || p["topic_title"] == "Original topic title"+ end+ .once++ Jobs::DeliverPushNotification.new.execute(user_id: user.id, payload: localizable_payload)+ end+ end+enddiff --git a/spec/jobs/push_notification_spec.rb b/spec/jobs/push_notification_spec.rbindex c397aa62ba0..f3b8556e9aa 100644--- a/spec/jobs/push_notification_spec.rb+++ b/spec/jobs/push_notification_spec.rb@@ -2,114 +2,11 @@RSpec.describe Jobs::PushNotification dofab!(:user)- fab!(:user2, :user)- fab!(:post)- let(:data) do- {- "user_id" => user.id,- "payload" => {- "notification_type" => 1,- "post_url" => "/t/#{post.topic_id}/#{post.post_number}",- "excerpt" => "Hello you",- },- "clients" => [[user.id, "http://test.localhost"]],- }- end- let(:data_with_two_clients) do- {- "user_id" => user.id,- "payload" => {- "notification_type" => 1,- "post_url" => "/t/#{post.topic_id}/#{post.post_number}",- "excerpt" => "Hello you",- },- "clients" => [[user2.id, "https://test2.localhost"], [user.id, "http://test.localhost"]],- }- end-- let!(:request) do- stub_request(:post, "http://test.localhost").with(- body: {- secret_key: SiteSetting.push_api_secret_key,- url: "http://test.localhost",- title: "Discourse",- description: "",- notifications: [- {- notification_type: 1,- excerpt: "Hello you",- url: "http://test.localhost/t/#{post.topic_id}/#{post.post_number}",- client_id: user.id,- },- ],- }.to_json,- headers: {- "Content-Type" => "application/json",- },- ).to_return(status: 200, body: "", headers: {})- end-- let!(:bad_request) do- stub_request(:post, "https://test2.localhost/").with(- body: {- secret_key: SiteSetting.push_api_secret_key,- url: "http://test.localhost",- title: "Discourse",- description: "",- notifications: [- {- notification_type: 1,- excerpt: "Hello you",- url: "http://test.localhost/t/#{post.topic_id}/#{post.post_number}",- client_id: user2.id,- },- ],- }.to_json,- headers: {- "Content-Type" => "application/json",- },- ).to_return(status: 404, body: "", headers: {})- end-- before { SiteSetting.push_notification_time_window_mins = 5 }-- context "with valid user" do- it "does not send push notification when user is online" do- user.update!(last_seen_at: 1.minute.ago)-- Jobs::PushNotification.new.execute(data)-- expect(request).not_to have_been_requested- end-- it "sends push notification when user is offline" do- user.update!(last_seen_at: 10.minutes.ago)-- Jobs::PushNotification.new.execute(data)-- expect(request).to have_been_requested.once- end- end-- context "with invalid user" do- it "does not send push notification" do- data["user_id"] = -999-- Jobs::PushNotification.new.execute(data)-- expect(request).not_to have_been_requested- end- end-- context "with two clients" do- it "sends push notifications to both clients" do- user.update!(last_seen_at: 10.minutes.ago)- user2.update!(last_seen_at: 10.minutes.ago)-- Jobs::PushNotification.new.execute(data_with_two_clients)+ let(:payload) { { "notification_type" => 1, "excerpt" => "Hello you" } }- expect(request).to have_been_requested- expect(bad_request).to have_been_requested- end+ it "delegates to DeliverPushNotification" do+ args = { user_id: user.id, payload: payload }+ Jobs::DeliverPushNotification.any_instance.expects(:execute).with(args)+ Jobs::PushNotification.new.execute(args)endenddiff --git a/spec/jobs/send_push_notification_spec.rb b/spec/jobs/send_push_notification_spec.rbindex eb03dc20f7e..c14d5644998 100644--- a/spec/jobs/send_push_notification_spec.rb+++ b/spec/jobs/send_push_notification_spec.rb@@ -2,49 +2,11 @@RSpec.describe Jobs::SendPushNotification dofab!(:user)- fab!(:subscription, :push_subscription)let(:payload) { { notification_type: 1, excerpt: "Hello you" } }- before do- freeze_time- SiteSetting.push_notification_time_window_mins = 10- end-- context "with valid user" do- it "does not send push notification when user is online" do- user.update!(last_seen_at: 2.minutes.ago)-- PushNotificationPusher.expects(:push).with(user, payload).never-- Jobs::SendPushNotification.new.execute(user_id: user, payload: payload)- end-- it "bypasses the online window when bypass_time_window is passed in" do- user.update!(last_seen_at: 2.minutes.ago)-- PushNotificationPusher.expects(:push).with(user, payload)-- Jobs::SendPushNotification.new.execute(- user_id: user,- bypass_time_window: true,- payload: payload,- )- end-- it "sends push notification when user is offline" do- user.update!(last_seen_at: 20.minutes.ago)-- PushNotificationPusher.expects(:push).with(user, payload)-- Jobs::SendPushNotification.new.execute(user_id: user, payload: payload)- end- end-- context "with invalid user" do- it "does not send push notification" do- PushNotificationPusher.expects(:push).with(user, payload).never-- Jobs::SendPushNotification.new.execute(user_id: -999, payload: payload)- end+ it "delegates to DeliverPushNotification" do+ args = { user_id: user.id, payload: payload }+ Jobs::DeliverPushNotification.any_instance.expects(:execute).with(args)+ Jobs::SendPushNotification.new.execute(args)endenddiff --git a/spec/services/hub_push_notification_pusher_spec.rb b/spec/services/hub_push_notification_pusher_spec.rbnew file mode 100644index 00000000000..ac0f7589e55--- /dev/null+++ b/spec/services/hub_push_notification_pusher_spec.rb@@ -0,0 +1,99 @@+# frozen_string_literal: true++RSpec.describe HubPushNotificationPusher do+ fab!(:user)+ fab!(:post)++ let(:payload) do+ {+ "notification_type" => 1,+ "post_url" => "/t/#{post.topic_id}/#{post.post_number}",+ "excerpt" => "Hello you",+ }+ end++ def setup_hub_client(user, push_url: "https://hub.example.com/push", client_id: nil)+ client =+ Fabricate(+ :user_api_key_client,+ client_id: client_id || SecureRandom.hex,+ application_name: "TestApp",+ )+ Fabricate(+ :user_api_key,+ user: user,+ scopes: ["notifications"].map { |name| UserApiKeyScope.new(name: name) },+ push_url: push_url,+ user_api_key_client_id: client.id,+ )+ end++ before { SiteSetting.allowed_user_api_push_urls = "https://hub.example.com/push" }++ it "does nothing when no hub clients exist" do+ stub = stub_request(:post, "https://hub.example.com/push").to_return(status: 200)++ HubPushNotificationPusher.push(user, payload)+ expect(stub).not_to have_been_requested+ end++ it "sends HTTP POST to push_url" do+ setup_hub_client(user)++ stub =+ stub_request(:post, "https://hub.example.com/push").with(+ headers: {+ "Content-Type" => "application/json",+ },+ ).to_return(status: 200)++ HubPushNotificationPusher.push(user, payload)+ expect(stub).to have_been_requested.once+ end++ it "includes correct payload structure" do+ setup_hub_client(user)+ body = nil++ stub_request(:post, "https://hub.example.com/push").to_return do |request|+ body = JSON.parse(request.body)+ { status: 200 }+ end++ HubPushNotificationPusher.push(user, payload)++ expect(body["secret_key"]).to eq(SiteSetting.push_api_secret_key)+ expect(body["url"]).to eq(Discourse.base_url)+ expect(body["title"]).to eq(SiteSetting.title)+ expect(body["notifications"]).to be_present+ expect(body["notifications"].first["url"]).to include("/t/#{post.topic_id}/#{post.post_number}")+ expect(body["notifications"].first).not_to have_key("post_url")+ end++ it "groups notifications by push_url" do+ SiteSetting.allowed_user_api_push_urls =+ "https://hub1.example.com/push|https://hub2.example.com/push"++ setup_hub_client(user, push_url: "https://hub1.example.com/push", client_id: "client1")+ setup_hub_client(user, push_url: "https://hub1.example.com/push", client_id: "client2")+ setup_hub_client(user, push_url: "https://hub2.example.com/push", client_id: "client3")++ stub1 = stub_request(:post, "https://hub1.example.com/push").to_return(status: 200)+ stub2 = stub_request(:post, "https://hub2.example.com/push").to_return(status: 200)++ HubPushNotificationPusher.push(user, payload)++ expect(stub1).to have_been_requested.once+ expect(stub2).to have_been_requested.once+ end++ it "does not mutate the original payload" do+ setup_hub_client(user)+ stub_request(:post, "https://hub.example.com/push").to_return(status: 200)++ original_payload = payload.deep_dup+ HubPushNotificationPusher.push(user, payload)++ expect(payload).to eq(original_payload)+ end+enddiff --git a/spec/services/post_alerter_spec.rb b/spec/services/post_alerter_spec.rbindex 29e9a9d5b43..0d5526d5235 100644--- a/spec/services/post_alerter_spec.rb+++ b/spec/services/post_alerter_spec.rb@@ -1239,7 +1239,7 @@ RSpec.describe PostAlerter doalerts =MessageBus.track_publish("/notification-alert/#{evil_trout.id}") do- expect { mention_post }.to change { Jobs::PushNotification.jobs.count }.by(1)+ expect { mention_post }.to change { Jobs::DeliverPushNotification.jobs.count }.by(1)endexpect(alerts).not_to be_empty@@ -1250,7 +1250,7 @@ RSpec.describe PostAlerter doalerts =MessageBus.track_publish("/notification-alert/#{evil_trout.id}") do- expect { mention_post }.not_to change { Jobs::PushNotification.jobs.count }+ expect { mention_post }.not_to change { Jobs::DeliverPushNotification.jobs.count }endexpect(alerts).to be_empty@@ -1269,7 +1269,7 @@ RSpec.describe PostAlerter doit "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 }+ expect { mention_post }.to_not change { Jobs::DeliverPushNotification.jobs.count }events = DiscourseEvent.track_events { mention_post }expect(events.find { |event| event[:event_name] == :push_notification }).not_to be_present@@ -1283,7 +1283,7 @@ RSpec.describe PostAlerter doends_at: 1.day.from_now,)- expect { mention_post }.to_not change { Jobs::PushNotification.jobs.count }+ expect { mention_post }.to_not change { Jobs::DeliverPushNotification.jobs.count }events = DiscourseEvent.track_events { mention_post }expect(events.find { |event| event[:event_name] == :push_notification }).not_to be_present@@ -1410,8 +1410,8 @@ RSpec.describe PostAlerter doSiteSetting.push_notification_time_window_mins = 10evil_trout.update!(last_seen_at: 5.minutes.ago)- expect { mention_post }.to change { Jobs::PushNotification.jobs.count }- expect(Jobs::PushNotification.jobs[0]["at"]).to be_within(30.seconds).of(+ expect { mention_post }.to change { Jobs::DeliverPushNotification.jobs.count }+ expect(Jobs::DeliverPushNotification.jobs[0]["at"]).to be_within(30.seconds).of(5.minutes.from_now.to_f,)end@@ -1425,8 +1425,8 @@ RSpec.describe PostAlerter doit "delays sending push notification for active online user" doevil_trout.update!(last_seen_at: 5.minutes.ago)- expect { mention_post }.to change { Jobs::SendPushNotification.jobs.count }- expect(Jobs::SendPushNotification.jobs[0]["at"]).not_to be_nil+ expect { mention_post }.to change { Jobs::DeliverPushNotification.jobs.count }+ expect(Jobs::DeliverPushNotification.jobs[0]["at"]).not_to be_nilendit "delays sending push notification for active online user for the correct delay ammount" do@@ -1437,15 +1437,15 @@ RSpec.describe PostAlerter do# 10 minutes from now - 5 minutes ago = 5 minutesdelay = 5.minutes.from_now.to_f- expect { mention_post }.to change { Jobs::SendPushNotification.jobs.count }- expect(Jobs::SendPushNotification.jobs[0]["at"]).to be_within(30.seconds).of(delay)+ expect { mention_post }.to change { Jobs::DeliverPushNotification.jobs.count }+ expect(Jobs::DeliverPushNotification.jobs[0]["at"]).to be_within(30.seconds).of(delay)endit "does not delay push notification for inactive offline user" doevil_trout.update!(last_seen_at: 40.minutes.ago)- expect { mention_post }.to change { Jobs::SendPushNotification.jobs.count }- expect(Jobs::SendPushNotification.jobs[0]["at"]).to be_nil+ expect { mention_post }.to change { Jobs::DeliverPushNotification.jobs.count }+ expect(Jobs::DeliverPushNotification.jobs[0]["at"]).to be_nilendendend@@ -1524,7 +1524,7 @@ RSpec.describe PostAlerter doexpect(events.size).to eq(0)expect(messages.size).to eq(0)- expect(Jobs::PushNotification.jobs.size).to eq(0)+ expect(Jobs::DeliverPushNotification.jobs.size).to eq(0)endit "does not publish to MessageBus /notification-alert if the user has not been seen for > 30 days, but still sends a push notification" do@@ -1559,7 +1559,7 @@ RSpec.describe PostAlerter do:post_notification_alert,)expect(messages.size).to eq(0)- expect(Jobs::PushNotification.jobs.size).to eq(1)+ expect(Jobs::DeliverPushNotification.jobs.size).to eq(1)endenddiff --git a/spec/services/push_notification_pusher_spec.rb b/spec/services/push_notification_pusher_spec.rbindex be88224b4a3..c430693cd17 100644--- a/spec/services/push_notification_pusher_spec.rb+++ b/spec/services/push_notification_pusher_spec.rb@@ -225,91 +225,6 @@ RSpec.describe PushNotificationPusher doendend- describe "localized content" do- fab!(:topic) { Fabricate(:topic, title: "Original topic title") }- fab!(:post) { Fabricate(:post, topic: topic, raw: "Original post content") }-- before do- SiteSetting.content_localization_enabled = true- SiteSetting.allow_user_locale = true- user.update!(locale: "ja")- end-- def execute_localized_push- PushNotificationPusher.push(- user,- {- topic_title: topic.title,- username: "system",- excerpt: "Original post content",- topic_id: topic.id,- post_id: post.id,- post_url: "/t/#{topic.id}/#{post.post_number}",- notification_type: Notification.types[:mentioned],- post_number: post.post_number,- },- )- end-- it "uses localized topic title and post excerpt when localization exists" do- Fabricate(- :topic_localization,- topic: topic,- locale: "ja",- title: "ローカライズされたトピック",- fancy_title: "ローカライズされたトピック",- )- Fabricate(- :post_localization,- post: post,- locale: "ja",- raw: "ローカライズされた投稿",- cooked: "<p>ローカライズされた投稿</p>",- )-- message = execute_localized_push-- expect(message[:title]).to include("ローカライズされたトピック")- expect(message[:body]).to eq("ローカライズされた投稿")- end-- it "falls back to original content when no localization exists" do- message = execute_localized_push-- expect(message[:title]).to include("Original topic title")- expect(message[:body]).to eq("Original post content")- end-- it "falls back to original content when content_localization_enabled is false" do- SiteSetting.content_localization_enabled = false- Fabricate(- :topic_localization,- topic: topic,- locale: "ja",- title: "ローカライズされたトピック",- fancy_title: "ローカライズされたトピック",- )-- message = execute_localized_push-- expect(message[:title]).to include("Original topic title")- end-- it "matches regionless locale variants" do- Fabricate(- :topic_localization,- topic: topic,- locale: "ja_JP",- title: "ローカライズされたトピック",- fancy_title: "ローカライズされたトピック",- )-- message = execute_localized_push-- expect(message[:title]).to include("ローカライズされたトピック")- end- end-describe "push_notification_pusher_title_payload modifier" dolet(:modifier_block) doProc.new do |payload|