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.

Commit9f50d45

Browse files
committed
fix(community): editor count & tests
1 parentbea44ab commit9f50d45

File tree

7 files changed

+97
-39
lines changed

7 files changed

+97
-39
lines changed

‎lib/groupher_server/cms/community.ex‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ defmodule GroupherServer.CMS.Community do
5050
field(:subscribers_count,:integer,default:0)
5151

5252
field(:viewer_has_subscribed,:boolean,default:false,virtual:true)
53+
field(:viewer_is_editor,:boolean,default:false,virtual:true)
5354

5455
has_one(:wiki,CommunityWiki)
5556
has_one(:cheatsheet,CommunityCheatsheet)

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,28 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
5454
end
5555
end
5656

57+
@doc"""
58+
update editors_count of a community
59+
"""
60+
defupdate_community_count_field(%Community{}=community,user_id,:editors_count,opt)do
61+
count_query=from(sinCommunityEditor,where:s.community_id==^community.id)
62+
editors_count=Repo.aggregate(count_query,:count)
63+
community_meta=ifis_nil(community.meta),do:@default_meta,else:community.meta
64+
65+
editors_ids=
66+
caseoptdo
67+
:inc->(community_meta.editors_ids++[user_id])|>Enum.uniq()
68+
:dec->(community_meta.editors_ids--[user_id])|>Enum.uniq()
69+
end
70+
71+
meta=community_meta|>Map.put(:editors_ids,editors_ids)|>strip_struct
72+
73+
community
74+
|>Ecto.Changeset.change(%{editors_count:editors_count})
75+
|>Ecto.Changeset.put_embed(:meta,meta)
76+
|>Repo.update()
77+
end
78+
5779
@doc"""
5880
update subscribers_count of a community
5981
"""
@@ -213,7 +235,10 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
213235
end
214236

215237
defpviewer_has_states({:ok,community},%User{id:user_id})do
216-
viewer_has_states=%{viewer_has_subscribed:user_idincommunity.meta.subscribed_user_ids}
238+
viewer_has_states=%{
239+
viewer_has_subscribed:user_idincommunity.meta.subscribed_user_ids,
240+
viewer_is_editor:user_idincommunity.meta.editors_ids
241+
}
217242

218243
{:ok,Map.merge(community,viewer_has_states)}
219244
end

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,37 +71,39 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
7171
:insert_editor,
7272
CommunityEditor.changeset(%CommunityEditor{},~m(user_id community_id title)a)
7373
)
74+
|>Multi.run(:update_editors_count,fn_,_->
75+
with{:ok,community}<-ORM.find(Community,community_id)do
76+
CommunityCURD.update_community_count_field(community,user_id,:editors_count,:inc)
77+
end
78+
end)
7479
|>Multi.run(:stamp_passport,fn_,_->
7580
rules=Certification.passport_rules(cms:title)
7681
PassportCURD.stamp_passport(rules,%User{id:user_id})
7782
end)
7883
|>Repo.transaction()
79-
|>set_editor_result()
84+
|>result()
8085
end
8186

8287
@doc"""
8388
unset a community editor
8489
"""
8590
defunset_editor(%Community{id:community_id},%User{id:user_id})do
86-
with{:ok,_}<-ORM.findby_delete!(CommunityEditor,~m(user_id community_id)a),
87-
{:ok,_}<-PassportCURD.delete_passport(%User{id:user_id})do
88-
User|>ORM.find(user_id)
89-
end
90-
end
91-
92-
defpset_editor_result({:ok,%{insert_editor:editor}})do
93-
User|>ORM.find(editor.user_id)
91+
Multi.new()
92+
|>Multi.run(:delete_editor,fn_,_->
93+
ORM.findby_delete!(CommunityEditor,~m(user_id community_id)a)
94+
end)
95+
|>Multi.run(:update_editors_count,fn_,_->
96+
with{:ok,community}<-ORM.find(Community,community_id)do
97+
CommunityCURD.update_community_count_field(community,user_id,:editors_count,:dec)
98+
end
99+
end)
100+
|>Multi.run(:stamp_passport,fn_,_->
101+
PassportCURD.delete_passport(%User{id:user_id})
102+
end)
103+
|>Repo.transaction()
104+
|>result()
94105
end
95106

96-
defpset_editor_result({:error,:stamp_passport,%Ecto.Changeset{}=result,_steps}),
97-
do:{:error,result}
98-
99-
defpset_editor_result({:error,:stamp_passport,_result,_steps}),
100-
do:{:error,"stamp passport error"}
101-
102-
defpset_editor_result({:error,:insert_editor,_result,_steps}),
103-
do:{:error,"insert editor error"}
104-
105107
@doc"""
106108
subscribe a community. (ONLY community, post etc use watch )
107109
"""
@@ -305,13 +307,15 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
305307
Map.merge(geo_info,%{"value"=>max(geo_info["value"]-1,0)})
306308
end
307309

308-
defpresult({:ok,%{subscribed_community:result}})do
310+
defpresult({:ok,%{update_editors_count:result}})do
309311
{:ok,result}
310312
end
311313

312-
defpresult({:ok,%{unsubscribed_community:result}})do
313-
{:ok,result}
314-
end
314+
defpresult({:error,:stamp_passport,%Ecto.Changeset{}=result,_steps}),
315+
do:{:error,result}
316+
317+
defpresult({:error,:stamp_passport,_result,_steps}),
318+
do:{:error,"stamp passport error"}
315319

316320
defpresult({:error,_,result,_steps})do
317321
{:error,result}

‎lib/groupher_server/cms/embeds/community_meta.ex‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ defmodule GroupherServer.CMS.Embeds.CommunityMeta do
2929
@article_threadsget_config(:article,:article_threads)
3030

3131
@general_options%{
32+
editors_ids:[],
3233
subscribed_user_ids:[],
3334
contributes_digest:[]
3435
}
@@ -47,6 +48,7 @@ defmodule GroupherServer.CMS.Embeds.CommunityMeta do
4748
embedded_schemado
4849
thread_count_fields()
4950

51+
field(:editors_ids,{:array,:integer},default:[])
5052
# 关注相关
5153
field(:subscribed_user_ids,{:array,:integer},default:[])
5254
field(:contributes_digest,{:array,:integer},default:[])

‎lib/groupher_server_web/schema/cms/cms_types.ex‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,6 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
279279
resolve(dataloader(CMS,:editors))
280280
end
281281

282-
field:editors_count,:integerdo
283-
arg(:count,:count_type,default_value::count)
284-
arg(:type,:community_type,default_value::community)
285-
resolve(dataloader(CMS,:editors))
286-
middleware(M.ConvertToInt)
287-
end
288-
289282
# TODO: remove
290283
field:threads_count,:integerdo
291284
resolve(&R.CMS.threads_count/3)

‎test/groupher_server/cms/community/community_test.exs‎

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,47 @@ defmodule GroupherServer.Test.CMS.Community do
3939
assertnotcommunity.viewer_has_subscribed
4040
assertuser2.idnot incommunity.meta.subscribed_user_ids
4141
end
42+
43+
@tag:wip2
44+
test"read editored community should have a flag",~m(community user user2)ado
45+
title="chief editor"
46+
{:ok,community}=CMS.set_editor(community,title,user)
47+
48+
{:ok,community}=CMS.read_community(%{id:community.id},user)
49+
assertcommunity.viewer_is_editor
50+
51+
{:ok,community}=CMS.read_community(%{id:community.id},user2)
52+
assertnotcommunity.viewer_is_editor
53+
54+
{:ok,community}=CMS.unset_editor(community,user)
55+
{:ok,community}=CMS.read_community(%{id:community.id},user)
56+
assertnotcommunity.viewer_is_editor
57+
end
58+
end
59+
60+
describe"[cms community editor]"do
61+
@tag:wip2
62+
test"can set editor to a community",~m(user community)ado
63+
title="chief editor"
64+
{:ok,community}=CMS.set_editor(community,title,user)
65+
66+
assertcommunity.editors_count==1
67+
assertuser.idincommunity.meta.editors_ids
68+
end
69+
70+
@tag:wip2
71+
test"can unset editor to a community",~m(user community)ado
72+
title="chief editor"
73+
{:ok,community}=CMS.set_editor(community,title,user)
74+
assertcommunity.editors_count==1
75+
76+
{:ok,community}=CMS.unset_editor(community,user)
77+
assertcommunity.editors_count==0
78+
assertuser.idnot incommunity.meta.editors_ids
79+
end
4280
end
4381

4482
describe"[cms community subscribe]"do
45-
# @tag :wip2
4683
test"user can subscribe a community",~m(user community)ado
4784
{:ok,record}=CMS.subscribe_community(community,user)
4885
assertcommunity.id==record.id

‎test/groupher_server_web/query/cms/cms_test.exs‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,12 @@ defmodule GroupherServer.Test.Query.CMS.Basic do
326326
}
327327
}
328328
"""
329+
@tag:wip2
329330
test"guest can get editors list and count of a community",~m(guest_conn community)ado
330331
title="chief editor"
331332
{:ok,users}=db_insert_multi(:user,assert_v(:inner_page_size))
332333

333-
Enum.each(
334-
users,
335-
&CMS.set_editor(community,title,%User{id:&1.id})
336-
)
334+
Enum.each(users,&CMS.set_editor(community,title,%User{id:&1.id}))
337335

338336
variables=%{id:community.id}
339337
results=guest_conn|>query_result(@query,variables,"community")
@@ -363,14 +361,12 @@ defmodule GroupherServer.Test.Query.CMS.Basic do
363361
}
364362
}
365363
"""
364+
@tag:wip2
366365
test"guest user can get paged editors",~m(guest_conn community)ado
367366
title="chief editor"
368367
{:ok,users}=db_insert_multi(:user,25)
369368

370-
Enum.each(
371-
users,
372-
&CMS.set_editor(community,title,%User{id:&1.id})
373-
)
369+
Enum.each(users,&CMS.set_editor(community,title,%User{id:&1.id}))
374370

375371
variables=%{id:community.id,filter:%{page:1,size:10}}
376372
results=guest_conn|>query_result(@query,variables,"communityEditors")

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp