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

View file

@ -136,28 +136,15 @@ class BarcasController < ApplicationController
@barca = Barca.find(params[:barca_id])
end
# 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: 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)
def get_subject(view)
I18n.t("barcas.#{view}.subject", barca: @barca.nombre)
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)
def get_endpoint(_)
barca_path(@barca)
end
end

View file

@ -47,6 +47,7 @@ class ConsensosController < ApplicationController
@consenso.barca = @barca
if @consenso.save
notify(subject: :create, urgency: :high)
render status: :created
else
render json: { errors: @consenso.errors.messages },
@ -63,13 +64,10 @@ class ConsensosController < ApplicationController
# titulo: @string, texto: @string,
# posiciones: [] }
def destroy
begin
@consenso = @barca.consensos.find(params[:id])
rescue ActiveRecord::RecordNotFound
render(json: {}, status: :not_found) && return
end
@consenso = @barca.consensos.find(params[:id])
if @consenso.posiciones.empty? && @consenso.destroy
notify(subject: :destroy)
render status: :ok
else
render json: {}, status: :unprocessable_entity
@ -87,4 +85,20 @@ class ConsensosController < ApplicationController
def consenso_params
params.require(:consenso).permit(:titulo, :texto)
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

View file

@ -11,3 +11,10 @@ en:
destroy:
subject: '%{barca}'
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!'
update:
subject: '%{barca}'
consensos:
create:
subject: '%{barca} - %{consenso}'
message: '%{nick} inició: %{consenso}'
destroy:
subject: '%{barca} - %{consenso}'
message: '%{nick} borró: %{consenso}'