2019-08-09 19:49:25 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Se encargar de guardar cambios en sitios
|
|
|
|
# TODO: Implementar rollback en la configuración
|
|
|
|
# rubocop:disable Metrics/BlockLength
|
|
|
|
SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do
|
|
|
|
# Crea un sitio, agrega un rol nuevo y guarda los cambios a la
|
|
|
|
# configuración en el repositorio git
|
|
|
|
def create
|
|
|
|
self.site = Site.new params
|
|
|
|
|
|
|
|
add_role temporal: false, rol: 'usuarie'
|
|
|
|
|
|
|
|
I18n.with_locale(usuarie.try(:lang) || I18n.default_locale) do
|
|
|
|
site.save &&
|
|
|
|
site.config.write &&
|
|
|
|
commit_config(action: :create)
|
|
|
|
end
|
|
|
|
|
2019-09-17 21:27:51 +00:00
|
|
|
add_licencias
|
|
|
|
|
2019-08-09 19:49:25 +00:00
|
|
|
site
|
|
|
|
end
|
|
|
|
|
|
|
|
# Actualiza el sitio y guarda los cambios en la configuración
|
|
|
|
def update
|
|
|
|
I18n.with_locale(usuarie.try(:lang) || I18n.default_locale) do
|
2019-08-29 17:54:19 +00:00
|
|
|
site.update(params) &&
|
2019-08-09 19:49:25 +00:00
|
|
|
site.config.write &&
|
|
|
|
commit_config(action: :update)
|
|
|
|
end
|
|
|
|
|
2019-09-17 23:26:59 +00:00
|
|
|
change_licencias
|
|
|
|
|
2019-08-09 19:49:25 +00:00
|
|
|
site
|
|
|
|
end
|
|
|
|
|
2019-10-01 19:41:33 +00:00
|
|
|
def build_deploys
|
2020-07-18 19:26:54 +00:00
|
|
|
Site::DEPLOYS.map { |deploy| "Deploy#{deploy.to_s.camelcase}" }
|
2019-10-02 22:40:43 +00:00
|
|
|
.each do |deploy|
|
2019-10-01 19:41:33 +00:00
|
|
|
next if site.deploys.find_by type: deploy
|
|
|
|
|
|
|
|
site.deploys.build type: deploy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-18 19:26:54 +00:00
|
|
|
# Agregar una dirección oculta de Tor al DeployHiddenService y a la
|
|
|
|
# configuración del Site.
|
|
|
|
def add_onion
|
|
|
|
onion = params[:onion].strip
|
|
|
|
deploy = DeployHiddenService.find_by(site: site)
|
|
|
|
|
|
|
|
return false unless !onion.blank? && deploy
|
|
|
|
|
|
|
|
deploy.values[:onion] = onion
|
|
|
|
deploy.save
|
|
|
|
|
|
|
|
site.config['onion-location'] = onion
|
|
|
|
site.config.write
|
|
|
|
|
|
|
|
commit_config(action: :tor)
|
|
|
|
end
|
|
|
|
|
2019-08-09 19:49:25 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
# Guarda los cambios de la configuración en el repositorio git
|
|
|
|
def commit_config(action:)
|
|
|
|
site.repository
|
|
|
|
.commit(usuarie: usuarie,
|
|
|
|
file: site.config.path,
|
|
|
|
message: I18n.t("site_service.#{action}",
|
|
|
|
name: site.name))
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_role(temporal: true, rol: 'invitade')
|
|
|
|
site.roles << Rol.new(site: site, usuarie: usuarie,
|
|
|
|
temporal: temporal, rol: rol)
|
|
|
|
end
|
2019-09-17 21:27:51 +00:00
|
|
|
|
2020-05-12 13:11:59 +00:00
|
|
|
# Crea la licencia del sitio para cada locale disponible en el sitio
|
2019-09-17 21:27:51 +00:00
|
|
|
def add_licencias
|
2020-05-12 13:11:59 +00:00
|
|
|
site.locales.map(&:to_sym).each do |locale|
|
2020-05-23 15:38:03 +00:00
|
|
|
next unless I18n.available_locales.include? locale
|
|
|
|
|
2019-09-17 21:27:51 +00:00
|
|
|
Mobility.with_locale(locale) do
|
2019-09-17 23:26:59 +00:00
|
|
|
add_licencia lang: locale
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_licencia(lang:)
|
|
|
|
params = ActionController::Parameters.new(
|
|
|
|
post: {
|
|
|
|
lang: lang,
|
|
|
|
title: site.licencia.name,
|
2019-10-17 19:16:30 +00:00
|
|
|
description: I18n.t('sites.form.licencia.title'),
|
2019-09-17 23:26:59 +00:00
|
|
|
author: %w[Sutty],
|
|
|
|
permalink: "#{I18n.t('activerecord.models.licencia').downcase}/",
|
|
|
|
content: CommonMarker.render_html(site.licencia.deed)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
PostService.new(site: site, usuarie: usuarie, params: params).create
|
|
|
|
end
|
|
|
|
|
|
|
|
# Encuentra la licencia a partir de su enlace permanente y le cambia
|
|
|
|
# el contenido
|
|
|
|
#
|
|
|
|
# TODO: Crear un layout específico para licencias así es más certera
|
|
|
|
# la búsqueda.
|
|
|
|
def change_licencias
|
2020-05-12 13:11:59 +00:00
|
|
|
site.locales.map(&:to_sym).each do |locale|
|
2019-09-17 23:26:59 +00:00
|
|
|
Mobility.with_locale(locale) do
|
|
|
|
permalink = "#{I18n.t('activerecord.models.licencia').downcase}/"
|
|
|
|
post = site.posts(lang: locale).find_by(permalink: permalink)
|
|
|
|
|
|
|
|
post ? change_licencia(post: post) : add_licencia(lang: locale)
|
2019-09-17 21:27:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-09-17 23:26:59 +00:00
|
|
|
|
|
|
|
def change_licencia(post:)
|
|
|
|
params = ActionController::Parameters.new(
|
|
|
|
post: {
|
|
|
|
title: site.licencia.name,
|
|
|
|
content: CommonMarker.render_html(site.licencia.deed)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
PostService.new(site: site, usuarie: usuarie, post: post,
|
|
|
|
params: params).update
|
|
|
|
end
|
2019-08-09 19:49:25 +00:00
|
|
|
end
|
|
|
|
# rubocop:enable Metrics/BlockLength
|