2019-04-05 20:52:04 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-04-06 19:45:07 +00:00
|
|
|
# El controlador desde el que descienden todos los controladores
|
2019-04-05 20:20:20 +00:00
|
|
|
class ApplicationController < ActionController::API
|
2019-04-06 19:45:07 +00:00
|
|
|
# authenticate! obtiene la pirata a partir de los datos de
|
|
|
|
# autenticacion y la pone en este atributo
|
|
|
|
attr_reader :current_pirata
|
|
|
|
|
|
|
|
# Vamos a usar HTTP Basic Auth
|
|
|
|
include ActionController::HttpAuthentication::Basic::ControllerMethods
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-08-03 22:25:20 +00:00
|
|
|
def todas_menos_yo
|
|
|
|
Pirata.todas_menos(current_pirata)
|
|
|
|
end
|
|
|
|
|
|
|
|
def todas_menos_yo_ids
|
|
|
|
todas_menos_yo.pluck(:id)
|
|
|
|
end
|
|
|
|
|
2019-08-03 22:37:21 +00:00
|
|
|
# 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)
|
2020-06-21 00:59:13 +00:00
|
|
|
payload = { subject: get_subject(subject),
|
|
|
|
message: get_message(subject),
|
|
|
|
endpoint: get_endpoint(subject) }
|
2019-08-03 22:37:21 +00:00
|
|
|
|
|
|
|
WebpushJob.perform_later(piratas: todas_menos_yo_ids,
|
|
|
|
ttl: ttl.to_i,
|
|
|
|
urgency: urgency.to_s,
|
2020-06-21 00:59:13 +00:00
|
|
|
payload: WebpushPayload.new(payload).to_json)
|
|
|
|
|
|
|
|
TelegramJob.perform_later(piratas: todas_menos_yo_ids,
|
|
|
|
payload: payload.to_json)
|
2019-08-03 22:37:21 +00:00
|
|
|
end
|
|
|
|
|
2019-04-06 19:45:07 +00:00
|
|
|
# Autenticar a la pirata usando HTTP Basic Auth
|
|
|
|
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication
|
|
|
|
def authenticate!
|
|
|
|
@current_pirata ||= authenticate_with_http_basic do |email, password|
|
|
|
|
pirata = Pirata.find_by_email(email)
|
|
|
|
|
|
|
|
if pirata&.authenticate(password)
|
|
|
|
session[:pirata_id] = pirata.id
|
|
|
|
pirata
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Si no la encuentra, no nos deja hacer nada
|
|
|
|
render(json: {}, status: :forbidden) unless current_pirata
|
|
|
|
end
|
2019-04-05 20:20:20 +00:00
|
|
|
end
|