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.

Commitd16d227

Browse files
committed
refactor(emotion): extract sync embed replies concept
1 parent96583cd commitd16d227

File tree

4 files changed

+101
-59
lines changed

4 files changed

+101
-59
lines changed

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

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
44
"""
55
importEcto.Query,warn:false
66
importHelper.Utils,only:[done:1,strip_struct:1,get_config:2,ensure:2]
7-
importGroupherServer.CMS.Delegate.Helper,only:[article_of:1,thread_of:1]
7+
8+
importGroupherServer.CMS.Delegate.Helper,
9+
only:[article_of:1,thread_of:1,sync_embed_replies:1]
810

911
importHelper.ErrorCode
1012

@@ -179,7 +181,7 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
179181
|>done
180182
end)
181183
|>Multi.run(:upvote_comment_done,fn_,%{viewer_states:comment}->
182-
update_embed_comment_in_replies(comment)
184+
sync_embed_replies(comment)
183185
end)
184186
|>Multi.run(:after_hooks,fn_,_->
185187
Later.run({Hooks.Notify,:handle,[:upvote,comment,from_user]})
@@ -222,7 +224,7 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
222224
|>done
223225
end)
224226
|>Multi.run(:upvote_comment_done,fn_,%{viewer_states:comment}->
225-
update_embed_comment_in_replies(comment)
227+
sync_embed_replies(comment)
226228
end)
227229
|>Multi.run(:after_hooks,fn_,_->
228230
Later.run({Hooks.Notify,:handle,[:undo,:upvote,comment,from_user]})
@@ -386,38 +388,6 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
386388
ORM.update_meta(comment,meta)
387389
end
388390

389-
defpupdate_embed_comment_in_replies(%Comment{reply_to_id:nil}=comment)do
390-
{:ok,comment}
391-
end
392-
393-
# replies(embed_many) 不会自定更新,需要手动更新,否则在 replies 模式下数据会不同步。
394-
# update_embed_replies
395-
# upvote/undo-upvote
396-
# emotion/undo-emotion
397-
# update body
398-
# delete
399-
# report
400-
# ...
401-
defpupdate_embed_comment_in_replies(%Comment{reply_to_id:reply_to_id}=comment)do
402-
with{:ok,parent_comment}<-ORM.find(Comment,reply_to_id),
403-
embed_index<-Enum.find_index(parent_comment.replies,&(&1.id==comment.id))do
404-
caseis_nil(embed_index)do
405-
true->
406-
{:ok,comment}
407-
408-
false->
409-
replies=List.replace_at(parent_comment.replies,embed_index,comment)
410-
# IO.inspect(replies, label: "replies ---> ")
411-
412-
# 理论上更新一次即可,但 Changeset 无法识别数量一致的 replies ,不确定是业务代码的问题还是 Ecto 的问题,好坑啊
413-
{:ok,parent_comment}=ORM.update_embed(parent_comment,:replies,[])
414-
{:ok,_}=ORM.update_embed(parent_comment,:replies,replies)
415-
end
416-
417-
{:ok,comment}
418-
end
419-
end
420-
421391
defpresult({:ok,%{create_comment:result}}),do:{:ok,result}
422392
defpresult({:ok,%{add_reply_to:result}}),do:{:ok,result}
423393
defpresult({:ok,%{upvote_comment_done:result}}),do:{:ok,result}

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ defmodule GroupherServer.CMS.Delegate.CommentEmotion do
77
importHelper.Utils,only:[done:1]
88

99
importGroupherServer.CMS.Delegate.Helper,
10-
only:[update_emotions_field:4,mark_viewer_emotion_states:2]
10+
only:[
11+
update_emotions_field:4,
12+
mark_viewer_emotion_states:2,
13+
sync_embed_replies:1
14+
]
1115

1216
aliasHelper.ORM
1317
aliasGroupherServer.{Accounts,CMS,Repo}
@@ -43,7 +47,8 @@ defmodule GroupherServer.CMS.Delegate.CommentEmotion do
4347
query_emotion_states(comment,emotion)
4448
end)
4549
|>Multi.run(:update_emotions_field,fn_,%{query_emotion_states:status}->
46-
with{:ok,comment}<-update_emotions_field(comment,emotion,status,user)do
50+
with{:ok,comment}<-update_emotions_field(comment,emotion,status,user),
51+
{:ok,comment}<-sync_embed_replies(comment)do
4752
mark_viewer_emotion_states(comment,user)|>done
4853
end
4954
end)

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

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,19 @@ defmodule GroupherServer.CMS.Delegate.Helper do
7878
mark viewer emotions status for article or comment
7979
"""
8080
defmark_viewer_emotion_states(%{entries:entries}=artiments,%User{}=user)do
81-
new_entries=Enum.map(entries,&mark_viewer_emotion_states(&1,user))
82-
%{artiments|entries:new_entries}
81+
entries=
82+
Enum.map(entries,fnartiment->
83+
caseMap.has_key?(artiment,:replies)do
84+
true->
85+
mark_viewer_emotion_states(artiment,user)
86+
|>Map.put(:replies,mark_replies_emotion_states(artiment,user))
87+
88+
false->
89+
mark_viewer_emotion_states(artiment,user)
90+
end
91+
end)
92+
93+
%{artiments|entries:entries}
8394
end
8495

8596
defmark_viewer_emotion_states(%Comment{}=comment,%User{}=user)do
@@ -104,6 +115,14 @@ defmodule GroupherServer.CMS.Delegate.Helper do
104115
%{artiment|emotions:updated_emotions}
105116
end
106117

118+
defpmark_replies_emotion_states(%Comment{replies:[]},_),do:[]
119+
120+
defpmark_replies_emotion_states(%Comment{replies:replies},user)do
121+
Enum.map(replies,fnreply_comment->
122+
mark_viewer_emotion_states(reply_comment,user)
123+
end)
124+
end
125+
107126
@doc"""
108127
update emotions field for boty article and comment
109128
"""
@@ -129,6 +148,33 @@ defmodule GroupherServer.CMS.Delegate.Helper do
129148
|>done
130149
end
131150

151+
defsync_embed_replies(%Comment{reply_to_id:nil}=comment)do
152+
{:ok,comment}
153+
end
154+
155+
# replies(embed_many) 不会自定更新,需要手动更新,否则在 replies 模式下数据会不同步。
156+
# TODO: CURD comment in replies list
157+
# TODO: report / emotion / upvote comment in replies list
158+
# ...
159+
defsync_embed_replies(%Comment{reply_to_id:reply_to_id}=comment)do
160+
with{:ok,parent_comment}<-ORM.find(Comment,reply_to_id),
161+
embed_index<-Enum.find_index(parent_comment.replies,&(&1.id==comment.id))do
162+
caseis_nil(embed_index)do
163+
true->
164+
{:ok,comment}
165+
166+
false->
167+
replies=List.replace_at(parent_comment.replies,embed_index,comment)
168+
169+
# 理论上更新一次即可,但 Changeset 无法识别数量一致的 replies ,不确定是业务代码的问题还是 Ecto 的问题,好坑啊
170+
{:ok,parent_comment}=ORM.update_embed(parent_comment,:replies,[])
171+
{:ok,_}=ORM.update_embed(parent_comment,:replies,replies)
172+
end
173+
174+
{:ok,comment}
175+
end
176+
end
177+
132178
defpadd_viewer_emotioned_ifneed({:error,error},_),do:{:error,error}
133179

134180
defpadd_viewer_emotioned_ifneed({:ok,comment},emotions)do

‎test/groupher_server/cms/comments/post_comment_emotions_test.exs‎

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,6 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do
2121
end
2222

2323
describe"[emotion in paged article comment]"do
24-
@tag:wip
25-
test"emotioned comment should return valid viewer_has status",~m(post user user2)ado
26-
total_count=3
27-
28-
all_comment=
29-
Enum.reduce(0..total_count,[],fn_,acc->
30-
{:ok,comment}=CMS.create_comment(:post,post.id,mock_comment(),user)
31-
acc++[comment]
32-
end)
33-
34-
first_comment=List.first(all_comment)
35-
36-
{:ok,_}=CMS.emotion_to_comment(first_comment.id,:downvote,user)
37-
{:ok,_}=CMS.emotion_to_comment(first_comment.id,:beer,user2)
38-
{:ok,comment}=CMS.emotion_to_comment(first_comment.id,:beer,user)
39-
40-
assertcomment.emotions.viewer_has_downvoteed==true
41-
assertcomment.emotions.viewer_has_beered==true
42-
end
43-
4424
test"login user should got viewer has emotioned status",~m(post user)ado
4525
total_count=0
4626
page_number=10
@@ -81,6 +61,47 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do
8161
assertuser_exist_in?(user,target.emotions.latest_popcorn_users)
8262
asserttarget.emotions.viewer_has_popcorned
8363
end
64+
65+
test"emotioned comment should return valid viewer_has status",~m(post user user2)ado
66+
total_count=3
67+
68+
all_comment=
69+
Enum.reduce(0..total_count,[],fn_,acc->
70+
{:ok,comment}=CMS.create_comment(:post,post.id,mock_comment(),user)
71+
acc++[comment]
72+
end)
73+
74+
first_comment=List.first(all_comment)
75+
76+
{:ok,_}=CMS.emotion_to_comment(first_comment.id,:downvote,user)
77+
{:ok,_}=CMS.emotion_to_comment(first_comment.id,:beer,user2)
78+
{:ok,comment}=CMS.emotion_to_comment(first_comment.id,:beer,user)
79+
80+
assertcomment.emotions.viewer_has_downvoteed==true
81+
assertcomment.emotions.viewer_has_beered==true
82+
end
83+
84+
@tag:wip
85+
test"nested reply should have viewer emotion status in replies mode",~m(post user)ado
86+
{:ok,parent_comment}=CMS.create_comment(:post,post.id,mock_comment(),user)
87+
88+
{:ok,reply_comment}=
89+
CMS.reply_comment(parent_comment.id,mock_comment("reply_content"),user)
90+
91+
{:ok,_}=CMS.emotion_to_comment(parent_comment.id,:downvote,user)
92+
{:ok,_}=CMS.emotion_to_comment(reply_comment.id,:downvote,user)
93+
# IO.inspect(ff.emotions, label: "ff")
94+
95+
filter=%{page:1,size:10}
96+
{:ok,%{entries:entries}}=CMS.paged_comments(:post,post.id,filter,:replies,user)
97+
# IO.inspect(entries, label: "entries: ")
98+
parent=entries|>List.first()
99+
parent_emotion=parent|>Map.get(:emotions)
100+
reply_emotion=parent|>Map.get(:replies)|>List.first()|>Map.get(:emotions)
101+
102+
assertparent_emotion.viewer_has_downvoteed
103+
assertreply_emotion.viewer_has_downvoteed
104+
end
84105
end
85106

86107
describe"[basic article comment emotion]"do

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp