@@ -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
@@ -363,11 +363,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
363363 def upvote_article_comment ( comment_id , % User { id: user_id } ) do
364364 with { :ok , comment } <- ORM . find ( ArticleComment , comment_id ) ,
365365 false <- comment . is_deleted do
366+ # TODO: is user upvoted before?
366367 # IO.inspect(comment, label: "the comment")
367368 Multi . new ( )
368369 |> Multi . run ( :create_comment_upvote , fn _ , _ ->
369370 ORM . create ( ArticleCommentUpvote , % { article_comment_id: comment . id , user_id: user_id } )
370371 end )
372+ |> Multi . run ( :add_upvoted_user , fn _ , _ ->
373+ update_upvoted_user_list ( comment , user_id , :add )
374+ end )
371375 |> Multi . run ( :inc_upvotes_count , fn _ , _ ->
372376 count_query = from ( c in ArticleCommentUpvote , where: c . article_comment_id == ^ comment . id )
373377 upvotes_count = Repo . aggregate ( count_query , :count )
@@ -381,6 +385,20 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
381385 end
382386 end
383387
388+ defp update_upvoted_user_list ( comment , user_id , opt ) do
389+ IO . inspect ( comment . meta , label: "update_upvoted_user_list meta" )
390+ cur_user_ids = get_in ( comment , [ :meta , :upvoted_user_ids ] )
391+
392+ user_ids =
393+ case opt do
394+ :add -> [ user_id ] ++ cur_user_ids
395+ :remove -> cur_user_ids -- [ user_id ]
396+ end
397+
398+ meta = comment . meta |> Map . merge ( % { upvoted_user_ids: user_ids } ) |> strip_struct
399+ ORM . update_meta ( comment , meta )
400+ end
401+
384402 @ doc "upvote a comment"
385403 def undo_upvote_article_comment ( comment_id , % User { id: user_id } ) do
386404 with { :ok , comment } <- ORM . find ( ArticleComment , comment_id ) ,
0 commit comments