notificar en cambios en consensos

This commit is contained in:
fauno 2019-08-03 19:37:21 -03:00
parent ea7254c9a5
commit 75310fc255
No known key found for this signature in database
GPG key ID: 456032D717A4CD9C
5 changed files with 60 additions and 22 deletions

View file

@ -19,6 +19,29 @@ class ApplicationController < ActionController::API
todas_menos_yo.pluck(:id) todas_menos_yo.pluck(:id)
end end
# Enviar notificaciones
#
# Necesita que el controlador defina
#
# #get_subject(:symbol)
# #get_message(:symbol)
# #get_endpoint(:symbol)
#
# @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: get_subject(subject),
message: get_message(subject),
endpoint: get_endpoint(subject))
WebpushJob.perform_later(piratas: todas_menos_yo_ids,
ttl: ttl.to_i,
urgency: urgency.to_s,
payload: payload.to_json)
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

@ -136,28 +136,15 @@ class BarcasController < ApplicationController
@barca = Barca.find(params[:barca_id]) @barca = Barca.find(params[:barca_id])
end end
# Enviar notificaciones def get_subject(view)
# I18n.t("barcas.#{view}.subject", barca: @barca.nombre)
# @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: get_subject(subject),
message: get_message(subject),
endpoint: barca_path(@barca))
WebpushJob.perform_later(piratas: todas_menos_yo_ids,
ttl: ttl.to_i,
urgency: urgency.to_s,
payload: payload.to_json)
end end
def get_message(view) def get_message(view)
I18n.t("barcas.#{view}.message", nick: current_pirata.nick) I18n.t("barcas.#{view}.message", nick: current_pirata.nick)
end end
def get_subject(view) def get_endpoint(_)
I18n.t("barcas.#{view}.subject", barca: @barca.nombre) barca_path(@barca)
end end
end end

View file

@ -47,6 +47,7 @@ class ConsensosController < ApplicationController
@consenso.barca = @barca @consenso.barca = @barca
if @consenso.save if @consenso.save
notify(subject: :create, urgency: :high)
render status: :created render status: :created
else else
render json: { errors: @consenso.errors.messages }, render json: { errors: @consenso.errors.messages },
@ -63,13 +64,10 @@ class ConsensosController < ApplicationController
# titulo: @string, texto: @string, # titulo: @string, texto: @string,
# posiciones: [] } # posiciones: [] }
def destroy def destroy
begin @consenso = @barca.consensos.find(params[:id])
@consenso = @barca.consensos.find(params[:id])
rescue ActiveRecord::RecordNotFound
render(json: {}, status: :not_found) && return
end
if @consenso.posiciones.empty? && @consenso.destroy if @consenso.posiciones.empty? && @consenso.destroy
notify(subject: :destroy)
render status: :ok render status: :ok
else else
render json: {}, status: :unprocessable_entity render json: {}, status: :unprocessable_entity
@ -87,4 +85,20 @@ class ConsensosController < ApplicationController
def consenso_params def consenso_params
params.require(:consenso).permit(:titulo, :texto) params.require(:consenso).permit(:titulo, :texto)
end end
def get_subject(view)
I18n.t("consensos.#{view}.subject",
barca: @barca.nombre,
consenso: @consenso.titulo)
end
def get_message(view)
I18n.t("consensos.#{view}.message",
consenso: @consenso.texto[0..140],
nick: current_pirata.nick)
end
def get_endpoint(_)
barca_consenso_path(@barca, @consenso)
end
end end

View file

@ -11,3 +11,10 @@ en:
destroy: destroy:
subject: '%{barca}' subject: '%{barca}'
message: '%{nick} sunk the ship!' message: '%{nick} sunk the ship!'
consensos:
create:
subject: '%{barca} - %{consenso}'
message: '%{nick} started: %{consenso}'
destroy:
subject: '%{barca} - %{consenso}'
message: '%{nick} deleted: %{consenso}'

View file

@ -13,3 +13,10 @@ es:
message: '¡%{nick} hundió la barca!' message: '¡%{nick} hundió la barca!'
update: update:
subject: '%{barca}' subject: '%{barca}'
consensos:
create:
subject: '%{barca} - %{consenso}'
message: '%{nick} inició: %{consenso}'
destroy:
subject: '%{barca} - %{consenso}'
message: '%{nick} borró: %{consenso}'