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