From fa4af24423416cbcd065f9dabe34d293ced6a135 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Sun, 16 May 2021 18:09:34 +0800 Subject: [PATCH 1/5] refactor(cms-macros): extract article_belongs_to macro --- lib/groupher_server/cms/article_comment.ex | 25 ++++++++---------- lib/groupher_server/cms/helper/macros.ex | 30 ++++++++++++++++++++++ 2 files changed, 40 insertions(+), 15 deletions(-) create mode 100644 lib/groupher_server/cms/helper/macros.ex diff --git a/lib/groupher_server/cms/article_comment.ex b/lib/groupher_server/cms/article_comment.ex index b996772ae..375ed3b6b 100644 --- a/lib/groupher_server/cms/article_comment.ex +++ b/lib/groupher_server/cms/article_comment.ex @@ -6,23 +6,20 @@ defmodule GroupherServer.CMS.ArticleComment do use Accessible import Ecto.Changeset + import GroupherServer.CMS.Helper.Macros alias GroupherServer.{Accounts, CMS} - - alias CMS.{ - Post, - Job, - Repo, - Embeds, - ArticleCommentUpvote - } + alias CMS.{Embeds, ArticleCommentUpvote} # alias Helper.HTML + @article_threads CMS.Community.article_threads() @required_fields ~w(body_html author_id)a - @optional_fields ~w(post_id job_id repo_id reply_to_id replies_count is_folded is_deleted floor is_article_author)a + @optional_fields ~w(reply_to_id replies_count is_folded is_deleted floor is_article_author)a @updatable_fields ~w(is_folded is_deleted floor upvotes_count is_pinned)a + @article_fields @article_threads |> Enum.map(&:"#{&1}_id") + @max_participator_count 5 @max_parent_replies_count 3 @@ -69,10 +66,6 @@ defmodule GroupherServer.CMS.ArticleComment do field(:is_pinned, :boolean, default: false) field(:viewer_has_upvoted, :boolean, default: false, virtual: true) - belongs_to(:post, Post, foreign_key: :post_id) - belongs_to(:job, Job, foreign_key: :job_id) - belongs_to(:repo, Repo, foreign_key: :repo_id) - belongs_to(:reply_to, ArticleComment, foreign_key: :reply_to_id) embeds_many(:replies, ArticleComment, on_replace: :delete) @@ -83,13 +76,15 @@ defmodule GroupherServer.CMS.ArticleComment do has_many(:upvotes, {"articles_comments_upvotes", ArticleCommentUpvote}) + article_belongs_to() + timestamps(type: :utc_datetime) end @doc false def changeset(%ArticleComment{} = article_comment, attrs) do article_comment - |> cast(attrs, @required_fields ++ @optional_fields) + |> cast(attrs, @required_fields ++ @optional_fields ++ @article_fields) |> cast_embed(:emotions, required: true, with: &Embeds.ArticleCommentEmotion.changeset/2) |> cast_embed(:meta, required: true, with: &Embeds.ArticleCommentMeta.changeset/2) |> validate_required(@required_fields) @@ -99,7 +94,7 @@ defmodule GroupherServer.CMS.ArticleComment do # @doc false def update_changeset(%ArticleComment{} = article_comment, attrs) do article_comment - |> cast(attrs, @required_fields ++ @updatable_fields) + |> cast(attrs, @required_fields ++ @updatable_fields ++ @article_fields) |> cast_embed(:meta, required: true, with: &Embeds.ArticleCommentMeta.changeset/2) |> generl_changeset end diff --git a/lib/groupher_server/cms/helper/macros.ex b/lib/groupher_server/cms/helper/macros.ex new file mode 100644 index 000000000..cd40f2eb6 --- /dev/null +++ b/lib/groupher_server/cms/helper/macros.ex @@ -0,0 +1,30 @@ +defmodule GroupherServer.CMS.Helper.Macros do + @moduledoc """ + macros for define article related fields in CMS models + """ + + alias GroupherServer.CMS + + @article_threads CMS.Community.article_threads() + + @doc """ + generate belongs to fields for given thread + + e.g: + belongs_to(:post, Post, foreign_key: :post_id) + + NOTE: should do migration to DB manually + """ + defmacro article_belongs_to() do + @article_threads + |> Enum.map(fn thread -> + thread_module = unquote(thread) |> to_string |> Recase.to_pascal() + + quote do + belongs_to(unquote(thread), Module.concat(CMS, thread_module), + foreign_key: unquote(:"#{thread}_id") + ) + end + end) + end +end From cbc9f25a66ff7a02999f2d586ebe0897d6df0e35 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Sun, 16 May 2021 18:54:59 +0800 Subject: [PATCH 2/5] refactor: wip --- .../accounts/delegates/collect_folder.ex | 16 ++++-------- lib/groupher_server/cms/abuse_report.ex | 17 +++++++------ lib/groupher_server/cms/article_collect.ex | 25 ++++++++++--------- .../cms/article_pinned_comment.ex | 15 ++++++----- lib/groupher_server/cms/article_upvote.ex | 23 +++++++++-------- .../cms/article_user_emotion.ex | 8 +++--- lib/groupher_server/cms/helper/macros.ex | 7 +++--- lib/groupher_server/cms/pinned_article.ex | 25 +++++++++---------- .../accounts/collect_folder_test.exs | 1 + .../cms/article_community/job_test.exs | 2 +- .../cms/article_community/post_test.exs | 2 +- .../cms/article_community/repo_test.exs | 2 +- .../query/cms/comments/repo_comment_test.exs | 1 - 13 files changed, 73 insertions(+), 71 deletions(-) diff --git a/lib/groupher_server/accounts/delegates/collect_folder.ex b/lib/groupher_server/accounts/delegates/collect_folder.ex index 5008eefad..1825a2ddb 100644 --- a/lib/groupher_server/accounts/delegates/collect_folder.ex +++ b/lib/groupher_server/accounts/delegates/collect_folder.ex @@ -152,7 +152,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do CMS.set_collect_folder(article_collect, folder) end) |> Repo.transaction() - |> upsert_collect_folder_result() + |> result() end end @@ -183,7 +183,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do CMS.undo_set_collect_folder(article_collect, folder) end) |> Repo.transaction() - |> upsert_collect_folder_result() + |> result() end end @@ -249,13 +249,7 @@ defmodule GroupherServer.Accounts.Delegate.CollectFolder do defp filter_thread_ifneed(query, _), do: query - defp upsert_collect_folder_result({:ok, %{add_to_collect_folder: result}}), do: {:ok, result} - - defp upsert_collect_folder_result({:ok, %{rm_from_collect_folder: result}}) do - {:ok, result} - end - - defp upsert_collect_folder_result({:error, _, result, _steps}) do - {:error, result} - end + defp result({:ok, %{add_to_collect_folder: result}}), do: {:ok, result} + defp result({:ok, %{rm_from_collect_folder: result}}), do: {:ok, result} + defp result({:error, _, result, _steps}), do: {:error, result} end diff --git a/lib/groupher_server/cms/abuse_report.ex b/lib/groupher_server/cms/abuse_report.ex index ef44a5719..969f83e54 100644 --- a/lib/groupher_server/cms/abuse_report.ex +++ b/lib/groupher_server/cms/abuse_report.ex @@ -5,20 +5,22 @@ defmodule GroupherServer.CMS.AbuseReport do use Ecto.Schema use Accessible import Ecto.Changeset + import GroupherServer.CMS.Helper.Macros alias GroupherServer.{Accounts, CMS} - alias CMS.{ArticleComment, Embeds, Post, Job, Repo} + alias CMS.{ArticleComment, Embeds} + + @article_threads CMS.Community.article_threads() # @required_fields ~w(article_comment_id user_id recived_user_id)a - @optional_fields ~w(article_comment_id post_id job_id repo_id account_id operate_user_id deal_with report_cases_count)a + @optional_fields ~w(article_comment_id account_id operate_user_id deal_with report_cases_count)a @update_fields ~w(operate_user_id deal_with report_cases_count)a + @article_fields @article_threads |> Enum.map(&:"#{&1}_id") + @type t :: %AbuseReport{} schema "abuse_reports" do belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id) - belongs_to(:post, Post, foreign_key: :post_id) - belongs_to(:job, Job, foreign_key: :job_id) - belongs_to(:repo, Repo, foreign_key: :repo_id) belongs_to(:account, Accounts.User, foreign_key: :account_id) embeds_many(:report_cases, Embeds.AbuseReportCase, on_replace: :delete) @@ -28,19 +30,20 @@ defmodule GroupherServer.CMS.AbuseReport do field(:deal_with, :string) + article_belongs_to() timestamps(type: :utc_datetime) end @doc false def changeset(%AbuseReport{} = struct, attrs) do struct - |> cast(attrs, @optional_fields) + |> cast(attrs, @optional_fields ++ @article_fields) |> cast_embed(:report_cases, required: true, with: &Embeds.AbuseReportCase.changeset/2) end def update_changeset(%AbuseReport{} = struct, attrs) do struct - |> cast(attrs, @update_fields) + |> cast(attrs, @update_fields ++ @article_fields) |> cast_embed(:report_cases, required: true, with: &Embeds.AbuseReportCase.changeset/2) end end diff --git a/lib/groupher_server/cms/article_collect.ex b/lib/groupher_server/cms/article_collect.ex index 5f5c2dc07..ea61f5fce 100644 --- a/lib/groupher_server/cms/article_collect.ex +++ b/lib/groupher_server/cms/article_collect.ex @@ -4,38 +4,39 @@ defmodule GroupherServer.CMS.ArticleCollect do use Ecto.Schema import Ecto.Changeset + import GroupherServer.CMS.Helper.Macros alias GroupherServer.{Accounts, CMS} - alias Accounts.{User, CollectFolder} - alias CMS.{Post, Job, Repo} + + @article_threads CMS.Community.article_threads() @required_fields ~w(user_id)a - @optional_fields ~w(thread post_id job_id repo_id)a + @optional_fields ~w(thread)a + + @article_fields @article_threads |> Enum.map(&:"#{&1}_id") @type t :: %ArticleCollect{} schema "article_collects" do field(:thread, :string) - belongs_to(:user, User, foreign_key: :user_id) - belongs_to(:post, Post, foreign_key: :post_id) - belongs_to(:job, Job, foreign_key: :job_id) - belongs_to(:repo, Repo, foreign_key: :repo_id) - embeds_many(:collect_folders, CollectFolder, on_replace: :delete) + article_belongs_to() + timestamps(type: :utc_datetime) end @doc false def changeset(%ArticleCollect{} = article_collect, attrs) do article_collect - |> cast(attrs, @optional_fields ++ @required_fields) + |> cast(attrs, @optional_fields ++ @required_fields ++ @article_fields) |> validate_required(@required_fields) |> cast_embed(:collect_folders, with: &CollectFolder.changeset/2) |> foreign_key_constraint(:user_id) - |> foreign_key_constraint(:post_id) - |> foreign_key_constraint(:job_id) - |> foreign_key_constraint(:repo_id) + + # |> foreign_key_constraint(:post_id) + # |> foreign_key_constraint(:job_id) + # |> foreign_key_constraint(:repo_id) end end diff --git a/lib/groupher_server/cms/article_pinned_comment.ex b/lib/groupher_server/cms/article_pinned_comment.ex index 9473a5420..a3c2af039 100644 --- a/lib/groupher_server/cms/article_pinned_comment.ex +++ b/lib/groupher_server/cms/article_pinned_comment.ex @@ -6,21 +6,24 @@ defmodule GroupherServer.CMS.ArticlePinnedComment do use Accessible import Ecto.Changeset + import GroupherServer.CMS.Helper.Macros alias GroupherServer.CMS alias CMS.ArticleComment # alias Helper.HTML + @article_threads CMS.Community.article_threads() @required_fields ~w(article_comment_id)a - @optional_fields ~w(post_id job_id repo_id)a + # @optional_fields ~w(post_id job_id repo_id)a + + @article_fields @article_threads |> Enum.map(&:"#{&1}_id") @type t :: %ArticlePinnedComment{} schema "articles_pinned_comments" do belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id) - belongs_to(:post, CMS.Post, foreign_key: :post_id) - belongs_to(:job, CMS.Job, foreign_key: :job_id) - belongs_to(:repo, CMS.Repo, foreign_key: :repo_id) + + article_belongs_to() timestamps(type: :utc_datetime) end @@ -28,13 +31,13 @@ defmodule GroupherServer.CMS.ArticlePinnedComment do @doc false def changeset(%ArticlePinnedComment{} = article_pined_comment, attrs) do article_pined_comment - |> cast(attrs, @required_fields ++ @optional_fields) + |> cast(attrs, @required_fields ++ @article_fields) |> validate_required(@required_fields) end # @doc false def update_changeset(%ArticlePinnedComment{} = article_pined_comment, attrs) do article_pined_comment - |> cast(attrs, @required_fields ++ @optional_fields) + |> cast(attrs, @required_fields ++ @article_fields) end end diff --git a/lib/groupher_server/cms/article_upvote.ex b/lib/groupher_server/cms/article_upvote.ex index 8a93d1a47..e44c6ef06 100644 --- a/lib/groupher_server/cms/article_upvote.ex +++ b/lib/groupher_server/cms/article_upvote.ex @@ -4,24 +4,24 @@ defmodule GroupherServer.CMS.ArticleUpvote do use Ecto.Schema import Ecto.Changeset + import GroupherServer.CMS.Helper.Macros alias GroupherServer.{Accounts, CMS} - alias Accounts.User - alias CMS.{Post, Job, Repo} + + @article_threads CMS.Community.article_threads() @required_fields ~w(user_id)a - @optional_fields ~w(thread post_id job_id repo_id)a + @optional_fields ~w(thread)a + @article_fields @article_threads |> Enum.map(&:"#{&1}_id") @type t :: %ArticleUpvote{} schema "article_upvotes" do # for user-center to filter field(:thread, :string) - belongs_to(:user, User, foreign_key: :user_id) - belongs_to(:post, Post, foreign_key: :post_id) - belongs_to(:job, Job, foreign_key: :job_id) - belongs_to(:repo, Repo, foreign_key: :repo_id) + + article_belongs_to() timestamps(type: :utc_datetime) end @@ -29,11 +29,12 @@ defmodule GroupherServer.CMS.ArticleUpvote do @doc false def changeset(%ArticleUpvote{} = article_upvote, attrs) do article_upvote - |> cast(attrs, @optional_fields ++ @required_fields) + |> cast(attrs, @optional_fields ++ @required_fields ++ @article_fields) |> validate_required(@required_fields) |> foreign_key_constraint(:user_id) - |> foreign_key_constraint(:post_id) - |> foreign_key_constraint(:job_id) - |> foreign_key_constraint(:repo_id) + + # |> foreign_key_constraint(:post_id) + # |> foreign_key_constraint(:job_id) + # |> foreign_key_constraint(:repo_id) end end diff --git a/lib/groupher_server/cms/article_user_emotion.ex b/lib/groupher_server/cms/article_user_emotion.ex index 23a387940..7e4f68496 100644 --- a/lib/groupher_server/cms/article_user_emotion.ex +++ b/lib/groupher_server/cms/article_user_emotion.ex @@ -50,8 +50,8 @@ defmodule GroupherServer.CMS.ArticleUserEmotion do struct |> cast(attrs, @required_fields ++ @optional_fields) |> validate_required(@required_fields) - |> foreign_key_constraint(:post_id) - |> foreign_key_constraint(:job_id) + # |> foreign_key_constraint(:post_id) + # |> foreign_key_constraint(:job_id) |> foreign_key_constraint(:user_id) |> foreign_key_constraint(:recived_user_id) end @@ -60,8 +60,8 @@ defmodule GroupherServer.CMS.ArticleUserEmotion do struct |> cast(attrs, @required_fields ++ @optional_fields) |> validate_required(@required_fields) - |> foreign_key_constraint(:post_id) - |> foreign_key_constraint(:job_id) + # |> foreign_key_constraint(:post_id) + # |> foreign_key_constraint(:job_id) |> foreign_key_constraint(:user_id) |> foreign_key_constraint(:recived_user_id) end diff --git a/lib/groupher_server/cms/helper/macros.ex b/lib/groupher_server/cms/helper/macros.ex index cd40f2eb6..b54c830ef 100644 --- a/lib/groupher_server/cms/helper/macros.ex +++ b/lib/groupher_server/cms/helper/macros.ex @@ -18,10 +18,11 @@ defmodule GroupherServer.CMS.Helper.Macros do defmacro article_belongs_to() do @article_threads |> Enum.map(fn thread -> - thread_module = unquote(thread) |> to_string |> Recase.to_pascal() - quote do - belongs_to(unquote(thread), Module.concat(CMS, thread_module), + # thread_module = unquote(thread) |> to_string |> Recase.to_pascal() + belongs_to( + unquote(thread), + Module.concat(CMS, unquote(thread) |> to_string |> Recase.to_pascal()), foreign_key: unquote(:"#{thread}_id") ) end diff --git a/lib/groupher_server/cms/pinned_article.ex b/lib/groupher_server/cms/pinned_article.ex index 1fd93c3c3..e328f5451 100644 --- a/lib/groupher_server/cms/pinned_article.ex +++ b/lib/groupher_server/cms/pinned_article.ex @@ -4,37 +4,36 @@ defmodule GroupherServer.CMS.PinnedArticle do use Ecto.Schema import Ecto.Changeset + import GroupherServer.CMS.Helper.Macros alias GroupherServer.CMS - alias CMS.{Community, Post, Job, Repo} + alias CMS.Community + + @article_threads CMS.Community.article_threads() @required_fields ~w(community_id thread)a - @optional_fields ~w(post_id job_id repo_id)a + # @optional_fields ~w(post_id job_id repo_id)a + @article_fields @article_threads |> Enum.map(&:"#{&1}_id") @type t :: %PinnedArticle{} schema "pinned_articles" do - belongs_to(:post, Post, foreign_key: :post_id) - belongs_to(:job, Job, foreign_key: :job_id) - belongs_to(:repo, Repo, foreign_key: :repo_id) belongs_to(:community, Community, foreign_key: :community_id) - field(:thread, :string) + article_belongs_to() timestamps(type: :utc_datetime) end @doc false def changeset(%PinnedArticle{} = pinned_article, attrs) do pinned_article - |> cast(attrs, @optional_fields ++ @required_fields) + |> cast(attrs, @article_fields ++ @required_fields) |> validate_required(@required_fields) - |> foreign_key_constraint(:post_id) - |> foreign_key_constraint(:job_id) - |> foreign_key_constraint(:repo_id) + # |> foreign_key_constraint(:post_id) + # |> foreign_key_constraint(:job_id) + # |> foreign_key_constraint(:repo_id) |> foreign_key_constraint(:community_id) - |> unique_constraint(:pinned_articles, name: :pinned_articles_post_id_community_id_index) - # |> unique_constraint(:pinned_articles, name: :pinned_articles_job_id_community_id_index) - # |> unique_constraint(:pinned_articles, name: :pinned_articles_repo_id_community_id_index) + # |> unique_constraint(:pinned_articles, name: :pinned_articles_post_id_community_id_index) end end diff --git a/test/groupher_server/accounts/collect_folder_test.exs b/test/groupher_server/accounts/collect_folder_test.exs index 398ab0c13..93774b6d6 100644 --- a/test/groupher_server/accounts/collect_folder_test.exs +++ b/test/groupher_server/accounts/collect_folder_test.exs @@ -215,6 +215,7 @@ defmodule GroupherServer.Test.Accounts.CollectFolder do assert result.total_count == 0 end + @tag :wip2 test "add post to exsit colect-folder should update meta", ~m(user post post2 job)a do {:ok, folder} = Accounts.create_collect_folder(%{title: "test folder"}, user) diff --git a/test/groupher_server_web/mutation/cms/article_community/job_test.exs b/test/groupher_server_web/mutation/cms/article_community/job_test.exs index 539e30262..4136750b5 100644 --- a/test/groupher_server_web/mutation/cms/article_community/job_test.exs +++ b/test/groupher_server_web/mutation/cms/article_community/job_test.exs @@ -113,7 +113,7 @@ defmodule GroupherServer.Test.Mutation.ArticleCommunity.Job do end end - describe "[mirror/unmirror/move psot to/from community]" do + describe "[mirror/unmirror/move job to/from community]" do @mirror_article_query """ mutation($id: ID!, $thread: Thread, $communityId: ID!) { mirrorArticle(id: $id, thread: $thread, communityId: $communityId) { diff --git a/test/groupher_server_web/mutation/cms/article_community/post_test.exs b/test/groupher_server_web/mutation/cms/article_community/post_test.exs index 7906231bf..40dcce8d2 100644 --- a/test/groupher_server_web/mutation/cms/article_community/post_test.exs +++ b/test/groupher_server_web/mutation/cms/article_community/post_test.exs @@ -114,7 +114,7 @@ defmodule GroupherServer.Test.Mutation.ArticleCommunity.Post do end end - describe "[mirror/unmirror/move psot to/from community]" do + describe "[mirror/unmirror/move post to/from community]" do @mirror_article_query """ mutation($id: ID!, $thread: Thread, $communityId: ID!) { mirrorArticle(id: $id, thread: $thread, communityId: $communityId) { diff --git a/test/groupher_server_web/mutation/cms/article_community/repo_test.exs b/test/groupher_server_web/mutation/cms/article_community/repo_test.exs index fc865ee4d..f813993f5 100644 --- a/test/groupher_server_web/mutation/cms/article_community/repo_test.exs +++ b/test/groupher_server_web/mutation/cms/article_community/repo_test.exs @@ -114,7 +114,7 @@ defmodule GroupherServer.Test.Mutation.ArticleCommunity.Repo do end end - describe "[mirror/unmirror/move psot to/from community]" do + describe "[mirror/unmirror/move repo to/from community]" do @mirror_article_query """ mutation($id: ID!, $thread: Thread, $communityId: ID!) { mirrorArticle(id: $id, thread: $thread, communityId: $communityId) { diff --git a/test/groupher_server_web/query/cms/comments/repo_comment_test.exs b/test/groupher_server_web/query/cms/comments/repo_comment_test.exs index 3201b8d60..e0fa1099a 100644 --- a/test/groupher_server_web/query/cms/comments/repo_comment_test.exs +++ b/test/groupher_server_web/query/cms/comments/repo_comment_test.exs @@ -238,7 +238,6 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do assert results["totalCount"] == total_count end - @tag :wip2 test "guest user can get paged comment with pinned comment in it", ~m(guest_conn repo user)a do total_count = 20 From 318897dda89b16d288e49845600348b5a0f9f4d0 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Sun, 16 May 2021 22:25:38 +0800 Subject: [PATCH 3/5] refactor: general articles_foreign_key_constraint helper --- lib/groupher_server/cms/abuse_report.ex | 3 + lib/groupher_server/cms/article_collect.ex | 7 +-- lib/groupher_server/cms/article_comment.ex | 2 + .../cms/article_pinned_comment.ex | 4 +- lib/groupher_server/cms/article_upvote.ex | 7 +-- lib/groupher_server/cms/helper/macros.ex | 1 - lib/groupher_server/cms/helper/utils.ex | 24 ++++++++ lib/groupher_server/cms/pinned_article.ex | 7 +-- .../cms/articles/job_pin_test.exs | 55 +++++++++++++++++++ .../post_pin_test.exs} | 16 ++---- .../cms/articles/repo_pin_test.exs | 55 +++++++++++++++++++ 11 files changed, 152 insertions(+), 29 deletions(-) create mode 100644 lib/groupher_server/cms/helper/utils.ex create mode 100644 test/groupher_server/cms/articles/job_pin_test.exs rename test/groupher_server/cms/{article_pin_test.exs => articles/post_pin_test.exs} (82%) create mode 100644 test/groupher_server/cms/articles/repo_pin_test.exs diff --git a/lib/groupher_server/cms/abuse_report.ex b/lib/groupher_server/cms/abuse_report.ex index 969f83e54..24a10cf60 100644 --- a/lib/groupher_server/cms/abuse_report.ex +++ b/lib/groupher_server/cms/abuse_report.ex @@ -6,6 +6,7 @@ defmodule GroupherServer.CMS.AbuseReport do use Accessible import Ecto.Changeset import GroupherServer.CMS.Helper.Macros + import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1] alias GroupherServer.{Accounts, CMS} alias CMS.{ArticleComment, Embeds} @@ -39,11 +40,13 @@ defmodule GroupherServer.CMS.AbuseReport do struct |> cast(attrs, @optional_fields ++ @article_fields) |> cast_embed(:report_cases, required: true, with: &Embeds.AbuseReportCase.changeset/2) + |> articles_foreign_key_constraint end def update_changeset(%AbuseReport{} = struct, attrs) do struct |> cast(attrs, @update_fields ++ @article_fields) |> cast_embed(:report_cases, required: true, with: &Embeds.AbuseReportCase.changeset/2) + |> articles_foreign_key_constraint end end diff --git a/lib/groupher_server/cms/article_collect.ex b/lib/groupher_server/cms/article_collect.ex index ea61f5fce..92b9f7c78 100644 --- a/lib/groupher_server/cms/article_collect.ex +++ b/lib/groupher_server/cms/article_collect.ex @@ -5,6 +5,7 @@ defmodule GroupherServer.CMS.ArticleCollect do use Ecto.Schema import Ecto.Changeset import GroupherServer.CMS.Helper.Macros + import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1] alias GroupherServer.{Accounts, CMS} alias Accounts.{User, CollectFolder} @@ -23,7 +24,6 @@ defmodule GroupherServer.CMS.ArticleCollect do embeds_many(:collect_folders, CollectFolder, on_replace: :delete) article_belongs_to() - timestamps(type: :utc_datetime) end @@ -34,9 +34,6 @@ defmodule GroupherServer.CMS.ArticleCollect do |> validate_required(@required_fields) |> cast_embed(:collect_folders, with: &CollectFolder.changeset/2) |> foreign_key_constraint(:user_id) - - # |> foreign_key_constraint(:post_id) - # |> foreign_key_constraint(:job_id) - # |> foreign_key_constraint(:repo_id) + |> articles_foreign_key_constraint end end diff --git a/lib/groupher_server/cms/article_comment.ex b/lib/groupher_server/cms/article_comment.ex index 375ed3b6b..294938501 100644 --- a/lib/groupher_server/cms/article_comment.ex +++ b/lib/groupher_server/cms/article_comment.ex @@ -7,6 +7,7 @@ defmodule GroupherServer.CMS.ArticleComment do import Ecto.Changeset import GroupherServer.CMS.Helper.Macros + import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1] alias GroupherServer.{Accounts, CMS} alias CMS.{Embeds, ArticleCommentUpvote} @@ -102,6 +103,7 @@ defmodule GroupherServer.CMS.ArticleComment do defp generl_changeset(content) do content |> foreign_key_constraint(:author_id) + |> articles_foreign_key_constraint # |> validate_length(:body_html, min: 3, max: 2000) # |> HTML.safe_string(:body_html) diff --git a/lib/groupher_server/cms/article_pinned_comment.ex b/lib/groupher_server/cms/article_pinned_comment.ex index a3c2af039..14c64b810 100644 --- a/lib/groupher_server/cms/article_pinned_comment.ex +++ b/lib/groupher_server/cms/article_pinned_comment.ex @@ -7,6 +7,7 @@ defmodule GroupherServer.CMS.ArticlePinnedComment do import Ecto.Changeset import GroupherServer.CMS.Helper.Macros + import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1] alias GroupherServer.CMS alias CMS.ArticleComment @@ -24,7 +25,6 @@ defmodule GroupherServer.CMS.ArticlePinnedComment do belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id) article_belongs_to() - timestamps(type: :utc_datetime) end @@ -33,11 +33,13 @@ defmodule GroupherServer.CMS.ArticlePinnedComment do article_pined_comment |> cast(attrs, @required_fields ++ @article_fields) |> validate_required(@required_fields) + |> articles_foreign_key_constraint end # @doc false def update_changeset(%ArticlePinnedComment{} = article_pined_comment, attrs) do article_pined_comment |> cast(attrs, @required_fields ++ @article_fields) + |> articles_foreign_key_constraint end end diff --git a/lib/groupher_server/cms/article_upvote.ex b/lib/groupher_server/cms/article_upvote.ex index e44c6ef06..f8447da10 100644 --- a/lib/groupher_server/cms/article_upvote.ex +++ b/lib/groupher_server/cms/article_upvote.ex @@ -5,6 +5,7 @@ defmodule GroupherServer.CMS.ArticleUpvote do use Ecto.Schema import Ecto.Changeset import GroupherServer.CMS.Helper.Macros + import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1] alias GroupherServer.{Accounts, CMS} alias Accounts.User @@ -22,7 +23,6 @@ defmodule GroupherServer.CMS.ArticleUpvote do belongs_to(:user, User, foreign_key: :user_id) article_belongs_to() - timestamps(type: :utc_datetime) end @@ -32,9 +32,6 @@ defmodule GroupherServer.CMS.ArticleUpvote do |> cast(attrs, @optional_fields ++ @required_fields ++ @article_fields) |> validate_required(@required_fields) |> foreign_key_constraint(:user_id) - - # |> foreign_key_constraint(:post_id) - # |> foreign_key_constraint(:job_id) - # |> foreign_key_constraint(:repo_id) + |> articles_foreign_key_constraint end end diff --git a/lib/groupher_server/cms/helper/macros.ex b/lib/groupher_server/cms/helper/macros.ex index b54c830ef..1bec4b5f4 100644 --- a/lib/groupher_server/cms/helper/macros.ex +++ b/lib/groupher_server/cms/helper/macros.ex @@ -19,7 +19,6 @@ defmodule GroupherServer.CMS.Helper.Macros do @article_threads |> Enum.map(fn thread -> quote do - # thread_module = unquote(thread) |> to_string |> Recase.to_pascal() belongs_to( unquote(thread), Module.concat(CMS, unquote(thread) |> to_string |> Recase.to_pascal()), diff --git a/lib/groupher_server/cms/helper/utils.ex b/lib/groupher_server/cms/helper/utils.ex new file mode 100644 index 000000000..7d2acfca3 --- /dev/null +++ b/lib/groupher_server/cms/helper/utils.ex @@ -0,0 +1,24 @@ +defmodule GroupherServer.CMS.Helper.Utils do + @moduledoc """ + utils for CMS helper + """ + import Ecto.Changeset + + alias GroupherServer.CMS + alias CMS.Community + + @article_threads CMS.Community.article_threads() + @article_fields @article_threads |> Enum.map(&:"#{&1}_id") + + @doc """ + foreign_key_constraint for artilces thread + + e.g + foreign_key_constraint(struct, :post_id) + """ + def articles_foreign_key_constraint(%Ecto.Changeset{} = changeset) do + Enum.reduce(@article_fields, changeset, fn thread_id, acc -> + foreign_key_constraint(acc, thread_id) + end) + end +end diff --git a/lib/groupher_server/cms/pinned_article.ex b/lib/groupher_server/cms/pinned_article.ex index e328f5451..43fea5d31 100644 --- a/lib/groupher_server/cms/pinned_article.ex +++ b/lib/groupher_server/cms/pinned_article.ex @@ -5,6 +5,7 @@ defmodule GroupherServer.CMS.PinnedArticle do use Ecto.Schema import Ecto.Changeset import GroupherServer.CMS.Helper.Macros + import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1] alias GroupherServer.CMS alias CMS.Community @@ -29,11 +30,7 @@ defmodule GroupherServer.CMS.PinnedArticle do pinned_article |> cast(attrs, @article_fields ++ @required_fields) |> validate_required(@required_fields) - # |> foreign_key_constraint(:post_id) - # |> foreign_key_constraint(:job_id) - # |> foreign_key_constraint(:repo_id) |> foreign_key_constraint(:community_id) - - # |> unique_constraint(:pinned_articles, name: :pinned_articles_post_id_community_id_index) + |> articles_foreign_key_constraint end end diff --git a/test/groupher_server/cms/articles/job_pin_test.exs b/test/groupher_server/cms/articles/job_pin_test.exs new file mode 100644 index 000000000..3e1654979 --- /dev/null +++ b/test/groupher_server/cms/articles/job_pin_test.exs @@ -0,0 +1,55 @@ +defmodule GroupherServer.Test.CMS.Artilces.JobPin do + @moduledoc false + + use GroupherServer.TestTools + + alias Helper.ORM + alias GroupherServer.CMS + + alias CMS.{Community, PinnedArticle} + + @max_pinned_article_count_per_thread Community.max_pinned_article_count_per_thread() + + setup do + {:ok, user} = db_insert(:user) + {:ok, community} = db_insert(:community) + + {:ok, job} = CMS.create_article(community, :job, mock_attrs(:job), user) + + {:ok, ~m(user community job)a} + end + + describe "[cms job pin]" do + test "can pin a job", ~m(community job)a do + {:ok, _} = CMS.pin_article(:job, job.id, community.id) + {:ok, pind_article} = ORM.find_by(PinnedArticle, %{job_id: job.id}) + + assert pind_article.job_id == job.id + end + + test "one community & thread can only pin certern count of job", ~m(community user)a do + Enum.reduce(1..@max_pinned_article_count_per_thread, [], fn _, acc -> + {:ok, new_job} = CMS.create_article(community, :job, mock_attrs(:job), user) + {:ok, _} = CMS.pin_article(:job, new_job.id, community.id) + acc + end) + + {:ok, new_job} = CMS.create_article(community, :job, mock_attrs(:job), user) + {:error, reason} = CMS.pin_article(:job, new_job.id, community.id) + assert reason |> Keyword.get(:code) == ecode(:too_much_pinned_article) + end + + @tag :wip2 + test "can not pin a non-exsit job", ~m(community)a do + assert {:error, _} = CMS.pin_article(:job, 8848, community.id) + end + + test "can undo pin to a job", ~m(community job)a do + {:ok, _} = CMS.pin_article(:job, job.id, community.id) + + assert {:ok, unpinned} = CMS.undo_pin_article(:job, job.id, community.id) + + assert {:error, _} = ORM.find_by(PinnedArticle, %{job_id: job.id}) + end + end +end diff --git a/test/groupher_server/cms/article_pin_test.exs b/test/groupher_server/cms/articles/post_pin_test.exs similarity index 82% rename from test/groupher_server/cms/article_pin_test.exs rename to test/groupher_server/cms/articles/post_pin_test.exs index b2a144340..26775596e 100644 --- a/test/groupher_server/cms/article_pin_test.exs +++ b/test/groupher_server/cms/articles/post_pin_test.exs @@ -1,4 +1,4 @@ -defmodule GroupherServer.Test.CMS.ArticlePin do +defmodule GroupherServer.Test.CMS.Artilces.PostPin do @moduledoc false use GroupherServer.TestTools @@ -6,12 +6,7 @@ defmodule GroupherServer.Test.CMS.ArticlePin do alias Helper.ORM alias GroupherServer.CMS - alias CMS.{ - Community, - PinnedArticle - # Post, - # Job - } + alias CMS.{Community, PinnedArticle} @max_pinned_article_count_per_thread Community.max_pinned_article_count_per_thread() @@ -20,10 +15,8 @@ defmodule GroupherServer.Test.CMS.ArticlePin do {:ok, community} = db_insert(:community) {:ok, post} = CMS.create_article(community, :post, mock_attrs(:post), user) - {:ok, job} = CMS.create_article(community, :job, mock_attrs(:job), user) - {:ok, repo} = CMS.create_article(community, :repo, mock_attrs(:repo), user) - {:ok, ~m(user community post job repo)a} + {:ok, ~m(user community post)a} end describe "[cms post pin]" do @@ -46,6 +39,7 @@ defmodule GroupherServer.Test.CMS.ArticlePin do assert reason |> Keyword.get(:code) == ecode(:too_much_pinned_article) end + @tag :wip2 test "can not pin a non-exsit post", ~m(community)a do assert {:error, _} = CMS.pin_article(:post, 8848, community.id) end @@ -58,6 +52,4 @@ defmodule GroupherServer.Test.CMS.ArticlePin do assert {:error, _} = ORM.find_by(PinnedArticle, %{post_id: post.id}) end end - - # TODO: Job, Repo end diff --git a/test/groupher_server/cms/articles/repo_pin_test.exs b/test/groupher_server/cms/articles/repo_pin_test.exs new file mode 100644 index 000000000..7577847dd --- /dev/null +++ b/test/groupher_server/cms/articles/repo_pin_test.exs @@ -0,0 +1,55 @@ +defmodule GroupherServer.Test.CMS.Artilces.RepoPin do + @moduledoc false + + use GroupherServer.TestTools + + alias Helper.ORM + alias GroupherServer.CMS + + alias CMS.{Community, PinnedArticle} + + @max_pinned_article_count_per_thread Community.max_pinned_article_count_per_thread() + + setup do + {:ok, user} = db_insert(:user) + {:ok, community} = db_insert(:community) + + {:ok, repo} = CMS.create_article(community, :repo, mock_attrs(:repo), user) + + {:ok, ~m(user community repo)a} + end + + describe "[cms repo pin]" do + test "can pin a repo", ~m(community repo)a do + {:ok, _} = CMS.pin_article(:repo, repo.id, community.id) + {:ok, pind_article} = ORM.find_by(PinnedArticle, %{repo_id: repo.id}) + + assert pind_article.repo_id == repo.id + end + + test "one community & thread can only pin certern count of repo", ~m(community user)a do + Enum.reduce(1..@max_pinned_article_count_per_thread, [], fn _, acc -> + {:ok, new_repo} = CMS.create_article(community, :repo, mock_attrs(:repo), user) + {:ok, _} = CMS.pin_article(:repo, new_repo.id, community.id) + acc + end) + + {:ok, new_repo} = CMS.create_article(community, :repo, mock_attrs(:repo), user) + {:error, reason} = CMS.pin_article(:repo, new_repo.id, community.id) + assert reason |> Keyword.get(:code) == ecode(:too_much_pinned_article) + end + + @tag :wip2 + test "can not pin a non-exsit repo", ~m(community)a do + assert {:error, _} = CMS.pin_article(:repo, 8848, community.id) + end + + test "can undo pin to a repo", ~m(community repo)a do + {:ok, _} = CMS.pin_article(:repo, repo.id, community.id) + + assert {:ok, unpinned} = CMS.undo_pin_article(:repo, repo.id, community.id) + + assert {:error, _} = ORM.find_by(PinnedArticle, %{repo_id: repo.id}) + end + end +end From 682a9845766a5691073fec17be2b7672283236e7 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Sun, 16 May 2021 22:38:42 +0800 Subject: [PATCH 4/5] refactor(cms-macros): clean up warnings --- .../mutation/cms/article_community/job_test.exs | 2 -- .../mutation/cms/article_community/post_test.exs | 2 -- .../mutation/cms/article_community/repo_test.exs | 2 -- .../query/cms/paged_articles/paged_posts_test.exs | 2 +- 4 files changed, 1 insertion(+), 7 deletions(-) diff --git a/test/groupher_server_web/mutation/cms/article_community/job_test.exs b/test/groupher_server_web/mutation/cms/article_community/job_test.exs index 4136750b5..95995ec3c 100644 --- a/test/groupher_server_web/mutation/cms/article_community/job_test.exs +++ b/test/groupher_server_web/mutation/cms/article_community/job_test.exs @@ -213,8 +213,6 @@ defmodule GroupherServer.Test.Mutation.ArticleCommunity.Job do } } """ - 3 - test "auth user can move job to other community", ~m(job)a do passport_rules = %{"job.community.mirror" => true} rule_conn = simu_conn(:user, cms: passport_rules) diff --git a/test/groupher_server_web/mutation/cms/article_community/post_test.exs b/test/groupher_server_web/mutation/cms/article_community/post_test.exs index 40dcce8d2..990ad57dd 100644 --- a/test/groupher_server_web/mutation/cms/article_community/post_test.exs +++ b/test/groupher_server_web/mutation/cms/article_community/post_test.exs @@ -214,8 +214,6 @@ defmodule GroupherServer.Test.Mutation.ArticleCommunity.Post do } } """ - 3 - test "auth user can move post to other community", ~m(post)a do passport_rules = %{"post.community.mirror" => true} rule_conn = simu_conn(:user, cms: passport_rules) diff --git a/test/groupher_server_web/mutation/cms/article_community/repo_test.exs b/test/groupher_server_web/mutation/cms/article_community/repo_test.exs index f813993f5..c82845517 100644 --- a/test/groupher_server_web/mutation/cms/article_community/repo_test.exs +++ b/test/groupher_server_web/mutation/cms/article_community/repo_test.exs @@ -214,8 +214,6 @@ defmodule GroupherServer.Test.Mutation.ArticleCommunity.Repo do } } """ - 3 - test "auth user can move repo to other community", ~m(repo)a do passport_rules = %{"repo.community.mirror" => true} rule_conn = simu_conn(:user, cms: passport_rules) 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 16b6f7012..6d88fbfed 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,7 @@ defmodule GroupherServer.Test.Query.PagedArticles.PagedPosts do assert results["entries"] |> Enum.any?(&(&1["id"] == to_string(post.id))) end - @tag :skip_travis + @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") From c1bd515dedecdf02a52b8d03178f60aabbeea1bb Mon Sep 17 00:00:00 2001 From: mydearxym Date: Sun, 16 May 2021 22:53:17 +0800 Subject: [PATCH 5/5] refactor(cms-macros): unused emotion config --- config/config.exs | 1 - lib/groupher_server/cms/article_comment.ex | 1 - lib/groupher_server/cms/article_user_emotion.ex | 16 +++++++--------- ...0210516144011_add_repo_support_in_emotion.exs | 9 +++++++++ .../cms/comments/post_comment_test.exs | 1 + 5 files changed, 17 insertions(+), 11 deletions(-) create mode 100644 priv/repo/migrations/20210516144011_add_repo_support_in_emotion.exs diff --git a/config/config.exs b/config/config.exs index 7a25dd0db..fcd5f1437 100644 --- a/config/config.exs +++ b/config/config.exs @@ -61,7 +61,6 @@ config :groupher_server, :customization, sidebar_communities_index: %{} config :groupher_server, :article, - emotionable_threads: [:post, :job], # NOTE: if you want to add/remove emotion, just edit the list below # and migrate the field to table "articles_users_emotions" supported_emotions: [ diff --git a/lib/groupher_server/cms/article_comment.ex b/lib/groupher_server/cms/article_comment.ex index 294938501..850e275f5 100644 --- a/lib/groupher_server/cms/article_comment.ex +++ b/lib/groupher_server/cms/article_comment.ex @@ -78,7 +78,6 @@ defmodule GroupherServer.CMS.ArticleComment do has_many(:upvotes, {"articles_comments_upvotes", ArticleCommentUpvote}) article_belongs_to() - timestamps(type: :utc_datetime) end diff --git a/lib/groupher_server/cms/article_user_emotion.ex b/lib/groupher_server/cms/article_user_emotion.ex index 7e4f68496..5198a26ac 100644 --- a/lib/groupher_server/cms/article_user_emotion.ex +++ b/lib/groupher_server/cms/article_user_emotion.ex @@ -23,25 +23,25 @@ defmodule GroupherServer.CMS.ArticleUserEmotion do import Ecto.Changeset import GroupherServer.CMS.ArticleUserEmotion.Macros import Helper.Utils, only: [get_config: 2] + import GroupherServer.CMS.Helper.Macros + import GroupherServer.CMS.Helper.Utils, only: [articles_foreign_key_constraint: 1] alias GroupherServer.{Accounts, CMS} - alias CMS.{Post, Job} @supported_emotions get_config(:article, :supported_emotions) - @supported_threads get_config(:article, :emotionable_threads) + @article_threads CMS.Community.article_threads() @required_fields ~w(user_id recived_user_id)a - @optional_fields Enum.map(@supported_threads, &:"#{&1}_id") ++ + @optional_fields Enum.map(@article_threads, &:"#{&1}_id") ++ Enum.map(@supported_emotions, &:"#{&1}") @type t :: %ArticleUserEmotion{} schema "articles_users_emotions" do - belongs_to(:post, Post, foreign_key: :post_id) - belongs_to(:job, Job, foreign_key: :job_id) belongs_to(:recived_user, Accounts.User, foreign_key: :recived_user_id) belongs_to(:user, Accounts.User, foreign_key: :user_id) emotion_fields() + article_belongs_to() timestamps(type: :utc_datetime) end @@ -50,19 +50,17 @@ defmodule GroupherServer.CMS.ArticleUserEmotion do struct |> cast(attrs, @required_fields ++ @optional_fields) |> validate_required(@required_fields) - # |> foreign_key_constraint(:post_id) - # |> foreign_key_constraint(:job_id) |> foreign_key_constraint(:user_id) |> foreign_key_constraint(:recived_user_id) + |> articles_foreign_key_constraint end def update_changeset(%ArticleUserEmotion{} = struct, attrs) do struct |> cast(attrs, @required_fields ++ @optional_fields) |> validate_required(@required_fields) - # |> foreign_key_constraint(:post_id) - # |> foreign_key_constraint(:job_id) |> foreign_key_constraint(:user_id) |> foreign_key_constraint(:recived_user_id) + |> articles_foreign_key_constraint end end diff --git a/priv/repo/migrations/20210516144011_add_repo_support_in_emotion.exs b/priv/repo/migrations/20210516144011_add_repo_support_in_emotion.exs new file mode 100644 index 000000000..7d21ec5c5 --- /dev/null +++ b/priv/repo/migrations/20210516144011_add_repo_support_in_emotion.exs @@ -0,0 +1,9 @@ +defmodule GroupherServer.Repo.Migrations.AddRepoSupportInEmotion do + use Ecto.Migration + + def change do + alter table(:articles_users_emotions) do + add(:repo_id, references(:cms_repos, on_delete: :delete_all)) + end + 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 69210d99a..1333ee4ca 100644 --- a/test/groupher_server/cms/comments/post_comment_test.exs +++ b/test/groupher_server/cms/comments/post_comment_test.exs @@ -350,6 +350,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do assert results.total_count == total_count + 1 end + @tag :wip2 test "paged article comments folded flag should be false", ~m(user post)a do total_count = 30 page_number = 1