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

Commit 5ebace6

Browse files
committed
refactor(tag): move set/unset refined tag to new func
1 parent 181e954 commit 5ebace6

File tree

8 files changed

+441
-21
lines changed

8 files changed

+441
-21
lines changed

lib/helper/certification.ex

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ defmodule Helper.Certification do
8888
"post.tag.update",
8989
"post.tag.delete",
9090
"post.tag.set",
91+
"post.refinedtag.set",
9192
"post.tag.unset",
9293
# post flag
9394
"post.pin",
@@ -99,17 +100,37 @@ defmodule Helper.Certification do
99100
"job.tag.update",
100101
"job.tag.delete",
101102
"job.tag.set",
103+
"job.refinedtag.set",
102104
"job.tag.unset",
103105
# job flag
104106
"job.pin",
105107
"job.undo_pin",
106108
"job.trash",
107109
"job.undo_trash",
110+
# video tag
111+
"video.tag.create",
112+
"video.tag.update",
113+
"video.tag.delete",
114+
"video.tag.set",
115+
"video.refinedtag.set",
116+
"video.tag.unset",
108117
# video flag
109118
"video.pin",
110119
"video.undo_pin",
111120
"video.trash",
112-
"video.undo_trash"
121+
"video.undo_trash",
122+
# repo tag
123+
"repo.tag.create",
124+
"repo.tag.update",
125+
"repo.tag.delete",
126+
"repo.tag.set",
127+
"repo.refinedtag.set",
128+
"repo.tag.unset",
129+
# repo flag
130+
"repo.pin",
131+
"repo.undo_pin",
132+
"repo.trash",
133+
"repo.undo_trash"
113134
]
114135
}
115136
end

lib/mastani_server/cms/cms.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ defmodule MastaniServer.CMS do
9494
# >> tag: set / unset
9595
defdelegate set_tag(community, thread, tag, content_id), to: ArticleOperation
9696
defdelegate unset_tag(thread, tag, content_id), to: ArticleOperation
97+
defdelegate set_refined_tag(community, thread, topic, content_id), to: ArticleOperation
98+
defdelegate set_refined_tag(community, thread, content_id), to: ArticleOperation
99+
defdelegate unset_refined_tag(community, thread, topic, content_id), to: ArticleOperation
100+
defdelegate unset_refined_tag(community, thread, content_id), to: ArticleOperation
97101
# >> community: set / unset
98102
defdelegate set_community(community, thread, content_id), to: ArticleOperation
99103
defdelegate unset_community(community, thread, content_id), to: ArticleOperation

lib/mastani_server/cms/delegates/article_operation.ex

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,21 @@ defmodule MastaniServer.CMS.Delegate.ArticleOperation do
151151
end
152152

153153
@doc """
154-
set tag for post / tuts / videos ...
154+
set general tag for post / tuts / videos ...
155+
refined tag can't set by this func, use set_refined_tag instead
155156
"""
156157
# check community first
157158
def set_tag(%Community{id: _communitId}, thread, %Tag{id: tag_id}, content_id) do
158159
with {:ok, action} <- match_action(thread, :tag),
159160
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
160161
{:ok, tag} <- ORM.find(action.reactor, tag_id) do
161-
content
162-
|> Ecto.Changeset.change()
163-
|> Ecto.Changeset.put_assoc(:tags, content.tags ++ [tag])
164-
|> Repo.update()
162+
case tag.title != "refined" do
163+
true ->
164+
update_content_tag(content, tag)
165+
166+
false ->
167+
{:error, "use set_refined_tag instead"}
168+
end
165169

166170
# NOTE: this should be control by Middleware
167171
# case tag_in_community_thread?(%Community{id: communitId}, thread, tag) do
@@ -181,13 +185,71 @@ defmodule MastaniServer.CMS.Delegate.ArticleOperation do
181185
with {:ok, action} <- match_action(thread, :tag),
182186
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
183187
{:ok, tag} <- ORM.find(action.reactor, tag_id) do
184-
content
185-
|> Ecto.Changeset.change()
186-
|> Ecto.Changeset.put_assoc(:tags, content.tags -- [tag])
187-
|> Repo.update()
188+
update_content_tag(content, tag, :drop)
189+
end
190+
end
191+
192+
@doc """
193+
set refined_tag to common content
194+
"""
195+
def set_refined_tag(%Community{id: community_id}, thread, topic_raw, content_id) do
196+
with {:ok, action} <- match_action(thread, :tag),
197+
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
198+
{:ok, topic} <- ORM.find_by(Topic, %{raw: topic_raw}),
199+
{:ok, tag} <-
200+
ORM.find_by(action.reactor, %{
201+
title: "refined",
202+
community_id: community_id,
203+
topic_id: topic.id
204+
}) do
205+
update_content_tag(content, tag)
206+
end
207+
end
208+
209+
def set_refined_tag(%Community{id: community_id}, thread, content_id) do
210+
with {:ok, action} <- match_action(thread, :tag),
211+
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
212+
{:ok, tag} <-
213+
ORM.find_by(action.reactor, %{title: "refined", community_id: community_id}) do
214+
update_content_tag(content, tag)
215+
end
216+
end
217+
218+
@doc """
219+
unset refined_tag to common content
220+
"""
221+
def unset_refined_tag(%Community{id: community_id}, thread, topic_raw, content_id) do
222+
with {:ok, action} <- match_action(thread, :tag),
223+
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
224+
{:ok, topic} <- ORM.find_by(Topic, %{raw: topic_raw}),
225+
{:ok, tag} <-
226+
ORM.find_by(action.reactor, %{
227+
title: "refined",
228+
community_id: community_id,
229+
topic_id: topic.id
230+
}) do
231+
update_content_tag(content, tag, :drop)
188232
end
189233
end
190234

235+
def unset_refined_tag(%Community{id: community_id}, thread, content_id) do
236+
with {:ok, action} <- match_action(thread, :tag),
237+
{:ok, content} <- ORM.find(action.target, content_id, preload: :tags),
238+
{:ok, tag} <-
239+
ORM.find_by(action.reactor, %{title: "refined", community_id: community_id}) do
240+
update_content_tag(content, tag, :drop)
241+
end
242+
end
243+
244+
defp update_content_tag(content, %Tag{} = tag, opt \\ :add) do
245+
new_tags = if opt == :add, do: content.tags ++ [tag], else: content.tags -- [tag]
246+
247+
content
248+
|> Ecto.Changeset.change()
249+
|> Ecto.Changeset.put_assoc(:tags, new_tags)
250+
|> Repo.update()
251+
end
252+
191253
@doc """
192254
set topic only for post
193255
"""

lib/mastani_server_web/resolvers/cms_resolver.ex

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,26 @@ defmodule MastaniServerWeb.Resolvers.CMS do
272272
CMS.set_tag(%Community{id: community_id}, thread, %Tag{id: tag_id}, id)
273273
end
274274

275+
def set_refined_tag(_root, ~m(community_id thread id topic)a, _info) do
276+
CMS.set_refined_tag(%Community{id: community_id}, thread, topic, id)
277+
end
278+
279+
def set_refined_tag(_root, ~m(community_id thread id)a, _info) do
280+
CMS.set_refined_tag(%Community{id: community_id}, thread, id)
281+
end
282+
275283
def unset_tag(_root, ~m(id thread tag_id)a, _info) do
276284
CMS.unset_tag(thread, %Tag{id: tag_id}, id)
277285
end
278286

287+
def unset_refined_tag(_root, ~m(community_id thread id topic)a, _info) do
288+
CMS.unset_refined_tag(%Community{id: community_id}, thread, topic, id)
289+
end
290+
291+
def unset_refined_tag(_root, ~m(community_id thread id)a, _info) do
292+
CMS.unset_refined_tag(%Community{id: community_id}, thread, id)
293+
end
294+
279295
def get_tags(_root, %{community_id: community_id, all: true}, _info) do
280296
CMS.get_tags(%Community{id: community_id})
281297
end

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

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,35 @@ defmodule MastaniServerWeb.Schema.CMS.Mutations.Operation do
7878
resolve(&R.CMS.unsubscribe_community/3)
7979
end
8080

81-
@desc "set a tag within community"
81+
@desc "set a tag to content"
8282
field :set_tag, :tag do
8383
arg(:id, non_null(:id))
8484
arg(:tag_id, non_null(:id))
8585
arg(:community_id, non_null(:id))
8686
arg(:thread, :cms_thread, default_value: :post)
8787

8888
middleware(M.Authorize, :login)
89-
# middleware(M.PassportLoader, source: :community)
90-
# middleware(M.Passport, claim: "cms->c?->t?.tag.set")
89+
middleware(M.PassportLoader, source: :community)
90+
middleware(M.Passport, claim: "cms->c?->t?.tag.set")
9191

9292
resolve(&R.CMS.set_tag/3)
9393
end
9494

95-
@desc "unset a tag within community"
95+
@desc "set a refined tag to content"
96+
field :set_refined_tag, :tag do
97+
arg(:id, non_null(:id))
98+
arg(:community_id, non_null(:id))
99+
arg(:thread, :cms_thread, default_value: :post)
100+
arg(:topic, :string)
101+
102+
middleware(M.Authorize, :login)
103+
middleware(M.PassportLoader, source: :community)
104+
middleware(M.Passport, claim: "cms->c?->t?.refinedtag.set")
105+
106+
resolve(&R.CMS.set_refined_tag/3)
107+
end
108+
109+
@desc "unset a tag to content"
96110
field :unset_tag, :tag do
97111
# thread id
98112
arg(:id, non_null(:id))
@@ -101,12 +115,26 @@ defmodule MastaniServerWeb.Schema.CMS.Mutations.Operation do
101115
arg(:thread, :cms_thread, default_value: :post)
102116

103117
middleware(M.Authorize, :login)
104-
# middleware(M.PassportLoader, source: :community)
105-
# middleware(M.Passport, claim: "cms->c?->t?.tag.unset")
118+
middleware(M.PassportLoader, source: :community)
119+
middleware(M.Passport, claim: "cms->c?->t?.tag.unset")
106120

107121
resolve(&R.CMS.unset_tag/3)
108122
end
109123

124+
@desc "unset a refined tag to content"
125+
field :unset_refined_tag, :tag do
126+
arg(:id, non_null(:id))
127+
arg(:community_id, non_null(:id))
128+
arg(:thread, :cms_thread, default_value: :post)
129+
arg(:topic, :string)
130+
131+
middleware(M.Authorize, :login)
132+
middleware(M.PassportLoader, source: :community)
133+
middleware(M.Passport, claim: "cms->c?->t?.refinedtag.set")
134+
135+
resolve(&R.CMS.unset_refined_tag/3)
136+
end
137+
110138
# TODO: use community loader
111139
field :set_community, :community do
112140
arg(:id, non_null(:id))

test/mastani_server_web/mutation/cms/job_test.exs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,15 @@ defmodule MastaniServer.Test.Mutation.Job do
291291
}
292292
}
293293
"""
294+
@set_refined_tag_query """
295+
mutation($communityId: ID!, $thread: CmsThread, $topic: String, $id: ID!) {
296+
setRefinedTag(communityId: $communityId, thread: $thread, topic: $topic, id: $id) {
297+
id
298+
title
299+
}
300+
}
301+
"""
302+
@tag :wip
294303
test "auth user can set a valid tag to job", ~m(job)a do
295304
{:ok, community} = db_insert(:community)
296305
{:ok, tag} = db_insert(:tag, %{thread: "job", community: community})
@@ -300,10 +309,39 @@ defmodule MastaniServer.Test.Mutation.Job do
300309

301310
variables = %{thread: "JOB", id: job.id, tagId: tag.id, communityId: community.id}
302311
rule_conn |> mutation_result(@set_tag_query, variables, "setTag")
303-
# {:ok, found} = ORM.find(CMS.Job, job.id, preload: :tags)
312+
{:ok, found} = ORM.find(CMS.Job, job.id, preload: :tags)
313+
314+
assoc_tags = found.tags |> Enum.map(& &1.id)
315+
assert tag.id in assoc_tags
316+
end
317+
318+
@tag :wip
319+
test "can not set refined tag to job", ~m(job)a do
320+
{:ok, community} = db_insert(:community)
321+
{:ok, tag} = db_insert(:tag, %{thread: "job", community: community, title: "refined"})
322+
323+
passport_rules = %{community.title => %{"job.tag.set" => true}}
324+
rule_conn = simu_conn(:user, cms: passport_rules)
325+
326+
variables = %{id: job.id, tagId: tag.id, communityId: community.id}
327+
328+
assert rule_conn |> mutation_get_error?(@set_tag_query, variables)
329+
end
330+
331+
@tag :wip
332+
test "auth user can set refined tag to job", ~m(job)a do
333+
{:ok, community} = db_insert(:community)
334+
{:ok, tag} = db_insert(:tag, %{thread: "job", community: community, title: "refined"})
335+
336+
passport_rules = %{community.title => %{"job.refinedtag.set" => true}}
337+
rule_conn = simu_conn(:user, cms: passport_rules)
338+
339+
variables = %{id: job.id, communityId: community.id, thread: "JOB"}
340+
rule_conn |> mutation_result(@set_refined_tag_query, variables, "setRefinedTag")
341+
{:ok, found} = ORM.find(CMS.Job, job.id, preload: :tags)
304342

305-
# assoc_tags = found.tags |> Enum.map(& &1.id)
306-
# assert tag.id in assoc_tags
343+
assoc_tags = found.tags |> Enum.map(& &1.id)
344+
assert tag.id in assoc_tags
307345
end
308346

309347
# TODO: should fix in auth layer
@@ -338,5 +376,33 @@ defmodule MastaniServer.Test.Mutation.Job do
338376
assert tag.id in assoc_tags
339377
assert tag2.id in assoc_tags
340378
end
379+
380+
@unset_refined_tag_query """
381+
mutation($communityId: ID!, $thread: CmsThread, $topic: String, $id: ID!) {
382+
unsetRefinedTag(communityId: $communityId, thread: $thread, topic: $topic, id: $id) {
383+
id
384+
title
385+
}
386+
}
387+
"""
388+
@tag :wip
389+
test "can unset refined tag to a job", ~m(job)a do
390+
{:ok, community} = db_insert(:community)
391+
{:ok, tag} = db_insert(:tag, %{thread: "job", community: community, title: "refined"})
392+
393+
passport_rules = %{community.title => %{"job.refinedtag.set" => true}}
394+
rule_conn = simu_conn(:user, cms: passport_rules)
395+
396+
variables = %{id: job.id, communityId: community.id, thread: "JOB"}
397+
rule_conn |> mutation_result(@set_refined_tag_query, variables, "setRefinedTag")
398+
399+
variables = %{id: job.id, communityId: community.id, thread: "JOB"}
400+
rule_conn |> mutation_result(@unset_refined_tag_query, variables, "unsetRefinedTag")
401+
402+
{:ok, found} = ORM.find(CMS.Job, job.id, preload: :tags)
403+
404+
assoc_tags = found.tags |> Enum.map(& &1.id)
405+
assert tag.id not in assoc_tags
406+
end
341407
end
342408
end

0 commit comments

Comments
 (0)