@@ -8,6 +8,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
8
8
import GroupherServer.CMS.Utils.Matcher2
9
9
import ShortMaps
10
10
11
+ alias Helper.Types , as: T
11
12
alias Helper . { ORM , QueryBuilder }
12
13
alias GroupherServer . { Accounts , CMS , Repo }
13
14
@@ -33,6 +34,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
33
34
@ delete_hint ArticleComment . delete_hint ( )
34
35
@ report_threshold_for_fold ArticleComment . report_threshold_for_fold ( )
35
36
37
+ @ default_comment_meta Embeds.ArticleCommentMeta . default_meta ( )
38
+
36
39
@ doc """
37
40
list paged article comments
38
41
"""
@@ -317,10 +320,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
317
320
end
318
321
319
322
@ doc "upvote a comment"
320
- # TODO: user has upvoted logic, move to GraphQL
321
323
def upvote_article_comment ( comment_id , % User { id: user_id } ) do
322
324
with { :ok , comment } <- ORM . find ( ArticleComment , comment_id ) ,
323
325
false <- comment . is_deleted do
326
+ # IO.inspect(comment, label: "the comment")
324
327
Multi . new ( )
325
328
|> Multi . run ( :create_comment_upvote , fn _ , _ ->
326
329
ORM . create ( ArticleCommentUpvote , % { article_comment_id: comment . id , user_id: user_id } )
@@ -330,15 +333,68 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
330
333
upvotes_count = Repo . aggregate ( count_query , :count )
331
334
ORM . update ( comment , % { upvotes_count: upvotes_count } )
332
335
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 )
333
354
|> Repo . transaction ( )
334
355
|> upsert_comment_result ( )
335
356
end
336
357
end
337
358
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
+
338
393
# creat article comment for parent or reply
339
394
# set floor
340
395
# TODO: parse editor-json
341
396
# set default emotions
397
+ # TODO: meta
342
398
defp do_create_comment (
343
399
content ,
344
400
foreign_key ,
@@ -358,7 +414,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
358
414
body_html: content ,
359
415
emotions: @ default_emotions ,
360
416
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
362
419
} ,
363
420
foreign_key ,
364
421
article_id