@@ -8,6 +8,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
88 import GroupherServer.CMS.Utils.Matcher2
99 import ShortMaps
1010
11+ alias Helper.Types , as: T
1112 alias Helper . { ORM , QueryBuilder }
1213 alias GroupherServer . { Accounts , CMS , Repo }
1314
@@ -33,6 +34,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
3334 @ delete_hint ArticleComment . delete_hint ( )
3435 @ report_threshold_for_fold ArticleComment . report_threshold_for_fold ( )
3536
37+ @ default_comment_meta Embeds.ArticleCommentMeta . default_meta ( )
38+
3639 @ doc """
3740 list paged article comments
3841 """
@@ -317,10 +320,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
317320 end
318321
319322 @ doc "upvote a comment"
320- # TODO: user has upvoted logic, move to GraphQL
321323 def upvote_article_comment ( comment_id , % User { id: user_id } ) do
322324 with { :ok , comment } <- ORM . find ( ArticleComment , comment_id ) ,
323325 false <- comment . is_deleted do
326+ # IO.inspect(comment, label: "the comment")
324327 Multi . new ( )
325328 |> Multi . run ( :create_comment_upvote , fn _ , _ ->
326329 ORM . create ( ArticleCommentUpvote , % { article_comment_id: comment . id , user_id: user_id } )
@@ -330,15 +333,68 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
330333 upvotes_count = Repo . aggregate ( count_query , :count )
331334 ORM . update ( comment , % { upvotes_count: upvotes_count } )
332335 end )
336+ |> 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
353+ end )
333354 |> Repo . transaction ( )
334355 |> upsert_comment_result ( )
335356 end
336357 end
337358
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 )
362+
363+ with { :ok , comment } <- Repo . one ( query ) |> done ( ) do
364+ extract_article_info ( comment )
365+ end
366+ end
367+
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
375+
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+ }
388+
389+ { :ok , % { thread: thread , article: article_info , author: author_info } }
390+ end
391+ end
392+
338393 # creat article comment for parent or reply
339394 # set floor
340395 # TODO: parse editor-json
341396 # set default emotions
397+ # TODO: meta
342398 defp do_create_comment (
343399 content ,
344400 foreign_key ,
@@ -358,7 +414,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
358414 body_html: content ,
359415 emotions: @ default_emotions ,
360416 floor: floor ,
361- is_article_author: user_id == article_author_id
417+ is_article_author: user_id == article_author_id ,
418+ meta: @ default_comment_meta
362419 } ,
363420 foreign_key ,
364421 article_id
0 commit comments