@@ -19,7 +19,8 @@ defmodule MastaniServer.CMS do
19
19
Community ,
20
20
Passport ,
21
21
CommunitySubscriber ,
22
- CommunityEditor
22
+ CommunityEditor ,
23
+ PostCommentReply
23
24
}
24
25
25
26
alias MastaniServer . { Repo , Accounts }
@@ -194,15 +195,77 @@ defmodule MastaniServer.CMS do
194
195
@ doc """
195
196
Creates a comment for psot, job ...
196
197
"""
197
- def create_comment ( part , react , part_id , user_id , body ) do
198
+ def create_comment ( part , react , part_id , % Accounts.User { id: user_id } , body ) do
198
199
with { :ok , action } <- match_action ( part , react ) ,
199
200
{ :ok , content } <- ORM . find ( action . target , part_id ) ,
200
201
{ :ok , user } <- ORM . find ( Accounts.User , user_id ) do
202
+ # TODO
201
203
attrs = % { post_id: content . id , author_id: user . id , body: body }
202
204
action . reactor |> ORM . create ( attrs )
203
205
end
204
206
end
205
207
208
+ def list_comments ( part , part_id , % { page: page , size: size } = filters ) do
209
+ with { :ok , action } <- match_action ( part , :comment ) do
210
+ action . reactor
211
+ |> where ( [ c ] , c . post_id == ^ part_id )
212
+ |> QueryBuilder . filter_pack ( filters )
213
+ |> ORM . paginater ( page: page , size: size )
214
+ |> done ( )
215
+ end
216
+ end
217
+
218
+ def list_replies ( part , comment_id , % Accounts.User { id: user_id } ) do
219
+ with { :ok , action } <- match_action ( part , :comment ) do
220
+ action . reactor
221
+ |> where ( [ c ] , c . author_id == ^ user_id )
222
+ |> join ( :inner , [ c ] , r in assoc ( c , :reply_to ) )
223
+ |> where ( [ c , r ] , r . id == ^ comment_id )
224
+ |> Repo . all ( )
225
+ |> done ( )
226
+ end
227
+ end
228
+
229
+ def reply_comment ( part , comment_id , % Accounts.User { id: user_id } , body ) do
230
+ with { :ok , action } <- match_action ( part , :comment ) ,
231
+ { :ok , comment } <- ORM . find ( action . reactor , comment_id ) do
232
+ attrs = % { post_id: comment . post_id , author_id: user_id , body: body , reply_to: comment }
233
+ # TODO: use Multi task to refactor
234
+ case action . reactor |> ORM . create ( attrs ) do
235
+ { :ok , reply } ->
236
+ ORM . update ( reply , % { reply_id: comment . id } )
237
+
238
+ { :ok , _ } =
239
+ PostCommentReply |> ORM . create ( % { post_comment_id: comment . id , reply_id: reply . id } )
240
+
241
+ { :ok , reply }
242
+
243
+ { :error , error } ->
244
+ { :error , error }
245
+ end
246
+ end
247
+ end
248
+
249
+ # can not use spectial: post_comment_id
250
+ def like_comment ( part , comment_id , % Accounts.User { id: user_id } ) do
251
+ with { :ok , action } <- match_action ( part , :comment ) do
252
+ case ORM . find_by ( action . like , post_comment_id: comment_id , user_id: user_id ) do
253
+ { :ok , _ } ->
254
+ { :error , "user has liked this comment" }
255
+
256
+ { :error , _ } ->
257
+ attrs = % { post_comment_id: comment_id , user_id: user_id }
258
+ action . like |> ORM . create ( attrs )
259
+ end
260
+ end
261
+ end
262
+
263
+ def undo_like_comment ( part , comment_id , % Accounts.User { id: user_id } ) do
264
+ with { :ok , action } <- match_action ( part , :comment ) do
265
+ ORM . findby_delete ( action . like , post_comment_id: comment_id , user_id: user_id )
266
+ end
267
+ end
268
+
206
269
@ doc """
207
270
subscribe a community. (ONLY community, post etc use watch )
208
271
"""
@@ -246,8 +309,8 @@ defmodule MastaniServer.CMS do
246
309
247
310
with or without page info
248
311
"""
249
- def reaction_users ( part , react , id , % { page: page , size: size } = filters )
250
- when valid_reaction ( part , react ) do
312
+ def reaction_users ( part , react , id , % { page: page , size: size } = filters ) do
313
+ # when valid_reaction(part, react) do
251
314
with { :ok , action } <- match_action ( part , react ) ,
252
315
{ :ok , where } <- dynamic_where ( part , id ) do
253
316
# common_filter(action.reactor)
@@ -262,7 +325,9 @@ defmodule MastaniServer.CMS do
262
325
@ doc """
263
326
favorite / star / watch CMS contents like post / tuts / video ...
264
327
"""
265
- def reaction ( part , react , part_id , user_id ) when valid_reaction ( part , react ) do
328
+ # TODO: def reaction(part, react, part_id, %Accounts.User{id: user_id}) when valid_reaction(part, react) do
329
+ def reaction ( part , react , part_id , % Accounts.User { id: user_id } )
330
+ when valid_reaction ( part , react ) do
266
331
with { :ok , action } <- match_action ( part , react ) ,
267
332
{ :ok , content } <- ORM . find ( action . target , part_id ) ,
268
333
{ :ok , user } <- ORM . find ( Accounts.User , user_id ) do