discourse/app/jobs/regular/push_notification.rb
Blake Erickson 2123561125
FIX: Mobile app notification urls w/ subfolder (#12282)
The urls that we generate for mobile post notifications don't take into
account the subfolder url if a site happens to have one configured. When
this happens when you tap on a new mobile notification it takes you to
a url that doesn't work because it is missing the subfolder portion.

I honestly think this should be handled in the Post model like we do
with the Topic model. `Post.url` should know how to handle subfolder
installs, but that seemed like a very risky change because there are
lots of other places in the codebase where we tack on the base_path and
I didn't want to risk duplicating it.

I also found a small typo in the topics controller spec.
2021-03-04 07:07:37 -07:00

39 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module Jobs
class PushNotification < ::Jobs::Base
def execute(args)
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 do |client_id, _|
notification.merge(client_id: client_id)
end
next unless push_url.present?
result = Excon.post(push_url,
body: payload.merge(notifications: notifications).to_json,
headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
)
if result.status != 200
# we failed to push a notification ... log it
Rails.logger.warn("Failed to push a notification to #{push_url} Status: #{result.status}: #{result.status_line}")
end
end
end
end
end