From 4b3f5bafa898c245c4069b48069c82ebc5544a1a Mon Sep 17 00:00:00 2001 From: mydearxym Date: Mon, 17 May 2021 16:46:46 +0800 Subject: [PATCH] refactor(article-comments): remove spec article in action --- lib/groupher_server/cms/article_collect.ex | 2 +- lib/groupher_server/cms/article_upvote.ex | 2 +- .../cms/article_user_emotion.ex | 2 +- .../cms/delegates/article_comment_action.ex | 56 ++++++------------- .../comments/post_comment_replies_test.exs | 1 + .../cms/paged_articles/paged_posts_test.exs | 1 - 6 files changed, 20 insertions(+), 44 deletions(-) diff --git a/lib/groupher_server/cms/article_collect.ex b/lib/groupher_server/cms/article_collect.ex index 723dcd232..bc361b3f5 100644 --- a/lib/groupher_server/cms/article_collect.ex +++ b/lib/groupher_server/cms/article_collect.ex @@ -9,7 +9,7 @@ defmodule GroupherServer.CMS.ArticleCollect do import GroupherServer.CMS.Helper.Macros import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1] - alias GroupherServer.{Accounts, CMS} + alias GroupherServer.Accounts alias Accounts.{User, CollectFolder} @article_threads get_config(:article, :article_threads) diff --git a/lib/groupher_server/cms/article_upvote.ex b/lib/groupher_server/cms/article_upvote.ex index bc67482a3..9ee913c2f 100644 --- a/lib/groupher_server/cms/article_upvote.ex +++ b/lib/groupher_server/cms/article_upvote.ex @@ -9,7 +9,7 @@ defmodule GroupherServer.CMS.ArticleUpvote do import GroupherServer.CMS.Helper.Macros import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1] - alias GroupherServer.{Accounts, CMS} + alias GroupherServer.Accounts alias Accounts.User @article_threads get_config(:article, :article_threads) diff --git a/lib/groupher_server/cms/article_user_emotion.ex b/lib/groupher_server/cms/article_user_emotion.ex index 621fd5cd3..557134de2 100644 --- a/lib/groupher_server/cms/article_user_emotion.ex +++ b/lib/groupher_server/cms/article_user_emotion.ex @@ -26,7 +26,7 @@ defmodule GroupherServer.CMS.ArticleUserEmotion do import GroupherServer.CMS.Helper.Macros import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1] - alias GroupherServer.{Accounts, CMS} + alias GroupherServer.Accounts @supported_emotions get_config(:article, :supported_emotions) @article_threads get_config(:article, :article_threads) diff --git a/lib/groupher_server/cms/delegates/article_comment_action.ex b/lib/groupher_server/cms/delegates/article_comment_action.ex index 6345627ba..54ebc4953 100644 --- a/lib/groupher_server/cms/delegates/article_comment_action.ex +++ b/lib/groupher_server/cms/delegates/article_comment_action.ex @@ -16,17 +16,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do alias GroupherServer.{Accounts, CMS, Repo} alias Accounts.User - - alias CMS.{ - ArticleComment, - ArticlePinnedComment, - ArticleCommentUpvote, - ArticleCommentReply, - Community, - # TODO: remove spec type - Post, - Job - } + alias CMS.{ArticleComment, ArticlePinnedComment, ArticleCommentUpvote, ArticleCommentReply} alias Ecto.Multi @@ -250,21 +240,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do {:ok, parent_comment} end - defp get_article(%ArticleComment{post_id: post_id} = comment) when not is_nil(post_id) do - with {:ok, article} <- ORM.find(Post, comment.post_id, preload: [author: :user]) do - {:post, article} - end - end - - defp get_article(%ArticleComment{job_id: job_id} = comment) when not is_nil(job_id) do - with {:ok, article} <- ORM.find(Job, comment.job_id, preload: [author: :user]) do - {:job, article} - end - end - - defp get_article(%ArticleComment{repo_id: repo_id} = comment) when not is_nil(repo_id) do - with {:ok, article} <- ORM.find(CMS.Repo, comment.repo_id, preload: [author: :user]) do - {:repo, article} + defp get_article(%ArticleComment{} = comment) do + with article_thread <- find_comment_article_thread(comment), + {:ok, info} <- match(article_thread), + article_id <- Map.get(comment, info.foreign_key), + {:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do + {article_thread, article} end end @@ -272,23 +253,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do defp get_full_comment(comment_id) do query = from(c in ArticleComment, where: c.id == ^comment_id, preload: ^@article_threads) - with {:ok, comment} <- Repo.one(query) |> done() do - extract_article_info(comment) + with {:ok, comment} <- Repo.one(query) |> done(), + article_thread <- find_comment_article_thread(comment) do + do_extract_article_info(article_thread, Map.get(comment, article_thread)) end end - defp extract_article_info(%ArticleComment{post: %Post{} = post}) when not is_nil(post) do - do_extract_article_info(:post, post) - end - - defp extract_article_info(%ArticleComment{job: %Job{} = job}) when not is_nil(job) do - do_extract_article_info(:job, job) - end - - defp extract_article_info(%ArticleComment{repo: %CMS.Repo{} = repo}) when not is_nil(repo) do - do_extract_article_info(:repo, repo) - end - @spec do_extract_article_info(T.article_thread(), T.article_common()) :: {:ok, T.article_info()} defp do_extract_article_info(thread, article) do with {:ok, article_with_author} <- Repo.preload(article, author: :user) |> done(), @@ -306,6 +276,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do end end + defp find_comment_article_thread(%ArticleComment{} = comment) do + @article_threads + |> Enum.filter(&Map.get(comment, :"#{&1}_id")) + |> List.first() + end + # used in replies mode, for those reply to other user in replies box (for frontend) # 用于回复模式,指代这条回复是回复“回复列表其他人的” (方便前端展示) defp update_reply_to_others_state(parent_comment, replying_comment, replyed_comment) do diff --git a/test/groupher_server/cms/comments/post_comment_replies_test.exs b/test/groupher_server/cms/comments/post_comment_replies_test.exs index 9a558beea..9e64787d0 100644 --- a/test/groupher_server/cms/comments/post_comment_replies_test.exs +++ b/test/groupher_server/cms/comments/post_comment_replies_test.exs @@ -19,6 +19,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do end describe "[basic article comment replies]" do + @tag :wip2 test "exsit comment can be reply", ~m(post user user2)a do parent_content = "parent comment" reply_content = "reply comment" diff --git a/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs b/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs index f8e3c0ea9..62de039ac 100644 --- a/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs +++ b/test/groupher_server_web/query/cms/paged_articles/paged_posts_test.exs @@ -119,7 +119,6 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do assert results["entries"] |> Enum.any?(&(&1["id"] == to_string(post.id))) end - @tag :wip2 test "filter sort should have default :desc_inserted", ~m(guest_conn)a do variables = %{filter: %{}} results = guest_conn |> query_result(@query, variables, "pagedPosts")