diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0621f7a..e5c3d60 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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! diff --git a/app/controllers/barcas_controller.rb b/app/controllers/barcas_controller.rb index 3ace4c7..b1828ab 100644 --- a/app/controllers/barcas_controller.rb +++ b/app/controllers/barcas_controller.rb @@ -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 diff --git a/app/controllers/consensos_controller.rb b/app/controllers/consensos_controller.rb index 6ac7e77..804aba5 100644 --- a/app/controllers/consensos_controller.rb +++ b/app/controllers/consensos_controller.rb @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml index 338275f..a5421a3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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}' diff --git a/config/locales/es.yml b/config/locales/es.yml index 0780099..2f31b5e 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -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}'