@@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
33 CURD and operations for article comments
44 """
55 import Ecto.Query , warn: false
6- import Helper.Utils , only: [ done: 1 ]
6+ import Helper.Utils , only: [ done: 1 , strip_struct: 1 ]
77 import Helper.ErrorCode
88
99 import GroupherServer.CMS.Utils.Matcher2
@@ -281,13 +281,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
281281 end )
282282 |> Repo . transaction ( )
283283 |> upsert_comment_result
284-
285- # is not work this way, why?
286- # updated_emotions =
287- # Map.merge(comment.emotions, %{
288- # downvote_count: comment.emotions.downvote_count + Enum.random([1, 2, 3]),
289- # tada_count: comment.emotions.tada_count + Enum.random([1, 2, 3])
290- # })
291284 end
292285 end
293286
@@ -363,12 +356,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
363356 def upvote_article_comment ( comment_id , % User { id: user_id } ) do
364357 with { :ok , comment } <- ORM . find ( ArticleComment , comment_id ) ,
365358 false <- comment . is_deleted do
366- # IO.inspect(comment, label: "the comment")
359+ # TODO: is user upvoted before?
367360 Multi . new ( )
368361 |> Multi . run ( :create_comment_upvote , fn _ , _ ->
369362 ORM . create ( ArticleCommentUpvote , % { article_comment_id: comment . id , user_id: user_id } )
370363 end )
371- |> Multi . run ( :inc_upvotes_count , fn _ , _ ->
364+ |> Multi . run ( :add_upvoted_user , fn _ , _ ->
365+ update_upvoted_user_list ( comment , user_id , :add )
366+ end )
367+ |> Multi . run ( :inc_upvotes_count , fn _ , % { add_upvoted_user: comment } ->
372368 count_query = from ( c in ArticleCommentUpvote , where: c . article_comment_id == ^ comment . id )
373369 upvotes_count = Repo . aggregate ( count_query , :count )
374370 ORM . update ( comment , % { upvotes_count: upvotes_count } )
@@ -392,7 +388,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
392388 user_id: user_id
393389 } )
394390 end )
395- |> Multi . run ( :desc_upvotes_count , fn _ , _ ->
391+ |> Multi . run ( :remove_upvoted_user , fn _ , _ ->
392+ update_upvoted_user_list ( comment , user_id , :remove )
393+ end )
394+ |> Multi . run ( :desc_upvotes_count , fn _ , % { remove_upvoted_user: comment } ->
396395 count_query = from ( c in ArticleCommentUpvote , where: c . article_comment_id == ^ comment_id )
397396 upvotes_count = Repo . aggregate ( count_query , :count )
398397
@@ -419,6 +418,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
419418 |> ORM . paginater ( ~m( page size) a )
420419 |> set_viewer_emotion_ifneed ( user )
421420 |> add_pined_comments_ifneed ( thread , article_id , filters )
421+ |> mark_viewer_has_upvoted ( user )
422422 |> done ( )
423423 end
424424 end
@@ -435,6 +435,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
435435 |> QueryBuilder . filter_pack ( filters )
436436 |> ORM . paginater ( ~m( page size) a )
437437 |> set_viewer_emotion_ifneed ( user )
438+ |> mark_viewer_has_upvoted ( user )
438439 |> done ( )
439440 end
440441
@@ -607,6 +608,18 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
607608 % { paged_comments | entries: new_entries }
608609 end
609610
611+ defp mark_viewer_has_upvoted ( paged_comments , nil ) , do: paged_comments
612+
613+ defp mark_viewer_has_upvoted ( % { entries: entries } = paged_comments , % User { } = user ) do
614+ entries =
615+ Enum . map (
616+ entries ,
617+ & Map . merge ( & 1 , % { viewer_has_upvoted: Enum . member? ( & 1 . meta . upvoted_user_ids , user . id ) } )
618+ )
619+
620+ Map . merge ( paged_comments , % { entries: entries } )
621+ end
622+
610623 defp get_article ( % ArticleComment { post_id: post_id } = comment ) when not is_nil ( post_id ) do
611624 with { :ok , article } <- ORM . find ( Post , comment . post_id , preload: [ author: :user ] ) do
612625 { :post , article }
@@ -674,6 +687,19 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
674687 end
675688 end
676689
690+ defp update_upvoted_user_list ( comment , user_id , opt ) do
691+ cur_user_ids = get_in ( comment , [ :meta , :upvoted_user_ids ] )
692+
693+ user_ids =
694+ case opt do
695+ :add -> [ user_id ] ++ cur_user_ids
696+ :remove -> cur_user_ids -- [ user_id ]
697+ end
698+
699+ meta = comment . meta |> Map . merge ( % { upvoted_user_ids: user_ids } ) |> strip_struct
700+ ORM . update_meta ( comment , meta )
701+ end
702+
677703 defp upsert_comment_result ( { :ok , % { create_article_comment: result } } ) , do: { :ok , result }
678704 defp upsert_comment_result ( { :ok , % { add_reply_to: result } } ) , do: { :ok , result }
679705 defp upsert_comment_result ( { :ok , % { check_article_author_upvoted: result } } ) , do: { :ok , result }
0 commit comments