docbrown/app/services/push_notifications/send.rb
rhymes cf9f6094cd
Have specs run without Redis and refactor ConsumerApp and Device (#13647)
* Add fakeredis gem

* Mock Pusher::PushNotifications in specs

* Remove Redis from Travis

* Add Android tests to ConsumerApp specs

* Use mock for push notification tests and refactor ConsumerApp and Device code

* Fix remaining broken specs

* Use symbols

* Cleanup rpush helpers

* Pusher::PushNotifications is no more

* Use human friendly platform in Admin::ConsumerApps index page
2021-05-07 08:03:16 +02:00

29 lines
1.1 KiB
Ruby

module PushNotifications
class Send
def self.call(user_ids:, title:, body:, payload:)
new(user_ids: user_ids, title: title, body: body, payload: payload).call
end
def initialize(user_ids:, title:, body:, payload:)
@user_ids = user_ids
@title = title
@body = body
@payload = payload
end
def call
relation = Device.where(user_id: @user_ids)
relation.find_each do |device|
device.create_notification(@title, @body, @payload)
end
# perform_in(30.seconds) is key: The background worker will execute Rpush.push which will send out all
# pending notifications. This means we don't need to run it for every PN. PushNotifications::DeliverWorker
# has a constraint that will execute the job only once (30 seconds from now). So if we need to send 1 PN
# every 5 seconds we only execute this once every 30s (no duplicate/unnecessary processing).
# If no PNs are scheduled to be sent for a 6h span then 0 jobs are executed.
PushNotifications::DeliverWorker.perform_in(30.seconds) if relation.any?
end
end
end