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.

Commit889a562

Browse files
authored
refactor: apply community logic (#441)
* refactor(comment): add checker api for exist* refactor(pending): add operation api & tests* refactor(pending): add api endpoint* chore: more test on apply workflow* chore: more test on apply workflow* refactor(community-apply): more apply info & tests* refactor(community-apply): edge case
1 parent66c4062 commit889a562

File tree

26 files changed

+482
-52
lines changed

26 files changed

+482
-52
lines changed

‎lib/groupher_server/cms/cms.ex‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ defmodule GroupherServer.CMS do
3737
defdelegateread_community(args,user),to:CommunityCURD
3838
defdelegatecreate_community(args),to:CommunityCURD
3939
defdelegateupdate_community(id,args),to:CommunityCURD
40+
defdelegateapply_community(args),to:CommunityCURD
41+
defdelegateapprove_community_apply(id),to:CommunityCURD
42+
defdelegatedeny_community_apply(id),to:CommunityCURD
43+
defdelegateis_community_exist?(raw),to:CommunityCURD
44+
defdelegatehas_pending_community_apply?(user),to:CommunityCURD
45+
4046
# >> editor ..
4147
defdelegateupdate_editor(user,community,title),to:CommunityCURD
4248
# >> geo info ..

‎lib/groupher_server/cms/constant.ex‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,23 @@ defmodule GroupherServer.CMS.Constant do
88
@artiment_illegal1
99
@artiment_audit_failed2
1010

11+
@community_normal0
12+
@community_applying1
13+
14+
@apply_public"PUBLIC"
15+
@apply_city"CITY"
16+
@apply_works"WORKS"
17+
@apply_team"TEAM"
18+
1119
defpending(:legal),do:@artiment_legal
1220
defpending(:illegal),do:@artiment_illegal
1321
defpending(:audit_failed),do:@artiment_audit_failed
22+
23+
defpending(:normal),do:@community_normal
24+
defpending(:applying),do:@community_applying
25+
26+
defapply_category(:public),do:@apply_public
27+
defapply_category(:city),do:@apply_city
28+
defapply_category(:works),do:@apply_works
29+
defapply_category(:team),do:@apply_team
1430
end

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommunity do
187187
@doc"update isEdited meta label if needed"
188188
# TODO: diff history
189189
defupdate_edit_status(%{meta:%Embeds.ArticleMeta{is_edited:_}=meta}=content)do
190-
meta=meta|>strip_struct|>Map.merge(%{is_edited:true})
190+
meta=meta|>Map.merge(%{is_edited:true})
191191
ORM.update_meta(content,meta)
192192
end
193193

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

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,18 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
2424
Thread
2525
}
2626

27+
aliasCMS.Constant
28+
2729
@default_metaEmbeds.CommunityMeta.default_meta()
2830
@article_threadsget_config(:article,:threads)
2931

30-
defread_community(clauses,user),do:read_community(clauses)|>viewer_has_states(user)
31-
defread_community(%{id:id}),do:ORM.read(Community,id,inc::views)
32-
defread_community(%{raw:raw}=clauses),do:do_read_community(clauses,raw)
33-
defread_community(%{title:title}=clauses),do:do_read_community(clauses,title)
32+
@community_normalConstant.pending(:normal)
33+
@community_applyingConstant.pending(:applying)
34+
35+
@default_apply_categoryConstant.apply_category(:public)
36+
37+
defread_community(raw,user),do:read_community(raw)|>viewer_has_states(user)
38+
defread_community(raw),do:do_read_community(raw)
3439

3540
@doc"""
3641
create a community
@@ -54,6 +59,59 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
5459
end
5560
end
5661

62+
@doc"""
63+
check if community exist
64+
"""
65+
defis_community_exist?(raw)do
66+
caseORM.find_by(Community,raw:raw)do
67+
{:ok,_}->{:ok,%{exist:true}}
68+
{:error,_}->{:ok,%{exist:false}}
69+
end
70+
end
71+
72+
defhas_pending_community_apply?(%User{}=user)do
73+
with{:ok,paged_applies}<-paged_community_applies(user,%{page:1,size:1})do
74+
casepaged_applies.total_count>0do
75+
true->{:ok,%{exist:true}}
76+
false->{:ok,%{exist:false}}
77+
end
78+
end
79+
end
80+
81+
defpaged_community_applies(%User{}=user,%{page:page,size:size}=_filter)do
82+
Community
83+
|>where([c],c.pending==^@community_applying)
84+
|>where([c],c.user_id==^user.id)
85+
|>ORM.paginator(~m(page size)a)
86+
|>done
87+
end
88+
89+
defapply_community(args)do
90+
with{:ok,community}<-create_community(Map.merge(args,%{pending:@community_applying}))do
91+
apply_msg=Map.get(args,:apply_msg,"")
92+
apply_category=Map.get(args,:apply_category,@default_apply_category)
93+
94+
meta=community.meta|>Map.merge(~m(apply_msg apply_category)a)
95+
ORM.update_meta(community,meta)
96+
end
97+
end
98+
99+
defapprove_community_apply(id)do
100+
# TODO: create community with thread, category and tags
101+
with{:ok,community}<-ORM.find(Community,id)do
102+
ORM.update(community,%{pending:@community_normal})
103+
end
104+
end
105+
106+
defdeny_community_apply(id)do
107+
with{:ok,community}<-ORM.find(Community,id)do
108+
casecommunity.pending==@community_applyingdo
109+
true->ORM.delete(community)
110+
false->{:ok,community}
111+
end
112+
end
113+
end
114+
57115
@doc"""
58116
update editors_count of a community
59117
"""
@@ -235,13 +293,20 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
235293
end
236294
end
237295

238-
defpdo_read_community(clauses,aka)do
239-
caseORM.read_by(Community,clauses,inc::views)do
240-
{:ok,community}->{:ok,community}
241-
{:error,_}->ORM.find_by(Community,aka:aka)
296+
defpdo_read_community(raw)do
297+
with{:ok,community}<-find_community(raw)do
298+
community|>ORM.read(inc::views)
242299
end
243300
end
244301

302+
defpfind_community(raw)do
303+
Community
304+
|>where([c],c.pending==^@community_normal)
305+
|>where([c],c.raw==^raworc.aka==^raw)
306+
|>Repo.one()
307+
|>done
308+
end
309+
245310
defpviewer_has_states({:ok,community},%User{id:user_id})do
246311
viewer_has_states=%{
247312
viewer_has_subscribed:user_idincommunity.meta.subscribed_user_ids,

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ defmodule GroupherServer.CMS.Delegate.Document do
33
CURD operation on post/job ...
44
"""
55
importEcto.Query,warn:false
6-
importHelper.Utils,only:[done:1,thread_of:2,get_config:2]
6+
importHelper.Utils,only:[thread_of:2]
77

88
importHelper.ErrorCode
9-
importShortMaps
109

1110
aliasHelper.{ORM,Converter}
1211
aliasGroupherServer.{CMS,Repo}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ defmodule GroupherServer.CMS.Delegate.Search do
3030

3131
defpdo_search_communities(queryable,title)do
3232
queryable
33-
|>where([c],ilike(c.title,^"%#{title}%")orilike(c.raw,^"%#{title}%"))
33+
|>where(
34+
[c],
35+
ilike(c.title,^"%#{title}%")orilike(c.raw,^"%#{title}%")orilike(c.aka,^"%#{title}%")
36+
)
3437
|>ORM.paginator(page:1,size:@search_items_count)
3538
|>done()
3639
end

‎lib/groupher_server/cms/models/article_document.ex‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ defmodule GroupherServer.CMS.Model.ArticleDocument do
88
useAccessible
99

1010
importEcto.Changeset
11-
importGroupherServer.CMS.Helper.Macros
1211
importHelper.Utils,only:[get_config:2]
1312

1413
@timestamps_opts[type::utc_datetime_usec]

‎lib/groupher_server/cms/models/community.ex‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule GroupherServer.CMS.Model.Community do
33
alias__MODULE__
44

55
useEcto.Schema
6+
useAccessible
7+
68
importEcto.Changeset
79

810
aliasGroupherServer.{Accounts,CMS}
@@ -22,7 +24,7 @@ defmodule GroupherServer.CMS.Model.Community do
2224

2325
@required_fields~w(title desc user_id logo raw)a
2426
# @required_fields ~w(title desc user_id)a
25-
@optional_fields~w(label geo_info index aka contributes_digest)a
27+
@optional_fields~w(label geo_info index aka contributes_digest pending)a
2628

2729
defmax_pinned_article_count_per_thread,do:@max_pinned_article_count_per_thread
2830

@@ -51,6 +53,8 @@ defmodule GroupherServer.CMS.Model.Community do
5153
field(:article_tags_count,:integer,default:0)
5254
field(:threads_count,:integer,default:0)
5355

56+
field(:pending,:integer,default:0)
57+
5458
field(:viewer_has_subscribed,:boolean,default:false,virtual:true)
5559
field(:viewer_is_editor,:boolean,default:false,virtual:true)
5660
field(:contributes_digest,{:array,:integer},default:[])
@@ -81,8 +85,9 @@ defmodule GroupherServer.CMS.Model.Community do
8185
|>validate_required(@required_fields)
8286
|>cast_embed(:meta,with:&Embeds.CommunityMeta.changeset/2)
8387
|>validate_length(:title,min:1,max:30)
88+
|>validate_length(:raw,min:1,max:30)
8489
|>foreign_key_constraint(:user_id)
85-
|>unique_constraint(:title,name::communities_title_index)
90+
|>unique_constraint(:raw,name::communities_raw_index)
8691
|>unique_constraint(:aka,name::communities_aka_index)
8792

8893
# |> foreign_key_constraint(:communities_author_fkey)

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ defmodule GroupherServer.CMS.Model.Embeds.CommunityMeta do
3131
@general_options%{
3232
editors_ids:[],
3333
subscribed_user_ids:[],
34-
contributes_digest:[]
34+
contributes_digest:[],
35+
apply_msg:"",
36+
apply_category:""
3537
}
3638

3739
@optional_fieldsMap.keys(@general_options)++
@@ -53,6 +55,9 @@ defmodule GroupherServer.CMS.Model.Embeds.CommunityMeta do
5355
# 关注相关
5456
field(:subscribed_user_ids,{:array,:integer},default:[])
5557
field(:contributes_digest,{:array,:integer},default:[])
58+
# 申请信息
59+
field(:apply_msg,:string,default:"")
60+
field(:apply_category,:string,default:"")
5661
end
5762

5863
defchangeset(struct,params)do

‎lib/groupher_server_web/resolvers/cms_resolver.ex‎

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,48 @@ defmodule GroupherServerWeb.Resolvers.CMS do
1414
# #######################
1515
# community ..
1616
# #######################
17-
defcommunity(_root,args,%{context:%{cur_user:user}})do
18-
caseEnum.empty?(args)do
19-
false->CMS.read_community(args,user)
20-
true->{:error,"please provide community id or title or raw"}
21-
end
17+
defcommunity(_root,%{raw:raw},%{context:%{cur_user:user}})do
18+
CMS.read_community(raw,user)
2219
end
2320

24-
defcommunity(_root,args,_info)do
25-
caseEnum.empty?(args)do
26-
false->CMS.read_community(args)
27-
true->{:error,"please provide community id or title or raw"}
28-
end
21+
defcommunity(_root,%{raw:raw},_info)do
22+
CMS.read_community(raw)
2923
end
3024

3125
defpaged_communities(_root,~m(filter)a,_info),do:Community|>ORM.find_all(filter)
3226

3327
defcreate_community(_root,args,%{context:%{cur_user:user}})do
3428
args=args|>Map.merge(%{user_id:user.id})
35-
Community|>ORM.create(args)
29+
CMS.create_community(args)
3630
end
3731

38-
defupdate_community(_root,args,_info),do:Community|>ORM.find_update(args)
32+
defupdate_community(_root,args,_info)do
33+
CMS.update_community(args.id,args)
34+
end
3935

4036
defdelete_community(_root,%{id:id},_info),do:Community|>ORM.find_delete!(id)
4137

38+
defapply_community(_root,args,%{context:%{cur_user:user}})do
39+
args=args|>Map.merge(%{user_id:user.id})
40+
CMS.apply_community(args)
41+
end
42+
43+
defapprove_community_apply(_root,%{id:id},_)do
44+
CMS.approve_community_apply(id)
45+
end
46+
47+
defdeny_community_apply(_root,%{id:id},_)do
48+
CMS.deny_community_apply(id)
49+
end
50+
51+
defis_community_exist?(_root,%{raw:raw},_)do
52+
CMS.is_community_exist?(raw)
53+
end
54+
55+
defhas_pending_community_apply?(_root,_,%{context:%{cur_user:user}})do
56+
CMS.has_pending_community_apply?(user)
57+
end
58+
4259
# #######################
4360
# community thread (post, job), login user should be logged
4461
# #######################

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp