From b9c5c5de36c962e51a4ddc68dedcc090536fe574 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Fri, 25 Jun 2021 10:37:26 +0800 Subject: [PATCH 1/6] refactor(naming): re-org cms helper import --- .../cms/delegates/article_curd.ex | 7 +++-- .../cms/delegates/cited_artiment.ex | 19 +++++++++----- .../cms/delegates/comment_action.ex | 19 +++++++------- lib/groupher_server/cms/delegates/document.ex | 8 +++--- lib/groupher_server/cms/delegates/helper.ex | 26 +++++++++++++++---- .../cms/delegates/hooks/cite.ex | 9 ++++--- .../cms/delegates/hooks/mention.ex | 6 ++--- .../cms/delegates/hooks/notify.ex | 24 ++++++++--------- .../cms/models/embeds/article_meta.ex | 2 ++ .../delivery/delegates/mention.ex | 4 +-- lib/helper/utils/utils.ex | 11 -------- 11 files changed, 73 insertions(+), 62 deletions(-) diff --git a/lib/groupher_server/cms/delegates/article_curd.ex b/lib/groupher_server/cms/delegates/article_curd.ex index c3756a1a6..5227b21b2 100644 --- a/lib/groupher_server/cms/delegates/article_curd.ex +++ b/lib/groupher_server/cms/delegates/article_curd.ex @@ -13,11 +13,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do module_to_atom: 1, get_config: 2, ensure: 2, - module_to_upcase: 1, - thread_of_article: 1 + module_to_upcase: 1 ] - import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 2] + import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 2, thread_of: 1] import Helper.ErrorCode import ShortMaps @@ -353,7 +352,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do """ def delete_article(article, reason \\ @remove_article_hint) do article = Repo.preload(article, [:communities, [author: :user]]) - {:ok, thread} = thread_of_article(article) + {:ok, thread} = thread_of(article) Multi.new() |> Multi.run(:delete_article, fn _, _ -> diff --git a/lib/groupher_server/cms/delegates/cited_artiment.ex b/lib/groupher_server/cms/delegates/cited_artiment.ex index c15034e56..08379b16a 100644 --- a/lib/groupher_server/cms/delegates/cited_artiment.ex +++ b/lib/groupher_server/cms/delegates/cited_artiment.ex @@ -5,7 +5,14 @@ defmodule GroupherServer.CMS.Delegate.CitedArtiment do import Ecto.Query, warn: false import Helper.Utils, - only: [done: 1, get_config: 2, thread_of_article: 1, atom_values_to_upcase: 1, to_upcase: 1] + only: [ + done: 1, + get_config: 2, + atom_values_to_upcase: 1, + to_upcase: 1 + ] + + import GroupherServer.CMS.Delegate.Helper, only: [thread_of: 1, article_of: 1] import GroupherServer.CMS.Helper.Matcher import ShortMaps @@ -45,7 +52,7 @@ defmodule GroupherServer.CMS.Delegate.CitedArtiment do end def batch_delete_by(article) do - with {:ok, thread} <- thread_of_article(article), + with {:ok, thread} <- thread_of(article), {:ok, info} <- match(thread) do thread = to_upcase(thread) @@ -104,9 +111,9 @@ defmodule GroupherServer.CMS.Delegate.CitedArtiment do defp shape(%CitedArtiment{comment_id: comment_id} = cited) when not is_nil(comment_id) do %{block_linker: block_linker, comment: comment, inserted_at: inserted_at} = cited - comment_thread = comment.thread |> String.downcase() |> String.to_atom() - article = comment |> Map.get(comment_thread) - article_thread = thread_to_atom(article.meta.thread) + {:ok, article} = article_of(comment) + {:ok, article_thread} = thread_of(article) + user = comment.author |> Map.take([:login, :nickname, :avatar]) article @@ -144,6 +151,4 @@ defmodule GroupherServer.CMS.Delegate.CitedArtiment do defp citing_thread(cited) do @article_threads |> Enum.find(fn thread -> not is_nil(Map.get(cited, :"#{thread}_id")) end) end - - defp thread_to_atom(thread), do: thread |> String.downcase() |> String.to_atom() end diff --git a/lib/groupher_server/cms/delegates/comment_action.ex b/lib/groupher_server/cms/delegates/comment_action.ex index 103f87965..3ada47a86 100644 --- a/lib/groupher_server/cms/delegates/comment_action.ex +++ b/lib/groupher_server/cms/delegates/comment_action.ex @@ -40,26 +40,25 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do {:ok, info} <- match(full_comment.thread) do Multi.new() |> Multi.run(:checked_pined_comments_count, fn _, _ -> - {:ok, pined_comments_count} = + pined_comments_query = from(p in PinnedComment, where: field(p, ^info.foreign_key) == ^full_comment.article.id ) - |> ORM.count() - case pined_comments_count >= @pinned_comment_limit do - true -> {:error, "max #{@pinned_comment_limit} pinned comment for each article"} - false -> {:ok, :pass} + with {:ok, pined_comments_count} <- ORM.count(pined_comments_query) do + case pined_comments_count >= @pinned_comment_limit do + true -> {:error, "max #{@pinned_comment_limit} pinned comment for each article"} + false -> {:ok, :pass} + end end end) |> Multi.run(:update_comment_flag, fn _, _ -> ORM.update(comment, %{is_pinned: true}) end) |> Multi.run(:add_pined_comment, fn _, _ -> - PinnedComment - |> ORM.create( - %{comment_id: comment.id} - |> Map.put(info.foreign_key, full_comment.article.id) - ) + attrs = %{comment_id: comment.id} |> Map.put(info.foreign_key, full_comment.article.id) + + PinnedComment |> ORM.create(attrs) end) |> Repo.transaction() |> result() diff --git a/lib/groupher_server/cms/delegates/document.ex b/lib/groupher_server/cms/delegates/document.ex index feada3a07..6435905f5 100644 --- a/lib/groupher_server/cms/delegates/document.ex +++ b/lib/groupher_server/cms/delegates/document.ex @@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.Document do CURD operation on post/job ... """ import Ecto.Query, warn: false - import Helper.Utils, only: [done: 1, thread_of_article: 2, get_config: 2] + import Helper.Utils, only: [done: 1, thread_of: 2, get_config: 2] import Helper.ErrorCode import ShortMaps @@ -28,7 +28,7 @@ defmodule GroupherServer.CMS.Delegate.Document do # for create artilce step in Multi.new def create(article, %{body: body} = attrs) do - with {:ok, article_thread} <- thread_of_article(article, :upcase), + with {:ok, article_thread} <- thread_of(article, :upcase), {:ok, parsed} <- Converter.Article.parse_body(body) do attrs = Map.take(parsed, [:body, :body_html]) @@ -59,7 +59,7 @@ defmodule GroupherServer.CMS.Delegate.Document do update both article and thread document """ def update(article, %{body: body} = attrs) when not is_nil(body) do - with {:ok, article_thread} <- thread_of_article(article, :upcase), + with {:ok, article_thread} <- thread_of(article, :upcase), {:ok, article_doc} <- find_article_document(article_thread, article), {:ok, thread_doc} <- find_thread_document(article_thread, article), {:ok, parsed} <- Converter.Article.parse_body(body) do @@ -82,7 +82,7 @@ defmodule GroupherServer.CMS.Delegate.Document do # 只更新 title 的情况 def update(article, %{title: title} = attrs) do - with {:ok, article_thread} <- thread_of_article(article, :upcase), + with {:ok, article_thread} <- thread_of(article, :upcase), {:ok, article_doc} <- find_article_document(article_thread, article) do article_doc |> ORM.update(%{title: attrs.title}) end diff --git a/lib/groupher_server/cms/delegates/helper.ex b/lib/groupher_server/cms/delegates/helper.ex index f65a69344..a09527909 100644 --- a/lib/groupher_server/cms/delegates/helper.ex +++ b/lib/groupher_server/cms/delegates/helper.ex @@ -2,10 +2,10 @@ defmodule GroupherServer.CMS.Delegate.Helper do @moduledoc """ helpers for GroupherServer.CMS.Delegate """ - import Helper.Utils, only: [get_config: 2, done: 1, strip_struct: 1] import Ecto.Query, warn: false import GroupherServer.CMS.Helper.Matcher import ShortMaps + import Helper.Utils, only: [get_config: 2, done: 1, strip_struct: 1] alias Helper.{ORM, QueryBuilder} alias GroupherServer.{Accounts, Repo, CMS} @@ -42,13 +42,29 @@ defmodule GroupherServer.CMS.Delegate.Helper do end @doc "get parent article of a comment" - def parent_article_of(%Comment{} = comment) do - article_thread = comment.thread |> String.downcase() |> String.to_atom() + def article_of(%Comment{} = comment) do + with {:ok, article_thread} <- thread_of(comment) do + comment |> Repo.preload(article_thread) |> Map.get(article_thread) |> done + end + end + + def article_of(_), do: {:error, "only support comment"} - comment |> Repo.preload(article_thread) |> Map.get(article_thread) |> done + # get thread of comment + def thread_of(%Comment{thread: thread}) do + thread |> String.downcase() |> String.to_atom() |> done end - def parent_article_of(_), do: {:error, "only support comment"} + # get thread of article + def thread_of(%{meta: %{thread: thread}}) do + thread |> String.downcase() |> String.to_atom() |> done + end + + def thread_of(_), do: {:error, "invalid article"} + + def thread_of(%{meta: %{thread: thread}}, :upcase) do + thread |> to_string |> String.upcase() |> done + end ####### # emotion related diff --git a/lib/groupher_server/cms/delegates/hooks/cite.ex b/lib/groupher_server/cms/delegates/hooks/cite.ex index 5da3f350c..e7623641f 100644 --- a/lib/groupher_server/cms/delegates/hooks/cite.ex +++ b/lib/groupher_server/cms/delegates/hooks/cite.ex @@ -29,9 +29,10 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Cite do """ import Ecto.Query, warn: false - import Helper.Utils, only: [get_config: 2, thread_of_article: 1] + import Helper.Utils, only: [get_config: 2] + import GroupherServer.CMS.Helper.Matcher - import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1] + import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1, thread_of: 1] import GroupherServer.CMS.Delegate.Hooks.Helper, only: [merge_same_block_linker: 2] import Helper.ErrorCode @@ -221,7 +222,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Cite do # cite article in article # 文章之间相互引用 defp shape(article, %{type: :article, artiment: cited}, block_id) do - {:ok, thread} = thread_of_article(article) + {:ok, thread} = thread_of(article) {:ok, info} = match(thread) %{ @@ -241,7 +242,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Cite do # cite comment in article # 文章中引用评论 defp shape(article, %{type: :comment, artiment: cited}, block_id) do - {:ok, thread} = thread_of_article(article) + {:ok, thread} = thread_of(article) {:ok, info} = match(thread) %{ diff --git a/lib/groupher_server/cms/delegates/hooks/mention.ex b/lib/groupher_server/cms/delegates/hooks/mention.ex index 5b49dea64..923afc60c 100644 --- a/lib/groupher_server/cms/delegates/hooks/mention.ex +++ b/lib/groupher_server/cms/delegates/hooks/mention.ex @@ -5,9 +5,9 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Mention do parse and fmt(see shape function) mentions to Delivery module """ import Ecto.Query, warn: false - import Helper.Utils, only: [get_config: 2, thread_of_article: 1] + import Helper.Utils, only: [get_config: 2] - import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1, author_of: 1] + import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1, author_of: 1, thread_of: 1] import GroupherServer.CMS.Delegate.Hooks.Helper, only: [merge_same_block_linker: 2] alias GroupherServer.{Accounts, CMS, Delivery, Repo} @@ -91,7 +91,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Mention do end defp shape(article, to_user_id, block_id) do - {:ok, thread} = thread_of_article(article) + {:ok, thread} = thread_of(article) %{ thread: thread, diff --git a/lib/groupher_server/cms/delegates/hooks/notify.ex b/lib/groupher_server/cms/delegates/hooks/notify.ex index 69120047b..b2fc87c61 100644 --- a/lib/groupher_server/cms/delegates/hooks/notify.ex +++ b/lib/groupher_server/cms/delegates/hooks/notify.ex @@ -2,8 +2,8 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Notify do @moduledoc """ notify hooks, for upvote, collect, comment, reply """ - import Helper.Utils, only: [thread_of_article: 1] - import GroupherServer.CMS.Delegate.Helper, only: [preload_author: 1, parent_article_of: 1] + import GroupherServer.CMS.Delegate.Helper, + only: [preload_author: 1, article_of: 1, thread_of: 1] alias GroupherServer.{Accounts, CMS, Delivery} @@ -12,9 +12,9 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Notify do # 发布评论是特殊情况,单独处理 def handle(:comment, %Comment{} = comment, %User{} = from_user) do - {:ok, article} = parent_article_of(comment) + {:ok, article} = article_of(comment) {:ok, article} = preload_author(article) - {:ok, thread} = thread_of_article(article) + {:ok, thread} = thread_of(article) notify_attrs = %{ action: :comment, @@ -31,9 +31,9 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Notify do # 回复评论是特殊情况,单独处理 def handle(:reply, %Comment{} = reply_comment, %User{} = from_user) do - {:ok, article} = parent_article_of(reply_comment) + {:ok, article} = article_of(reply_comment) {:ok, article} = preload_author(article) - {:ok, thread} = thread_of_article(article) + {:ok, thread} = thread_of(article) notify_attrs = %{ action: :reply, @@ -49,8 +49,8 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Notify do end def handle(action, %Comment{} = comment, %User{} = from_user) do - {:ok, article} = parent_article_of(comment) - {:ok, thread} = thread_of_article(article) + {:ok, article} = article_of(comment) + {:ok, thread} = thread_of(article) notify_attrs = %{ action: action, @@ -66,7 +66,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Notify do def handle(action, article, %User{} = from_user) do {:ok, article} = preload_author(article) - {:ok, thread} = thread_of_article(article) + {:ok, thread} = thread_of(article) notify_attrs = %{ action: action, @@ -80,8 +80,8 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Notify do end def handle(:undo, action, %Comment{} = comment, %User{} = from_user) do - {:ok, article} = parent_article_of(comment) - {:ok, thread} = thread_of_article(article) + {:ok, article} = article_of(comment) + {:ok, thread} = thread_of(article) notify_attrs = %{ action: action, @@ -97,7 +97,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Notify do def handle(:undo, action, article, %User{} = from_user) do {:ok, article} = preload_author(article) - {:ok, thread} = thread_of_article(article) + {:ok, thread} = thread_of(article) notify_attrs = %{ action: action, diff --git a/lib/groupher_server/cms/models/embeds/article_meta.ex b/lib/groupher_server/cms/models/embeds/article_meta.ex index 7a6132823..fae98bd98 100644 --- a/lib/groupher_server/cms/models/embeds/article_meta.ex +++ b/lib/groupher_server/cms/models/embeds/article_meta.ex @@ -14,6 +14,7 @@ defmodule GroupherServer.CMS.Model.Embeds.ArticleMeta do thread: "POST", is_edited: false, is_comment_locked: false, + folded_comment_count: 0, upvoted_user_ids: [], collected_user_ids: [], viewed_user_ids: [], @@ -30,6 +31,7 @@ defmodule GroupherServer.CMS.Model.Embeds.ArticleMeta do field(:thread, :string) field(:is_edited, :boolean, default: false) field(:is_comment_locked, :boolean, default: false) + field(:folded_comment_count, :integer, default: 0) # reaction history field(:upvoted_user_ids, {:array, :integer}, default: []) field(:collected_user_ids, {:array, :integer}, default: []) diff --git a/lib/groupher_server/delivery/delegates/mention.ex b/lib/groupher_server/delivery/delegates/mention.ex index de63b5a8d..bcbc892b2 100644 --- a/lib/groupher_server/delivery/delegates/mention.ex +++ b/lib/groupher_server/delivery/delegates/mention.ex @@ -3,7 +3,7 @@ defmodule GroupherServer.Delivery.Delegate.Mention do The Delivery context. """ import Ecto.Query, warn: false - import Helper.Utils, only: [done: 1, thread_of_article: 2, atom_values_to_upcase: 1] + import Helper.Utils, only: [done: 1, thread_of: 2, atom_values_to_upcase: 1] import ShortMaps alias GroupherServer.{Accounts, CMS, Delivery, Repo} @@ -109,7 +109,7 @@ defmodule GroupherServer.Delivery.Delegate.Mention do end defp batch_delete_mentions(article, %User{} = from_user) do - with {:ok, thread} <- thread_of_article(article, :upcase) do + with {:ok, thread} <- thread_of(article, :upcase) do from(m in Mention, where: m.article_id == ^article.id, where: m.thread == ^thread, diff --git a/lib/helper/utils/utils.ex b/lib/helper/utils/utils.ex index 110282706..ce630a796 100644 --- a/lib/helper/utils/utils.ex +++ b/lib/helper/utils/utils.ex @@ -209,17 +209,6 @@ defmodule Helper.Utils do end end - # get thread of article - def thread_of_article(%{meta: %{thread: thread}}) do - thread |> String.downcase() |> String.to_atom() |> done - end - - def thread_of_article(_), do: {:error, "invalid article"} - - def thread_of_article(%{meta: %{thread: thread}}, :upcase) do - thread |> to_string |> String.upcase() |> done - end - def to_upcase(v) when is_atom(v), do: v |> to_string |> String.upcase() def to_upcase(v) when is_binary(v), do: v |> String.upcase() def to_upcase(_), do: nil From 5718d767741f428ac0ce36973aba05c3aa4ad6e3 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Fri, 25 Jun 2021 10:53:23 +0800 Subject: [PATCH 2/6] refactor(import): fix import & clean up warnings --- lib/groupher_server/cms/models/article_document.ex | 5 ----- lib/groupher_server/cms/models/blog_document.ex | 2 +- lib/groupher_server/cms/models/job_document.ex | 2 +- lib/groupher_server/cms/models/post_document.ex | 2 +- lib/groupher_server/cms/models/repo_document.ex | 2 +- lib/groupher_server_web/resolvers/accounts_resolver.ex | 2 +- lib/helper/utils/utils.ex | 3 +++ 7 files changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/groupher_server/cms/models/article_document.ex b/lib/groupher_server/cms/models/article_document.ex index 167c6bd15..82e651210 100644 --- a/lib/groupher_server/cms/models/article_document.ex +++ b/lib/groupher_server/cms/models/article_document.ex @@ -11,11 +11,6 @@ defmodule GroupherServer.CMS.Model.ArticleDocument do import GroupherServer.CMS.Helper.Macros import Helper.Utils, only: [get_config: 2] - alias GroupherServer.CMS - alias CMS.Model.Embeds - - alias Helper.HTML - @timestamps_opts [type: :utc_datetime_usec] @max_body_length get_config(:article, :max_length) diff --git a/lib/groupher_server/cms/models/blog_document.ex b/lib/groupher_server/cms/models/blog_document.ex index 6e3bdb356..d9ed6de1d 100644 --- a/lib/groupher_server/cms/models/blog_document.ex +++ b/lib/groupher_server/cms/models/blog_document.ex @@ -11,7 +11,7 @@ defmodule GroupherServer.CMS.Model.BlogDocument do import Helper.Utils, only: [get_config: 2] alias GroupherServer.CMS - alias CMS.Model.{Embeds, Blog} + alias CMS.Model.Blog @timestamps_opts [type: :utc_datetime_usec] diff --git a/lib/groupher_server/cms/models/job_document.ex b/lib/groupher_server/cms/models/job_document.ex index 7f1fab644..81c7e7e88 100644 --- a/lib/groupher_server/cms/models/job_document.ex +++ b/lib/groupher_server/cms/models/job_document.ex @@ -11,7 +11,7 @@ defmodule GroupherServer.CMS.Model.JobDocument do import Helper.Utils, only: [get_config: 2] alias GroupherServer.CMS - alias CMS.Model.{Embeds, Job} + alias CMS.Model.Job @timestamps_opts [type: :utc_datetime_usec] diff --git a/lib/groupher_server/cms/models/post_document.ex b/lib/groupher_server/cms/models/post_document.ex index b7e7bec9b..f60081335 100644 --- a/lib/groupher_server/cms/models/post_document.ex +++ b/lib/groupher_server/cms/models/post_document.ex @@ -11,7 +11,7 @@ defmodule GroupherServer.CMS.Model.PostDocument do import Helper.Utils, only: [get_config: 2] alias GroupherServer.CMS - alias CMS.Model.{Embeds, Post} + alias CMS.Model.Post @timestamps_opts [type: :utc_datetime_usec] diff --git a/lib/groupher_server/cms/models/repo_document.ex b/lib/groupher_server/cms/models/repo_document.ex index 5d08129e9..7bcb3d4c7 100644 --- a/lib/groupher_server/cms/models/repo_document.ex +++ b/lib/groupher_server/cms/models/repo_document.ex @@ -10,7 +10,7 @@ defmodule GroupherServer.CMS.Model.RepoDocument do import Ecto.Changeset alias GroupherServer.CMS - alias CMS.Model.{Embeds, Repo} + alias CMS.Model.Repo @timestamps_opts [type: :utc_datetime_usec] diff --git a/lib/groupher_server_web/resolvers/accounts_resolver.ex b/lib/groupher_server_web/resolvers/accounts_resolver.ex index a7945860c..eb70c7722 100644 --- a/lib/groupher_server_web/resolvers/accounts_resolver.ex +++ b/lib/groupher_server_web/resolvers/accounts_resolver.ex @@ -252,7 +252,7 @@ defmodule GroupherServerWeb.Resolvers.Accounts do end end - def subscribed_communities(_root, %{filter: filter} = args, _info) do + def subscribed_communities(_root, %{filter: filter}, _info) do Accounts.default_subscribed_communities(filter) end diff --git a/lib/helper/utils/utils.ex b/lib/helper/utils/utils.ex index ce630a796..8d6a27718 100644 --- a/lib/helper/utils/utils.ex +++ b/lib/helper/utils/utils.ex @@ -8,6 +8,7 @@ defmodule Helper.Utils do import Helper.Validator.Guards, only: [g_none_empty_str: 1] + alias GroupherServer.CMS alias Helper.{Cache, Utils} # Map utils @@ -26,6 +27,8 @@ defmodule Helper.Utils do defdelegate count_words(str), to: Utils.String defdelegate str_occurence(string, substr), to: Utils.String + defdelegate thread_of(artiment, opt), to: CMS.Delegate.Helper + def get_config(section, key, app \\ :groupher_server) def get_config(section, :all, app) do From 66ee6b4252688a41a487afeb66eb325272c32718 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Fri, 25 Jun 2021 11:08:49 +0800 Subject: [PATCH 3/6] refactor(matcher): clean up unused matcher --- .../cms/delegates/comment_curd.ex | 12 ++++--- lib/groupher_server/cms/helper/matcher.ex | 1 - .../cms/helper/matcher_macros.ex | 32 ------------------- 3 files changed, 8 insertions(+), 37 deletions(-) diff --git a/lib/groupher_server/cms/delegates/comment_curd.ex b/lib/groupher_server/cms/delegates/comment_curd.ex index d714febfd..12af301e7 100644 --- a/lib/groupher_server/cms/delegates/comment_curd.ex +++ b/lib/groupher_server/cms/delegates/comment_curd.ex @@ -6,7 +6,9 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do import Helper.Utils, only: [done: 1, ensure: 2] import Helper.ErrorCode - import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 3] + import GroupherServer.CMS.Delegate.Helper, + only: [mark_viewer_emotion_states: 3, article_of: 1, thread_of: 1] + import GroupherServer.CMS.Helper.Matcher import ShortMaps @@ -280,10 +282,12 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do # update comment's parent article's comments total count @spec update_comments_count(Comment.t(), :inc | :dec) :: Comment.t() def update_comments_count(%Comment{} = comment, opt) do - with {:ok, article_info} <- match(:comment_article, comment), - {:ok, article} <- ORM.find(article_info.model, article_info.id) do + with {:ok, article} <- article_of(comment), + {:ok, article_thread} <- thread_of(article) do + foreign_key = :"#{article_thread}_id" + {:ok, cur_count} = - from(c in Comment, where: field(c, ^article_info.foreign_key) == ^article_info.id) + from(c in Comment, where: field(c, ^foreign_key) == ^article.id) |> ORM.count() # dec 是 comment 还没有删除的时候的操作,和 inc 不同 diff --git a/lib/groupher_server/cms/helper/matcher.ex b/lib/groupher_server/cms/helper/matcher.ex index 26ddce4bf..b939df125 100644 --- a/lib/groupher_server/cms/helper/matcher.ex +++ b/lib/groupher_server/cms/helper/matcher.ex @@ -33,5 +33,4 @@ defmodule GroupherServer.CMS.Helper.Matcher do thread_matches() thread_query_matches() - comment_article_matches() end diff --git a/lib/groupher_server/cms/helper/matcher_macros.ex b/lib/groupher_server/cms/helper/matcher_macros.ex index 35a97bae9..db458368e 100644 --- a/lib/groupher_server/cms/helper/matcher_macros.ex +++ b/lib/groupher_server/cms/helper/matcher_macros.ex @@ -59,36 +59,4 @@ defmodule GroupherServer.CMS.Helper.MatcherMacros do end end) end - - @doc """ - mapping basic comment -> thread - - {:ok, info} <- match(:comment_article, %Comment{post_id: id} = comment) - info: - %{ - id: id, - model: CMS.Model.Post, - foreign_key: :post_id, - } - """ - defmacro comment_article_matches() do - @article_threads - |> Enum.map(fn thread -> - # def match(:comment_article, %Comment{post_id: id}) - quote do - # see https://elixirforum.com/t/generate-map-pattern-matching-functions/21928/2 - def match(:comment_article, %Comment{unquote(:"#{thread}_id") => id}) - when not is_nil(id) do - thread_module = unquote(thread) |> to_string |> Recase.to_pascal() - - {:ok, - %{ - id: id, - model: Module.concat(CMS.Model, thread_module), - foreign_key: unquote(:"#{thread}_id") - }} - end - end - end) - end end From 4b702f9ff6ee32353ea3582db2e1c3e771f184e9 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Fri, 25 Jun 2021 11:10:03 +0800 Subject: [PATCH 4/6] chore: fmt --- lib/groupher_server/cms/delegates/comment_curd.ex | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/groupher_server/cms/delegates/comment_curd.ex b/lib/groupher_server/cms/delegates/comment_curd.ex index 12af301e7..aba7bce8c 100644 --- a/lib/groupher_server/cms/delegates/comment_curd.ex +++ b/lib/groupher_server/cms/delegates/comment_curd.ex @@ -261,10 +261,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do end # add participator to article-like(Post, Job ...) and update count - def add_participant_to_article( - %{comments_participants: participants} = article, - %User{} = user - ) do + def add_participant_to_article(%{comments_participants: participants} = article, %User{} = user) do total_participants = participants |> List.insert_at(0, user) |> Enum.uniq_by(& &1.id) latest_participants = total_participants |> Enum.slice(0, @max_participator_count) From a98aafb58d4b2252989c4dfbeedf2d52c67c45dd Mon Sep 17 00:00:00 2001 From: mydearxym Date: Fri, 25 Jun 2021 11:18:20 +0800 Subject: [PATCH 5/6] chore: fmt --- lib/groupher_server/cms/delegates/comment_curd.ex | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/groupher_server/cms/delegates/comment_curd.ex b/lib/groupher_server/cms/delegates/comment_curd.ex index aba7bce8c..38bc605f1 100644 --- a/lib/groupher_server/cms/delegates/comment_curd.ex +++ b/lib/groupher_server/cms/delegates/comment_curd.ex @@ -13,13 +13,14 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do import ShortMaps alias Helper.Types, as: T - alias Helper.{Later, ORM, QueryBuilder, Converter} alias GroupherServer.{Accounts, CMS, Repo} - alias CMS.Model.Post - alias CMS.Delegate.Hooks alias Accounts.Model.User - alias CMS.Model.{Comment, PinnedComment, Embeds} + alias CMS.Model.{Post, Comment, PinnedComment, Embeds} + + alias CMS.Delegate.Hooks + alias Helper.{Later, ORM, QueryBuilder, Converter} + alias Ecto.Multi @max_participator_count Comment.max_participator_count() From 0f00d2e37ffc5bf05cfb28e49076005bebe80131 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Fri, 25 Jun 2021 12:06:56 +0800 Subject: [PATCH 6/6] refactor(fold-comment): update article meta & tests --- .../cms/delegates/comment_action.ex | 34 +++++++++++++++---- .../cms/comments/blog_comment_test.exs | 8 +++++ .../cms/comments/job_comment_test.exs | 8 +++++ .../cms/comments/post_comment_test.exs | 8 +++++ .../cms/comments/repo_comment_test.exs | 8 +++++ 5 files changed, 59 insertions(+), 7 deletions(-) diff --git a/lib/groupher_server/cms/delegates/comment_action.ex b/lib/groupher_server/cms/delegates/comment_action.ex index 3ada47a86..e3fe434a3 100644 --- a/lib/groupher_server/cms/delegates/comment_action.ex +++ b/lib/groupher_server/cms/delegates/comment_action.ex @@ -4,6 +4,8 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do """ import Ecto.Query, warn: false import Helper.Utils, only: [done: 1, strip_struct: 1, get_config: 2, ensure: 2] + import GroupherServer.CMS.Delegate.Helper, only: [article_of: 1, thread_of: 1] + import Helper.ErrorCode import GroupherServer.CMS.Delegate.CommentCurd, @@ -80,21 +82,19 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do end @doc "fold a comment" - def fold_comment(%Comment{} = comment, %User{} = _user) do - comment |> ORM.update(%{is_folded: true}) - end + def fold_comment(%Comment{} = comment, %User{} = _user), do: do_fold_comment(comment, true) - @doc "fold a comment" + @doc "fold a comment by id" def fold_comment(comment_id, %User{} = _user) do with {:ok, comment} <- ORM.find(Comment, comment_id) do - comment |> ORM.update(%{is_folded: true}) + do_fold_comment(comment, true) end end @doc "unfold a comment" def unfold_comment(comment_id, %User{} = _user) do with {:ok, comment} <- ORM.find(Comment, comment_id) do - comment |> ORM.update(%{is_folded: false}) + do_fold_comment(comment, false) end end @@ -248,6 +248,26 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do end end + # do (un)fold and update folded count in article meta + defp do_fold_comment(%Comment{} = comment, is_folded) when is_boolean(is_folded) do + Multi.new() + |> Multi.run(:fold_comment, fn _, _ -> + comment |> ORM.update(%{is_folded: is_folded}) + end) + |> Multi.run(:update_article_fold_count, fn _, _ -> + {:ok, article} = article_of(comment) + {:ok, article_thread} = thread_of(article) + + {:ok, %{total_count: total_count}} = + CMS.paged_folded_comments(article_thread, article.id, %{page: 1, size: 1}) + + meta = article.meta |> Map.put(:folded_comment_count, total_count) + article |> ORM.update_meta(meta) + end) + |> Repo.transaction() + |> result() + end + defp update_article_author_upvoted_info(%Comment{} = comment, user_id) do with {:ok, article} = get_full_comment(comment.id) do is_article_author_upvoted = article.author.id == user_id @@ -366,9 +386,9 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do defp result({:ok, %{create_comment: result}}), do: {:ok, result} defp result({:ok, %{add_reply_to: result}}), do: {:ok, result} defp result({:ok, %{upvote_comment_done: result}}), do: {:ok, result} - defp result({:ok, %{fold_comment_report_too_many: result}}), do: {:ok, result} defp result({:ok, %{update_comment_flag: result}}), do: {:ok, result} defp result({:ok, %{delete_comment: result}}), do: {:ok, result} + defp result({:ok, %{fold_comment: result}}), do: {:ok, result} defp result({:error, :create_comment, result, _steps}) do raise_error(:create_comment, result) diff --git a/test/groupher_server/cms/comments/blog_comment_test.exs b/test/groupher_server/cms/comments/blog_comment_test.exs index e5fd7f8df..b86bd854b 100644 --- a/test/groupher_server/cms/comments/blog_comment_test.exs +++ b/test/groupher_server/cms/comments/blog_comment_test.exs @@ -229,6 +229,7 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do end describe "[article comment fold/unfold]" do + @tag :wip test "user can fold a comment", ~m(user blog)a do {:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user) {:ok, comment} = ORM.find(Comment, comment.id) @@ -238,8 +239,12 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do {:ok, comment} = CMS.fold_comment(comment.id, user) {:ok, comment} = ORM.find(Comment, comment.id) assert comment.is_folded + + {:ok, blog} = ORM.find(Blog, blog.id) + assert blog.meta.folded_comment_count == 1 end + @tag :wip test "user can unfold a comment", ~m(user blog)a do {:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user) {:ok, _comment} = CMS.fold_comment(comment.id, user) @@ -250,6 +255,9 @@ defmodule GroupherServer.Test.CMS.Comments.BlogComment do {:ok, _comment} = CMS.unfold_comment(comment.id, user) {:ok, comment} = ORM.find(Comment, comment.id) assert not comment.is_folded + + {:ok, blog} = ORM.find(Blog, blog.id) + assert blog.meta.folded_comment_count == 0 end end diff --git a/test/groupher_server/cms/comments/job_comment_test.exs b/test/groupher_server/cms/comments/job_comment_test.exs index 2e98b7c60..2ccc63230 100644 --- a/test/groupher_server/cms/comments/job_comment_test.exs +++ b/test/groupher_server/cms/comments/job_comment_test.exs @@ -229,6 +229,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do end describe "[article comment fold/unfold]" do + @tag :wip test "user can fold a comment", ~m(user job)a do {:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user) {:ok, comment} = ORM.find(Comment, comment.id) @@ -238,8 +239,12 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do {:ok, comment} = CMS.fold_comment(comment.id, user) {:ok, comment} = ORM.find(Comment, comment.id) assert comment.is_folded + + {:ok, job} = ORM.find(Job, job.id) + assert job.meta.folded_comment_count == 1 end + @tag :wip test "user can unfold a comment", ~m(user job)a do {:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user) {:ok, _comment} = CMS.fold_comment(comment.id, user) @@ -250,6 +255,9 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do {:ok, _comment} = CMS.unfold_comment(comment.id, user) {:ok, comment} = ORM.find(Comment, comment.id) assert not comment.is_folded + + {:ok, job} = ORM.find(Job, job.id) + assert job.meta.folded_comment_count == 0 end end diff --git a/test/groupher_server/cms/comments/post_comment_test.exs b/test/groupher_server/cms/comments/post_comment_test.exs index 66d66a69d..6a3f13a9d 100644 --- a/test/groupher_server/cms/comments/post_comment_test.exs +++ b/test/groupher_server/cms/comments/post_comment_test.exs @@ -229,6 +229,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do end describe "[article comment fold/unfold]" do + @tag :wip test "user can fold a comment", ~m(user post)a do {:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user) {:ok, comment} = ORM.find(Comment, comment.id) @@ -238,8 +239,12 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do {:ok, comment} = CMS.fold_comment(comment.id, user) {:ok, comment} = ORM.find(Comment, comment.id) assert comment.is_folded + + {:ok, post} = ORM.find(Post, post.id) + assert post.meta.folded_comment_count == 1 end + @tag :wip test "user can unfold a comment", ~m(user post)a do {:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user) {:ok, _comment} = CMS.fold_comment(comment.id, user) @@ -250,6 +255,9 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do {:ok, _comment} = CMS.unfold_comment(comment.id, user) {:ok, comment} = ORM.find(Comment, comment.id) assert not comment.is_folded + + {:ok, post} = ORM.find(Post, post.id) + assert post.meta.folded_comment_count == 0 end end diff --git a/test/groupher_server/cms/comments/repo_comment_test.exs b/test/groupher_server/cms/comments/repo_comment_test.exs index 434b9252c..b857cc161 100644 --- a/test/groupher_server/cms/comments/repo_comment_test.exs +++ b/test/groupher_server/cms/comments/repo_comment_test.exs @@ -229,6 +229,7 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do end describe "[article comment fold/unfold]" do + @tag :wip test "user can fold a comment", ~m(user repo)a do {:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user) {:ok, comment} = ORM.find(Comment, comment.id) @@ -238,8 +239,12 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do {:ok, comment} = CMS.fold_comment(comment.id, user) {:ok, comment} = ORM.find(Comment, comment.id) assert comment.is_folded + + {:ok, repo} = ORM.find(Repo, repo.id) + assert repo.meta.folded_comment_count == 1 end + @tag :wip test "user can unfold a comment", ~m(user repo)a do {:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user) {:ok, _comment} = CMS.fold_comment(comment.id, user) @@ -250,6 +255,9 @@ defmodule GroupherServer.Test.CMS.Comments.RepoComment do {:ok, _comment} = CMS.unfold_comment(comment.id, user) {:ok, comment} = ORM.find(Comment, comment.id) assert not comment.is_folded + + {:ok, repo} = ORM.find(Repo, repo.id) + assert repo.meta.folded_comment_count == 0 end end