From 6f5103944ab20a379aac29debd45e689d64b1433 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Thu, 13 May 2021 23:06:01 +0800 Subject: [PATCH 1/4] refactor: wip --- lib/groupher_server/cms/community.ex | 2 + .../cms/delegates/abuse_report.ex | 4 +- .../cms/delegates/article_comment.ex | 6 +- .../cms/delegates/article_curd.ex | 28 +++--- lib/groupher_server/cms/helper/matcher2.ex | 99 +++++++++---------- lib/helper/utils/utils.ex | 10 ++ .../cms/abuse_reports/post_report_test.exs | 1 + .../query/cms/flags/posts_flags_test.exs | 1 + 8 files changed, 79 insertions(+), 72 deletions(-) diff --git a/lib/groupher_server/cms/community.ex b/lib/groupher_server/cms/community.ex index f7a830b87..10c1358a6 100644 --- a/lib/groupher_server/cms/community.ex +++ b/lib/groupher_server/cms/community.ex @@ -19,12 +19,14 @@ defmodule GroupherServer.CMS.Community do CommunityCheatsheet } + @article_threads [:post, :job, :repo] @max_pinned_article_count_per_thread 2 @required_fields ~w(title desc user_id logo raw)a # @required_fields ~w(title desc user_id)a @optional_fields ~w(label geo_info index aka)a + def article_threads, do: @article_threads def max_pinned_article_count_per_thread, do: @max_pinned_article_count_per_thread schema "communities" do diff --git a/lib/groupher_server/cms/delegates/abuse_report.ex b/lib/groupher_server/cms/delegates/abuse_report.ex index 5d5f265be..e39b3912e 100644 --- a/lib/groupher_server/cms/delegates/abuse_report.ex +++ b/lib/groupher_server/cms/delegates/abuse_report.ex @@ -13,13 +13,13 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do alias GroupherServer.{Accounts, CMS, Repo} alias Accounts.User - alias CMS.{AbuseReport, ArticleComment, Embeds} + alias CMS.{Community, AbuseReport, ArticleComment, Embeds} alias Ecto.Multi @report_threshold_for_fold ArticleComment.report_threshold_for_fold() - @article_threads [:post, :job, :repo] + @article_threads Community.article_threads() @export_author_keys [:id, :login, :nickname, :avatar] @export_article_keys [:id, :title, :digest, :upvotes_count, :views] @export_report_keys [ diff --git a/lib/groupher_server/cms/delegates/article_comment.ex b/lib/groupher_server/cms/delegates/article_comment.ex index 9050ba9de..1714df92e 100644 --- a/lib/groupher_server/cms/delegates/article_comment.ex +++ b/lib/groupher_server/cms/delegates/article_comment.ex @@ -96,19 +96,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do # make sure the article exsit # author is passed by middleware, it's exsit for sure {:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do - IO.inspect(info, label: "bb") Multi.new() |> Multi.run(:create_article_comment, fn _, _ -> - IO.inspect("11") do_create_comment(content, info.foreign_key, article, user) end) |> Multi.run(:update_article_comments_count, fn _, %{create_article_comment: comment} -> - IO.inspect("22") - update_article_comments_count(comment, :inc) |> IO.inspect(label: "22 after") + update_article_comments_count(comment, :inc) end) |> Multi.run(:add_participator, fn _, _ -> - IO.inspect("33") add_participator_to_article(article, user) end) |> Repo.transaction() diff --git a/lib/groupher_server/cms/delegates/article_curd.ex b/lib/groupher_server/cms/delegates/article_curd.ex index ceea9d7ca..3e6489782 100644 --- a/lib/groupher_server/cms/delegates/article_curd.ex +++ b/lib/groupher_server/cms/delegates/article_curd.ex @@ -7,7 +7,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do import GroupherServer.CMS.Helper.Matcher2 import GroupherServer.CMS.Helper.Matcher, only: [match_action: 2] - import Helper.Utils, only: [done: 1, pick_by: 2, integerfy: 1, strip_struct: 1] + import Helper.Utils, + only: [done: 1, pick_by: 2, integerfy: 1, strip_struct: 1, module_to_thread: 1] + import GroupherServer.CMS.Delegate.Helper, only: [mark_viewer_emotion_states: 2] import Helper.ErrorCode import ShortMaps @@ -301,19 +303,19 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do defp domain_filter_query(queryable, _filter), do: queryable defp add_pin_contents_ifneed(contents, querable, %{community: community} = filter) do - with {:ok, _} <- should_add_pin?(filter), - {:ok, info} <- match(querable), - true <- 1 == Map.get(contents, :page_number) do - {:ok, pinned_articles} = - PinnedArticle - |> join(:inner, [p], c in assoc(p, :community)) - # |> join(:inner, [p], article in assoc(p, ^filter.thread)) - |> join(:inner, [p], article in assoc(p, ^info.thread)) - |> where([p, c, article], c.raw == ^community) - |> select([p, c, article], article) - # 10 pined contents per community/thread, at most - |> ORM.find_all(%{page: 1, size: 10}) + thread = module_to_thread(querable) + with {:ok, _} <- should_add_pin?(filter), + true <- 1 == Map.get(contents, :page_number), + {:ok, pinned_articles} <- + PinnedArticle + |> join(:inner, [p], c in assoc(p, :community)) + # |> join(:inner, [p], article in assoc(p, ^filter.thread)) + |> join(:inner, [p], article in assoc(p, ^thread)) + |> where([p, c, article], c.raw == ^community) + |> select([p, c, article], article) + # 10 pined contents per community/thread, at most + |> ORM.find_all(%{page: 1, size: 10}) do concat_contents(pinned_articles, contents) else _error -> contents diff --git a/lib/groupher_server/cms/helper/matcher2.ex b/lib/groupher_server/cms/helper/matcher2.ex index badd57048..e15d81571 100644 --- a/lib/groupher_server/cms/helper/matcher2.ex +++ b/lib/groupher_server/cms/helper/matcher2.ex @@ -1,15 +1,61 @@ +defmodule GroupherServer.CMS.Helper.Matcher2.Macros do + @moduledoc """ + generate match functions + """ + + alias GroupherServer.CMS + alias CMS.{Community, Embeds} + + @article_threads Community.article_threads() + + defmacro thread_matchs() do + @article_threads + |> Enum.map(fn thread -> + quote do + def match(unquote(thread)) do + thread_module = unquote(thread) |> to_string |> Recase.to_pascal() + + {:ok, + %{ + model: Module.concat(CMS, thread_module), + thread: unquote(thread), + foreign_key: unquote(:"#{thread}_id"), + preload: unquote(thread), + default_meta: Embeds.ArticleMeta.default_meta() + }} + end + + # def match(Module.concat(CMS, Recase.to_pascal(unquote(thread)))) do + # {:ok, %{thread: :post}} + # end + end + end) + end +end + defmodule GroupherServer.CMS.Helper.Matcher2 do @moduledoc """ this module defined the matches and handy guard ... """ import Ecto.Query, warn: false + import GroupherServer.CMS.Helper.Matcher2.Macros alias GroupherServer.{Accounts, CMS} alias Accounts.User alias CMS.{ArticleComment, Post, Job, Repo} + def match(:account) do + {:ok, + %{ + model: User, + foreign_key: :account_id, + preload: :account, + default_meta: Accounts.Embeds.UserMeta.default_meta() + }} + end + def match(:article_comment) do {:ok, %{ @@ -36,58 +82,7 @@ defmodule GroupherServer.CMS.Helper.Matcher2 do {:error, "match error, not supported"} end - def match(:account) do - {:ok, - %{ - model: User, - foreign_key: :account_id, - preload: :account, - default_meta: Accounts.Embeds.UserMeta.default_meta() - }} - end - - # used for paged pin articles - def match(Post) do - {:ok, %{thread: :post}} - end - - def match(:post) do - {:ok, - %{ - model: Post, - foreign_key: :post_id, - preload: :post, - default_meta: CMS.Embeds.ArticleMeta.default_meta() - }} - end - - def match(Job) do - {:ok, %{thread: :job}} - end - - def match(:job) do - {:ok, - %{ - model: Job, - foreign_key: :job_id, - preload: :job, - default_meta: CMS.Embeds.ArticleMeta.default_meta() - }} - end - - def match(Repo) do - {:ok, %{thread: :repo}} - end - - def match(:repo) do - {:ok, - %{ - model: Repo, - foreign_key: :repo_id, - preload: :repo, - default_meta: CMS.Embeds.ArticleMeta.default_meta() - }} - end + thread_matchs() def match(:post, :query, id), do: {:ok, dynamic([c], c.post_id == ^id)} def match(:job, :query, id), do: {:ok, dynamic([c], c.job_id == ^id)} diff --git a/lib/helper/utils/utils.ex b/lib/helper/utils/utils.ex index 1ea595ff9..9216bf786 100644 --- a/lib/helper/utils/utils.ex +++ b/lib/helper/utils/utils.ex @@ -175,6 +175,16 @@ defmodule Helper.Utils do def strip_struct(map) when is_map(map), do: map + @doc "turn GroupherServer.CMS.Post -> :post" + def module_to_thread(module) do + module + |> to_string + |> String.split(".") + |> List.last() + |> String.downcase() + |> String.to_atom() + end + @doc "html uniq id generator for editorjs" @spec uid(:html, map) :: String.t() def uid(:html, %{"id" => id}) when g_none_empty_str(id), do: id diff --git a/test/groupher_server/cms/abuse_reports/post_report_test.exs b/test/groupher_server/cms/abuse_reports/post_report_test.exs index 743fd1b0d..fcd638368 100644 --- a/test/groupher_server/cms/abuse_reports/post_report_test.exs +++ b/test/groupher_server/cms/abuse_reports/post_report_test.exs @@ -79,6 +79,7 @@ defmodule GroupherServer.Test.CMS.AbuseReports.PostReport do assert user.id not in post.meta.reported_user_ids end + @tag :wip2 test "can undo a report with other user report it too", ~m(community user user2 post_attrs)a do {:ok, post} = CMS.create_content(community, :post, post_attrs, user) diff --git a/test/groupher_server_web/query/cms/flags/posts_flags_test.exs b/test/groupher_server_web/query/cms/flags/posts_flags_test.exs index 9d71db79f..90671a222 100644 --- a/test/groupher_server_web/query/cms/flags/posts_flags_test.exs +++ b/test/groupher_server_web/query/cms/flags/posts_flags_test.exs @@ -89,6 +89,7 @@ defmodule GroupherServer.Test.Query.Flags.PostsFlags do assert results["entries"] |> Enum.any?(&(&1["id"] !== random_id)) end + @tag :wip2 test "if have trashed posts, the trashed posts should not appears in result", ~m(guest_conn community)a do variables = %{filter: %{community: community.raw}} From ef9d67d41d4fa4c780f00c4305bec9d9b010fe79 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Thu, 13 May 2021 23:06:21 +0800 Subject: [PATCH 2/4] refactor: wip --- lib/groupher_server/cms/delegates/article_comment.ex | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/groupher_server/cms/delegates/article_comment.ex b/lib/groupher_server/cms/delegates/article_comment.ex index 1714df92e..8fbf44db8 100644 --- a/lib/groupher_server/cms/delegates/article_comment.ex +++ b/lib/groupher_server/cms/delegates/article_comment.ex @@ -96,7 +96,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do # make sure the article exsit # author is passed by middleware, it's exsit for sure {:ok, article} <- ORM.find(info.model, article_id, preload: [author: :user]) do - Multi.new() |> Multi.run(:create_article_comment, fn _, _ -> do_create_comment(content, info.foreign_key, article, user) From c746633d37fdb89fcd5fc04c119b3cf1d825cd16 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Thu, 13 May 2021 23:52:51 +0800 Subject: [PATCH 3/4] refactor: wip --- lib/groupher_server/cms/helper/matcher2.ex | 63 +++++++++++++--------- priv/mock/tag_patch_seeds.exs | 2 - 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/lib/groupher_server/cms/helper/matcher2.ex b/lib/groupher_server/cms/helper/matcher2.ex index e15d81571..bf02589ca 100644 --- a/lib/groupher_server/cms/helper/matcher2.ex +++ b/lib/groupher_server/cms/helper/matcher2.ex @@ -4,11 +4,11 @@ defmodule GroupherServer.CMS.Helper.Matcher2.Macros do """ alias GroupherServer.CMS - alias CMS.{Community, Embeds} + alias CMS.{ArticleComment, Community, Embeds} @article_threads Community.article_threads() - defmacro thread_matchs() do + defmacro thread_matches() do @article_threads |> Enum.map(fn thread -> quote do @@ -24,10 +24,38 @@ defmodule GroupherServer.CMS.Helper.Matcher2.Macros do default_meta: Embeds.ArticleMeta.default_meta() }} end + end + end) + end + + defmacro thread_query_matches() do + @article_threads + |> Enum.map(fn thread -> + quote do + def match(unquote(thread), :query, id) do + {:ok, dynamic([c], field(c, unquote(:"#{thread}_id")) == ^id)} + end + end + end) + end + + defmacro comment_article_matches() do + @article_threads + |> Enum.map(fn thread -> + # def match(:comment_article, %ArticleComment{post_id: id}) + quote do + # see https://elixirforum.com/t/generate-map-pattern-matching-functions/21928/2 + def match(:comment_article, %ArticleComment{unquote(:"#{thread}_id") => id}) + when not is_nil(id) do + thread_module = unquote(thread) |> to_string |> Recase.to_pascal() - # def match(Module.concat(CMS, Recase.to_pascal(unquote(thread)))) do - # {:ok, %{thread: :post}} - # end + {:ok, + %{ + id: id, + model: Module.concat(CMS, thread_module), + foreign_key: unquote(:"#{thread}_id") + }} + end end end) end @@ -44,7 +72,7 @@ defmodule GroupherServer.CMS.Helper.Matcher2 do alias GroupherServer.{Accounts, CMS} alias Accounts.User - alias CMS.{ArticleComment, Post, Job, Repo} + alias CMS.{ArticleComment} def match(:account) do {:ok, @@ -66,24 +94,7 @@ defmodule GroupherServer.CMS.Helper.Matcher2 do }} end - def match(:comment_article, %ArticleComment{post_id: post_id}) when not is_nil(post_id) do - {:ok, %{model: Post, id: post_id, foreign_key: :post_id}} - end - - def match(:comment_article, %ArticleComment{job_id: job_id}) when not is_nil(job_id) do - {:ok, %{model: Job, id: job_id, foreign_key: :job_id}} - end - - def match(:comment_article, %ArticleComment{repo_id: repo_id}) when not is_nil(repo_id) do - {:ok, %{model: Repo, id: repo_id, foreign_key: :repo_id}} - end - - def match(:comment_article, %ArticleComment{}) do - {:error, "match error, not supported"} - end - - thread_matchs() - - def match(:post, :query, id), do: {:ok, dynamic([c], c.post_id == ^id)} - def match(:job, :query, id), do: {:ok, dynamic([c], c.job_id == ^id)} + thread_matches() + thread_query_matches() + comment_article_matches() end diff --git a/priv/mock/tag_patch_seeds.exs b/priv/mock/tag_patch_seeds.exs index a162171d8..02a4f80d3 100644 --- a/priv/mock/tag_patch_seeds.exs +++ b/priv/mock/tag_patch_seeds.exs @@ -32,8 +32,6 @@ Enum.each(patch_communities, fn raw -> case community.raw not in ["cps-support"] do true -> # create_tags(community, :repo) - IO.inspect(community.raw, label: "patching community") - Enum.each(patch_tags, fn attr -> {:ok, _} = CMS.create_tag(community, :repo, attr, bot) end) From 14983c804489cb1aa5ea39836a1924ba2af3890b Mon Sep 17 00:00:00 2001 From: mydearxym Date: Fri, 14 May 2021 10:26:59 +0800 Subject: [PATCH 4/4] refactor: add some doc --- lib/groupher_server/cms/helper/matcher2.ex | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/lib/groupher_server/cms/helper/matcher2.ex b/lib/groupher_server/cms/helper/matcher2.ex index bf02589ca..d9a9805c4 100644 --- a/lib/groupher_server/cms/helper/matcher2.ex +++ b/lib/groupher_server/cms/helper/matcher2.ex @@ -8,6 +8,19 @@ defmodule GroupherServer.CMS.Helper.Matcher2.Macros do @article_threads Community.article_threads() + @doc """ + match basic threads + + {:ok, info} <- match(:post) + info: + %{ + model: Post, + thread: :post, + foreign_key: post_id, + preload: :post + default_meta: ... + } + """ defmacro thread_matches() do @article_threads |> Enum.map(fn thread -> @@ -28,6 +41,13 @@ defmodule GroupherServer.CMS.Helper.Matcher2.Macros do end) end + @doc """ + match basic thread query + + {:ok, info} <- match(:post, :query, id) + info: + %{dynamic([c], field(c, :post_id) == ^id)} + """ defmacro thread_query_matches() do @article_threads |> Enum.map(fn thread -> @@ -39,6 +59,17 @@ defmodule GroupherServer.CMS.Helper.Matcher2.Macros do end) end + @doc """ + mapping basic article_comment -> thread + + {:ok, info} <- match(:comment_article, %ArticleComment{post_id: id} = comment) + info: + %{ + id: id, + model: CMS.Post, + foreign_key: :post_id, + } + """ defmacro comment_article_matches() do @article_threads |> Enum.map(fn thread ->