A platform for community discussion. Free, open, simple.
FIX: Use FinalDestination::HTTP to push notifications (#29858)
Bianca Nenciu429cf656e78a
diff --git a/app/jobs/regular/push_notification.rb b/app/jobs/regular/push_notification.rbindex 8db0c47f345..2494927cfb3 100644--- a/app/jobs/regular/push_notification.rb+++ b/app/jobs/regular/push_notification.rb@@ -28,19 +28,22 @@ module Jobsnext if push_url.blank?+ uri = URI.parse(push_url)++ http = FinalDestination::HTTP.new(uri.host, uri.port)+ http.use_ssl = true++ request =+ FinalDestination::HTTP::Post.new(+ uri.request_uri,+ { "Content-Type" => "application/json" },+ )+ request.body = payload.merge(notifications: notifications).to_json+begin- result =- Excon.post(- push_url,- body: payload.merge(notifications: notifications).to_json,- headers: {- "Content-Type" => "application/json",- "Accept" => "application/json",- },- )+ response = http.request(request)- if result.status != 200- # we failed to push a notification ... log it+ if response.code.to_i != 200Rails.logger.warn("Failed to push a notification to #{push_url} Status: #{result.status}: #{result.status_line}",)diff --git a/spec/jobs/push_notification_spec.rb b/spec/jobs/push_notification_spec.rbindex e081fd677c5..a1f1648c8b6 100644--- a/spec/jobs/push_notification_spec.rb+++ b/spec/jobs/push_notification_spec.rb@@ -17,23 +17,45 @@ RSpec.describe Jobs::PushNotification do}end+ let!(:request) do+ stub_request(:post, "https://test.localhost:80/").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+before { SiteSetting.push_notification_time_window_mins = 5 }context "with valid user" doit "does not send push notification when user is online" douser.update!(last_seen_at: 1.minute.ago)- Excon.expects(:post).never-Jobs::PushNotification.new.execute(data)++ expect(request).not_to have_been_requestedendit "sends push notification when user is offline" douser.update!(last_seen_at: 10.minutes.ago)- Excon.expects(:post).once-Jobs::PushNotification.new.execute(data)++ expect(request).to have_been_requested.onceendend@@ -41,9 +63,9 @@ RSpec.describe Jobs::PushNotification doit "does not send push notification" dodata["user_id"] = -999- Excon.expects(:post).never-Jobs::PushNotification.new.execute(data)++ expect(request).not_to have_been_requestedendendend