Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 085c66d

Browse files
committed
refactor: move logic adjust
1 parent 151f4e7 commit 085c66d

File tree

10 files changed

+77
-25
lines changed

10 files changed

+77
-25
lines changed

lib/groupher_server/cms/delegates/article_community.ex

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
145145
def move_article(thread, article_id, community_id) do
146146
with {:ok, info} <- match(thread),
147147
{:ok, community} <- ORM.find(Community, community_id),
148-
{:ok, article} <- ORM.find(info.model, article_id, preload: [:communities]) do
148+
{:ok, article} <-
149+
ORM.find(info.model, article_id, preload: [:communities, :original_community]) do
150+
cur_original_community = article.original_community
151+
149152
Multi.new()
150153
|> Multi.run(:change_original_community, fn _, _ ->
151154
article
@@ -156,15 +159,21 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
156159
|> Multi.run(:unmirror_article, fn _, %{change_original_community: article} ->
157160
article
158161
|> Ecto.Changeset.change()
159-
|> Ecto.Changeset.put_assoc(:communities, article.communities -- [community])
162+
|> Ecto.Changeset.put_assoc(:communities, article.communities -- [cur_original_community])
163+
|> Repo.update()
164+
end)
165+
|> Multi.run(:mirror_target_community, fn _, %{unmirror_article: article} ->
166+
article
167+
|> Ecto.Changeset.change()
168+
|> Ecto.Changeset.put_assoc(:communities, article.communities ++ [community])
160169
|> Repo.update()
161170
end)
162171
|> Repo.transaction()
163172
|> result()
164173
end
165174
end
166175

167-
defp result({:ok, %{change_original_community: result}}), do: result |> done()
176+
defp result({:ok, %{mirror_target_community: result}}), do: result |> done()
168177
defp result({:error, _, result, _steps}), do: {:error, result}
169178

170179
@doc """

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ defmodule GroupherServerWeb.Resolvers.CMS do
298298
CMS.unmirror_article(thread, id, community_id)
299299
end
300300

301+
def move_article(_root, ~m(thread id community_id)a, _info) do
302+
CMS.move_article(thread, id, community_id)
303+
end
304+
301305
# #######################
302306
# comemnts ..
303307
# #######################

lib/groupher_server_web/schema/cms/mutations/operation.ex

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Operation do
130130
resolve(&R.CMS.unmirror_article/3)
131131
end
132132

133-
# @desc "move article to other community"
134-
# field :move_article, :article do
135-
# arg(:id, non_null(:id))
136-
# arg(:community_id, non_null(:id))
137-
# arg(:thread, :thread, default_value: :post)
138-
139-
# middleware(M.Authorize, :login)
140-
# middleware(M.Passport, claim: "cms->t?.community.mirror")
141-
# resolve(&R.CMS.mirror_article/3)
142-
# end
133+
@desc "move article to other community"
134+
field :move_article, :article do
135+
arg(:id, non_null(:id))
136+
arg(:community_id, non_null(:id))
137+
arg(:thread, :thread, default_value: :post)
138+
139+
middleware(M.Authorize, :login)
140+
middleware(M.Passport, claim: "cms->t?.community.move")
141+
resolve(&R.CMS.move_article/3)
142+
end
143143
end
144144
end

lib/helper/certification.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ defmodule Helper.Certification do
5252
"category.unset",
5353
"thread.create",
5454
"post.community.mirror",
55+
"post.community.move",
5556
"post.community.unmirror",
5657
"job.community.mirror",
58+
"job.community.move",
5759
"job.community.unmirror",
5860
# flag on content
5961
"post.pin",

test/groupher_server/cms/article_community/job_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defmodule GroupherServer.Test.CMS.ArticleCommunity.Job do
3434
{:ok, job} = ORM.find(CMS.Job, job.id, preload: [:original_community, :communities])
3535

3636
assert job.original_community.id == community2.id
37-
assert is_nil(Enum.find(job.communities, &(&1.id == community2.id)))
37+
assert not is_nil(Enum.find(job.communities, &(&1.id == community2.id)))
3838
end
3939

4040
test "job can be mirror to other community", ~m(user community community2 job_attrs)a do

test/groupher_server/cms/article_community/post_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defmodule GroupherServer.Test.CMS.ArticleCommunity.Post do
3434
{:ok, post} = ORM.find(CMS.Post, post.id, preload: [:original_community, :communities])
3535

3636
assert post.original_community.id == community2.id
37-
assert is_nil(Enum.find(post.communities, &(&1.id == community2.id)))
37+
assert not is_nil(Enum.find(post.communities, &(&1.id == community2.id)))
3838
end
3939

4040
test "post can be mirror to other community", ~m(user community community2 post_attrs)a do

test/groupher_server/cms/article_community/repo_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ defmodule GroupherServer.Test.CMS.ArticleCommunity.Repo do
3434
{:ok, repo} = ORM.find(CMS.Repo, repo.id, preload: [:original_community, :communities])
3535

3636
assert repo.original_community.id == community2.id
37-
assert is_nil(Enum.find(repo.communities, &(&1.id == community2.id)))
37+
assert not is_nil(Enum.find(repo.communities, &(&1.id == community2.id)))
3838
end
3939

4040
test "repo can be mirror to other community", ~m(user community community2 repo_attrs)a do

test/groupher_server_web/mutation/cms/article_community/post_test.exs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
defmodule GroupherServer.Test.Mutation.Articles.Post do
1+
defmodule GroupherServer.Test.Mutation.ArticleCommunity.Post do
22
use GroupherServer.TestTools
33

4-
alias Helper.{ORM, Utils}
5-
alias GroupherServer.{CMS, Delivery}
4+
alias Helper.ORM
5+
alias GroupherServer.CMS
66

77
setup do
88
{:ok, post} = db_insert(:post)
@@ -122,7 +122,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Post do
122122
}
123123
}
124124
"""
125-
test "auth user can set a community to post", ~m(post)a do
125+
test "auth user can mirror a post to other community", ~m(post)a do
126126
passport_rules = %{"post.community.mirror" => true}
127127
rule_conn = simu_conn(:user, cms: passport_rules)
128128

@@ -135,7 +135,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Post do
135135
assert community.id in assoc_communities
136136
end
137137

138-
test "unauth user set a community to post fails", ~m(user_conn guest_conn post)a do
138+
test "unauth user cannot mirror a post to a community", ~m(user_conn guest_conn post)a do
139139
{:ok, community} = db_insert(:community)
140140
variables = %{id: post.id, communityId: community.id}
141141
rule_conn = simu_conn(:user, cms: %{"what.ever" => true})
@@ -150,7 +150,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Post do
150150
|> mutation_get_error?(@mirror_article_query, variables, ecode(:passport))
151151
end
152152

153-
test "auth user can set multi community to a post", ~m(post)a do
153+
test "auth user can mirror multi post to other communities", ~m(post)a do
154154
passport_rules = %{"post.community.mirror" => true}
155155
rule_conn = simu_conn(:user, cms: passport_rules)
156156

@@ -178,7 +178,7 @@ defmodule GroupherServer.Test.Mutation.Articles.Post do
178178
}
179179
"""
180180
@tag :wip2
181-
test "auth user can unset community from a post", ~m(post)a do
181+
test "auth user can unmirror post to a community", ~m(post)a do
182182
passport_rules = %{"post.community.mirror" => true}
183183
rule_conn = simu_conn(:user, cms: passport_rules)
184184

@@ -206,5 +206,42 @@ defmodule GroupherServer.Test.Mutation.Articles.Post do
206206
assert community.id not in assoc_communities
207207
assert community2.id in assoc_communities
208208
end
209+
210+
@move_article_query """
211+
mutation($id: ID!, $communityId: ID!) {
212+
moveArticle(id: $id, communityId: $communityId) {
213+
id
214+
}
215+
}
216+
"""
217+
@tag :wip2
218+
test "auth user can move post to other community", ~m(post)a do
219+
passport_rules = %{"post.community.mirror" => true}
220+
rule_conn = simu_conn(:user, cms: passport_rules)
221+
222+
{:ok, community} = db_insert(:community)
223+
{:ok, community2} = db_insert(:community)
224+
225+
variables = %{id: post.id, communityId: community.id}
226+
rule_conn |> mutation_result(@mirror_article_query, variables, "mirrorArticle")
227+
{:ok, found} = ORM.find(CMS.Post, post.id, preload: [:original_community, :communities])
228+
assoc_communities = found.communities |> Enum.map(& &1.id)
229+
assert community.id in assoc_communities
230+
231+
passport_rules = %{"post.community.move" => true}
232+
rule_conn = simu_conn(:user, cms: passport_rules)
233+
234+
pre_original_community_id = found.original_community.id
235+
236+
variables = %{id: post.id, communityId: community2.id}
237+
rule_conn |> mutation_result(@move_article_query, variables, "moveArticle")
238+
{:ok, found} = ORM.find(CMS.Post, post.id, preload: [:original_community, :communities])
239+
assoc_communities = found.communities |> Enum.map(& &1.id)
240+
assert pre_original_community_id not in assoc_communities
241+
assert community2.id in assoc_communities
242+
assert community2.id == found.original_community_id
243+
244+
assert found.original_community.id == community2.id
245+
end
209246
end
210247
end

test/groupher_server_web/query/cms/abuse_reports/job_report_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ defmodule GroupherServer.Test.Query.AbuseReports.JobReport do
9191
assert results["totalCount"] == 1
9292
end
9393

94-
@tag :wip3
94+
@tag :wip2
9595
test "support article_comment", ~m(guest_conn job user)a do
9696
{:ok, comment} = CMS.create_article_comment(:job, job.id, "comment", user)
9797
{:ok, _} = CMS.report_article_comment(comment.id, "reason", "attr", user)

test/groupher_server_web/query/cms/abuse_reports/post_report_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ defmodule GroupherServer.Test.Query.AbuseReports.PostReport do
9191
assert results["totalCount"] == 1
9292
end
9393

94-
@tag :wip3
94+
@tag :wip2
9595
test "support article_comment", ~m(guest_conn post user)a do
9696
{:ok, comment} = CMS.create_article_comment(:post, post.id, "comment", user)
9797
{:ok, _} = CMS.report_article_comment(comment.id, "reason", "attr", user)

0 commit comments

Comments
 (0)