From ea7254c9a58891be203d997af43bdba633a698c8 Mon Sep 17 00:00:00 2001 From: fauno Date: Sat, 3 Aug 2019 19:25:20 -0300 Subject: [PATCH] notificar en cambios de las barcas --- app/controllers/application_controller.rb | 8 ++++++ app/controllers/barcas_controller.rb | 31 +++++++++++++++++------ app/jobs/webpush_job.rb | 2 +- config/locales/en.yml | 9 +++++++ config/locales/es.yml | 11 ++++++++ 5 files changed, 52 insertions(+), 9 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 5aa1baf..0621f7a 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -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! diff --git a/app/controllers/barcas_controller.rb b/app/controllers/barcas_controller.rb index 92335e3..3ace4c7 100644 --- a/app/controllers/barcas_controller.rb +++ b/app/controllers/barcas_controller.rb @@ -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 diff --git a/app/jobs/webpush_job.rb b/app/jobs/webpush_job.rb index f9804e7..07a8e54 100644 --- a/app/jobs/webpush_job.rb +++ b/app/jobs/webpush_job.rb @@ -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) diff --git a/config/locales/en.yml b/config/locales/en.yml index 7d88ef3..338275f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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!' diff --git a/config/locales/es.yml b/config/locales/es.yml index 2f4423d..0780099 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -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}'