5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-29 22:16:22 +00:00
panel/app/controllers/application_controller.rb

149 lines
4.7 KiB
Ruby
Raw Normal View History

2019-03-26 15:32:20 +00:00
# frozen_string_literal: true
2018-01-29 22:19:10 +00:00
# Forma de ingreso a Sutty
2018-01-02 17:19:25 +00:00
class ApplicationController < ActionController::Base
include ExceptionHandler
include Pundit
2021-03-26 15:45:43 +00:00
protect_from_forgery with: :null_session, prepend: true
before_action :prepare_exception_notifier
before_action :configure_permitted_parameters, if: :devise_controller?
before_action :notify_unconfirmed_email, unless: :devise_controller?
2020-05-23 15:38:03 +00:00
around_action :set_locale
2018-01-29 18:09:30 +00:00
rescue_from Pundit::NilPolicyError, with: :page_not_found
rescue_from ActionController::RoutingError, with: :page_not_found
rescue_from ActionController::ParameterMissing, with: :page_not_found
2020-05-11 21:56:42 +00:00
before_action do
2020-10-04 00:32:32 +00:00
Rack::MiniProfiler.authorize_request if current_usuarie&.email&.ends_with?('@' + ENV.fetch('SUTTY', 'sutty.nl'))
2020-05-11 21:56:42 +00:00
end
2018-01-29 22:19:10 +00:00
# No tenemos índice de sutty, vamos directamente a ver el listado de
# sitios
2018-01-29 18:09:30 +00:00
def index
redirect_to sites_path
end
2018-01-29 22:19:10 +00:00
private
2024-01-31 18:41:12 +00:00
# Traer datos de muestra de la cola de moderación
def dummy_data
@moderation_queue = YAML.safe_load(File.read(Rails.root.join('db', 'seeds', 'moderation_queue.yaml')))
@remote_profile = YAML.safe_load(File.read(Rails.root.join('db', 'seeds', 'remote_profile.yaml')))
@instances = YAML.safe_load(File.read(Rails.root.join('db', 'seeds', 'instances.yaml')))
@moderation_queue.each do |activity|
activity['attributedTo'] = @remote_profile
end
end
2018-01-29 22:19:10 +00:00
# parámetros de botones:
# text: [texto del botón]
# class: [clases css]
# href: [href del botón]
def botoneras_moderation_queue
# botones de comentarios
@comment_btn_params = [[t('moderation_queue.button_box.text_pause'), 'btn-secondary', ''],
[t('moderation_queue.button_box.text_reject'), 'btn-primary', ''],
[t('moderation_queue.button_box.text_accept'), 'bg-blue white', ''],
[t('moderation_queue.button_box.text_reply'), 'btn-outline-primary', ''],
[t('moderation_queue.button_box.text_report'), 'btn-danger', '']]
#botones de remote_profile (cuentas)
@profile_btn_params = [[t('moderation_queue.profile_button_box.text_deny'), 'btn-outline-info', ''],
[t('moderation_queue.profile_button_box.text_allow'), 'btn-success', 'https://sutty.nl/'],
[t('moderation_queue.profile_button_box.text_report'), 'btn-outline-danger', 'https://sutty.nl/']]
end
def notify_unconfirmed_email
return unless current_usuarie
return if current_usuarie.confirmed?
2023-04-04 16:34:19 +00:00
I18n.with_locale(current_usuarie.lang) do
flash[:notice] ||= I18n.t('devise.registrations.signed_up')
end
end
def uuid?(string)
/[a-f0-9]{8}-([a-f0-9]{4}-){3}[a-f0-9]{12}/ =~ string
end
2019-07-03 23:25:23 +00:00
# Encontrar un sitio por su nombre
2018-01-29 22:19:10 +00:00
def find_site
id = params[:site_id] || params[:id]
unless (site = current_usuarie&.sites&.find_by_name(id))
raise SiteNotFound
end
2019-07-04 16:23:43 +00:00
site
2018-01-29 22:19:10 +00:00
end
2018-01-30 15:20:19 +00:00
2021-05-10 17:48:48 +00:00
# Devuelve el idioma actual y si no lo encuentra obtiene uno por
# defecto.
#
# Esto se refiere al idioma de la interfaz, no de los artículos.
#
# @return [String,Symbol]
def current_locale
locale = params[:change_locale_to]
if locale.present? && I18n.locale_available?(locale)
session[:locale] = params[:change_locale_to]
end
2021-05-10 17:48:48 +00:00
session[:locale] || current_usuarie&.lang || I18n.locale
2021-05-10 17:48:48 +00:00
end
2020-05-23 15:38:03 +00:00
# El idioma es el preferido por le usuarie, pero no necesariamente se
# corresponde con el idioma de los artículos, porque puede querer
# traducirlos.
def set_locale(&action)
I18n.with_locale(current_locale, &action)
end
# Muestra una página 404
def page_not_found
render 'application/page_not_found', status: :not_found
end
# Necesario para poder acceder a Blazer. Solo les usuaries de este
# sitio pueden acceder al panel.
def require_usuarie
site = find_site
authorize SiteBlazer.new(site)
# Necesario para los breadcrumbs.
ActionView::Base.include Loaf::ViewExtensions unless ActionView::Base.included_modules.include? Loaf::ViewExtensions
breadcrumb current_usuarie.email, main_app.edit_usuarie_registration_path
breadcrumb 'sites.index', main_app.sites_path, match: :exact
breadcrumb site.title, main_app.site_path(site), match: :exact
breadcrumb 'stats.index', root_path, match: :exact
end
2023-04-15 21:36:56 +00:00
def site
@site ||= find_site
end
protected
def configure_permitted_parameters
2023-04-04 16:34:19 +00:00
devise_parameter_sanitizer.permit(:sign_up, keys: Usuarie::CONSENT_FIELDS)
devise_parameter_sanitizer.permit(:account_update, keys: %i[lang])
2018-02-26 22:08:21 +00:00
end
def prepare_exception_notifier
request.env['exception_notifier.exception_data'] = { usuarie: current_usuarie }
end
# Olvidar el idioma elegido antes de iniciar la sesión y reenviar a
# los sitios en el idioma de le usuarie.
def after_sign_in_path_for(resource)
session[:locale] = nil
sites_path
end
2024-01-31 18:41:12 +00:00
2018-01-02 17:19:25 +00:00
end