feat: tag deletion and other elements
This commit is contained in:
@@ -4,9 +4,11 @@ defmodule BDS.Templates do
|
||||
import Ecto.Query
|
||||
|
||||
alias BDS.Frontmatter
|
||||
alias BDS.Posts
|
||||
alias BDS.Projects
|
||||
alias BDS.Repo
|
||||
alias BDS.Slug
|
||||
alias BDS.Tags
|
||||
alias BDS.Templates.Template
|
||||
|
||||
def create_template(attrs) do
|
||||
@@ -71,24 +73,58 @@ defmodule BDS.Templates do
|
||||
end
|
||||
|
||||
content_changed? = has_attr?(attrs, :content) and attr(attrs, :content) != template.content
|
||||
slug_changed? = next_slug != template.slug
|
||||
now = System.system_time(:second)
|
||||
next_status = if(template.status == :published and content_changed?, do: :draft, else: template.status)
|
||||
next_file_path = next_template_file_path(template, next_slug)
|
||||
|
||||
updates = %{}
|
||||
|> maybe_put(:title, attr(attrs, :title))
|
||||
|> maybe_put(:kind, attr(attrs, :kind))
|
||||
|> maybe_put(:enabled, attr(attrs, :enabled))
|
||||
|> maybe_put(:content, attr(attrs, :content))
|
||||
|> Map.put(:file_path, next_file_path)
|
||||
|> Map.put(:slug, next_slug)
|
||||
|> Map.put(:version, template.version + 1)
|
||||
|> Map.put(:updated_at, now)
|
||||
|> maybe_put(:status, if(template.status == :published and content_changed?, do: :draft, else: nil))
|
||||
|> Map.put(:status, next_status)
|
||||
|
||||
template
|
||||
|> Template.changeset(updates)
|
||||
|> Repo.update()
|
||||
Repo.transaction(fn ->
|
||||
updated_template =
|
||||
template
|
||||
|> Template.changeset(updates)
|
||||
|> Repo.update!()
|
||||
|
||||
if slug_changed? do
|
||||
cascade_template_slug_change(template, updated_template, now)
|
||||
end
|
||||
|
||||
if template.file_path not in [nil, ""] and next_file_path != template.file_path do
|
||||
rewrite_template_file(template, updated_template)
|
||||
end
|
||||
|
||||
updated_template
|
||||
end)
|
||||
|> case do
|
||||
{:ok, updated_template} -> {:ok, updated_template}
|
||||
{:error, reason} -> {:error, reason}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def rebuild_templates_from_files(project_id) do
|
||||
project = Projects.get_project!(project_id)
|
||||
|
||||
templates =
|
||||
project
|
||||
|> Projects.project_data_dir()
|
||||
|> Path.join("templates")
|
||||
|> list_matching_files("*.liquid")
|
||||
|> Enum.map(&upsert_template_from_file(project_id, project, &1))
|
||||
|
||||
{:ok, templates}
|
||||
end
|
||||
|
||||
def delete_template(template_id, opts \\ []) do
|
||||
case Repo.get(Template, template_id) do
|
||||
nil ->
|
||||
@@ -154,6 +190,9 @@ defmodule BDS.Templates do
|
||||
Path.join(Projects.project_data_dir(project), relative_path)
|
||||
end
|
||||
|
||||
defp next_template_file_path(%Template{file_path: ""}, _next_slug), do: ""
|
||||
defp next_template_file_path(%Template{}, next_slug), do: template_file_path(next_slug)
|
||||
|
||||
defp serialize_template_file(template, content) do
|
||||
Frontmatter.serialize_document(
|
||||
[
|
||||
@@ -195,14 +234,108 @@ defmodule BDS.Templates do
|
||||
|> Repo.update_all(set: [post_template_slug: nil, updated_at: now])
|
||||
|
||||
Enum.each(affected_posts, fn post ->
|
||||
BDS.Posts.rewrite_published_post(post.id)
|
||||
Posts.rewrite_published_post(post.id)
|
||||
end)
|
||||
|
||||
BDS.Tags.sync_tags_json(template.project_id)
|
||||
Tags.sync_tags_json(template.project_id)
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
defp cascade_template_slug_change(original_template, updated_template, updated_at) do
|
||||
affected_posts =
|
||||
Repo.all(
|
||||
from(post in BDS.Posts.Post,
|
||||
where: post.project_id == ^original_template.project_id and post.template_slug == ^original_template.slug
|
||||
)
|
||||
)
|
||||
|
||||
from(post in BDS.Posts.Post,
|
||||
where: post.project_id == ^original_template.project_id and post.template_slug == ^original_template.slug
|
||||
)
|
||||
|> Repo.update_all(set: [template_slug: updated_template.slug, updated_at: updated_at])
|
||||
|
||||
from(tag in BDS.Tags.Tag,
|
||||
where: tag.project_id == ^original_template.project_id and tag.post_template_slug == ^original_template.slug
|
||||
)
|
||||
|> Repo.update_all(set: [post_template_slug: updated_template.slug, updated_at: updated_at])
|
||||
|
||||
Enum.each(affected_posts, fn post ->
|
||||
Posts.rewrite_published_post(post.id)
|
||||
end)
|
||||
|
||||
Tags.sync_tags_json(original_template.project_id)
|
||||
end
|
||||
|
||||
defp rewrite_template_file(original_template, updated_template) do
|
||||
body = published_template_body(original_template)
|
||||
new_full_path = full_file_path(updated_template.project_id, updated_template.file_path)
|
||||
:ok = File.mkdir_p(Path.dirname(new_full_path))
|
||||
:ok = File.write(new_full_path, serialize_template_file(updated_template, body))
|
||||
|
||||
if original_template.file_path != updated_template.file_path do
|
||||
_ = delete_file_if_present(original_template.project_id, original_template.file_path)
|
||||
end
|
||||
|
||||
:ok
|
||||
end
|
||||
|
||||
defp published_template_body(%Template{content: content}) when is_binary(content), do: content
|
||||
|
||||
defp published_template_body(template) do
|
||||
case File.read(full_file_path(template.project_id, template.file_path)) do
|
||||
{:ok, contents} ->
|
||||
case Frontmatter.parse_document(contents) do
|
||||
{:ok, %{body: body}} -> body
|
||||
{:error, _reason} -> ""
|
||||
end
|
||||
|
||||
{:error, _reason} ->
|
||||
""
|
||||
end
|
||||
end
|
||||
|
||||
defp upsert_template_from_file(project_id, project, path) do
|
||||
contents = File.read!(path)
|
||||
{:ok, %{fields: fields}} = Frontmatter.parse_document(contents)
|
||||
relative_path = Path.relative_to(path, Projects.project_data_dir(project))
|
||||
now = System.system_time(:second)
|
||||
|
||||
attrs = %{
|
||||
id: Map.get(fields, "id") || Ecto.UUID.generate(),
|
||||
project_id: project_id,
|
||||
slug: Map.fetch!(fields, "slug"),
|
||||
title: Map.get(fields, "title") || "",
|
||||
kind: parse_template_kind(Map.fetch!(fields, "kind")),
|
||||
enabled: Map.get(fields, "enabled", true),
|
||||
version: Map.get(fields, "version", 1),
|
||||
file_path: relative_path,
|
||||
status: :published,
|
||||
content: nil,
|
||||
created_at: Map.get(fields, "created_at", now),
|
||||
updated_at: Map.get(fields, "updated_at", now)
|
||||
}
|
||||
|
||||
template = Repo.get_by(Template, project_id: project_id, slug: attrs.slug) || %Template{}
|
||||
|
||||
template
|
||||
|> Template.changeset(attrs)
|
||||
|> Repo.insert_or_update!()
|
||||
end
|
||||
|
||||
defp parse_template_kind(kind) when is_atom(kind), do: kind
|
||||
defp parse_template_kind(kind), do: String.to_existing_atom(kind)
|
||||
|
||||
defp list_matching_files(dir, pattern) do
|
||||
if File.dir?(dir) do
|
||||
Path.join(dir, pattern)
|
||||
|> Path.wildcard()
|
||||
|> Enum.sort()
|
||||
else
|
||||
[]
|
||||
end
|
||||
end
|
||||
|
||||
defp delete_file_if_present(_project_id, file_path) when file_path in [nil, ""], do: :ok
|
||||
|
||||
defp delete_file_if_present(project_id, file_path) do
|
||||
|
||||
Reference in New Issue
Block a user