2019-08-03 21:44:28 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Envía las notificaciones
|
|
|
|
class WebpushJob < ApplicationJob
|
|
|
|
queue_as :default
|
|
|
|
|
2020-03-02 15:13:28 +00:00
|
|
|
# @param :piratas [Symbol,Array] :all para traer todas, array de IDs para filtrar
|
2019-08-03 21:44:28 +00:00
|
|
|
# @param :payload [WebpushPayload] La notificación a enviar
|
|
|
|
# @param :urgency [Symbol] La urgencia
|
|
|
|
# @param :ttl [Any] Duración convertible a segundos
|
|
|
|
def perform(piratas:, payload:, urgency: :normal, ttl: 1.day.to_i)
|
|
|
|
# Encontrar todas las piratas
|
|
|
|
piratas = find_piratas(piratas)
|
|
|
|
payload = WebpushPayload.from_json payload
|
|
|
|
|
|
|
|
# No cargar todas las piratas en memoria!
|
|
|
|
piratas.find_each do |pirata|
|
|
|
|
# Enviarles a todas sus suscripciones
|
|
|
|
pirata.webpush_subscriptions.find_each do |subscription|
|
|
|
|
subscription.payload_send(payload: payload,
|
|
|
|
urgency: urgency,
|
|
|
|
ttl: ttl)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_piratas(ids)
|
2019-08-03 22:25:20 +00:00
|
|
|
if ids == :all
|
2019-08-03 21:44:28 +00:00
|
|
|
Pirata.all
|
|
|
|
else
|
|
|
|
Pirata.where(id: ids)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|