@@ -88,73 +88,26 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
88
88
do_list_comment_replies ( comment_id , filters , user )
89
89
end
90
90
91
- defp do_list_article_comment ( thread , article_id , filters , where_query , user ) do
91
+ @ spec list_article_comments_participators ( T . comment_thread ( ) , Integer . t ( ) , T . paged_filter ( ) ) ::
92
+ { :ok , T . paged_users ( ) }
93
+ def list_article_comments_participators ( thread , article_id , filters ) do
92
94
% { page: page , size: size } = filters
93
95
94
96
with { :ok , thread_query } <- match ( thread , :query , article_id ) do
95
- query = from ( c in ArticleComment , preload: [ reply_to: :author ] )
96
-
97
- query
97
+ ArticleComment
98
98
|> where ( ^ thread_query )
99
- |> where ( ^ where_query )
100
- |> QueryBuilder . filter_pack ( Map . merge ( filters , % { sort: :asc_inserted } ) )
99
+ |> QueryBuilder . filter_pack ( Map . merge ( filters , % { sort: :desc_inserted } ) )
100
+ |> join ( :inner , [ c ] , a in assoc ( c , :author ) )
101
+ |> distinct ( [ c , a ] , a . id )
102
+ |> group_by ( [ c , a ] , a . id )
103
+ |> group_by ( [ c , a ] , c . inserted_at )
104
+ |> select ( [ c , a ] , a )
101
105
|> ORM . paginater ( ~m( page size) a )
102
- |> set_viewer_emotion_ifneed ( user )
103
- |> add_pined_comments_ifneed ( thread , article_id , filters )
104
106
|> done ( )
105
107
end
106
108
end
107
109
108
- defp do_list_comment_replies ( comment_id , filters , user ) do
109
- % { page: page , size: size } = filters
110
- query = from ( c in ArticleComment , preload: [ reply_to: :author ] )
111
-
112
- where_query =
113
- dynamic ( [ c ] , not c . is_reported and not c . is_folded and c . reply_to_id == ^ comment_id )
114
-
115
- query
116
- |> where ( ^ where_query )
117
- |> QueryBuilder . filter_pack ( filters )
118
- |> ORM . paginater ( ~m( page size) a )
119
- |> set_viewer_emotion_ifneed ( user )
120
- |> done ( )
121
- end
122
-
123
- defp add_pined_comments_ifneed ( % { entries: entries } = paged_comments , thread , article_id , % {
124
- page: 1
125
- } ) do
126
- with { :ok , info } <- match ( thread ) ,
127
- query <-
128
- from ( p in ArticlePinedComment ,
129
- join: c in ArticleComment ,
130
- on: p . article_comment_id == c . id ,
131
- where: field ( p , ^ info . foreign_key ) == ^ article_id ,
132
- select: c
133
- ) ,
134
- { :ok , pined_comments } <- Repo . all ( query ) |> done ( ) do
135
- case pined_comments do
136
- [ ] ->
137
- paged_comments
138
-
139
- _ ->
140
- preloaded_pined_comments =
141
- Enum . slice ( pined_comments , 0 , @ pined_comment_limit )
142
- |> Repo . preload ( reply_to: :author )
143
-
144
- updated_entries = Enum . concat ( preloaded_pined_comments , entries )
145
-
146
- pined_comment_count = length ( pined_comments )
147
-
148
- Map . merge ( paged_comments , % {
149
- entries: updated_entries ,
150
- total_count: paged_comments . total_count + pined_comment_count
151
- } )
152
- end
153
- end
154
- end
155
-
156
- defp add_pined_comments_ifneed ( paged_comments , _thread , _article_id , _ ) , do: paged_comments
157
-
110
+ @ spec pin_article_comment ( Integer . t ( ) ) :: { :ok , ArticleComment . t ( ) }
158
111
@ doc "pin a comment"
159
112
def pin_article_comment ( comment_id ) do
160
113
with { :ok , comment } <- ORM . find ( ArticleComment , comment_id ) ,
@@ -452,6 +405,73 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
452
405
end
453
406
end
454
407
408
+ defp do_list_article_comment ( thread , article_id , filters , where_query , user ) do
409
+ % { page: page , size: size } = filters
410
+
411
+ with { :ok , thread_query } <- match ( thread , :query , article_id ) do
412
+ query = from ( c in ArticleComment , preload: [ reply_to: :author ] )
413
+
414
+ query
415
+ |> where ( ^ thread_query )
416
+ |> where ( ^ where_query )
417
+ |> QueryBuilder . filter_pack ( Map . merge ( filters , % { sort: :asc_inserted } ) )
418
+ |> ORM . paginater ( ~m( page size) a )
419
+ |> set_viewer_emotion_ifneed ( user )
420
+ |> add_pined_comments_ifneed ( thread , article_id , filters )
421
+ |> done ( )
422
+ end
423
+ end
424
+
425
+ defp do_list_comment_replies ( comment_id , filters , user ) do
426
+ % { page: page , size: size } = filters
427
+ query = from ( c in ArticleComment , preload: [ reply_to: :author ] )
428
+
429
+ where_query =
430
+ dynamic ( [ c ] , not c . is_reported and not c . is_folded and c . reply_to_id == ^ comment_id )
431
+
432
+ query
433
+ |> where ( ^ where_query )
434
+ |> QueryBuilder . filter_pack ( filters )
435
+ |> ORM . paginater ( ~m( page size) a )
436
+ |> set_viewer_emotion_ifneed ( user )
437
+ |> done ( )
438
+ end
439
+
440
+ defp add_pined_comments_ifneed ( % { entries: entries } = paged_comments , thread , article_id , % {
441
+ page: 1
442
+ } ) do
443
+ with { :ok , info } <- match ( thread ) ,
444
+ query <-
445
+ from ( p in ArticlePinedComment ,
446
+ join: c in ArticleComment ,
447
+ on: p . article_comment_id == c . id ,
448
+ where: field ( p , ^ info . foreign_key ) == ^ article_id ,
449
+ select: c
450
+ ) ,
451
+ { :ok , pined_comments } <- Repo . all ( query ) |> done ( ) do
452
+ case pined_comments do
453
+ [ ] ->
454
+ paged_comments
455
+
456
+ _ ->
457
+ preloaded_pined_comments =
458
+ Enum . slice ( pined_comments , 0 , @ pined_comment_limit )
459
+ |> Repo . preload ( reply_to: :author )
460
+
461
+ updated_entries = Enum . concat ( preloaded_pined_comments , entries )
462
+
463
+ pined_comment_count = length ( pined_comments )
464
+
465
+ Map . merge ( paged_comments , % {
466
+ entries: updated_entries ,
467
+ total_count: paged_comments . total_count + pined_comment_count
468
+ } )
469
+ end
470
+ end
471
+ end
472
+
473
+ defp add_pined_comments_ifneed ( paged_comments , _thread , _article_id , _ ) , do: paged_comments
474
+
455
475
defp update_article_author_upvoted_info ( % ArticleComment { } = comment , user_id ) do
456
476
with { :ok , article } = get_full_comment ( comment . id ) do
457
477
is_article_author_upvoted = article . author . id == user_id