Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit a100e25

Browse files
committed
feat(post-qa): improve pinned logic readabily
1 parent cea7bcd commit a100e25

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
282282
|> where(^where_query)
283283
|> QueryBuilder.filter_pack(Map.merge(filters, %{sort: sort}))
284284
|> ORM.paginater(~m(page size)a)
285-
|> add_pined_comments_ifneed(thread, article_id, filters)
285+
|> add_pinned_comments_ifneed(thread, article_id, filters)
286286
|> mark_viewer_emotion_states(user, :comment)
287287
|> mark_viewer_has_upvoted(user)
288288
|> done()
@@ -304,49 +304,51 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
304304
|> done()
305305
end
306306

307-
defp add_pined_comments_ifneed(%{entries: entries} = paged_comments, thread, article_id, %{
308-
page: 1
309-
}) do
307+
defp add_pinned_comments_ifneed(paged_comments, thread, article_id, %{page: 1}) do
310308
with {:ok, info} <- match(thread),
311-
query <-
312-
from(p in ArticlePinnedComment,
313-
join: c in ArticleComment,
314-
on: p.article_comment_id == c.id,
315-
where: field(p, ^info.foreign_key) == ^article_id,
316-
order_by: [desc: p.inserted_at],
317-
select: c
318-
),
319-
{:ok, pined_comments} <- Repo.all(query) |> done() do
320-
case pined_comments do
309+
{:ok, pinned_comments} <- paged_pinned_comments(info, article_id) do
310+
case pinned_comments do
321311
[] ->
322312
paged_comments
323313

324314
_ ->
325-
preloaded_pined_comments =
326-
Enum.slice(pined_comments, 0, @pinned_comment_limit)
315+
pinned_comments =
316+
sort_solution_to_front(pinned_comments)
317+
|> Enum.slice(0, @pinned_comment_limit)
327318
|> Repo.preload(reply_to: :author)
328-
|> sort_solution_to_front
329319

330-
entries = Enum.concat(preloaded_pined_comments, entries)
331-
pined_comment_count = length(pined_comments)
320+
entries = pinned_comments ++ paged_comments.entries
321+
pinned_comment_count = length(pinned_comments)
332322

333-
total_count = paged_comments.total_count + pined_comment_count
323+
total_count = paged_comments.total_count + pinned_comment_count
334324
paged_comments |> Map.merge(%{entries: entries, total_count: total_count})
335325
end
336326
end
337327
end
338328

339-
defp add_pined_comments_ifneed(paged_comments, _thread, _article_id, _), do: paged_comments
329+
defp add_pinned_comments_ifneed(paged_comments, _thread, _article_id, _), do: paged_comments
340330

341-
defp sort_solution_to_front(pined_comments) do
342-
solution_index = Enum.find_index(pined_comments, & &1.is_solution)
331+
defp paged_pinned_comments(info, article_id) do
332+
from(p in ArticlePinnedComment,
333+
join: c in ArticleComment,
334+
on: p.article_comment_id == c.id,
335+
where: field(p, ^info.foreign_key) == ^article_id,
336+
order_by: [desc: p.inserted_at],
337+
select: c
338+
)
339+
|> Repo.all()
340+
|> done
341+
end
342+
343+
defp sort_solution_to_front(pinned_comments) do
344+
solution_index = Enum.find_index(pinned_comments, & &1.is_solution)
343345

344346
case is_nil(solution_index) do
345347
true ->
346-
pined_comments
348+
pinned_comments
347349

348350
false ->
349-
{solution_comment, rest_comments} = List.pop_at(pined_comments, solution_index)
351+
{solution_comment, rest_comments} = List.pop_at(pinned_comments, solution_index)
350352
[solution_comment] ++ rest_comments
351353
end
352354
end

0 commit comments

Comments
 (0)