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
|
|
|
|
# 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'
|
2022-04-06 21:39:52 +00:00
|
|
|
sync_nodes
|
2019-08-09 19:49:25 +00:00
|
|
|
|
2021-03-03 12:43:15 +00:00
|
|
|
I18n.with_locale(usuarie&.lang&.to_sym || I18n.default_locale) do
|
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-22 22:04:15 +00:00
|
|
|
add_licencias &&
|
2023-03-23 00:01:29 +00:00
|
|
|
add_code_of_conduct &&
|
|
|
|
add_privacy_policy
|
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) &&
|
|
|
|
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
|
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
|
|
|
|
|
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
|
|
|
|
|
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
|
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
|
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-22 22:44:17 +00:00
|
|
|
description: site.licencia.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-03-22 22:23:51 +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,
|
|
|
|
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|
|
2022-04-06 21:38:03 +00:00
|
|
|
site.deploys.build(type: 'DeployRsync', destination: "sutty@#{node}:#{site.hostname}")
|
|
|
|
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
|