@@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
3
3
CURD and operations for article comments
4
4
"""
5
5
import Ecto.Query , warn: false
6
- import Helper.Utils , only: [ done: 1 , ensure: 2 , get_config: 2 ]
6
+ import Helper.Utils , only: [ done: 1 , ensure: 2 , strip_struct: 1 , get_config: 2 ]
7
7
import Helper.ErrorCode
8
8
9
9
import GroupherServer.CMS.Delegate.Helper ,
@@ -110,24 +110,40 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
110
110
@ spec paged_comments_participants ( T . article_thread ( ) , Integer . t ( ) , T . paged_filter ( ) ) ::
111
111
{ :ok , T . paged_users ( ) }
112
112
def paged_comments_participants ( thread , article_id , filters ) do
113
- % { page: page , size: size } = filters
113
+ with { :ok , thread_query } <- match ( thread , :query , article_id ) ,
114
+ { :ok , info } <- match ( thread ) ,
115
+ { :ok , article } <- ORM . find ( info . model , article_id ) ,
116
+ { :ok , paged_data } <- do_paged_comments_participants ( thread_query , filters ) do
117
+ # check participants_count if history data do not match
118
+ case article . comments_participants_count !== paged_data . total_count do
119
+ true ->
120
+ article |> ORM . update ( % { comments_participants_count: paged_data . total_count } )
121
+
122
+ false ->
123
+ { :ok , :pass }
124
+ end
114
125
115
- with { :ok , thread_query } <- match ( thread , :query , article_id ) do
116
- Comment
117
- |> where ( ^ thread_query )
118
- |> QueryBuilder . filter_pack ( Map . merge ( filters , % { sort: :desc_inserted } ) )
119
- |> join ( :inner , [ c ] , a in assoc ( c , :author ) )
120
- |> distinct ( [ c , a ] , a . id )
121
- # group_by
122
- |> group_by ( [ c , a ] , a . id )
123
- |> group_by ( [ c , a ] , c . inserted_at )
124
- |> group_by ( [ c , a ] , c . id )
125
- |> select ( [ c , a ] , a )
126
- |> ORM . paginator ( ~m( page size) a )
127
- |> done ( )
126
+ paged_data |> done
128
127
end
129
128
end
130
129
130
+ defp do_paged_comments_participants ( query , filters ) do
131
+ % { page: page , size: size } = filters
132
+
133
+ Comment
134
+ |> where ( ^ query )
135
+ |> QueryBuilder . filter_pack ( Map . merge ( filters , % { sort: :desc_inserted } ) )
136
+ |> join ( :inner , [ c ] , a in assoc ( c , :author ) )
137
+ |> distinct ( [ c , a ] , a . id )
138
+ # group_by
139
+ |> group_by ( [ c , a ] , a . id )
140
+ |> group_by ( [ c , a ] , c . inserted_at )
141
+ |> group_by ( [ c , a ] , c . id )
142
+ |> select ( [ c , a ] , a )
143
+ |> ORM . paginator ( ~m( page size) a )
144
+ |> done ( )
145
+ end
146
+
131
147
@ doc """
132
148
creates a comment for article like psot, job ...
133
149
"""
@@ -273,15 +289,19 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
273
289
274
290
# add participator to article-like(Post, Job ...) and update count
275
291
def add_participant_to_article ( % { comments_participants: participants } = article , % User { } = user ) do
276
- total_participants = participants |> List . insert_at ( 0 , user ) |> Enum . uniq_by ( & & 1 . id )
292
+ cur_participants = participants |> List . insert_at ( 0 , user ) |> Enum . uniq_by ( & & 1 . id )
293
+
294
+ meta = article . meta |> strip_struct
295
+ cur_participants_ids = ( meta . comments_participant_user_ids ++ [ user . id ] ) |> Enum . uniq ( )
296
+ meta = Map . merge ( meta , % { comments_participant_user_ids: cur_participants_ids } )
277
297
278
- latest_participants = total_participants |> Enum . slice ( 0 , @ max_participator_count )
279
- total_participants_count = length ( total_participants )
298
+ latest_participants = cur_participants |> Enum . slice ( 0 , @ max_participator_count )
280
299
281
300
article
282
301
|> Ecto.Changeset . change ( )
283
- |> Ecto.Changeset . put_change ( :comments_participants_count , total_participants_count )
302
+ |> Ecto.Changeset . put_change ( :comments_participants_count , cur_participants_ids |> length )
284
303
|> Ecto.Changeset . put_embed ( :comments_participants , latest_participants )
304
+ |> Ecto.Changeset . put_embed ( :meta , meta )
285
305
|> Repo . update ( )
286
306
end
287
307