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

Commit 2b1c9f6

Browse files
authored
refactor(article-comments): remove spec article in action (#362)
1 parent c56723a commit 2b1c9f6

File tree

6 files changed

+20
-44
lines changed

6 files changed

+20
-44
lines changed

lib/groupher_server/cms/article_collect.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule GroupherServer.CMS.ArticleCollect do
99
import GroupherServer.CMS.Helper.Macros
1010
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
1111

12-
alias GroupherServer.{Accounts, CMS}
12+
alias GroupherServer.Accounts
1313
alias Accounts.{User, CollectFolder}
1414

1515
@article_threads get_config(:article, :article_threads)

lib/groupher_server/cms/article_upvote.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ defmodule GroupherServer.CMS.ArticleUpvote do
99
import GroupherServer.CMS.Helper.Macros
1010
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
1111

12-
alias GroupherServer.{Accounts, CMS}
12+
alias GroupherServer.Accounts
1313
alias Accounts.User
1414

1515
@article_threads get_config(:article, :article_threads)

lib/groupher_server/cms/article_user_emotion.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ defmodule GroupherServer.CMS.ArticleUserEmotion do
2626
import GroupherServer.CMS.Helper.Macros
2727
import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1]
2828

29-
alias GroupherServer.{Accounts, CMS}
29+
alias GroupherServer.Accounts
3030

3131
@supported_emotions get_config(:article, :supported_emotions)
3232
@article_threads get_config(:article, :article_threads)

lib/groupher_server/cms/delegates/article_comment_action.ex

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
1616
alias GroupherServer.{Accounts, CMS, Repo}
1717

1818
alias Accounts.User
19-
20-
alias CMS.{
21-
ArticleComment,
22-
ArticlePinnedComment,
23-
ArticleCommentUpvote,
24-
ArticleCommentReply,
25-
Community,
26-
# TODO: remove spec type
27-
Post,
28-
Job
29-
}
19+
alias CMS.{ArticleComment, ArticlePinnedComment, ArticleCommentUpvote, ArticleCommentReply}
3020

3121
alias Ecto.Multi
3222

@@ -250,45 +240,25 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
250240
{:ok, parent_comment}
251241
end
252242

253-
defp get_article(%ArticleComment{post_id: post_id} = comment) when not is_nil(post_id) do
254-
with {:ok, article} <- ORM.find(Post, comment.post_id, preload: [author: :user]) do
255-
{:post, article}
256-
end
257-
end
258-
259-
defp get_article(%ArticleComment{job_id: job_id} = comment) when not is_nil(job_id) do
260-
with {:ok, article} <- ORM.find(Job, comment.job_id, preload: [author: :user]) do
261-
{:job, article}
262-
end
263-
end
264-
265-
defp get_article(%ArticleComment{repo_id: repo_id} = comment) when not is_nil(repo_id) do
266-
with {:ok, article} <- ORM.find(CMS.Repo, comment.repo_id, preload: [author: :user]) do
267-
{:repo, article}
243+
defp get_article(%ArticleComment{} = comment) do
244+
with article_thread <- find_comment_article_thread(comment),
245+
{:ok, info} <- match(article_thread),
246+
article_id <- Map.get(comment, info.foreign_key),
247+
{:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do
248+
{article_thread, article}
268249
end
269250
end
270251

271252
@spec get_full_comment(String.t()) :: {:ok, T.article_info()} | {:error, nil}
272253
defp get_full_comment(comment_id) do
273254
query = from(c in ArticleComment, where: c.id == ^comment_id, preload: ^@article_threads)
274255

275-
with {:ok, comment} <- Repo.one(query) |> done() do
276-
extract_article_info(comment)
256+
with {:ok, comment} <- Repo.one(query) |> done(),
257+
article_thread <- find_comment_article_thread(comment) do
258+
do_extract_article_info(article_thread, Map.get(comment, article_thread))
277259
end
278260
end
279261

280-
defp extract_article_info(%ArticleComment{post: %Post{} = post}) when not is_nil(post) do
281-
do_extract_article_info(:post, post)
282-
end
283-
284-
defp extract_article_info(%ArticleComment{job: %Job{} = job}) when not is_nil(job) do
285-
do_extract_article_info(:job, job)
286-
end
287-
288-
defp extract_article_info(%ArticleComment{repo: %CMS.Repo{} = repo}) when not is_nil(repo) do
289-
do_extract_article_info(:repo, repo)
290-
end
291-
292262
@spec do_extract_article_info(T.article_thread(), T.article_common()) :: {:ok, T.article_info()}
293263
defp do_extract_article_info(thread, article) do
294264
with {:ok, article_with_author} <- Repo.preload(article, author: :user) |> done(),
@@ -306,6 +276,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
306276
end
307277
end
308278

279+
defp find_comment_article_thread(%ArticleComment{} = comment) do
280+
@article_threads
281+
|> Enum.filter(&Map.get(comment, :"#{&1}_id"))
282+
|> List.first()
283+
end
284+
309285
# used in replies mode, for those reply to other user in replies box (for frontend)
310286
# 用于回复模式,指代这条回复是回复“回复列表其他人的” (方便前端展示)
311287
defp update_reply_to_others_state(parent_comment, replying_comment, replyed_comment) do

test/groupher_server/cms/comments/post_comment_replies_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do
1919
end
2020

2121
describe "[basic article comment replies]" do
22+
@tag :wip2
2223
test "exsit comment can be reply", ~m(post user user2)a do
2324
parent_content = "parent comment"
2425
reply_content = "reply comment"

test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do
119119
assert results["entries"] |> Enum.any?(&(&1["id"] == to_string(post.id)))
120120
end
121121

122-
@tag :wip2
123122
test "filter sort should have default :desc_inserted", ~m(guest_conn)a do
124123
variables = %{filter: %{}}
125124
results = guest_conn |> query_result(@query, variables, "pagedPosts")

0 commit comments

Comments
 (0)