2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-25 22:47:10 -04:00
|
|
|
module Jobs
|
2019-10-02 00:01:53 -04:00
|
|
|
class PushNotification < ::Jobs::Base
|
2016-08-25 22:47:10 -04:00
|
|
|
def execute(args)
|
|
|
|
notification = args["payload"]
|
2016-08-25 23:45:56 -04:00
|
|
|
notification["url"] = UrlHelper.absolute_without_cdn(notification["post_url"])
|
2016-08-25 22:47:10 -04:00
|
|
|
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"]
|
2017-07-27 21:20:09 -04:00
|
|
|
clients.group_by { |r| r[1] }.each do |push_url, group|
|
2016-08-25 22:47:10 -04:00
|
|
|
notifications = group.map do |client_id, _|
|
2017-07-27 21:20:09 -04:00
|
|
|
notification.merge(client_id: client_id)
|
2016-08-25 22:47:10 -04:00
|
|
|
end
|
|
|
|
|
2019-10-16 10:59:06 -04:00
|
|
|
next unless push_url.present?
|
2019-10-04 11:52:10 -04:00
|
|
|
|
2017-06-30 10:45:53 -04:00
|
|
|
result = Excon.post(push_url,
|
2017-06-15 20:08:15 -04:00
|
|
|
body: payload.merge(notifications: notifications).to_json,
|
2017-06-30 08:40:43 -04:00
|
|
|
headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' }
|
2017-06-15 20:08:15 -04:00
|
|
|
)
|
2017-06-30 10:45:53 -04:00
|
|
|
|
|
|
|
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
|
2016-08-25 22:47:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|