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

Commit 5b592b4

Browse files
authored
refactor(extract): article action mutations (#344)
* refactor(cms-mutations): extract pin/undo_pin mutation * refactor(cms-mutations): extract trash/undo_trash mutation * refactor(cms-mutations): extract delete mutation
1 parent 520b918 commit 5b592b4

File tree

9 files changed

+113
-179
lines changed

9 files changed

+113
-179
lines changed

lib/groupher_server_web/schema/Helper/mutations.ex

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,78 @@ defmodule GroupherServerWeb.Schema.Helper.Mutations do
2626
end
2727
end
2828
end
29+
30+
defmacro article_pin_mutation(thread) do
31+
quote do
32+
@desc unquote("pin to #{thread}")
33+
field unquote(:"pin_#{thread}"), unquote(thread) do
34+
arg(:id, non_null(:id))
35+
arg(:community_id, non_null(:id))
36+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
37+
38+
middleware(M.Authorize, :login)
39+
middleware(M.PassportLoader, source: :community)
40+
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.pin"))
41+
resolve(&R.CMS.pin_article/3)
42+
end
43+
44+
@desc unquote("undo pin to #{thread}")
45+
field unquote(:"undo_pin_#{thread}"), unquote(thread) do
46+
arg(:id, non_null(:id))
47+
arg(:community_id, non_null(:id))
48+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
49+
50+
middleware(M.Authorize, :login)
51+
middleware(M.PassportLoader, source: :community)
52+
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.undo_pin"))
53+
resolve(&R.CMS.undo_pin_article/3)
54+
end
55+
end
56+
end
57+
58+
defmacro article_trash_mutation(thread) do
59+
quote do
60+
@desc unquote("trash a #{thread}, not delete")
61+
field unquote(:"trash_#{thread}"), unquote(thread) do
62+
arg(:id, non_null(:id))
63+
arg(:community_id, non_null(:id))
64+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
65+
66+
middleware(M.Authorize, :login)
67+
middleware(M.PassportLoader, source: :community)
68+
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.trash"))
69+
70+
resolve(&R.CMS.trash_content/3)
71+
end
72+
73+
@desc unquote("undo trash a #{thread}, not delete")
74+
field unquote(:"undo_trash_#{thread}"), unquote(thread) do
75+
arg(:id, non_null(:id))
76+
arg(:community_id, non_null(:id))
77+
arg(:thread, unquote(:"#{thread}_thread"), default_value: unquote(thread))
78+
79+
middleware(M.Authorize, :login)
80+
middleware(M.PassportLoader, source: :community)
81+
middleware(M.Passport, claim: unquote("cms->c?->#{to_string(thread)}.undo_trash"))
82+
83+
resolve(&R.CMS.undo_trash_content/3)
84+
end
85+
end
86+
end
87+
88+
# TODO: if post belongs to multi communities, unset instead delete
89+
defmacro article_delete_mutation(thread) do
90+
quote do
91+
@desc unquote("delete a #{thread}, not delete")
92+
field unquote(:"delete_#{thread}"), unquote(thread) do
93+
arg(:id, non_null(:id))
94+
95+
middleware(M.Authorize, :login)
96+
middleware(M.PassportLoader, source: unquote(thread))
97+
middleware(M.Passport, claim: unquote("owner;cms->c?->#{to_string(thread)}.delete"))
98+
99+
resolve(&R.CMS.delete_content/3)
100+
end
101+
end
102+
end
29103
end

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

Lines changed: 3 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -72,67 +72,9 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Job do
7272

7373
#############
7474
article_upvote_mutation(:job)
75+
article_pin_mutation(:job)
76+
article_trash_mutation(:job)
77+
article_delete_mutation(:job)
7578
#############
76-
77-
@desc "pin a job"
78-
field :pin_job, :job do
79-
arg(:id, non_null(:id))
80-
arg(:thread, :job_thread, default_value: :job)
81-
arg(:community_id, non_null(:id))
82-
83-
middleware(M.Authorize, :login)
84-
middleware(M.PassportLoader, source: :community)
85-
middleware(M.Passport, claim: "cms->c?->job.pin")
86-
resolve(&R.CMS.pin_article/3)
87-
end
88-
89-
@desc "unpin a job"
90-
field :undo_pin_job, :job do
91-
arg(:id, non_null(:id))
92-
arg(:thread, :job_thread, default_value: :job)
93-
arg(:community_id, non_null(:id))
94-
95-
middleware(M.Authorize, :login)
96-
middleware(M.PassportLoader, source: :community)
97-
middleware(M.Passport, claim: "cms->c?->job.undo_pin")
98-
resolve(&R.CMS.undo_pin_article/3)
99-
end
100-
101-
@desc "trash a job, not delete"
102-
field :trash_job, :job do
103-
arg(:id, non_null(:id))
104-
arg(:thread, :job_thread, default_value: :job)
105-
arg(:community_id, non_null(:id))
106-
107-
middleware(M.Authorize, :login)
108-
middleware(M.PassportLoader, source: :community)
109-
middleware(M.Passport, claim: "cms->c?->job.trash")
110-
111-
resolve(&R.CMS.trash_content/3)
112-
end
113-
114-
@desc "trash a job, not delete"
115-
field :undo_trash_job, :job do
116-
arg(:id, non_null(:id))
117-
arg(:thread, :job_thread, default_value: :job)
118-
arg(:community_id, non_null(:id))
119-
120-
middleware(M.Authorize, :login)
121-
middleware(M.PassportLoader, source: :community)
122-
middleware(M.Passport, claim: "cms->c?->job.undo_trash")
123-
124-
resolve(&R.CMS.undo_trash_content/3)
125-
end
126-
127-
@desc "delete a job"
128-
field :delete_job, :job do
129-
arg(:id, non_null(:id))
130-
131-
middleware(M.Authorize, :login)
132-
middleware(M.PassportLoader, source: :job)
133-
middleware(M.Passport, claim: "owner;cms->c?->job.delete")
134-
135-
resolve(&R.CMS.delete_content/3)
136-
end
13779
end
13880
end

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

Lines changed: 3 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -46,68 +46,9 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Post do
4646

4747
#############
4848
article_upvote_mutation(:post)
49+
article_pin_mutation(:post)
50+
article_trash_mutation(:post)
51+
article_delete_mutation(:post)
4952
#############
50-
51-
@desc "pin a post"
52-
field :pin_post, :post do
53-
arg(:id, non_null(:id))
54-
arg(:community_id, non_null(:id))
55-
arg(:thread, :post_thread, default_value: :post)
56-
57-
middleware(M.Authorize, :login)
58-
middleware(M.PassportLoader, source: :community)
59-
middleware(M.Passport, claim: "cms->c?->post.pin")
60-
resolve(&R.CMS.pin_article/3)
61-
end
62-
63-
@desc "unpin a post"
64-
field :undo_pin_post, :post do
65-
arg(:id, non_null(:id))
66-
arg(:thread, :post_thread, default_value: :post)
67-
arg(:community_id, non_null(:id))
68-
69-
middleware(M.Authorize, :login)
70-
middleware(M.PassportLoader, source: :community)
71-
middleware(M.Passport, claim: "cms->c?->post.undo_pin")
72-
resolve(&R.CMS.undo_pin_article/3)
73-
end
74-
75-
@desc "trash a post, not delete"
76-
field :trash_post, :post do
77-
arg(:id, non_null(:id))
78-
arg(:thread, :post_thread, default_value: :post)
79-
arg(:community_id, non_null(:id))
80-
81-
middleware(M.Authorize, :login)
82-
middleware(M.PassportLoader, source: :community)
83-
middleware(M.Passport, claim: "cms->c?->post.trash")
84-
85-
resolve(&R.CMS.trash_content/3)
86-
end
87-
88-
@desc "trash a post, not delete"
89-
field :undo_trash_post, :post do
90-
arg(:id, non_null(:id))
91-
arg(:thread, :post_thread, default_value: :post)
92-
arg(:community_id, non_null(:id))
93-
94-
middleware(M.Authorize, :login)
95-
middleware(M.PassportLoader, source: :community)
96-
middleware(M.Passport, claim: "cms->c?->post.undo_trash")
97-
98-
resolve(&R.CMS.undo_trash_content/3)
99-
end
100-
101-
@desc "delete a cms/post"
102-
# TODO: if post belongs to multi communities, unset instead delete
103-
field :delete_post, :post do
104-
arg(:id, non_null(:id))
105-
106-
middleware(M.Authorize, :login)
107-
middleware(M.PassportLoader, source: :post)
108-
middleware(M.Passport, claim: "owner;cms->c?->post.delete")
109-
110-
resolve(&R.CMS.delete_content/3)
111-
end
11253
end
11354
end

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

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do
33
CMS mutations for job
44
"""
55
use Helper.GqlSchemaSuite
6+
import GroupherServerWeb.Schema.Helper.Mutations
67

78
object :cms_repo_mutations do
89
@desc "create a repo"
@@ -69,55 +70,10 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Repo do
6970
resolve(&R.CMS.update_content/3)
7071
end
7172

72-
@desc "pin a repo"
73-
field :pin_repo, :repo do
74-
arg(:id, non_null(:id))
75-
arg(:thread, :repo_thread, default_value: :repo)
76-
arg(:community_id, non_null(:id))
77-
78-
middleware(M.Authorize, :login)
79-
middleware(M.PassportLoader, source: :community)
80-
middleware(M.Passport, claim: "cms->c?->repo.pin")
81-
resolve(&R.CMS.pin_article/3)
82-
end
83-
84-
@desc "unpin a repo"
85-
field :undo_pin_repo, :repo do
86-
arg(:id, non_null(:id))
87-
arg(:thread, :repo_thread, default_value: :repo)
88-
arg(:community_id, non_null(:id))
89-
90-
middleware(M.Authorize, :login)
91-
middleware(M.PassportLoader, source: :community)
92-
middleware(M.Passport, claim: "cms->c?->repo.undo_pin")
93-
resolve(&R.CMS.undo_pin_article/3)
94-
end
95-
96-
@desc "trash a repo, not delete"
97-
field :trash_repo, :repo do
98-
arg(:id, non_null(:id))
99-
arg(:thread, :repo_thread, default_value: :repo)
100-
arg(:community_id, non_null(:id))
101-
102-
middleware(M.Authorize, :login)
103-
middleware(M.PassportLoader, source: :community)
104-
middleware(M.Passport, claim: "cms->c?->repo.trash")
105-
106-
resolve(&R.CMS.trash_content/3)
107-
end
108-
109-
@desc "trash a repo, not delete"
110-
field :undo_trash_repo, :repo do
111-
arg(:id, non_null(:id))
112-
arg(:thread, :repo_thread, default_value: :repo)
113-
arg(:community_id, non_null(:id))
114-
115-
middleware(M.Authorize, :login)
116-
middleware(M.PassportLoader, source: :community)
117-
middleware(M.Passport, claim: "cms->c?->repo.undo_trash")
118-
119-
resolve(&R.CMS.undo_trash_content/3)
120-
end
73+
#############
74+
article_pin_mutation(:repo)
75+
article_trash_mutation(:repo)
76+
#############
12177

12278
@desc "delete a repo"
12379
field :delete_repo, :repo do

test/groupher_server_web/mutation/cms/job_flag_test.exs renamed to test/groupher_server_web/mutation/cms/flags/job_flag_test.exs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule GroupherServer.Test.Mutation.JobFlag do
1+
defmodule GroupherServer.Test.Mutation.Flags.JobFlag do
22
use GroupherServer.TestTools
33

44
alias GroupherServer.CMS
@@ -25,6 +25,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
2525
}
2626
}
2727
"""
28+
@tag :wip2
2829
test "auth user can trash job", ~m(community job)a do
2930
variables = %{id: job.id, communityId: community.id}
3031

@@ -37,6 +38,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
3738
assert updated["trash"] == true
3839
end
3940

41+
@tag :wip2
4042
test "unauth user trash job fails", ~m(user_conn guest_conn job community)a do
4143
variables = %{id: job.id, communityId: community.id}
4244
rule_conn = simu_conn(:user, cms: %{"what.ever" => true})
@@ -54,6 +56,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
5456
}
5557
}
5658
"""
59+
@tag :wip2
5760
test "auth user can undo trash job", ~m(community job)a do
5861
variables = %{id: job.id, communityId: community.id}
5962

@@ -84,6 +87,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
8487
}
8588
}
8689
"""
90+
@tag :wip2
8791
test "auth user can pin job", ~m(community job)a do
8892
variables = %{id: job.id, communityId: community.id}
8993

@@ -95,6 +99,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
9599
assert updated["id"] == to_string(job.id)
96100
end
97101

102+
@tag :wip2
98103
test "unauth user pin job fails", ~m(user_conn guest_conn community job)a do
99104
variables = %{id: job.id, communityId: community.id}
100105
rule_conn = simu_conn(:user, cms: %{"what.ever" => true})
@@ -112,7 +117,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
112117
}
113118
}
114119
"""
115-
@tag :wip
120+
@tag :wip2
116121
test "auth user can undo pin job", ~m(community job)a do
117122
variables = %{id: job.id, communityId: community.id}
118123

@@ -126,6 +131,7 @@ defmodule GroupherServer.Test.Mutation.JobFlag do
126131
assert updated["isPinned"] == false
127132
end
128133

134+
@tag :wip2
129135
test "unauth user undo pin job fails", ~m(user_conn guest_conn community job)a do
130136
variables = %{id: job.id, communityId: community.id}
131137
rule_conn = simu_conn(:user, cms: %{"what.ever" => true})

0 commit comments

Comments
 (0)