2020-03-23 20:46:19 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Api
|
|
|
|
module V1
|
|
|
|
# API para formulario de contacto
|
2020-06-16 22:10:54 +00:00
|
|
|
class ContactController < ProtectedController
|
2020-03-23 20:46:19 +00:00
|
|
|
# Recibe un mensaje a través del formulario de contacto y lo envía
|
|
|
|
# a les usuaries del sitio.
|
|
|
|
#
|
|
|
|
# Tenemos que verificar que el sitio exista y que algunos campos
|
|
|
|
# estén llenos para detener spambots o DDOS. También nos vamos a
|
|
|
|
# estar apoyando en la limitación de peticiones en el servidor web.
|
|
|
|
def receive
|
|
|
|
# No hacer nada si no se pasaron los chequeos
|
|
|
|
return if performed?
|
|
|
|
|
2020-05-30 19:43:25 +00:00
|
|
|
# TODO: Verificar que los campos obligatorios hayan llegado!
|
|
|
|
|
2020-03-23 20:46:19 +00:00
|
|
|
# Si todo salió bien, enviar los correos y redirigir al sitio.
|
|
|
|
# El sitio nos dice a dónde tenemos que ir.
|
2020-09-29 21:22:28 +00:00
|
|
|
ContactJob.perform_async site.id,
|
|
|
|
params[:form],
|
2020-12-24 15:50:33 +00:00
|
|
|
contact_params.to_h.symbolize_keys,
|
|
|
|
params[:redirect]
|
2020-03-23 20:46:19 +00:00
|
|
|
|
2020-09-29 21:22:28 +00:00
|
|
|
redirect_to params[:redirect] || origin.to_s
|
2020-03-23 20:46:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def from_is_address?
|
2020-06-24 22:48:01 +00:00
|
|
|
return if contact_params[:from].blank?
|
2020-03-23 20:46:19 +00:00
|
|
|
return if EmailAddress.valid? contact_params[:from]
|
|
|
|
|
2020-06-16 22:10:54 +00:00
|
|
|
@reason = 'email_invalid'
|
2020-05-30 19:43:25 +00:00
|
|
|
head :precondition_required
|
2020-03-23 20:46:19 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def gave_consent?
|
2020-05-30 19:43:25 +00:00
|
|
|
return if contact_params[:consent].present?
|
2020-03-23 20:46:19 +00:00
|
|
|
|
2020-06-16 22:10:54 +00:00
|
|
|
@reason = 'no_consent'
|
2020-09-29 21:22:28 +00:00
|
|
|
render plain: Rails.env.production? ? nil : @reason, status: :precondition_required
|
2020-03-23 20:46:19 +00:00
|
|
|
end
|
|
|
|
|
2020-05-30 19:43:25 +00:00
|
|
|
# Los campos que se envían tienen que corresponder con un
|
|
|
|
# formulario de contacto.
|
2020-06-19 16:26:10 +00:00
|
|
|
def destination_exists?
|
2020-06-16 22:10:54 +00:00
|
|
|
return if form? && site.form?(params[:form])
|
2020-05-30 19:43:25 +00:00
|
|
|
|
2020-06-16 22:10:54 +00:00
|
|
|
@reason = 'form_doesnt_exist'
|
2020-09-29 21:22:28 +00:00
|
|
|
render plain: Rails.env.production? ? nil : @reason, status: :precondition_required
|
2020-03-25 18:36:06 +00:00
|
|
|
end
|
|
|
|
|
2020-03-23 20:46:19 +00:00
|
|
|
# Parámetros limpios
|
|
|
|
def contact_params
|
2020-05-30 19:43:25 +00:00
|
|
|
@contact_params ||= params.permit(site.form(params[:form]).params)
|
2020-03-23 20:46:19 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|