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

Commit ddb876f

Browse files
committed
feat(post-qa): improve nameing func matching
1 parent a100e25 commit ddb876f

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
1313
alias Helper.Types, as: T
1414
alias Helper.{ORM, QueryBuilder}
1515
alias GroupherServer.{Accounts, CMS, Repo}
16+
alias CMS.Post
1617

1718
alias Accounts.User
1819
alias CMS.{ArticleComment, ArticlePinnedComment, Embeds}
@@ -139,7 +140,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
139140

140141
def mark_comment_solution(article_comment_id, user) do
141142
with {:ok, article_comment} <- ORM.find(ArticleComment, article_comment_id),
142-
{:ok, post} <- ORM.find(CMS.Post, article_comment.post_id, preload: [author: :user]) do
143+
{:ok, post} <- ORM.find(Post, article_comment.post_id, preload: [author: :user]) do
143144
# 确保只有一个最佳答案
144145
batch_update_solution_flag(post, false)
145146
CMS.pin_article_comment(article_comment.id)
@@ -149,7 +150,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
149150

150151
def undo_mark_comment_solution(article_comment_id, user) do
151152
with {:ok, article_comment} <- ORM.find(ArticleComment, article_comment_id),
152-
{:ok, post} <- ORM.find(CMS.Post, article_comment.post_id, preload: [author: :user]) do
153+
{:ok, post} <- ORM.find(Post, article_comment.post_id, preload: [author: :user]) do
153154
do_mark_comment_solution(post, article_comment, user, false)
154155
end
155156
end
@@ -175,7 +176,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
175176
@doc """
176177
batch update is_question flag for post-only article
177178
"""
178-
def batch_update_question_flag(%CMS.Post{is_question: is_question} = post) do
179+
def batch_update_question_flag(%Post{is_question: is_question} = post) do
179180
from(c in ArticleComment,
180181
where: c.post_id == ^post.id,
181182
update: [set: [is_for_question: ^is_question]]
@@ -306,14 +307,14 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
306307

307308
defp add_pinned_comments_ifneed(paged_comments, thread, article_id, %{page: 1}) do
308309
with {:ok, info} <- match(thread),
309-
{:ok, pinned_comments} <- paged_pinned_comments(info, article_id) do
310+
{:ok, pinned_comments} <- list_pinned_comments(info, article_id) do
310311
case pinned_comments do
311312
[] ->
312313
paged_comments
313314

314315
_ ->
315316
pinned_comments =
316-
sort_solution_to_front(pinned_comments)
317+
sort_solution_to_front(thread, pinned_comments)
317318
|> Enum.slice(0, @pinned_comment_limit)
318319
|> Repo.preload(reply_to: :author)
319320

@@ -328,19 +329,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
328329

329330
defp add_pinned_comments_ifneed(paged_comments, _thread, _article_id, _), do: paged_comments
330331

331-
defp paged_pinned_comments(info, article_id) do
332+
defp list_pinned_comments(%{foreign_key: foreign_key}, article_id) do
332333
from(p in ArticlePinnedComment,
333334
join: c in ArticleComment,
334335
on: p.article_comment_id == c.id,
335-
where: field(p, ^info.foreign_key) == ^article_id,
336+
where: field(p, ^foreign_key) == ^article_id,
336337
order_by: [desc: p.inserted_at],
337338
select: c
338339
)
339340
|> Repo.all()
340341
|> done
341342
end
342343

343-
defp sort_solution_to_front(pinned_comments) do
344+
# only support post
345+
defp sort_solution_to_front(:post, pinned_comments) do
344346
solution_index = Enum.find_index(pinned_comments, & &1.is_solution)
345347

346348
case is_nil(solution_index) do
@@ -353,6 +355,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
353355
end
354356
end
355357

358+
defp sort_solution_to_front(_, pinned_comments), do: pinned_comments
359+
356360
defp mark_viewer_has_upvoted(paged_comments, nil), do: paged_comments
357361

358362
defp mark_viewer_has_upvoted(%{entries: entries} = paged_comments, %User{} = user) do
@@ -372,7 +376,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
372376
defp set_question_flag_ifneed(_, comment), do: ORM.update(comment, %{is_for_question: false})
373377

374378
# batch update is_solution flag for artilce comment
375-
defp batch_update_solution_flag(%CMS.Post{} = post, is_question) do
379+
defp batch_update_solution_flag(%Post{} = post, is_question) do
376380
from(c in ArticleComment,
377381
where: c.post_id == ^post.id,
378382
update: [set: [is_solution: ^is_question]]

0 commit comments

Comments
 (0)