@@ -24,13 +24,18 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
24
24
Thread
25
25
}
26
26
27
+ alias CMS.Constant
28
+
27
29
@ default_meta Embeds.CommunityMeta . default_meta ( )
28
30
@ article_threads get_config ( :article , :threads )
29
31
30
- def read_community ( clauses , user ) , do: read_community ( clauses ) |> viewer_has_states ( user )
31
- def read_community ( % { id: id } ) , do: ORM . read ( Community , id , inc: :views )
32
- def read_community ( % { raw: raw } = clauses ) , do: do_read_community ( clauses , raw )
33
- def read_community ( % { title: title } = clauses ) , do: do_read_community ( clauses , title )
32
+ @ community_normal Constant . pending ( :normal )
33
+ @ community_applying Constant . pending ( :applying )
34
+
35
+ @ default_apply_category Constant . apply_category ( :public )
36
+
37
+ def read_community ( raw , user ) , do: read_community ( raw ) |> viewer_has_states ( user )
38
+ def read_community ( raw ) , do: do_read_community ( raw )
34
39
35
40
@ doc """
36
41
create a community
@@ -54,6 +59,59 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
54
59
end
55
60
end
56
61
62
+ @ doc """
63
+ check if community exist
64
+ """
65
+ def is_community_exist? ( raw ) do
66
+ case ORM . find_by ( Community , raw: raw ) do
67
+ { :ok , _ } -> { :ok , % { exist: true } }
68
+ { :error , _ } -> { :ok , % { exist: false } }
69
+ end
70
+ end
71
+
72
+ def has_pending_community_apply? ( % User { } = user ) do
73
+ with { :ok , paged_applies } <- paged_community_applies ( user , % { page: 1 , size: 1 } ) do
74
+ case paged_applies . total_count > 0 do
75
+ true -> { :ok , % { exist: true } }
76
+ false -> { :ok , % { exist: false } }
77
+ end
78
+ end
79
+ end
80
+
81
+ def paged_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
+ def apply_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
+ def approve_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
+ def deny_community_apply ( id ) do
107
+ with { :ok , community } <- ORM . find ( Community , id ) do
108
+ case community . pending == @ community_applying do
109
+ true -> ORM . delete ( community )
110
+ false -> { :ok , community }
111
+ end
112
+ end
113
+ end
114
+
57
115
@ doc """
58
116
update editors_count of a community
59
117
"""
@@ -235,13 +293,20 @@ defmodule GroupherServer.CMS.Delegate.CommunityCURD do
235
293
end
236
294
end
237
295
238
- defp do_read_community ( clauses , aka ) do
239
- case ORM . read_by ( Community , clauses , inc: :views ) do
240
- { :ok , community } -> { :ok , community }
241
- { :error , _ } -> ORM . find_by ( Community , aka: aka )
296
+ defp do_read_community ( raw ) do
297
+ with { :ok , community } <- find_community ( raw ) do
298
+ community |> ORM . read ( inc: :views )
242
299
end
243
300
end
244
301
302
+ defp find_community ( raw ) do
303
+ Community
304
+ |> where ( [ c ] , c . pending == ^ @ community_normal )
305
+ |> where ( [ c ] , c . raw == ^ raw or c . aka == ^ raw )
306
+ |> Repo . one ( )
307
+ |> done
308
+ end
309
+
245
310
defp viewer_has_states ( { :ok , community } , % User { id: user_id } ) do
246
311
viewer_has_states = % {
247
312
viewer_has_subscribed: user_id in community . meta . subscribed_user_ids ,
0 commit comments