@@ -334,67 +334,55 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
334334 ORM . update ( comment , % { upvotes_count: upvotes_count } )
335335 end )
336336 |> Multi . run ( :check_article_author_upvoted , fn _ , _ ->
337- with { :ok , article } = get_full_comment ( comment_id ) do
338- is_article_author_upvoted = article . author . id == user_id
339-
340- case is_article_author_upvoted do
341- true ->
342- new_meta =
343- comment . meta
344- |> Map . from_struct ( )
345- |> Map . merge ( % { is_article_author_upvoted: true } )
346-
347- comment |> ORM . update ( % { meta: new_meta } )
348-
349- false ->
350- { :ok , :pass }
351- end
352- end
337+ update_article_author_upvoted_info ( comment , user_id )
353338 end )
354339 |> Repo . transaction ( )
355340 |> upsert_comment_result ( )
356341 end
357342 end
358343
359- @ spec get_full_comment ( String . t ( ) ) :: { :ok , T . article_info ( ) } | { :error , nil }
360- defp get_full_comment ( comment_id ) do
361- query = from ( c in ArticleComment , where: c . id == ^ comment_id , preload: :post , preload: :job )
344+ @ doc "upvote a comment"
345+ def undo_upvote_article_comment ( comment_id , % User { id: user_id } ) do
346+ with { :ok , comment } <- ORM . find ( ArticleComment , comment_id ) ,
347+ false <- comment . is_deleted do
348+ Multi . new ( )
349+ |> Multi . run ( :delete_comment_upvote , fn _ , _ ->
350+ ORM . findby_delete ( ArticleCommentUpvote , % {
351+ article_comment_id: comment . id ,
352+ user_id: user_id
353+ } )
354+ end )
355+ |> Multi . run ( :desc_upvotes_count , fn _ , _ ->
356+ count_query = from ( c in ArticleCommentUpvote , where: c . article_comment_id == ^ comment_id )
357+ upvotes_count = Repo . aggregate ( count_query , :count )
362358
363- with { :ok , comment } <- Repo . one ( query ) |> done ( ) do
364- extract_article_info ( comment )
359+ ORM . update ( comment , % { upvotes_count: Enum . max ( [ upvotes_count - 1 , 0 ] ) } )
360+ end )
361+ |> Multi . run ( :check_article_author_upvoted , fn _ , % { desc_upvotes_count: updated_comment } ->
362+ update_article_author_upvoted_info ( updated_comment , user_id )
363+ end )
364+ |> Repo . transaction ( )
365+ |> upsert_comment_result ( )
365366 end
366367 end
367368
368- defp extract_article_info ( % ArticleComment { post: % Post { } = post } ) when not is_nil ( post ) do
369- do_extract_article_info ( :post , post )
370- end
371-
372- defp extract_article_info ( % ArticleComment { job: % Job { } = job } ) when not is_nil ( job ) do
373- do_extract_article_info ( :job , job )
374- end
369+ defp update_article_author_upvoted_info ( % ArticleComment { } = comment , user_id ) do
370+ with { :ok , article } = get_full_comment ( comment . id ) do
371+ is_article_author_upvoted = article . author . id == user_id
375372
376- @ spec do_extract_article_info ( T . article_thread ( ) , T . article_common ( ) ) :: { :ok , T . article_info ( ) }
377- defp do_extract_article_info ( thread , article ) do
378- with { :ok , article_with_author } <- Repo . preload ( article , author: :user ) |> done ( ) ,
379- article_author <- get_in ( article_with_author , [ :author , :user ] ) do
380- #
381- article_info = % { title: article . title }
382-
383- author_info = % {
384- id: article_author . id ,
385- login: article_author . login ,
386- nickname: article_author . nickname
387- }
373+ new_meta =
374+ comment . meta
375+ |> Map . from_struct ( )
376+ |> Map . merge ( % { is_article_author_upvoted: is_article_author_upvoted } )
388377
389- { :ok , % { thread: thread , article: article_info , author: author_info } }
378+ comment |> ORM . update ( % { meta: new_meta } )
390379 end
391380 end
392381
393382 # creat article comment for parent or reply
394383 # set floor
395384 # TODO: parse editor-json
396385 # set default emotions
397- # TODO: meta
398386 defp do_create_comment (
399387 content ,
400388 foreign_key ,
@@ -511,9 +499,43 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
511499 % { paged_comments | entries: new_entries }
512500 end
513501
502+ @ spec get_full_comment ( String . t ( ) ) :: { :ok , T . article_info ( ) } | { :error , nil }
503+ defp get_full_comment ( comment_id ) do
504+ query = from ( c in ArticleComment , where: c . id == ^ comment_id , preload: :post , preload: :job )
505+
506+ with { :ok , comment } <- Repo . one ( query ) |> done ( ) do
507+ extract_article_info ( comment )
508+ end
509+ end
510+
511+ defp extract_article_info ( % ArticleComment { post: % Post { } = post } ) when not is_nil ( post ) do
512+ do_extract_article_info ( :post , post )
513+ end
514+
515+ defp extract_article_info ( % ArticleComment { job: % Job { } = job } ) when not is_nil ( job ) do
516+ do_extract_article_info ( :job , job )
517+ end
518+
519+ @ spec do_extract_article_info ( T . article_thread ( ) , T . article_common ( ) ) :: { :ok , T . article_info ( ) }
520+ defp do_extract_article_info ( thread , article ) do
521+ with { :ok , article_with_author } <- Repo . preload ( article , author: :user ) |> done ( ) ,
522+ article_author <- get_in ( article_with_author , [ :author , :user ] ) do
523+ #
524+ article_info = % { title: article . title }
525+
526+ author_info = % {
527+ id: article_author . id ,
528+ login: article_author . login ,
529+ nickname: article_author . nickname
530+ }
531+
532+ { :ok , % { thread: thread , article: article_info , author: author_info } }
533+ end
534+ end
535+
514536 defp upsert_comment_result ( { :ok , % { create_article_comment: result } } ) , do: { :ok , result }
515537 defp upsert_comment_result ( { :ok , % { add_reply_to: result } } ) , do: { :ok , result }
516- defp upsert_comment_result ( { :ok , % { inc_upvotes_count : result } } ) , do: { :ok , result }
538+ defp upsert_comment_result ( { :ok , % { check_article_author_upvoted : result } } ) , do: { :ok , result }
517539 defp upsert_comment_result ( { :ok , % { update_report_flag: result } } ) , do: { :ok , result }
518540 defp upsert_comment_result ( { :ok , % { update_comment_emotion: result } } ) , do: { :ok , result }
519541
0 commit comments