chore: another god module down
This commit is contained in:
295
lib/bds/rendering/list_archive.ex
Normal file
295
lib/bds/rendering/list_archive.ex
Normal file
@@ -0,0 +1,295 @@
|
||||
defmodule BDS.Rendering.ListArchive do
|
||||
@moduledoc false
|
||||
|
||||
alias BDS.I18n
|
||||
alias BDS.Persistence
|
||||
alias BDS.Rendering.LinksAndLanguages
|
||||
alias BDS.Rendering.Metadata, as: RenderMetadata
|
||||
alias BDS.Rendering.PostRendering
|
||||
alias BDS.Rendering.TemplateSelection
|
||||
|
||||
def list_assigns(project_id, assigns) do
|
||||
metadata = RenderMetadata.project_metadata(project_id)
|
||||
template_context = TemplateSelection.template_render_context(project_id)
|
||||
|
||||
language =
|
||||
Map.get(assigns, :language, Map.get(assigns, "language", metadata.main_language || "en"))
|
||||
|
||||
main_language = metadata.main_language || language
|
||||
archive_context = Map.get(assigns, :archive_context, Map.get(assigns, "archive_context", %{}))
|
||||
|
||||
canonical_post_paths = LinksAndLanguages.canonical_post_path_by_slug(project_id, main_language)
|
||||
canonical_media_paths = LinksAndLanguages.canonical_media_path_by_source_path(project_id)
|
||||
|
||||
posts =
|
||||
normalize_list_posts(
|
||||
Map.get(assigns, :posts, Map.get(assigns, "posts", [])),
|
||||
canonical_post_paths,
|
||||
canonical_media_paths,
|
||||
language,
|
||||
template_context
|
||||
)
|
||||
|
||||
pagination =
|
||||
normalize_pagination(Map.get(assigns, :pagination, Map.get(assigns, "pagination")), posts)
|
||||
|
||||
day_blocks = build_day_blocks(posts)
|
||||
min_date = min_date(posts)
|
||||
max_date = max_date(posts)
|
||||
normalized_archive_context = normalize_archive_context(archive_context)
|
||||
|
||||
%{
|
||||
language: language,
|
||||
language_prefix:
|
||||
Map.get(
|
||||
assigns,
|
||||
:language_prefix,
|
||||
Map.get(assigns, "language_prefix", LinksAndLanguages.language_prefix(language, main_language))
|
||||
),
|
||||
page_title: Map.get(assigns, :page_title, Map.get(assigns, "page_title")),
|
||||
posts: posts,
|
||||
pico_stylesheet_href:
|
||||
Map.get(
|
||||
assigns,
|
||||
:pico_stylesheet_href,
|
||||
Map.get(assigns, "pico_stylesheet_href", RenderMetadata.default_pico_stylesheet_href(metadata.pico_theme))
|
||||
),
|
||||
html_theme_attribute:
|
||||
Map.get(
|
||||
assigns,
|
||||
:html_theme_attribute,
|
||||
Map.get(assigns, "html_theme_attribute")
|
||||
),
|
||||
blog_languages: RenderMetadata.blog_languages(metadata, language),
|
||||
alternate_links: [],
|
||||
menu_items: RenderMetadata.menu_items(project_id),
|
||||
calendar_initial_year: calendar_initial_year_from_posts(posts),
|
||||
calendar_initial_month: calendar_initial_month_from_posts(posts),
|
||||
archive_context: normalized_archive_context,
|
||||
show_archive_range_heading: show_archive_range_heading?(normalized_archive_context, day_blocks),
|
||||
min_date: min_date,
|
||||
max_date: max_date,
|
||||
is_list_page: true,
|
||||
is_first_page: pagination.current_page <= 1,
|
||||
is_last_page: pagination.current_page >= pagination.total_pages,
|
||||
has_prev_page: pagination.has_prev_page,
|
||||
has_next_page: pagination.has_next_page,
|
||||
prev_page_href: pagination.prev_page_href,
|
||||
next_page_href: pagination.next_page_href,
|
||||
current_page: pagination.current_page,
|
||||
total_pages: pagination.total_pages,
|
||||
total_items: pagination.total_items,
|
||||
items_per_page: pagination.items_per_page,
|
||||
canonical_post_path_by_slug: canonical_post_paths,
|
||||
canonical_media_path_by_source_path: canonical_media_paths,
|
||||
post_data_json_by_id:
|
||||
Enum.into(posts, %{}, fn post -> {post.id, PostRendering.post_data_json_value(post)} end),
|
||||
day_blocks: day_blocks
|
||||
}
|
||||
end
|
||||
|
||||
def not_found_assigns(project_id, assigns) do
|
||||
metadata = RenderMetadata.project_metadata(project_id)
|
||||
|
||||
language =
|
||||
Map.get(assigns, :language, Map.get(assigns, "language", metadata.main_language || "en"))
|
||||
|
||||
main_language = metadata.main_language || language
|
||||
|
||||
%{
|
||||
page_title: Map.get(assigns, :page_title, Map.get(assigns, "page_title", "404")),
|
||||
language: language,
|
||||
language_prefix:
|
||||
Map.get(
|
||||
assigns,
|
||||
:language_prefix,
|
||||
Map.get(assigns, "language_prefix", LinksAndLanguages.language_prefix(language, main_language))
|
||||
),
|
||||
pico_stylesheet_href:
|
||||
Map.get(
|
||||
assigns,
|
||||
:pico_stylesheet_href,
|
||||
Map.get(assigns, "pico_stylesheet_href", RenderMetadata.default_pico_stylesheet_href(metadata.pico_theme))
|
||||
),
|
||||
html_theme_attribute:
|
||||
Map.get(
|
||||
assigns,
|
||||
:html_theme_attribute,
|
||||
Map.get(assigns, "html_theme_attribute")
|
||||
),
|
||||
blog_languages: RenderMetadata.blog_languages(metadata, language),
|
||||
menu_items: RenderMetadata.menu_items(project_id),
|
||||
alternate_links: [],
|
||||
not_found_message:
|
||||
Map.get(
|
||||
assigns,
|
||||
:not_found_message,
|
||||
Map.get(
|
||||
assigns,
|
||||
"not_found_message",
|
||||
I18n.translate(language, "render.notFound.message")
|
||||
)
|
||||
),
|
||||
not_found_back_label:
|
||||
Map.get(
|
||||
assigns,
|
||||
:not_found_back_label,
|
||||
Map.get(
|
||||
assigns,
|
||||
"not_found_back_label",
|
||||
I18n.translate(language, "render.notFound.back")
|
||||
)
|
||||
)
|
||||
}
|
||||
end
|
||||
|
||||
defp normalize_list_posts(posts, canonical_post_paths, canonical_media_paths, language, template_context) do
|
||||
Enum.map(posts, fn post ->
|
||||
post_record = PostRendering.load_post_record(post)
|
||||
raw_content =
|
||||
Map.get(
|
||||
post,
|
||||
:content,
|
||||
Map.get(post, "content", Map.get(post, :excerpt, Map.get(post, "excerpt", "")))
|
||||
)
|
||||
|
||||
%{
|
||||
id: Map.get(post, :id, Map.get(post, "id")),
|
||||
slug: Map.get(post, :slug, Map.get(post, "slug")),
|
||||
title: Map.get(post, :title, Map.get(post, "title")),
|
||||
content:
|
||||
PostRendering.render_post_content(
|
||||
raw_content,
|
||||
canonical_post_paths,
|
||||
canonical_media_paths,
|
||||
language,
|
||||
template_context
|
||||
),
|
||||
raw_content: raw_content,
|
||||
excerpt:
|
||||
Map.get(post, :excerpt, Map.get(post, "excerpt", Map.get(post_record || %{}, :excerpt))),
|
||||
author: Map.get(post, :author, Map.get(post, "author", Map.get(post_record || %{}, :author))),
|
||||
language:
|
||||
Map.get(
|
||||
post,
|
||||
:language,
|
||||
Map.get(post, "language", Map.get(post_record || %{}, :language))
|
||||
),
|
||||
published_at:
|
||||
Map.get(post, :published_at, Map.get(post, "published_at", Map.get(post_record || %{}, :published_at))),
|
||||
created_at:
|
||||
Map.get(post, :created_at, Map.get(post, "created_at", Map.get(post_record || %{}, :created_at))),
|
||||
updated_at:
|
||||
Map.get(post, :updated_at, Map.get(post, "updated_at", Map.get(post_record || %{}, :updated_at))),
|
||||
tags: Map.get(post, :tags, Map.get(post, "tags", Map.get(post_record || %{}, :tags, []))) || [],
|
||||
categories:
|
||||
Map.get(post, :categories, Map.get(post, "categories", Map.get(post_record || %{}, :categories, []))) || [],
|
||||
template_slug:
|
||||
Map.get(post, :template_slug, Map.get(post, "template_slug", Map.get(post_record || %{}, :template_slug))),
|
||||
do_not_translate:
|
||||
Map.get(post, :do_not_translate, Map.get(post, "do_not_translate", Map.get(post_record || %{}, :do_not_translate, false))),
|
||||
href: Map.get(post, :href, Map.get(post, "href")),
|
||||
show_title: true,
|
||||
linked_media: [],
|
||||
outgoing_links: [],
|
||||
incoming_links: []
|
||||
}
|
||||
end)
|
||||
end
|
||||
|
||||
defp normalize_pagination(nil, posts) do
|
||||
total_items = length(posts)
|
||||
|
||||
%{
|
||||
current_page: 1,
|
||||
total_pages: 1,
|
||||
total_items: total_items,
|
||||
items_per_page: total_items,
|
||||
has_prev_page: false,
|
||||
prev_page_href: "",
|
||||
has_next_page: false,
|
||||
next_page_href: ""
|
||||
}
|
||||
end
|
||||
|
||||
defp normalize_pagination(%{} = pagination, posts) do
|
||||
total_items =
|
||||
Map.get(pagination, :total_items, Map.get(pagination, "total_items", length(posts)))
|
||||
|
||||
items_per_page =
|
||||
Map.get(pagination, :items_per_page, Map.get(pagination, "items_per_page", total_items))
|
||||
|
||||
%{
|
||||
current_page: Map.get(pagination, :current_page, Map.get(pagination, "current_page", 1)),
|
||||
total_pages: Map.get(pagination, :total_pages, Map.get(pagination, "total_pages", 1)),
|
||||
total_items: total_items,
|
||||
items_per_page: items_per_page,
|
||||
has_prev_page:
|
||||
Map.get(pagination, :has_prev_page, Map.get(pagination, "has_prev_page", false)),
|
||||
prev_page_href:
|
||||
Map.get(pagination, :prev_page_href, Map.get(pagination, "prev_page_href", "")),
|
||||
has_next_page:
|
||||
Map.get(pagination, :has_next_page, Map.get(pagination, "has_next_page", false)),
|
||||
next_page_href:
|
||||
Map.get(pagination, :next_page_href, Map.get(pagination, "next_page_href", ""))
|
||||
}
|
||||
end
|
||||
|
||||
defp normalize_archive_context(nil), do: nil
|
||||
|
||||
defp normalize_archive_context(%{} = archive_context) do
|
||||
%{
|
||||
kind: Map.get(archive_context, :kind, Map.get(archive_context, "kind")),
|
||||
name: Map.get(archive_context, :name, Map.get(archive_context, "name")),
|
||||
month: Map.get(archive_context, :month, Map.get(archive_context, "month")),
|
||||
year: Map.get(archive_context, :year, Map.get(archive_context, "year")),
|
||||
day: Map.get(archive_context, :day, Map.get(archive_context, "day"))
|
||||
}
|
||||
end
|
||||
|
||||
defp build_day_blocks(posts) do
|
||||
grouped_blocks =
|
||||
posts
|
||||
|> Enum.filter(&is_integer(Map.get(&1, :created_at)))
|
||||
|> Enum.group_by(&(Map.get(&1, :created_at) |> Persistence.from_unix_ms!() |> DateTime.to_date() |> Date.to_iso8601()))
|
||||
|> Enum.sort_by(fn {label, _posts} -> label end)
|
||||
|
||||
grouped_blocks
|
||||
|> Enum.with_index()
|
||||
|> Enum.map(fn {{date_label, grouped_posts}, index} ->
|
||||
%{
|
||||
date_label: date_label,
|
||||
show_date_marker: true,
|
||||
show_separator: index < length(grouped_blocks) - 1,
|
||||
posts: Enum.sort_by(grouped_posts, &Map.get(&1, :created_at))
|
||||
}
|
||||
end)
|
||||
|> case do
|
||||
[] -> [%{date_label: "", show_date_marker: false, show_separator: false, posts: posts}]
|
||||
blocks -> blocks
|
||||
end
|
||||
end
|
||||
|
||||
defp min_date(posts) do
|
||||
posts
|
||||
|> Enum.map(&Map.get(&1, :created_at))
|
||||
|> Enum.filter(&is_integer/1)
|
||||
|> Enum.min(fn -> nil end)
|
||||
end
|
||||
|
||||
defp max_date(posts) do
|
||||
posts
|
||||
|> Enum.map(&Map.get(&1, :created_at))
|
||||
|> Enum.filter(&is_integer/1)
|
||||
|> Enum.max(fn -> nil end)
|
||||
end
|
||||
|
||||
defp show_archive_range_heading?(%{kind: "date"}, _day_blocks), do: true
|
||||
defp show_archive_range_heading?(_archive_context, _day_blocks), do: false
|
||||
|
||||
defp calendar_initial_year_from_posts([post | _rest]), do: RenderMetadata.calendar_initial_year(post)
|
||||
defp calendar_initial_year_from_posts([]), do: nil
|
||||
|
||||
defp calendar_initial_month_from_posts([post | _rest]), do: RenderMetadata.calendar_initial_month(post)
|
||||
defp calendar_initial_month_from_posts([]), do: nil
|
||||
end
|
||||
Reference in New Issue
Block a user