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
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
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication
def authenticate!

View file

@ -47,8 +47,7 @@ class BarcasController < ApplicationController
@barca.piratas << current_pirata
if @barca.save
piratas = Pirata.todas_menos(current_pirata).pluck(:id)
notify(subject: 'barcas.create.subject', piratas: piratas)
notify(subject: 'barcas.create.subject')
render status: :created
else
@ -69,6 +68,7 @@ class BarcasController < ApplicationController
@barca = current_pirata.barcas.find(params[:id])
if @barca.update_attributes barca_params
notify(subject: :update, urgency: :high)
render status: :no_content
else
render json: { errors: @barca.errors.messages },
@ -86,6 +86,7 @@ class BarcasController < ApplicationController
@barca = current_pirata.barcas.find(params[:id])
if @barca.destroy
notify(subject: :destroy, urgency: :high)
render status: :no_content
else
render json: { errors: @barca.errors.messages },
@ -102,6 +103,7 @@ class BarcasController < ApplicationController
@barca.piratas << current_pirata
if @barca.save
notify(subject: :abordar, urgency: :'very-low', ttl: 1.day)
render status: :no_content
else
render json: { errors: @barca.errors.messages },
@ -116,6 +118,7 @@ class BarcasController < ApplicationController
find_barca
if @barca.tripulaciones.find_by(pirata: current_pirata).destroy
notify(subject: :abandonar, urgency: :'very-low', ttl: 1.day)
render status: :no_content
else
render json: { errors: @barca.errors.messages },
@ -133,16 +136,28 @@ class BarcasController < ApplicationController
@barca = Barca.find(params[:barca_id])
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
# se puedan sumar
payload = WebpushPayload.new(subject: I18n.t(subject,
barca: @barca.nombre),
message: @barca.descripcion,
payload = WebpushPayload.new(subject: get_subject(subject),
message: get_message(subject),
endpoint: barca_path(@barca))
WebpushJob.perform_later(piratas: piratas,
ttl: 7.days.to_i,
WebpushJob.perform_later(piratas: todas_menos_yo_ids,
ttl: ttl.to_i,
urgency: urgency.to_s,
payload: payload.to_json)
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

View file

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

View file

@ -2,3 +2,12 @@ en:
barcas:
create:
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:
create:
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}'