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.

Commit271d095

Browse files
authored
Merge pull request#320 from coderplanets/article-comments-author-has-upvoted
refactor(article-comments): author has upvoted
2 parentsa3ae8ce +5549752 commit271d095

File tree

19 files changed

+190
-92
lines changed

19 files changed

+190
-92
lines changed

‎cover/excoveralls.json‎

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

‎lib/groupher_server/accounts/delegates/fans.ex‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ defmodule GroupherServer.Accounts.Delegate.Fans do
7575
{:ok,_follow_user}<-ORM.find(User,follower_id)do
7676
Multi.new()
7777
|>Multi.run(:delete_follower,fn_,_->
78-
ORM.findby_delete(UserFollower,%{user_id:follower_id,follower_id:user_id})
78+
ORM.findby_delete!(UserFollower,%{user_id:follower_id,follower_id:user_id})
7979
end)
8080
|>Multi.run(:delete_following,fn_,_->
81-
ORM.findby_delete(UserFollowing,%{user_id:user_id,following_id:follower_id})
81+
ORM.findby_delete!(UserFollowing,%{user_id:user_id,following_id:follower_id})
8282
end)
8383
|>Multi.run(:minus_achievement,fn_,_->
8484
Accounts.achieve(%User{id:follower_id},:minus,:follow)

‎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/cms.ex‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ defmodule GroupherServer.CMS do
124124

125125
defdelegatecreate_article_comment(thread,article_id,args,user),to:ArticleComment
126126
defdelegateupvote_article_comment(comment_id,user),to:ArticleComment
127+
defdelegateundo_upvote_article_comment(comment_id,user),to:ArticleComment
127128
defdelegatedelete_article_comment(comment_id,user),to:ArticleComment
128129
defdelegatereply_article_comment(comment_id,args,user),to:ArticleComment
129130

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

Lines changed: 88 additions & 16 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
"""
@@ -263,7 +266,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
263266
with{:ok,info}<-match(thread),
264267
# make sure the article exsit
265268
# author is passed by middleware, it's exsit for sure
266-
{:ok,article}<-ORM.find(info.model,article_id)do
269+
{:ok,article}<-ORM.find(info.model,article_id,preload:[author::user])do
267270
Multi.new()
268271
|>Multi.run(:create_article_comment,fn_,_->
269272
do_create_comment(content,info.foreign_key,article,user)
@@ -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,24 +333,58 @@ 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+
update_article_author_upvoted_info(comment,user_id)
338+
end)
339+
|>Repo.transaction()
340+
|>upsert_comment_result()
341+
end
342+
end
343+
344+
@doc"upvote a comment"
345+
defundo_upvote_article_comment(comment_id,%User{id:user_id})do
346+
with{:ok,comment}<-ORM.find(ArticleComment,comment_id),
347+
false<-comment.is_deleteddo
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(cinArticleCommentUpvote,where:c.article_comment_id==^comment_id)
357+
upvotes_count=Repo.aggregate(count_query,:count)
358+
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)
333364
|>Repo.transaction()
334365
|>upsert_comment_result()
335366
end
336367
end
337368

369+
defpupdate_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
372+
373+
new_meta=
374+
comment.meta
375+
|>Map.from_struct()
376+
|>Map.merge(%{is_article_author_upvoted:is_article_author_upvoted})
377+
378+
comment|>ORM.update(%{meta:new_meta})
379+
end
380+
end
381+
338382
# creat article comment for parent or reply
339383
# set floor
340384
# TODO: parse editor-json
341385
# set default emotions
342-
defpdo_create_comment(
343-
content,
344-
foreign_key,
345-
%{id:article_id,author_id:article_author_id},
346-
%User{
347-
id:user_id
348-
}
349-
)do
350-
count_query=from(cinArticleComment,where:field(c,^foreign_key)==^article_id)
386+
defpdo_create_comment(content,foreign_key,article,%User{id:user_id})do
387+
count_query=from(cinArticleComment,where:field(c,^foreign_key)==^article.id)
351388
floor=Repo.aggregate(count_query,:count)+1
352389

353390
ArticleComment
@@ -358,10 +395,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
358395
body_html:content,
359396
emotions:@default_emotions,
360397
floor:floor,
361-
is_article_author:user_id==article_author_id
398+
is_article_author:user_id==article.author.user.id,
399+
meta:@default_comment_meta
362400
},
363401
foreign_key,
364-
article_id
402+
article.id
365403
)
366404
)
367405
end
@@ -416,13 +454,13 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
416454
defpadd_participator_to_article(_,_),do:{:ok,:pass}
417455

418456
defpget_article(%ArticleComment{post_id:post_id}=comment)whennotis_nil(post_id)do
419-
with{:ok,article}<-ORM.find(Post,comment.post_id)do
457+
with{:ok,article}<-ORM.find(Post,comment.post_id,preload:[author::user])do
420458
{:post,article}
421459
end
422460
end
423461

424462
defpget_article(%ArticleComment{job_id:job_id}=comment)whennotis_nil(job_id)do
425-
with{:ok,article}<-ORM.find(Job,comment.job_id)do
463+
with{:ok,article}<-ORM.find(Job,comment.job_id,preload:[author::user])do
426464
{:job,article}
427465
end
428466
end
@@ -454,9 +492,43 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
454492
%{paged_comments|entries:new_entries}
455493
end
456494

495+
@specget_full_comment(String.t())::{:ok,T.article_info()}|{:error,nil}
496+
defpget_full_comment(comment_id)do
497+
query=from(cinArticleComment,where:c.id==^comment_id,preload::post,preload::job)
498+
499+
with{:ok,comment}<-Repo.one(query)|>done()do
500+
extract_article_info(comment)
501+
end
502+
end
503+
504+
defpextract_article_info(%ArticleComment{post:%Post{}=post})whennotis_nil(post)do
505+
do_extract_article_info(:post,post)
506+
end
507+
508+
defpextract_article_info(%ArticleComment{job:%Job{}=job})whennotis_nil(job)do
509+
do_extract_article_info(:job,job)
510+
end
511+
512+
@specdo_extract_article_info(T.article_thread(),T.article_common())::{:ok,T.article_info()}
513+
defpdo_extract_article_info(thread,article)do
514+
with{:ok,article_with_author}<-Repo.preload(article,author::user)|>done(),
515+
article_author<-get_in(article_with_author,[:author,:user])do
516+
#
517+
article_info=%{title:article.title}
518+
519+
author_info=%{
520+
id:article_author.id,
521+
login:article_author.login,
522+
nickname:article_author.nickname
523+
}
524+
525+
{:ok,%{thread:thread,article:article_info,author:author_info}}
526+
end
527+
end
528+
457529
defpupsert_comment_result({:ok,%{create_article_comment:result}}),do:{:ok,result}
458530
defpupsert_comment_result({:ok,%{add_reply_to:result}}),do:{:ok,result}
459-
defpupsert_comment_result({:ok,%{inc_upvotes_count:result}}),do:{:ok,result}
531+
defpupsert_comment_result({:ok,%{check_article_author_upvoted:result}}),do:{:ok,result}
460532
defpupsert_comment_result({:ok,%{update_report_flag:result}}),do:{:ok,result}
461533
defpupsert_comment_result({:ok,%{update_comment_emotion:result}}),do:{:ok,result}
462534

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ defmodule GroupherServer.CMS.Delegate.CommentReaction do
3737
with{:ok,action}<-match_action(thread,feeling)do
3838
clause=Map.merge(%{user_id:user_id},merge_thread_comment_id(thread,comment_id))
3939
# clause = %{post_comment_id: comment_id, user_id: user_id}
40-
ORM.findby_delete(action.reactor,clause)
40+
ORM.findby_delete!(action.reactor,clause)
4141
ORM.find(action.target,comment_id)
4242
end
4343
end

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
3535
"""
3636
defunset_category(%Community{id:community_id},%Category{id:category_id})do
3737
with{:ok,community_category}<-
38-
CommunityCategory|>ORM.findby_delete(~m(community_id category_id)a)do
38+
CommunityCategory|>ORM.findby_delete!(~m(community_id category_id)a)do
3939
Community|>ORM.find(community_category.community_id)
4040
end
4141
end
@@ -54,7 +54,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
5454
"""
5555
defunset_thread(%Community{id:community_id},%Thread{id:thread_id})do
5656
with{:ok,community_thread}<-
57-
CommunityThread|>ORM.findby_delete(~m(community_id thread_id)a)do
57+
CommunityThread|>ORM.findby_delete!(~m(community_id thread_id)a)do
5858
Community|>ORM.find(community_thread.community_id)
5959
end
6060
end
@@ -80,7 +80,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
8080
unset a community editor
8181
"""
8282
defunset_editor(%Community{id:community_id},%User{id:user_id})do
83-
with{:ok,_}<-ORM.findby_delete(CommunityEditor,~m(user_id community_id)a),
83+
with{:ok,_}<-ORM.findby_delete!(CommunityEditor,~m(user_id community_id)a),
8484
{:ok,_}<-PassportCURD.delete_passport(%User{id:user_id})do
8585
User|>ORM.find(user_id)
8686
end
@@ -132,7 +132,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
132132
with{:ok,community}<-ORM.find(Community,community_id),
133133
true<-community.raw!=="home",
134134
{:ok,record}<-
135-
ORM.findby_delete(CommunitySubscriber,community_id:community.id,user_id:user_id)do
135+
ORM.findby_delete!(CommunitySubscriber,community_id:community.id,user_id:user_id)do
136136
Community|>ORM.find(record.community_id)
137137
else
138138
false->
@@ -151,7 +151,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
151151
with{:ok,community}<-ORM.find(Community,community_id),
152152
true<-community.raw!=="home",
153153
{:ok,record}<-
154-
CommunitySubscriber|>ORM.findby_delete(community_id:community.id,user_id:user_id)do
154+
CommunitySubscriber|>ORM.findby_delete!(community_id:community.id,user_id:user_id)do
155155
update_community_geo(community_id,user_id,remote_ip,:dec)
156156
Community|>ORM.find(record.community_id)
157157
else
@@ -171,7 +171,7 @@ defmodule GroupherServer.CMS.Delegate.CommunityOperation do
171171
with{:ok,community}<-ORM.find(Community,community_id),
172172
true<-community.raw!=="home",
173173
{:ok,record}<-
174-
CommunitySubscriber|>ORM.findby_delete(community_id:community.id,user_id:user_id)do
174+
CommunitySubscriber|>ORM.findby_delete!(community_id:community.id,user_id:user_id)do
175175
update_community_geo_map(community.id,city,:dec)
176176
Community|>ORM.find(record.community_id)
177177
else

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ defmodule GroupherServer.CMS.Delegate.PassportCURD do
6565
end
6666

6767
defdelete_passport(%Accounts.User{id:user_id})do
68-
ORM.findby_delete(UserPasport,~m(user_id)a)
68+
ORM.findby_delete!(UserPasport,~m(user_id)a)
6969
end
7070

7171
defpreject_invalid_rules(rules)whenis_map(rules)do

‎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

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp