Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commitc0bbf97

Browse files
committed
refactor(article-comments): add is_article_author && meta
1 parenta3ae8ce commitc0bbf97

File tree

8 files changed

+110
-12
lines changed

8 files changed

+110
-12
lines changed

‎lib/groupher_server/cms/article_comment.ex‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ defmodule GroupherServer.CMS.ArticleComment do
8282
article_comment
8383
|>cast(attrs,@required_fields++@optional_fields)
8484
|>cast_embed(:emotions,required:true,with:&Embeds.ArticleCommentEmotion.changeset/2)
85+
|>cast_embed(:meta,required:true,with:&Embeds.ArticleCommentMeta.changeset/2)
8586
|>validate_required(@required_fields)
8687
|>generl_changeset
8788
end
@@ -90,7 +91,7 @@ defmodule GroupherServer.CMS.ArticleComment do
9091
defupdate_changeset(%ArticleComment{}=article_comment,attrs)do
9192
article_comment
9293
|>cast(attrs,@required_fields++@updatable_fields)
93-
#|> cast_embed(:emotions, required:false, with: &Embeds.ArticleCommentEmotion.changeset/2)
94+
|>cast_embed(:meta,required:true,with:&Embeds.ArticleCommentMeta.changeset/2)
9495
|>generl_changeset
9596
end
9697

‎lib/groupher_server/cms/author.ex‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ defmodule GroupherServer.CMS.Author do
33
alias__MODULE__
44

55
useEcto.Schema
6+
useAccessible
7+
68
importEcto.Changeset
79

810
aliasGroupherServer.{Accounts,CMS}
9-
1011
aliasCMS.Post
1112

1213
@typet::%Author{}

‎lib/groupher_server/cms/delegates/article_comment.ex‎

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
88
importGroupherServer.CMS.Utils.Matcher2
99
importShortMaps
1010

11+
aliasHelper.Types,as:T
1112
aliasHelper.{ORM,QueryBuilder}
1213
aliasGroupherServer.{Accounts,CMS,Repo}
1314

@@ -33,6 +34,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
3334
@delete_hintArticleComment.delete_hint()
3435
@report_threshold_for_foldArticleComment.report_threshold_for_fold()
3536

37+
@default_comment_metaEmbeds.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
defupvote_article_comment(comment_id,%User{id:user_id})do
322324
with{:ok,comment}<-ORM.find(ArticleComment,comment_id),
323325
false<-comment.is_deleteddo
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+
caseis_article_author_upvoteddo
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+
@specget_full_comment(String.t())::{:ok,T.article_info()}|{:error,nil}
360+
defpget_full_comment(comment_id)do
361+
query=from(cinArticleComment,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+
defpextract_article_info(%ArticleComment{post:%Post{}=post})whennotis_nil(post)do
369+
do_extract_article_info(:post,post)
370+
end
371+
372+
defpextract_article_info(%ArticleComment{job:%Job{}=job})whennotis_nil(job)do
373+
do_extract_article_info(:job,job)
374+
end
375+
376+
@specdo_extract_article_info(T.article_thread(),T.article_common())::{:ok,T.article_info()}
377+
defpdo_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
defpdo_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

‎lib/groupher_server/cms/embeds/article_comment_meta.ex‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
33
general article comment meta info
44
"""
55
useEcto.Schema
6+
importEcto.Changeset
67

7-
aliasCMS.Embeds
8+
@optional_fields~w(is_article_author_upvoted is_solution report_count)a
89

910
@default_meta%{
1011
is_article_author_upvoted:false,
1112
is_solution:false,
12-
report_count:0,
13-
report_users:[]
13+
report_count:0
1414
}
1515

1616
@doc"for test usage"
@@ -21,6 +21,10 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
2121
field(:is_solution,:boolean,default:false)
2222

2323
field(:report_count,:integer,default:0)
24-
embeds_many(:report_users,Embeds.User,on_replace::delete)
24+
end
25+
26+
defchangeset(struct,params)do
27+
struct
28+
|>cast(params,@optional_fields)
2529
end
2630
end

‎lib/groupher_server/cms/job.ex‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule GroupherServer.CMS.Job do
33
alias__MODULE__
44

55
useEcto.Schema
6+
useAccessible
7+
68
importEcto.Changeset
79

810
aliasGroupherServer.CMS

‎lib/groupher_server/cms/post.ex‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule GroupherServer.CMS.Post do
33
alias__MODULE__
44

55
useEcto.Schema
6+
useAccessible
7+
68
importEcto.Changeset
79

810
aliasGroupherServer.{CMS,Accounts}

‎lib/helper/types.ex‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,19 @@ defmodule Helper.Types do
2828
company:nil|String.t()
2929
}
3030

31+
@typearticle_thread:::post|:job
32+
@typearticle_info::%{
33+
thread:article_thread,
34+
article:%{
35+
title:String.t()
36+
},
37+
author:%{
38+
id:Integer.t(),
39+
login:String.t(),
40+
nickname:String.t()
41+
}
42+
}
43+
3144
@typedoc"""
3245
editor.js's header tool data format
3346
"""

‎test/groupher_server/cms/article_comment_test.exs‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
44
useGroupherServer.TestTools
55

66
aliasHelper.ORM
7-
aliasGroupherServer.CMS
7+
aliasGroupherServer.{Accounts,CMS}
88

9-
aliasCMS.{ArticleComment,Post,Job}
9+
aliasCMS.{ArticleComment,Embeds,Post,Job}
1010

1111
@delete_hintCMS.ArticleComment.delete_hint()
1212
@report_threshold_for_foldArticleComment.report_threshold_for_fold()
13+
@default_comment_metaEmbeds.ArticleCommentMeta.default_meta()
1314

1415
setupdo
1516
{:ok,user}=db_insert(:user)
@@ -21,7 +22,6 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
2122
end
2223

2324
describe"[basic article comment]"do
24-
@tag:wip2
2525
test"post, job are supported by article comment.",~m(user post job)ado
2626
post_comment_1="post_comment 1"
2727
post_comment_2="post_comment 2"
@@ -40,6 +40,12 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
4040
assertList.first(post.article_comments).body_html==post_comment_1
4141
assertList.first(job.article_comments).body_html==job_comment_1
4242
end
43+
44+
@tag:wip2
45+
test"comment should have default meta after create",~m(user post)ado
46+
{:ok,comment}=CMS.create_article_comment(:post,post.id,"post comment",user)
47+
assertcomment.meta|>Map.from_struct()|>Map.delete(:id)==@default_comment_meta
48+
end
4349
end
4450

4551
describe"[article comment floor]"do
@@ -112,6 +118,18 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
112118
assertList.first(comment.upvotes).user_id==user.id
113119
end
114120

121+
@tag:wip2
122+
test"article author upvote post comment will have flag",~m(post user)ado
123+
comment="post_comment"
124+
{:ok,comment}=CMS.create_article_comment(:post,post.id,comment,user)
125+
{:ok,author_user}=ORM.find(Accounts.User,post.author.user.id)
126+
127+
CMS.upvote_article_comment(comment.id,author_user)
128+
129+
{:ok,comment}=ORM.find(ArticleComment,comment.id,preload::upvotes)
130+
assertcomment.meta.is_article_author_upvoted
131+
end
132+
115133
@tag:wip
116134
test"user can upvote a job comment",~m(user job)ado
117135
comment="job_comment"
@@ -403,7 +421,7 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
403421
{:ok,comment}=CMS.create_article_comment(:post,post.id,"commment",user)
404422
assertnotcomment.is_article_author
405423

406-
{:ok,author_user}=db_insert(:user,%{id:post.author_id})
424+
{:ok,author_user}=db_insert(:user,%{id:post.author.user.id})
407425
{:ok,comment}=CMS.create_article_comment(:post,post.id,"commment",author_user)
408426
assertcomment.is_article_author
409427
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp