From d8edbce00298de9a09ed4dcb2d30a7b4ca79d6db Mon Sep 17 00:00:00 2001 From: mydearxym Date: Fri, 30 Apr 2021 10:02:06 +0800 Subject: [PATCH] refactor(tag): remove refined concept --- .../cms/delegates/article_curd.ex | 10 +-- .../cms/delegates/article_operation.ex | 39 +-------- .../schema/cms/mutations/operation.ex | 26 ------ .../mutation/cms/job_test.exs | 62 -------------- .../mutation/cms/post_test.exs | 82 ------------------- 5 files changed, 3 insertions(+), 216 deletions(-) diff --git a/lib/groupher_server/cms/delegates/article_curd.ex b/lib/groupher_server/cms/delegates/article_curd.ex index a299b7ae9..01b272890 100644 --- a/lib/groupher_server/cms/delegates/article_curd.ex +++ b/lib/groupher_server/cms/delegates/article_curd.ex @@ -133,7 +133,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do ArticleOperation.update_edit_status(update_content) end) |> Multi.run(:update_tag, fn _, _ -> - # TODO: move it to ArticleOperation moudel + # TODO: move it to ArticleOperation module exec_update_tags(content, args) end) |> Repo.transaction() @@ -426,13 +426,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do Enum.reduce(tags_ids, [], fn t, acc -> {:ok, tag} = ORM.find(Tag, t.id) - case tag.title == "refined" do - true -> - acc - - false -> - acc ++ [tag] - end + acc ++ [tag] end) content diff --git a/lib/groupher_server/cms/delegates/article_operation.ex b/lib/groupher_server/cms/delegates/article_operation.ex index c01c14003..906f5e99c 100644 --- a/lib/groupher_server/cms/delegates/article_operation.ex +++ b/lib/groupher_server/cms/delegates/article_operation.ex @@ -121,20 +121,13 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do @doc """ set general tag for post / tuts ... - refined tag can't set by this func, use set_refined_tag instead """ # check community first def set_tag(thread, %Tag{id: tag_id}, content_id) do with {:ok, action} <- match_action(thread, :tag), {:ok, content} <- ORM.find(action.target, content_id, preload: :tags), {:ok, tag} <- ORM.find(action.reactor, tag_id) do - case tag.title != "refined" do - true -> - update_content_tag(content, tag) - - false -> - {:error, "use set_refined_tag instead"} - end + update_content_tag(content, tag) # NOTE: this should be control by Middleware # case tag_in_community_thread?(%Community{id: communitId}, thread, tag) do @@ -158,36 +151,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do end end - @doc """ - set refined_tag to common content - """ - def set_refined_tag(%Community{id: community_id}, thread, content_id) do - with {:ok, action} <- match_action(thread, :tag), - {:ok, content} <- ORM.find(action.target, content_id, preload: :tags), - {:ok, tag} <- - ORM.find_by(action.reactor, %{ - title: "refined", - community_id: community_id - }) do - update_content_tag(content, tag) - end - end - - @doc """ - unset refined_tag to common content - """ - def unset_refined_tag(%Community{id: community_id}, thread, content_id) do - with {:ok, action} <- match_action(thread, :tag), - {:ok, content} <- ORM.find(action.target, content_id, preload: :tags), - {:ok, tag} <- - ORM.find_by(action.reactor, %{ - title: "refined", - community_id: community_id - }) do - update_content_tag(content, tag, :drop) - end - end - defp update_content_tag(content, %Tag{} = tag, opt \\ :add) do new_tags = if opt == :add, do: content.tags ++ [tag], else: content.tags -- [tag] diff --git a/lib/groupher_server_web/schema/cms/mutations/operation.ex b/lib/groupher_server_web/schema/cms/mutations/operation.ex index a247dc750..fb6104ac5 100644 --- a/lib/groupher_server_web/schema/cms/mutations/operation.ex +++ b/lib/groupher_server_web/schema/cms/mutations/operation.ex @@ -93,19 +93,6 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Operation do resolve(&R.CMS.set_tag/3) end - @desc "set a refined tag to content" - field :set_refined_tag, :tag do - arg(:id, non_null(:id)) - arg(:community_id, non_null(:id)) - arg(:thread, :cms_thread, default_value: :post) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->t?.refinedtag.set") - - resolve(&R.CMS.set_refined_tag/3) - end - @desc "unset a tag to content" field :unset_tag, :tag do # thread id @@ -121,19 +108,6 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Operation do resolve(&R.CMS.unset_tag/3) end - @desc "unset a refined tag to content" - field :unset_refined_tag, :tag do - arg(:id, non_null(:id)) - arg(:community_id, non_null(:id)) - arg(:thread, :cms_thread, default_value: :post) - - middleware(M.Authorize, :login) - middleware(M.PassportLoader, source: :community) - middleware(M.Passport, claim: "cms->c?->t?.refinedtag.set") - - resolve(&R.CMS.unset_refined_tag/3) - end - # TODO: use community loader field :set_community, :community do arg(:id, non_null(:id)) diff --git a/test/groupher_server_web/mutation/cms/job_test.exs b/test/groupher_server_web/mutation/cms/job_test.exs index 7fafe0aee..2f0c8dd21 100644 --- a/test/groupher_server_web/mutation/cms/job_test.exs +++ b/test/groupher_server_web/mutation/cms/job_test.exs @@ -309,14 +309,6 @@ defmodule GroupherServer.Test.Mutation.Job do } } """ - @set_refined_tag_query """ - mutation($communityId: ID!, $thread: CmsThread, $id: ID!) { - setRefinedTag(communityId: $communityId, thread: $thread, id: $id) { - id - title - } - } - """ test "auth user can set a valid tag to job", ~m(job)a do {:ok, community} = db_insert(:community) {:ok, tag} = db_insert(:tag, %{thread: "job", community: community}) @@ -332,33 +324,6 @@ defmodule GroupherServer.Test.Mutation.Job do assert tag.id in assoc_tags end - test "can not set refined tag to job", ~m(job)a do - {:ok, community} = db_insert(:community) - {:ok, tag} = db_insert(:tag, %{thread: "job", community: community, title: "refined"}) - - passport_rules = %{community.title => %{"job.tag.set" => true}} - rule_conn = simu_conn(:user, cms: passport_rules) - - variables = %{id: job.id, tagId: tag.id, communityId: community.id} - - assert rule_conn |> mutation_get_error?(@set_tag_query, variables) - end - - test "auth user can set refined tag to job", ~m(job)a do - {:ok, community} = db_insert(:community) - {:ok, tag} = db_insert(:tag, %{thread: "job", community: community, title: "refined"}) - - passport_rules = %{community.title => %{"job.refinedtag.set" => true}} - rule_conn = simu_conn(:user, cms: passport_rules) - - variables = %{id: job.id, communityId: community.id, thread: "JOB"} - rule_conn |> mutation_result(@set_refined_tag_query, variables, "setRefinedTag") - {:ok, found} = ORM.find(CMS.Job, job.id, preload: :tags) - - assoc_tags = found.tags |> Enum.map(& &1.id) - assert tag.id in assoc_tags - end - # TODO: should fix in auth layer # test "auth user set a other community's tag to job fails", ~m(job)a do # {:ok, community} = db_insert(:community) @@ -391,32 +356,5 @@ defmodule GroupherServer.Test.Mutation.Job do assert tag.id in assoc_tags assert tag2.id in assoc_tags end - - @unset_refined_tag_query """ - mutation($communityId: ID!, $thread: CmsThread, $id: ID!) { - unsetRefinedTag(communityId: $communityId, thread: $thread, id: $id) { - id - title - } - } - """ - test "can unset refined tag to a job", ~m(job)a do - {:ok, community} = db_insert(:community) - {:ok, tag} = db_insert(:tag, %{thread: "job", community: community, title: "refined"}) - - passport_rules = %{community.title => %{"job.refinedtag.set" => true}} - rule_conn = simu_conn(:user, cms: passport_rules) - - variables = %{id: job.id, communityId: community.id, thread: "JOB"} - rule_conn |> mutation_result(@set_refined_tag_query, variables, "setRefinedTag") - - variables = %{id: job.id, communityId: community.id, thread: "JOB"} - rule_conn |> mutation_result(@unset_refined_tag_query, variables, "unsetRefinedTag") - - {:ok, found} = ORM.find(CMS.Job, job.id, preload: :tags) - - assoc_tags = found.tags |> Enum.map(& &1.id) - assert tag.id not in assoc_tags - end end end diff --git a/test/groupher_server_web/mutation/cms/post_test.exs b/test/groupher_server_web/mutation/cms/post_test.exs index 36e8191c3..8a8bd35d7 100644 --- a/test/groupher_server_web/mutation/cms/post_test.exs +++ b/test/groupher_server_web/mutation/cms/post_test.exs @@ -233,26 +233,6 @@ defmodule GroupherServer.Test.Mutation.Post do assert tag2.id in tag_ids end - test "can update post with refined tag", ~m(owner_conn post)a do - {:ok, tag_refined} = db_insert(:tag, %{title: "refined"}) - {:ok, tag2} = db_insert(:tag) - - unique_num = System.unique_integer([:positive, :monotonic]) - - variables = %{ - id: post.id, - title: "updated title #{unique_num}", - tags: [%{id: tag_refined.id}, %{id: tag2.id}] - } - - updated = owner_conn |> mutation_result(@query, variables, "updatePost") - {:ok, post} = ORM.find(CMS.Post, updated["id"], preload: :tags) - tag_ids = post.tags |> Utils.pick_by(:id) - - assert tag_refined.id not in tag_ids - assert tag2.id in tag_ids - end - test "post can be update by owner", ~m(owner_conn post)a do unique_num = System.unique_integer([:positive, :monotonic]) @@ -332,14 +312,6 @@ defmodule GroupherServer.Test.Mutation.Post do } } """ - @set_refined_tag_query """ - mutation($communityId: ID!, $thread: CmsThread, $id: ID!) { - setRefinedTag(communityId: $communityId, thread: $thread, id: $id) { - id - title - } - } - """ test "auth user can set a valid tag to post", ~m(post)a do {:ok, community} = db_insert(:community) {:ok, tag} = db_insert(:tag, %{thread: "post", community: community}) @@ -355,33 +327,6 @@ defmodule GroupherServer.Test.Mutation.Post do assert tag.id in assoc_tags end - test "can not set refined tag to post", ~m(post)a do - {:ok, community} = db_insert(:community) - {:ok, tag} = db_insert(:tag, %{thread: "post", community: community, title: "refined"}) - - passport_rules = %{community.title => %{"post.tag.set" => true}} - rule_conn = simu_conn(:user, cms: passport_rules) - - variables = %{id: post.id, tagId: tag.id} - - assert rule_conn |> mutation_get_error?(@set_tag_query, variables) - end - - test "auth user can set refined tag to post", ~m(post)a do - {:ok, community} = db_insert(:community) - {:ok, tag} = db_insert(:tag, %{thread: "post", community: community, title: "refined"}) - - passport_rules = %{community.title => %{"post.refinedtag.set" => true}} - rule_conn = simu_conn(:user, cms: passport_rules) - - variables = %{id: post.id, communityId: community.id} - rule_conn |> mutation_result(@set_refined_tag_query, variables, "setRefinedTag") - {:ok, found} = ORM.find(CMS.Post, post.id, preload: :tags) - - assoc_tags = found.tags |> Enum.map(& &1.id) - assert tag.id in assoc_tags - end - # TODO: should fix in auth layer # test "auth user set a other community tag to post fails", ~m(post)a do # {:ok, community} = db_insert(:community) @@ -423,14 +368,6 @@ defmodule GroupherServer.Test.Mutation.Post do } } """ - @unset_refined_tag_query """ - mutation($communityId: ID!, $thread: CmsThread, $id: ID!) { - unsetRefinedTag(communityId: $communityId, thread: $thread, id: $id) { - id - title - } - } - """ test "can unset tag to a post", ~m(post)a do {:ok, community} = db_insert(:community) @@ -463,25 +400,6 @@ defmodule GroupherServer.Test.Mutation.Post do assert tag.id not in assoc_tags assert tag2.id in assoc_tags end - - test "can unset refined tag to a post", ~m(post)a do - {:ok, community} = db_insert(:community) - {:ok, tag} = db_insert(:tag, %{thread: "post", community: community, title: "refined"}) - - passport_rules = %{community.title => %{"post.refinedtag.set" => true}} - rule_conn = simu_conn(:user, cms: passport_rules) - - variables = %{id: post.id, communityId: community.id} - rule_conn |> mutation_result(@set_refined_tag_query, variables, "setRefinedTag") - - variables = %{id: post.id, communityId: community.id} - rule_conn |> mutation_result(@unset_refined_tag_query, variables, "unsetRefinedTag") - - {:ok, found} = ORM.find(CMS.Post, post.id, preload: :tags) - - assoc_tags = found.tags |> Enum.map(& &1.id) - assert tag.id not in assoc_tags - end end describe "[mutation post community]" do