2016-08-25 22:47:10 -04:00
|
|
|
module Jobs
|
|
|
|
class PushNotification < Jobs::Base
|
|
|
|
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"]
|
|
|
|
clients.group_by{|r| r[1]}.each do |push_url, group|
|
|
|
|
notifications = group.map do |client_id, _|
|
|
|
|
notification.merge({
|
|
|
|
client_id: client_id
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2017-06-15 20:08:15 -04:00
|
|
|
Excon.post(push_url,
|
|
|
|
body: payload.merge(notifications: notifications).to_json,
|
|
|
|
headers: { 'Content-Type' => 'application/json', 'Accept' => 'applicaton/json' }
|
|
|
|
)
|
2016-08-25 22:47:10 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|