fix: fix CSM-006
This commit is contained in:
@@ -24,9 +24,20 @@ defmodule BDS.Rendering.ListArchive do
|
||||
|
||||
canonical_media_paths = LinksAndLanguages.canonical_media_path_by_source_path(project_id)
|
||||
|
||||
input_posts = MapUtils.attr(assigns, :posts, [])
|
||||
|
||||
post_ids =
|
||||
input_posts
|
||||
|> Enum.map(&MapUtils.attr(&1, :id))
|
||||
|> Enum.reject(&is_nil/1)
|
||||
|> Enum.uniq()
|
||||
|
||||
post_records = PostRendering.load_post_records_batch(post_ids)
|
||||
|
||||
posts =
|
||||
normalize_list_posts(
|
||||
MapUtils.attr(assigns, :posts, []),
|
||||
input_posts,
|
||||
post_records,
|
||||
canonical_post_paths,
|
||||
canonical_media_paths,
|
||||
language,
|
||||
@@ -172,13 +183,15 @@ defmodule BDS.Rendering.ListArchive do
|
||||
|
||||
defp normalize_list_posts(
|
||||
posts,
|
||||
post_records,
|
||||
canonical_post_paths,
|
||||
canonical_media_paths,
|
||||
language,
|
||||
template_context
|
||||
) do
|
||||
Enum.map(posts, fn post ->
|
||||
post_record = PostRendering.load_post_record(post)
|
||||
post_id = MapUtils.attr(post, :id)
|
||||
post_record = Map.get(post_records, post_id)
|
||||
|
||||
raw_content =
|
||||
Map.get(
|
||||
@@ -332,4 +345,5 @@ defmodule BDS.Rendering.ListArchive do
|
||||
do: RenderMetadata.calendar_initial_month(post)
|
||||
|
||||
defp calendar_initial_month_from_posts([]), do: nil
|
||||
|
||||
end
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
defmodule BDS.Rendering.PostRendering do
|
||||
@moduledoc false
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
alias BDS.Rendering.Filters
|
||||
alias BDS.Rendering.Labels
|
||||
alias BDS.Rendering.LinksAndLanguages
|
||||
@@ -101,6 +103,7 @@ defmodule BDS.Rendering.PostRendering do
|
||||
}
|
||||
end
|
||||
|
||||
@spec load_post_record(map()) :: Post.t() | Translation.t() | nil
|
||||
def load_post_record(assigns) do
|
||||
case MapUtils.attr(assigns, :id) do
|
||||
nil -> nil
|
||||
@@ -108,6 +111,36 @@ defmodule BDS.Rendering.PostRendering do
|
||||
end
|
||||
end
|
||||
|
||||
@spec load_post_records_batch([String.t()]) :: %{String.t() => Post.t() | Translation.t() | nil}
|
||||
def load_post_records_batch([]), do: %{}
|
||||
|
||||
def load_post_records_batch(post_ids) when is_list(post_ids) do
|
||||
posts =
|
||||
Repo.all(from p in Post, where: p.id in ^post_ids)
|
||||
|> Enum.map(&{&1.id, &1})
|
||||
|> Map.new()
|
||||
|
||||
missing_ids = Enum.reject(post_ids, &Map.has_key?(posts, &1))
|
||||
|
||||
translations =
|
||||
if Enum.empty?(missing_ids) do
|
||||
%{}
|
||||
else
|
||||
Repo.all(from t in Translation, where: t.id in ^missing_ids)
|
||||
|> Enum.map(&{&1.id, &1})
|
||||
|> Map.new()
|
||||
end
|
||||
|
||||
missing_after_translations = Enum.reject(missing_ids, &Map.has_key?(translations, &1))
|
||||
|
||||
nil_entries =
|
||||
missing_after_translations
|
||||
|> Enum.map(&{&1, nil})
|
||||
|> Map.new()
|
||||
|
||||
Map.merge(posts, Map.merge(translations, nil_entries))
|
||||
end
|
||||
|
||||
defp canonical_post_record(%Translation{translation_for: post_id}), do: Repo.get(Post, post_id)
|
||||
defp canonical_post_record(%Post{} = post), do: post
|
||||
defp canonical_post_record(_other), do: nil
|
||||
|
||||
Reference in New Issue
Block a user