@@ -4,6 +4,8 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
44 """
55 import Ecto.Query , warn: false
66 import Helper.Utils , only: [ done: 1 , strip_struct: 1 , get_config: 2 , ensure: 2 ]
7+ import GroupherServer.CMS.Delegate.Helper , only: [ article_of: 1 , thread_of: 1 ]
8+
79 import Helper.ErrorCode
810
911 import GroupherServer.CMS.Delegate.CommentCurd ,
@@ -40,26 +42,25 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
4042 { :ok , info } <- match ( full_comment . thread ) do
4143 Multi . new ( )
4244 |> Multi . run ( :checked_pined_comments_count , fn _ , _ ->
43- { :ok , pined_comments_count } =
45+ pined_comments_query =
4446 from ( p in PinnedComment ,
4547 where: field ( p , ^ info . foreign_key ) == ^ full_comment . article . id
4648 )
47- |> ORM . count ( )
4849
49- case pined_comments_count >= @ pinned_comment_limit do
50- true -> { :error , "max #{ @ pinned_comment_limit } pinned comment for each article" }
51- false -> { :ok , :pass }
50+ with { :ok , pined_comments_count } <- ORM . count ( pined_comments_query ) do
51+ case pined_comments_count >= @ pinned_comment_limit do
52+ true -> { :error , "max #{ @ pinned_comment_limit } pinned comment for each article" }
53+ false -> { :ok , :pass }
54+ end
5255 end
5356 end )
5457 |> Multi . run ( :update_comment_flag , fn _ , _ ->
5558 ORM . update ( comment , % { is_pinned: true } )
5659 end )
5760 |> Multi . run ( :add_pined_comment , fn _ , _ ->
58- PinnedComment
59- |> ORM . create (
60- % { comment_id: comment . id }
61- |> Map . put ( info . foreign_key , full_comment . article . id )
62- )
61+ attrs = % { comment_id: comment . id } |> Map . put ( info . foreign_key , full_comment . article . id )
62+
63+ PinnedComment |> ORM . create ( attrs )
6364 end )
6465 |> Repo . transaction ( )
6566 |> result ( )
@@ -81,21 +82,19 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
8182 end
8283
8384 @ doc "fold a comment"
84- def fold_comment ( % Comment { } = comment , % User { } = _user ) do
85- comment |> ORM . update ( % { is_folded: true } )
86- end
85+ def fold_comment ( % Comment { } = comment , % User { } = _user ) , do: do_fold_comment ( comment , true )
8786
88- @ doc "fold a comment"
87+ @ doc "fold a comment by id "
8988 def fold_comment ( comment_id , % User { } = _user ) do
9089 with { :ok , comment } <- ORM . find ( Comment , comment_id ) do
91- comment |> ORM . update ( % { is_folded: true } )
90+ do_fold_comment ( comment , true )
9291 end
9392 end
9493
9594 @ doc "unfold a comment"
9695 def unfold_comment ( comment_id , % User { } = _user ) do
9796 with { :ok , comment } <- ORM . find ( Comment , comment_id ) do
98- comment |> ORM . update ( % { is_folded: false } )
97+ do_fold_comment ( comment , false )
9998 end
10099 end
101100
@@ -249,6 +248,26 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
249248 end
250249 end
251250
251+ # do (un)fold and update folded count in article meta
252+ defp do_fold_comment ( % Comment { } = comment , is_folded ) when is_boolean ( is_folded ) do
253+ Multi . new ( )
254+ |> Multi . run ( :fold_comment , fn _ , _ ->
255+ comment |> ORM . update ( % { is_folded: is_folded } )
256+ end )
257+ |> Multi . run ( :update_article_fold_count , fn _ , _ ->
258+ { :ok , article } = article_of ( comment )
259+ { :ok , article_thread } = thread_of ( article )
260+
261+ { :ok , % { total_count: total_count } } =
262+ CMS . paged_folded_comments ( article_thread , article . id , % { page: 1 , size: 1 } )
263+
264+ meta = article . meta |> Map . put ( :folded_comment_count , total_count )
265+ article |> ORM . update_meta ( meta )
266+ end )
267+ |> Repo . transaction ( )
268+ |> result ( )
269+ end
270+
252271 defp update_article_author_upvoted_info ( % Comment { } = comment , user_id ) do
253272 with { :ok , article } = get_full_comment ( comment . id ) do
254273 is_article_author_upvoted = article . author . id == user_id
@@ -367,9 +386,9 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
367386 defp result ( { :ok , % { create_comment: result } } ) , do: { :ok , result }
368387 defp result ( { :ok , % { add_reply_to: result } } ) , do: { :ok , result }
369388 defp result ( { :ok , % { upvote_comment_done: result } } ) , do: { :ok , result }
370- defp result ( { :ok , % { fold_comment_report_too_many: result } } ) , do: { :ok , result }
371389 defp result ( { :ok , % { update_comment_flag: result } } ) , do: { :ok , result }
372390 defp result ( { :ok , % { delete_comment: result } } ) , do: { :ok , result }
391+ defp result ( { :ok , % { fold_comment: result } } ) , do: { :ok , result }
373392
374393 defp result ( { :error , :create_comment , result , _steps } ) do
375394 raise_error ( :create_comment , result )
0 commit comments