|
| 1 | +defmodule GroupherServer.CMS.Delegate.CitedContent do |
| 2 | + @moduledoc """ |
| 3 | + CURD operation on post/job ... |
| 4 | + """ |
| 5 | + import Ecto.Query, warn: false |
| 6 | + |
| 7 | + import Helper.Utils, only: [done: 1, get_config: 2] |
| 8 | + import ShortMaps |
| 9 | + |
| 10 | + alias Helper.Types, as: T |
| 11 | + alias GroupherServer.{CMS, Repo} |
| 12 | + alias Helper.{ORM, QueryBuilder} |
| 13 | + |
| 14 | + alias CMS.Model.CitedContent |
| 15 | + |
| 16 | + @article_threads get_config(:article, :threads) |
| 17 | + |
| 18 | + @article_preloads @article_threads |> Enum.map(&Keyword.new([{&1, [author: :user]}])) |
| 19 | + |
| 20 | + @comment_article_preloads @article_threads |> Enum.map(&Keyword.new([{:comment, &1}])) |
| 21 | + @cited_preloads @article_preloads ++ [[comment: :author] ++ @comment_article_preloads] |
| 22 | + |
| 23 | + @doc "get paged citing contents" |
| 24 | + def paged_citing_contents(cited_by_type, cited_by_id, %{page: page, size: size} = filter) do |
| 25 | + cited_by_type = cited_by_type |> to_string |> String.upcase() |
| 26 | + |
| 27 | + CitedContent |
| 28 | + |> where([c], c.cited_by_id == ^cited_by_id and c.cited_by_type == ^cited_by_type) |
| 29 | + |> QueryBuilder.filter_pack(Map.merge(filter, %{sort: :asc_inserted})) |
| 30 | + |> ORM.paginater(~m(page size)a) |
| 31 | + |> extract_contents |
| 32 | + |> done |
| 33 | + end |
| 34 | + |
| 35 | + def extract_contents(%{entries: entries} = paged_contents) do |
| 36 | + entries = entries |> Repo.preload(@cited_preloads) |> Enum.map(&shape_article(&1)) |
| 37 | + |
| 38 | + Map.put(paged_contents, :entries, entries) |
| 39 | + end |
| 40 | + |
| 41 | + defp thread_to_atom(thread), do: thread |> String.downcase() |> String.to_atom() |
| 42 | + |
| 43 | + # shape comment cite |
| 44 | + @spec shape_article(CitedContent.t()) :: T.cite_info() |
| 45 | + defp shape_article(%CitedContent{comment_id: comment_id} = cited) when not is_nil(comment_id) do |
| 46 | + %{ |
| 47 | + block_linker: block_linker, |
| 48 | + cited_by_type: cited_by_type, |
| 49 | + comment: comment, |
| 50 | + inserted_at: inserted_at |
| 51 | + } = cited |
| 52 | + |
| 53 | + comment_thread = comment.thread |> String.downcase() |> String.to_atom() |
| 54 | + article = comment |> Map.get(comment_thread) |
| 55 | + user = comment.author |> Map.take([:login, :nickname, :avatar]) |
| 56 | + |
| 57 | + article |
| 58 | + |> Map.take([:id, :title]) |
| 59 | + |> Map.merge(%{ |
| 60 | + inserted_at: inserted_at, |
| 61 | + user: user, |
| 62 | + thread: thread_to_atom(cited_by_type), |
| 63 | + comment_id: comment.id, |
| 64 | + block_linker: block_linker |
| 65 | + }) |
| 66 | + end |
| 67 | + |
| 68 | + # shape general article cite |
| 69 | + defp shape_article(%CitedContent{} = cited) do |
| 70 | + %{block_linker: block_linker, cited_by_type: cited_by_type, inserted_at: inserted_at} = cited |
| 71 | + |
| 72 | + thread = thread_to_atom(cited_by_type) |
| 73 | + article = Map.get(cited, thread) |
| 74 | + |
| 75 | + user = get_in(article, [:author, :user]) |> Map.take([:login, :nickname, :avatar]) |
| 76 | + |
| 77 | + article |
| 78 | + |> Map.take([:id, :title]) |
| 79 | + |> Map.merge(%{ |
| 80 | + user: user, |
| 81 | + thread: thread, |
| 82 | + block_linker: block_linker, |
| 83 | + inserted_at: inserted_at |
| 84 | + }) |
| 85 | + end |
| 86 | +end |
0 commit comments