@@ -14,8 +14,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
1414 alias GroupherServer . { Accounts , CMS , Repo }
1515
1616 alias Accounts.User
17- # TODO: why Post?
18- alias CMS . { ArticleComment , ArticlePinedComment , Embeds , Post }
17+ alias CMS . { ArticleComment , ArticlePinedComment , Embeds }
1918 alias Ecto.Multi
2019
2120 @ max_participator_count ArticleComment . max_participator_count ( )
@@ -141,6 +140,68 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
141140 |> upsert_comment_result ( )
142141 end
143142
143+ # add participator to article-like content (Post, Job ...) and update count
144+ def add_participator_to_article (
145+ % { article_comments_participators: article_comments_participators } = article ,
146+ % User { } = user
147+ ) do
148+ total_participators = article_comments_participators |> List . insert_at ( 0 , user ) |> Enum . uniq ( )
149+ new_comment_participators = total_participators |> Enum . slice ( 0 , @ max_participator_count )
150+ total_participators_count = length ( total_participators )
151+
152+ article
153+ |> Ecto.Changeset . change ( )
154+ |> Ecto.Changeset . put_change ( :article_comments_participators_count , total_participators_count )
155+ |> Ecto.Changeset . put_embed ( :article_comments_participators , new_comment_participators )
156+ |> Repo . update ( )
157+ end
158+
159+ def add_participator_to_article ( _ , _ ) , do: { :ok , :pass }
160+
161+ # update comment's parent article's comments total count
162+ @ spec update_article_comments_count ( ArticleComment . t ( ) , :inc | :dec ) :: ArticleComment . t ( )
163+ def update_article_comments_count ( % ArticleComment { } = comment , opt ) do
164+ with { :ok , article_info } <- match ( :comment_article , comment ) ,
165+ { :ok , article } <- ORM . find ( article_info . model , article_info . id ) do
166+ count_query =
167+ from ( c in ArticleComment , where: field ( c , ^ article_info . foreign_key ) == ^ article_info . id )
168+
169+ cur_count = Repo . aggregate ( count_query , :count )
170+
171+ # dec 是 comment 还没有删除的时候的操作,和 inc 不同
172+ # 因为 dec 操作如果放在 delete 后面,那么 update 会失败
173+ case opt do
174+ :inc -> ORM . update ( article , % { article_comments_count: cur_count } )
175+ :dec -> ORM . update ( article , % { article_comments_count: Enum . max ( [ 1 , cur_count ] ) - 1 } )
176+ end
177+ end
178+ end
179+
180+ # creat article comment for parent or reply
181+ # set floor
182+ # TODO: parse editor-json
183+ # set default emotions
184+ def do_create_comment ( content , foreign_key , article , % User { id: user_id } ) do
185+ count_query = from ( c in ArticleComment , where: field ( c , ^ foreign_key ) == ^ article . id )
186+ floor = Repo . aggregate ( count_query , :count ) + 1
187+
188+ ArticleComment
189+ |> ORM . create (
190+ Map . put (
191+ % {
192+ author_id: user_id ,
193+ body_html: content ,
194+ emotions: @ default_emotions ,
195+ floor: floor ,
196+ is_article_author: user_id == article . author . user . id ,
197+ meta: @ default_comment_meta
198+ } ,
199+ foreign_key ,
200+ article . id
201+ )
202+ )
203+ end
204+
144205 defp do_list_article_comment ( thread , article_id , filters , where_query , user ) do
145206 % { page: page , size: size } = filters
146207
@@ -210,69 +271,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
210271
211272 defp add_pined_comments_ifneed ( paged_comments , _thread , _article_id , _ ) , do: paged_comments
212273
213- # update comment's parent article's comments total count
214- @ spec update_article_comments_count ( ArticleComment . t ( ) , :inc | :dec ) :: ArticleComment . t ( )
215- defp update_article_comments_count ( % ArticleComment { } = comment , opt ) do
216- with { :ok , article_info } <- match ( :comment_article , comment ) ,
217- { :ok , article } <- ORM . find ( article_info . model , article_info . id ) do
218- count_query =
219- from ( c in ArticleComment , where: field ( c , ^ article_info . foreign_key ) == ^ article_info . id )
220-
221- cur_count = Repo . aggregate ( count_query , :count )
222-
223- # dec 是 comment 还没有删除的时候的操作,和 inc 不同
224- # 因为 dec 操作如果放在 delete 后面,那么 update 会失败
225- case opt do
226- :inc -> ORM . update ( article , % { article_comments_count: cur_count } )
227- :dec -> ORM . update ( article , % { article_comments_count: Enum . max ( [ 1 , cur_count ] ) - 1 } )
228- end
229- end
230- end
231-
232- # creat article comment for parent or reply
233- # set floor
234- # TODO: parse editor-json
235- # set default emotions
236- defp do_create_comment ( content , foreign_key , article , % User { id: user_id } ) do
237- count_query = from ( c in ArticleComment , where: field ( c , ^ foreign_key ) == ^ article . id )
238- floor = Repo . aggregate ( count_query , :count ) + 1
239-
240- ArticleComment
241- |> ORM . create (
242- Map . put (
243- % {
244- author_id: user_id ,
245- body_html: content ,
246- emotions: @ default_emotions ,
247- floor: floor ,
248- is_article_author: user_id == article . author . user . id ,
249- meta: @ default_comment_meta
250- } ,
251- foreign_key ,
252- article . id
253- )
254- )
255- end
256-
257- # add participator to article-like content (Post, Job ...) and update count
258- defp add_participator_to_article ( % Post { } = article , % User { } = user ) do
259- total_participators =
260- article . article_comments_participators
261- |> List . insert_at ( 0 , user )
262- |> Enum . uniq ( )
263-
264- new_comment_participators = total_participators |> Enum . slice ( 0 , @ max_participator_count )
265- total_participators_count = length ( total_participators )
266-
267- article
268- |> Ecto.Changeset . change ( )
269- |> Ecto.Changeset . put_change ( :article_comments_participators_count , total_participators_count )
270- |> Ecto.Changeset . put_embed ( :article_comments_participators , new_comment_participators )
271- |> Repo . update ( )
272- end
273-
274- defp add_participator_to_article ( _ , _ ) , do: { :ok , :pass }
275-
276274 defp user_in_logins? ( [ ] , _ ) , do: false
277275 defp user_in_logins? ( ids_list , % User { login: login } ) , do: Enum . member? ( ids_list , login )
278276
0 commit comments