notificar en cambios de las barcas

This commit is contained in:
fauno 2019-08-03 19:25:20 -03:00
parent b1de74aafe
commit ea7254c9a5
No known key found for this signature in database
GPG key ID: 456032D717A4CD9C
5 changed files with 52 additions and 9 deletions

View file

@ -11,6 +11,14 @@ class ApplicationController < ActionController::API
private private
def todas_menos_yo
Pirata.todas_menos(current_pirata)
end
def todas_menos_yo_ids
todas_menos_yo.pluck(:id)
end
# Autenticar a la pirata usando HTTP Basic Auth # Autenticar a la pirata usando HTTP Basic Auth
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication # https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication
def authenticate! def authenticate!

View file

@ -47,8 +47,7 @@ class BarcasController < ApplicationController
@barca.piratas << current_pirata @barca.piratas << current_pirata
if @barca.save if @barca.save
piratas = Pirata.todas_menos(current_pirata).pluck(:id) notify(subject: 'barcas.create.subject')
notify(subject: 'barcas.create.subject', piratas: piratas)
render status: :created render status: :created
else else
@ -69,6 +68,7 @@ class BarcasController < ApplicationController
@barca = current_pirata.barcas.find(params[:id]) @barca = current_pirata.barcas.find(params[:id])
if @barca.update_attributes barca_params if @barca.update_attributes barca_params
notify(subject: :update, urgency: :high)
render status: :no_content render status: :no_content
else else
render json: { errors: @barca.errors.messages }, render json: { errors: @barca.errors.messages },
@ -86,6 +86,7 @@ class BarcasController < ApplicationController
@barca = current_pirata.barcas.find(params[:id]) @barca = current_pirata.barcas.find(params[:id])
if @barca.destroy if @barca.destroy
notify(subject: :destroy, urgency: :high)
render status: :no_content render status: :no_content
else else
render json: { errors: @barca.errors.messages }, render json: { errors: @barca.errors.messages },
@ -102,6 +103,7 @@ class BarcasController < ApplicationController
@barca.piratas << current_pirata @barca.piratas << current_pirata
if @barca.save if @barca.save
notify(subject: :abordar, urgency: :'very-low', ttl: 1.day)
render status: :no_content render status: :no_content
else else
render json: { errors: @barca.errors.messages }, render json: { errors: @barca.errors.messages },
@ -116,6 +118,7 @@ class BarcasController < ApplicationController
find_barca find_barca
if @barca.tripulaciones.find_by(pirata: current_pirata).destroy if @barca.tripulaciones.find_by(pirata: current_pirata).destroy
notify(subject: :abandonar, urgency: :'very-low', ttl: 1.day)
render status: :no_content render status: :no_content
else else
render json: { errors: @barca.errors.messages }, render json: { errors: @barca.errors.messages },
@ -133,16 +136,28 @@ class BarcasController < ApplicationController
@barca = Barca.find(params[:barca_id]) @barca = Barca.find(params[:barca_id])
end end
def notify(piratas:, subject:) # Enviar notificaciones
#
# @param :subject [Symbol|String] Título del mensaje para I18n
# @param :urgency [Symbol] Nivel de urgencia
def notify(subject:, urgency: :normal, ttl: 7.days)
# Notificar a todas las piratas que hay una nueva barca para que # Notificar a todas las piratas que hay una nueva barca para que
# se puedan sumar # se puedan sumar
payload = WebpushPayload.new(subject: I18n.t(subject, payload = WebpushPayload.new(subject: get_subject(subject),
barca: @barca.nombre), message: get_message(subject),
message: @barca.descripcion,
endpoint: barca_path(@barca)) endpoint: barca_path(@barca))
WebpushJob.perform_later(piratas: piratas, WebpushJob.perform_later(piratas: todas_menos_yo_ids,
ttl: 7.days.to_i, ttl: ttl.to_i,
urgency: urgency.to_s,
payload: payload.to_json) payload: payload.to_json)
end end
def get_message(view)
I18n.t("barcas.#{view}.message", nick: current_pirata.nick)
end
def get_subject(view)
I18n.t("barcas.#{view}.subject", barca: @barca.nombre)
end
end end

View file

@ -28,7 +28,7 @@ class WebpushJob < ApplicationJob
private private
def find_piratas(ids) def find_piratas(ids)
if ids == 'all' if ids == :all
Pirata.all Pirata.all
else else
Pirata.where(id: ids) Pirata.where(id: ids)

View file

@ -2,3 +2,12 @@ en:
barcas: barcas:
create: create:
subject: '%{barca} has been created!' subject: '%{barca} has been created!'
abordar:
subject: '%{barca}'
message: '%{nick} boarded the ship!'
abandonar:
subject: '%{barca}'
message: '%{nick} left the ship!'
destroy:
subject: '%{barca}'
message: '%{nick} sunk the ship!'

View file

@ -2,3 +2,14 @@ es:
barcas: barcas:
create: create:
subject: '%{barca} creada, ¡al abordaje!' subject: '%{barca} creada, ¡al abordaje!'
abordar:
subject: '%{barca}'
message: '¡%{nick} abordó la barca!'
abandonar:
subject: '%{barca}'
message: '¡%{nick} abandonó la barca!'
destroy:
subject: '%{barca}'
message: '¡%{nick} hundió la barca!'
update:
subject: '%{barca}'