5
0
Fork 0
mirror of https://0xacab.org/sutty/sutty synced 2024-11-26 08:16:26 +00:00
panel/app/services/post_service.rb

250 lines
7 KiB
Ruby
Raw Normal View History

2019-08-13 23:33:57 +00:00
# frozen_string_literal: true
# Este servicio se encarga de crear artículos y guardarlos en git,
# asignándoselos a une usuarie
PostService = Struct.new(:site, :usuarie, :post, :params, keyword_init: true) do
# Crea un artículo nuevo y modificar las asociaciones
2019-08-13 23:33:57 +00:00
#
# @return Post
def create
2023-10-06 13:16:47 +00:00
self.post = Post.build(site: site, locale: locale, layout: layout)
post.usuaries << usuarie
params[:post][:draft] = true if site.invitade? usuarie
params.require(:post).permit(:slug).tap do |p|
post.slug.value = p[:slug] if p[:slug].present?
end
if post.update(post_params)
added_paths = []
added_paths << post.path.value
# Recorrer todas las asociaciones y agregarse donde corresponda
update_associations(post)
associated_posts_to_save.each do |associated_post|
next unless associated_post.save(validate: false)
added_paths << associated_post.path.value
end
commit(action: :created, add: added_paths)
end
# Devolver el post aunque no se haya salvado para poder rescatar los
# errores
post
end
2020-06-16 22:10:54 +00:00
# Crear un post anónimo, con opciones más limitadas. No usamos post.
#
# @todo Permitir asociaciones?
2020-02-18 16:45:08 +00:00
def create_anonymous
# XXX: Confiamos en el parámetro de idioma porque estamos
# verificándolos en Site#posts
2023-10-06 13:16:47 +00:00
self.post = Post.build(site: site, locale: locale, layout: layouts)
2020-02-18 16:45:08 +00:00
# Los artículos anónimos siempre son borradores
2020-06-16 22:10:54 +00:00
params[:draft] = true
2020-02-18 16:45:08 +00:00
commit(action: :created, add: [post.path.absolute]) if post.update(anon_post_params)
2020-02-18 16:45:08 +00:00
post
end
# Al actualizar, modificamos un post pre-existente, todas las
# relaciones anteriores y las relaciones actuales.
def update
post.usuaries << usuarie
params[:post][:draft] = true if site.invitade? usuarie
if post.update(post_params)
# Eliminar ("mover") el archivo si cambió de ubicación.
rm = []
rm << post.path.value_was if post.path.changed?
2019-08-13 23:33:57 +00:00
added_paths = []
added_paths << post.path.value
# Recorrer todas las asociaciones y agregarse donde corresponda
update_associations(post)
associated_posts_to_save.each do |associated_post|
next unless associated_post.save(validate: false)
2019-08-13 23:33:57 +00:00
added_paths << associated_post.path.value
end
commit(action: :updated, add: added_paths, rm: rm)
end
2019-08-13 23:33:57 +00:00
# Devolver el post aunque no se haya salvado para poder rescatar los
# errores
post
end
2019-08-16 23:12:22 +00:00
def destroy
post.destroy!
commit(action: :destroyed, rm: [post.path.absolute]) if post.destroyed?
2019-08-16 23:12:22 +00:00
post
end
2019-11-06 22:35:48 +00:00
# Reordena todos los posts que soporten orden de acuerdo a un hash de
2020-01-02 23:29:04 +00:00
# uuids y nuevas posiciones. La posición actual la da la posición en
2019-10-18 20:35:09 +00:00
# el array.
#
2020-01-02 23:29:04 +00:00
# { uuid => 2, uuid => 1, uuid => 0 }
2019-10-18 20:35:09 +00:00
def reorder
2020-10-04 00:32:32 +00:00
reorder = params.require(:post).permit(reorder: {})&.dig(:reorder)&.transform_values(&:to_i)
posts = site.indexed_posts(locale: locale).where(post_id: reorder.keys).map(&:post)
2019-10-18 20:35:09 +00:00
2020-08-28 16:48:04 +00:00
files = posts.map do |post|
next unless post.attribute? :order
order = reorder[post.uuid.value]
next if post.order.value == order
2019-10-18 20:35:09 +00:00
post.order.value = order
2019-10-18 20:35:09 +00:00
post.path.absolute
end.compact
2020-08-28 16:48:04 +00:00
return if files.empty?
2019-10-18 20:35:09 +00:00
# TODO: Implementar transacciones!
2020-08-28 16:48:04 +00:00
posts.save_all(validate: false) &&
commit(action: :reorder, add: files)
2019-10-18 20:35:09 +00:00
end
2019-08-13 23:33:57 +00:00
private
def commit(action:, add: [], rm: [])
site.repository.commit(add: add,
rm: rm,
usuarie: usuarie,
message: I18n.t("post_service.#{action}",
2020-10-04 00:32:32 +00:00
title: post&.title&.value))
GitPushJob.perform_later(site)
end
2019-08-13 23:33:57 +00:00
# Solo permitir cambiar estos atributos de cada articulo
def post_params
params.require(:post).permit(post.params)
end
2019-10-18 20:35:09 +00:00
2020-02-18 16:45:08 +00:00
# Eliminar metadatos internos
def anon_post_params
2020-05-23 15:38:03 +00:00
params.permit(post.params).delete_if do |k, _|
2020-08-07 14:15:06 +00:00
%w[date slug order uuid].include? k.to_s
2020-02-18 16:45:08 +00:00
end
end
2023-10-06 13:16:47 +00:00
# @return [Symbol]
2020-05-23 15:38:03 +00:00
def locale
params.dig(:post, :lang)&.to_sym || I18n.locale
2020-03-19 18:31:29 +00:00
end
2023-10-06 13:16:47 +00:00
# @return [Layout]
2020-03-19 18:31:29 +00:00
def layout
2023-10-06 13:16:47 +00:00
site.layouts[
(params.dig(:post, :layout) || params[:layout]).to_sym
]
2019-10-18 20:35:09 +00:00
end
# Si les usuaries modifican o crean una licencia, considerarla
# personalizada en el panel.
def update_site_license!
if site.usuarie?(usuarie) && post.layout.name == :license && !site.licencia.custom?
site.update licencia: Licencia.find_by_icons('custom')
end
end
# @return [Array<String>]
def associated_posts_to_save
@associated_posts_to_save ||= Set.new
end
# Recolectar campos asociados que no estén vacíos
#
# @param [Post]
# @return [Array<Symbol>]
def association_attributes(post)
post.attributes.select do |attribute|
post[attribute].try(:inverse?)
end
end
# @param :post_ids [Array<String>]
# @return [Association]
def associated_posts(post_ids)
site.indexed_posts.where(post_id: post_ids).map(&:post)
end
# Modificar las asociaciones en cascada
def update_associations(post)
association_attributes(post).each do |attribute|
metadata = post[attribute]
next unless metadata.changed?
inverse_attribute = post[attribute].inverse
value_was = metadata.value_was.dup
value = metadata.value.dup
case metadata.type
when 'has_and_belongs_to_many'
associated_posts(value_was - value).each do |remove_post|
remove_relation_from(remove_post[inverse_attribute], post.uuid.value)
end
associated_posts(value - value_was).each do |add_post|
add_post[inverse_attribute].value << post.uuid.value
add_post[inverse_attribute].value.uniq!
associated_posts_to_save << add_post
end
when 'has_many'
associated_posts(value_was - value).each do |remove_post|
remove_relation_from(remove_post[inverse_attribute], '')
end
associated_posts(value - value_was).each do |add_post|
associated_posts(add_post[inverse_attribute].value_was).each do |remove_post|
remove_relation_from(remove_post[attribute], add_post.uuid.value)
end
add_post[inverse_attribute].value = post.uuid.value
associated_posts_to_save << add_post
end
when 'belongs_to'
if value_was.present?
associated_posts(value_was).each do |remove_post|
remove_relation_from(remove_post[inverse_attribute], value_was)
end
end
associated_posts(value).each do |add_post|
add_post[inverse_attribute].value << post.uuid.value
add_post[inverse_attribute].value.uniq!
associated_posts_to_save << add_post
end
when 'locales'
end
end
end
# @todo por qué no podemos usar nil para deshabilitar un valor?
def remove_relation_from(metadata, value)
case metadata.value
when Array then metadata.value.delete(value)
when String then metadata.value = ''
end
associated_posts_to_save << metadata.post
nil
end
2019-08-13 23:33:57 +00:00
end