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
|
|
|
|
SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do
|
2023-03-15 21:22:20 +00:00
|
|
|
def deploy
|
|
|
|
site.enqueue!
|
2023-04-10 20:33:07 +00:00
|
|
|
DeployJob.perform_later site.id
|
2023-03-15 21:22:20 +00:00
|
|
|
end
|
|
|
|
|
2019-08-09 19:49:25 +00:00
|
|
|
# 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'
|
2023-03-30 17:45:49 +00:00
|
|
|
site.deploys.build type: 'DeployLocal'
|
2023-04-24 17:46:56 +00:00
|
|
|
# Los sitios de testing no se sincronizan
|
|
|
|
sync_nodes unless site.name.end_with? '.testing'
|
2019-08-09 19:49:25 +00:00
|
|
|
|
2023-03-16 21:26:17 +00:00
|
|
|
I18n.with_locale(usuarie.lang.to_sym || I18n.default_locale) do
|
|
|
|
# No se puede llamar a site.config antes de save porque el sitio
|
|
|
|
# todavía no existe.
|
|
|
|
#
|
|
|
|
# TODO: hacer que el repositorio se cree cuando es necesario, para
|
|
|
|
# que no haya estados intermedios.
|
|
|
|
site.locales = [usuarie.lang] + I18n.available_locales
|
|
|
|
|
2019-08-09 19:49:25 +00:00
|
|
|
site.save &&
|
|
|
|
site.config.write &&
|
2023-03-22 21:45:41 +00:00
|
|
|
commit_config(action: :create) &&
|
2023-03-23 19:53:06 +00:00
|
|
|
site.reset.nil? &&
|
2023-03-22 22:04:15 +00:00
|
|
|
add_licencias &&
|
2023-03-23 00:01:29 +00:00
|
|
|
add_code_of_conduct &&
|
2023-03-30 16:51:09 +00:00
|
|
|
add_privacy_policy &&
|
2023-06-15 15:04:44 +00:00
|
|
|
site.index_posts! &&
|
2023-03-30 16:51:09 +00:00
|
|
|
deploy
|
2019-08-09 19:49:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
site
|
|
|
|
end
|
|
|
|
|
|
|
|
# Actualiza el sitio y guarda los cambios en la configuración
|
|
|
|
def update
|
2021-03-03 12:43:15 +00:00
|
|
|
I18n.with_locale(usuarie&.lang&.to_sym || 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 &&
|
2023-03-22 21:45:41 +00:00
|
|
|
commit_config(action: :update) &&
|
2023-03-23 19:53:06 +00:00
|
|
|
site.reset.nil? &&
|
2023-03-22 21:45:41 +00:00
|
|
|
change_licencias
|
2019-08-09 19:49:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
site
|
|
|
|
end
|
|
|
|
|
2020-09-28 22:38:24 +00:00
|
|
|
# Genera los Deploy necesarios para el sitio a menos que ya los tenga.
|
2019-10-01 19:41:33 +00:00
|
|
|
def build_deploys
|
2023-08-30 13:47:27 +00:00
|
|
|
Deploy.subclasses.each do |deploy|
|
|
|
|
next if site.deploys.find_by type: deploy.name
|
2019-10-01 19:41:33 +00:00
|
|
|
|
|
|
|
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
|
2023-03-28 19:21:29 +00:00
|
|
|
onion = params[:onion]
|
|
|
|
deploy = params[:deploy]
|
2020-07-18 19:26:54 +00:00
|
|
|
|
|
|
|
return false unless !onion.blank? && deploy
|
|
|
|
|
|
|
|
site.config['onion-location'] = onion
|
|
|
|
site.config.write
|
|
|
|
|
|
|
|
commit_config(action: :tor)
|
|
|
|
end
|
|
|
|
|
2021-05-31 16:23:40 +00:00
|
|
|
# Trae cambios desde la rama remota y reindexa los artículos.
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def merge
|
|
|
|
result = site.repository.merge(usuarie)
|
|
|
|
|
|
|
|
# TODO: Implementar callbacks
|
|
|
|
site.try(:index_posts!) if result
|
|
|
|
|
|
|
|
result.present?
|
|
|
|
end
|
|
|
|
|
2023-10-05 20:13:47 +00:00
|
|
|
def rename(name)
|
2023-11-21 15:45:20 +00:00
|
|
|
return if name == site.name
|
2023-11-14 15:05:19 +00:00
|
|
|
moved = false
|
2023-11-21 12:59:02 +00:00
|
|
|
site.name = name
|
2023-11-14 15:12:15 +00:00
|
|
|
|
2023-10-05 20:13:47 +00:00
|
|
|
Site.transaction do
|
2023-11-22 13:17:57 +00:00
|
|
|
raise ActiveRecord::Rollback if File.exists?(site.path)
|
2023-11-21 15:45:20 +00:00
|
|
|
FileUtils.mv (site.path_was, site.path)
|
2023-11-22 16:12:15 +00:00
|
|
|
moved = true
|
2023-10-05 20:13:47 +00:00
|
|
|
ActiveStorage::Blob.where(service_name: site.name_was).update_all(service_name: site.name)
|
2024-01-10 18:51:28 +00:00
|
|
|
site.save
|
2023-10-09 18:50:25 +00:00
|
|
|
rescue StandardError
|
2024-01-10 18:51:28 +00:00
|
|
|
FileUtils.mv(site.path, site.path_was) if moved
|
2023-10-09 18:50:25 +00:00
|
|
|
raise
|
2023-10-05 20:13:47 +00:00
|
|
|
end
|
|
|
|
end
|
2023-10-05 20:19:41 +00:00
|
|
|
|
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,
|
2023-07-03 16:23:54 +00:00
|
|
|
add: [site.config.path],
|
2019-08-09 19:49:25 +00:00
|
|
|
message: I18n.t("site_service.#{action}",
|
|
|
|
name: site.name))
|
2023-08-16 20:35:33 +00:00
|
|
|
|
|
|
|
GitPushJob.perform_later(site)
|
2019-08-09 19:49:25 +00:00
|
|
|
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
|
2023-03-22 22:32:52 +00:00
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2019-09-17 21:27:51 +00:00
|
|
|
def add_licencias
|
2023-03-22 22:32:52 +00:00
|
|
|
return true unless site.layout? :license
|
2023-04-03 20:50:04 +00:00
|
|
|
return true if site.licencia.custom?
|
2020-05-23 15:38:03 +00:00
|
|
|
|
2023-03-22 22:32:52 +00:00
|
|
|
with_all_locales do |locale|
|
|
|
|
add_licencia lang: locale
|
|
|
|
end.compact.map(&:valid?).all?
|
2019-09-17 23:26:59 +00:00
|
|
|
end
|
|
|
|
|
2023-03-22 22:32:52 +00:00
|
|
|
# Crea una licencia
|
|
|
|
#
|
|
|
|
# @return [Post]
|
2019-09-17 23:26:59 +00:00
|
|
|
def add_licencia(lang:)
|
|
|
|
params = ActionController::Parameters.new(
|
|
|
|
post: {
|
2023-03-22 21:39:56 +00:00
|
|
|
layout: 'license',
|
2023-03-22 22:21:52 +00:00
|
|
|
slug: Jekyll::Utils.slugify(I18n.t('activerecord.models.licencia')),
|
2019-09-17 23:26:59 +00:00
|
|
|
lang: lang,
|
|
|
|
title: site.licencia.name,
|
2023-03-25 16:46:30 +00:00
|
|
|
description: site.licencia.short_description,
|
2019-09-17 23:26:59 +00:00
|
|
|
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
|
|
|
|
#
|
2023-03-22 22:32:52 +00:00
|
|
|
# @return [Boolean]
|
2019-09-17 23:26:59 +00:00
|
|
|
def change_licencias
|
2023-03-22 22:32:52 +00:00
|
|
|
return true unless site.layout? :license
|
2023-04-03 20:50:04 +00:00
|
|
|
return true if site.licencia.custom?
|
2021-10-22 20:13:26 +00:00
|
|
|
|
2023-03-22 22:32:52 +00:00
|
|
|
with_all_locales do |locale|
|
|
|
|
post = site.posts(lang: locale).find_by(layout: 'license')
|
2019-09-17 23:26:59 +00:00
|
|
|
|
2023-03-22 22:32:52 +00:00
|
|
|
change_licencia(post: post) if post
|
|
|
|
end.compact.map(&:valid?).all?
|
2019-09-17 21:27:51 +00:00
|
|
|
end
|
2019-09-17 23:26:59 +00:00
|
|
|
|
2023-03-22 22:32:52 +00:00
|
|
|
# Cambia una licencia
|
|
|
|
#
|
|
|
|
# @param :post [Post]
|
|
|
|
# @return [Post]
|
2019-09-17 23:26:59 +00:00
|
|
|
def change_licencia(post:)
|
|
|
|
params = ActionController::Parameters.new(
|
|
|
|
post: {
|
|
|
|
title: site.licencia.name,
|
2023-03-25 16:51:07 +00:00
|
|
|
description: site.licencia.short_description,
|
2019-09-17 23:26:59 +00:00
|
|
|
content: CommonMarker.render_html(site.licencia.deed)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
PostService.new(site: site, usuarie: usuarie, post: post,
|
|
|
|
params: params).update
|
|
|
|
end
|
2022-04-06 21:38:03 +00:00
|
|
|
|
2023-03-22 22:32:52 +00:00
|
|
|
# Agrega un código de conducta
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
2023-03-22 22:04:15 +00:00
|
|
|
def add_code_of_conduct
|
2023-03-22 22:32:52 +00:00
|
|
|
return true unless site.layout?(:code_of_conduct) || site.layout?(:page)
|
2023-03-22 22:06:57 +00:00
|
|
|
|
2023-03-22 22:04:15 +00:00
|
|
|
# TODO: soportar más códigos de conducta
|
|
|
|
coc = CodeOfConduct.first
|
|
|
|
|
|
|
|
with_all_locales do |locale|
|
|
|
|
params = ActionController::Parameters.new(
|
|
|
|
post: {
|
2023-03-22 22:06:57 +00:00
|
|
|
layout: site.layout?(:code_of_conduct) ? 'code_of_conduct' : 'page',
|
2023-03-22 22:04:15 +00:00
|
|
|
lang: locale.to_s,
|
|
|
|
title: coc.title,
|
|
|
|
description: coc.description,
|
|
|
|
content: CommonMarker.render_html(coc.content)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
PostService.new(site: site, usuarie: usuarie, params: params).create
|
2023-03-22 22:32:52 +00:00
|
|
|
end.compact.map(&:valid?).all?
|
2023-03-22 22:04:15 +00:00
|
|
|
end
|
|
|
|
|
2023-03-23 00:01:29 +00:00
|
|
|
# Agrega política de privacidad
|
|
|
|
#
|
|
|
|
# @return [Boolean]
|
|
|
|
def add_privacy_policy
|
|
|
|
return true unless site.layout?(:privacy_policy) || site.layout?(:page)
|
|
|
|
|
|
|
|
pp = PrivacyPolicy.first
|
|
|
|
|
|
|
|
with_all_locales do |locale|
|
|
|
|
params = ActionController::Parameters.new(
|
|
|
|
post: {
|
|
|
|
layout: site.layout?(:privacy_policy) ? 'privacy_policy' : 'page',
|
|
|
|
lang: locale.to_s,
|
|
|
|
title: pp.title,
|
|
|
|
description: pp.description,
|
|
|
|
content: CommonMarker.render_html(pp.content)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
PostService.new(site: site, usuarie: usuarie, params: params).create
|
|
|
|
end.compact.map(&:valid?).all?
|
|
|
|
end
|
|
|
|
|
2022-04-06 21:38:03 +00:00
|
|
|
# Crea los deploys necesarios para sincronizar a otros nodos de Sutty
|
|
|
|
def sync_nodes
|
2022-04-06 22:05:50 +00:00
|
|
|
Rails.application.nodes.each do |node|
|
2023-04-24 17:41:45 +00:00
|
|
|
site.deploys.build(type: 'DeployFullRsync', destination: "rsync://rsyncd.#{node}/deploys/", hostname: node)
|
2022-04-06 21:38:03 +00:00
|
|
|
end
|
|
|
|
end
|
2023-03-22 22:04:15 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def with_all_locales(&block)
|
2023-03-22 22:32:52 +00:00
|
|
|
site.locales.map do |locale|
|
2023-03-22 22:04:15 +00:00
|
|
|
next unless I18n.available_locales.include? locale
|
|
|
|
|
|
|
|
Mobility.with_locale(locale) do
|
|
|
|
yield locale
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-08-09 19:49:25 +00:00
|
|
|
end
|