Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit66bc091

Browse files
authored
refactor: enhance article move & mirror (#436)
* refactor(move-article): clean up original tags* refactor(move-article): re-org && add more test* refactor(move-article): enhance GQ endpoint & clean up* refactor(move-article): blackhole GQ workflow* chore: clean up & fmt* chore(move-article): improve test
1 parent7a085f3 commit66bc091

File tree

17 files changed

+1455
-79
lines changed

17 files changed

+1455
-79
lines changed

‎lib/groupher_server/cms/cms.ex‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,13 @@ defmodule GroupherServer.CMS do
136136

137137
# >> community: set / unset
138138
defdelegatemirror_article(thread,article_id,community_id),to:ArticleCommunity
139+
defdelegatemirror_article(thread,article_id,community_id,article_ids),to:ArticleCommunity
139140
defdelegateunmirror_article(thread,article_id,community_id),to:ArticleCommunity
140141
defdelegatemove_article(thread,article_id,community_id),to:ArticleCommunity
142+
defdelegatemove_article(thread,article_id,community_id,article_ids),to:ArticleCommunity
143+
144+
defdelegatemove_to_blackhole(thread,article_id,article_ids),to:ArticleCommunity
145+
defdelegatemove_to_blackhole(thread,article_id),to:ArticleCommunity
141146

142147
defdelegateemotion_to_article(thread,article_id,args,user),to:ArticleEmotion
143148
defdelegateundo_emotion_to_article(thread,article_id,args,user),to:ArticleEmotion

‎lib/groupher_server/cms/delegates/article_community.ex‎

Lines changed: 64 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
1212
aliasHelper.Types,as:T
1313
aliasHelper.ORM
1414

15-
aliasGroupherServer.CMS.Model.{Embeds,Community,PinnedArticle}
16-
aliasGroupherServer.Repo
15+
aliasGroupherServer.{CMS,Repo}
16+
aliasCMS.Model.{Embeds,Community,PinnedArticle}
17+
aliasCMS.Delegate.{ArticleTag}
1718

1819
aliasEcto.Multi
1920

@@ -58,33 +59,45 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
5859
@doc"""
5960
mirror article to other community
6061
"""
61-
defmirror_article(thread,article_id,community_id)do
62+
defmirror_article(thread,article_id,community_id,article_tag_ids\\[])do
6263
with{:ok,info}<-match(thread),
6364
{:ok,article}<-ORM.find(info.model,article_id,preload::communities),
6465
{:ok,community}<-ORM.find(Community,community_id)do
65-
article
66-
|>Ecto.Changeset.change()
67-
|>Ecto.Changeset.put_assoc(:communities,article.communities++[community])
68-
|>Repo.update()
66+
Multi.new()
67+
|>Multi.run(:mirror_target_community,fn_,_->
68+
article
69+
|>Ecto.Changeset.change()
70+
|>Ecto.Changeset.put_assoc(:communities,article.communities++[community])
71+
|>Repo.update()
72+
end)
73+
|>Multi.run(:set_target_tags,fn_,%{mirror_target_community:article}->
74+
ArticleTag.set_article_tags(community,thread,article,%{article_tags:article_tag_ids})
75+
end)
76+
|>Repo.transaction()
77+
|>result()
6978
end
7079
end
7180

7281
@doc"""
7382
unmirror article to a community
7483
"""
7584
defunmirror_article(thread,article_id,community_id)do
85+
preload=[:communities,:original_community,:article_tags]
86+
7687
with{:ok,info}<-match(thread),
77-
{:ok,article}<-
78-
ORM.find(info.model,article_id,preload:[:communities,:original_community]),
88+
{:ok,article}<-ORM.find(info.model,article_id,preload:preload),
7989
{:ok,community}<-ORM.find(Community,community_id)do
8090
casearticle.original_community.id==community.iddo
8191
true->
8292
raise_error(:mirror_article,"can not unmirror original_community")
8393

8494
false->
95+
article_tags=tags_without_community(article,community)
96+
8597
article
8698
|>Ecto.Changeset.change()
8799
|>Ecto.Changeset.put_assoc(:communities,article.communities--[community])
100+
|>Ecto.Changeset.put_assoc(:article_tags,article_tags)
88101
|>Repo.update()
89102
end
90103
end
@@ -93,40 +106,57 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
93106
@doc"""
94107
move article original community to other community
95108
"""
96-
defmove_article(thread,article_id,community_id)do
109+
defmove_article(thread,article_id,community_id,article_tag_ids\\[])do
110+
preload=[:communities,:original_community,:article_tags]
111+
97112
with{:ok,info}<-match(thread),
98113
{:ok,community}<-ORM.find(Community,community_id),
99-
{:ok,article}<-
100-
ORM.find(info.model,article_id,preload:[:communities,:original_community])do
101-
cur_original_community=article.original_community
114+
{:ok,article}<-ORM.find(info.model,article_id,preload:preload)do
115+
original_community=article.original_community
102116

103117
Multi.new()
104-
|>Multi.run(:change_original_community,fn_,_->
118+
|>Multi.run(:move_article,fn_,_->
119+
communities=(article.communities--[original_community])++[community]
120+
article_tags=tags_without_community(article,original_community)
121+
105122
article
106123
|>Ecto.Changeset.change()
107124
|>Ecto.Changeset.put_change(:original_community_id,community.id)
125+
|>Ecto.Changeset.put_assoc(:communities,communities)
126+
|>Ecto.Changeset.put_assoc(:article_tags,article_tags)
108127
|>Repo.update()
109128
end)
110-
|>Multi.run(:unmirror_article,fn_,%{change_original_community:article}->
111-
article
112-
|>Ecto.Changeset.change()
113-
|>Ecto.Changeset.put_assoc(:communities,article.communities--[cur_original_community])
114-
|>Repo.update()
129+
|>Multi.run(:set_target_tags,fn_,%{move_article:article}->
130+
ArticleTag.set_article_tags(community,thread,article,%{article_tags:article_tag_ids})
115131
end)
116-
|>Multi.run(:mirror_target_community,fn_,%{unmirror_article:article}->
132+
|>Repo.transaction()
133+
|>result()
134+
end
135+
end
136+
137+
defmove_to_blackhole(thread,article_id,article_tag_ids\\[])do
138+
preload=[:communities,:original_community,:article_tags]
139+
140+
with{:ok,info}<-match(thread),
141+
{:ok,community}<-ORM.find_by(Community,%{raw:"blackhole"}),
142+
{:ok,article}<-ORM.find(info.model,article_id,preload:preload)do
143+
Multi.new()
144+
|>Multi.run(:set_community,fn_,_->
117145
article
118146
|>Ecto.Changeset.change()
119-
|>Ecto.Changeset.put_assoc(:communities,article.communities++[community])
147+
|>Ecto.Changeset.put_change(:original_community_id,community.id)
148+
|>Ecto.Changeset.put_assoc(:communities,[community])
149+
|>Ecto.Changeset.put_assoc(:article_tags,[])
120150
|>Repo.update()
121151
end)
152+
|>Multi.run(:set_target_tags,fn_,%{set_community:article}->
153+
ArticleTag.set_article_tags(community,thread,article,%{article_tags:article_tag_ids})
154+
end)
122155
|>Repo.transaction()
123156
|>result()
124157
end
125158
end
126159

127-
defpresult({:ok,%{mirror_target_community:result}}),do:result|>done()
128-
defpresult({:error,_,result,_steps}),do:{:error,result}
129-
130160
@doc"update isEdited meta label if needed"
131161
# TODO: diff history
132162
defupdate_edit_status(%{meta:%Embeds.ArticleMeta{is_edited:_}=meta}=content)do
@@ -157,4 +187,14 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
157187
_->{:ok,:pass}
158188
end
159189
end
190+
191+
defptags_without_community(article,%Community{id:community_id})do
192+
%{article_tags:article_tags}=article
193+
article_tags--Enum.filter(article_tags,&(&1.community_id===community_id))
194+
end
195+
196+
defpresult({:ok,%{set_target_tags:result}}),do:result|>done()
197+
defpresult({:ok,%{mirror_target_community:result}}),do:result|>done()
198+
199+
defpresult({:error,_,result,_steps}),do:{:error,result}
160200
end

‎lib/groupher_server/cms/delegates/article_tag.ex‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
7878
article_tag_ids=Enum.map(article_tag_ids,&to_string(&1))
7979

8080
Enum.all?(article_tag_ids,&Enum.member?(domain_tags_ids,&1))
81+
else
82+
_->false
8183
end
8284
end
8385

@@ -86,17 +88,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleTag do
8688
8789
used for create article with article_tags in args
8890
"""
91+
defset_article_tags(_,_,article,%{article_tags:[]}),do:{:ok,article}
92+
8993
defset_article_tags(%Community{id:cid},thread,article,%{article_tags:article_tag_ids})do
9094
check_filter=%{page:1,size:100,community_id:cid,thread:thread}
9195

92-
withtrue<-is_article_tag_in_some_thread?(article_tag_ids,check_filter)do
93-
Enum.each(article_tag_ids,&set_article_tag(thread,article,&1))|>done
96+
withtrue<-is_article_tag_in_some_thread?(article_tag_ids,check_filter),
97+
Enum.each(article_tag_ids,&set_article_tag(thread,article,&1))|>donedo
98+
{:ok,article}
9499
else
95100
false->raise_error(:invalid_domain_tag,"tag not in same community & thread")
96101
end
97102
end
98103

99-
defset_article_tags(_community,_thread,_id,_attrs),do:{:ok,:pass}
104+
defset_article_tags(_community,_thread,article,_),do:{:ok,article}
100105

101106
@doc"""
102107
set article a tag

‎lib/groupher_server_web/resolvers/cms_resolver.ex‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,8 +293,12 @@ defmodule GroupherServerWeb.Resolvers.CMS do
293293
CMS.unmirror_article(thread,id,community_id)
294294
end
295295

296-
defmove_article(_root,~m(thread id community_id)a,_info)do
297-
CMS.move_article(thread,id,community_id)
296+
defmove_article(_root,~m(thread id community_id article_tags)a,_info)do
297+
CMS.move_article(thread,id,community_id,article_tags)
298+
end
299+
300+
defmove_to_blackhole(_root,~m(thread id article_tags)a,_info)do
301+
CMS.move_to_blackhole(thread,id,article_tags)
298302
end
299303

300304
# #######################

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Operation do
112112
arg(:id,non_null(:id))
113113
arg(:community_id,non_null(:id))
114114
arg(:thread,:thread,default_value::post)
115+
arg(:article_tags,list_of(:id),default_value:[])
115116

116117
middleware(M.Authorize,:login)
117118
middleware(M.Passport,claim:"cms->t?.community.mirror")
@@ -134,10 +135,22 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Operation do
134135
arg(:id,non_null(:id))
135136
arg(:community_id,non_null(:id))
136137
arg(:thread,:thread,default_value::post)
138+
arg(:article_tags,list_of(:id),default_value:[])
137139

138140
middleware(M.Authorize,:login)
139141
middleware(M.Passport,claim:"cms->t?.community.move")
140142
resolve(&R.CMS.move_article/3)
141143
end
144+
145+
@desc"move article to other community"
146+
field:move_to_blackhole,:articledo
147+
arg(:id,non_null(:id))
148+
arg(:thread,:thread,default_value::post)
149+
arg(:article_tags,list_of(:id),default_value:[])
150+
151+
middleware(M.Authorize,:login)
152+
middleware(M.Passport,claim:"cms->blackeye")
153+
resolve(&R.CMS.move_to_blackhole/3)
154+
end
142155
end
143156
end

‎lib/helper/certification.ex‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ defmodule Helper.Certification do
6868
build_article_rules(@article_rules)++
6969
[
7070
"root",
71+
"blackeye",
7172
"system_accountant",
7273
"system_notification.publish",
7374
"stamp_passport",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp