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.

Commitdc2ab74

Browse files
authored
refactor: mutation debug with frontend 1 (#434)
* refactor(upvote): avoid load article multi times* fix(upvote): update embed replies upvote info* fix(upvote): return order* fix(upvote): upvotes_count error* fix(upvote): unique index & re-org* fix(emotion): set viewer status after emontion to comment & re-org* refactor(emotion): extract sync embed replies concept* refactor(comment): sync_embed_replies if need* fix: report multi-task result
1 parentdff149d commitdc2ab74

File tree

32 files changed

+389
-86
lines changed

32 files changed

+389
-86
lines changed

‎config/mock.exs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ config :groupher_server, GroupherServerWeb.Endpoint,
77
check_origin:false,
88
watchers:[]
99

10+
config:groupher_server,Helper.Guardian,
11+
issuer:"groupher_server",
12+
secret_key:"hello"
13+
1014
config:logger,:console,format:"[$level] $message\n"
1115

1216
config:phoenix,:stacktrace_depth,20

‎lib/groupher_server/cms/cms.ex‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ defmodule GroupherServer.CMS do
143143
defdelegateundo_emotion_to_article(thread,article_id,args,user),to:ArticleEmotion
144144

145145
# Comment CURD
146+
147+
defdelegateupdate_user_in_comments_participants(user),to:CommentCurd
146148
defdelegatepaged_comments(thread,article_id,filters,mode),to:CommentCurd
147149
defdelegatepaged_comments(thread,article_id,filters,mode,user),to:CommentCurd
148150

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
55
importEcto.Query,warn:false
66
importHelper.Utils,only:[done:1,strip_struct:1,get_config:2]
77

8+
importGroupherServer.CMS.Delegate.Helper,only:[sync_embed_replies:1]
9+
810
importGroupherServer.CMS.Helper.Matcher
911
importShortMaps
1012

@@ -187,6 +189,9 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
187189
do:CMS.fold_comment(comment,user),
188190
else:{:ok,comment}
189191
end)
192+
|>Multi.run(:sync_embed_replies,fn_,%{update_report_meta:comment}->
193+
sync_embed_replies(comment)
194+
end)
190195
|>Repo.transaction()
191196
|>result()
192197
end
@@ -258,10 +263,10 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
258263
_->
259264
report_cases=report.report_cases|>Enum.reject(&(&1.user.login==user.login))
260265

266+
changes=%{report_cases_count:length(report_cases)}
267+
261268
report
262-
|>Ecto.Changeset.change(%{report_cases_count:length(report_cases)})
263-
|>Ecto.Changeset.put_embed(:report_cases,report_cases)
264-
|>Repo.update()
269+
|>ORM.update_embed(:report_cases,report_cases,changes)
265270
end
266271
end
267272
end
@@ -379,6 +384,7 @@ defmodule GroupherServer.CMS.Delegate.AbuseReport do
379384
|>Map.merge(%{thread:article_thread})
380385
end
381386

387+
defpresult({:ok,%{sync_embed_replies:result}}),do:result|>done()
382388
defpresult({:ok,%{update_report_meta:result}}),do:result|>done()
383389
defpresult({:ok,%{update_content_reported_flag:result}}),do:result|>done()
384390

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCollect do
114114
defset_collect_folder(%ArticleCollect{}=collect,folder)do
115115
collect_folders=(collect.collect_folders++[folder])|>Enum.uniq()
116116

117-
collect
118-
|>Ecto.Changeset.change()
119-
|>Ecto.Changeset.put_embed(:collect_folders,collect_folders)
120-
|>Repo.update()
117+
ORM.update_embed(collect,:collect_folders,collect_folders)
121118
end
122119

123120
defundo_set_collect_folder(%ArticleCollect{}=collect,folder)do
@@ -129,10 +126,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCollect do
129126
{:ok,:pass}
130127

131128
_->
132-
collect
133-
|>Ecto.Changeset.change()
134-
|>Ecto.Changeset.put_embed(:collect_folders,collect_folders)
135-
|>Repo.update()
129+
ORM.update_embed(collect,:collect_folders,collect_folders)
136130
end
137131
end
138132

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleUpvote do
55
importGroupherServer.CMS.Helper.Matcher
66
importEcto.Query,warn:false
77
importHelper.Utils,only:[done:1]
8+
importHelper.ErrorCode
89

910
importGroupherServer.CMS.Delegate.Helper,
1011
only:[
@@ -33,22 +34,24 @@ defmodule GroupherServer.CMS.Delegate.ArticleUpvote do
3334
with{:ok,info}<-match(thread),
3435
{:ok,article}<-ORM.find(info.model,article_id,preload:[author::user])do
3536
Multi.new()
36-
|>Multi.run(:inc_article_upvotes_count,fn_,_->
37+
|>Multi.run(:update_upvotes_count,fn_,_->
3738
update_article_reactions_count(info,article,:upvotes_count,:inc)
3839
end)
39-
|>Multi.run(:update_article_reaction_user_list,fn_,_->
40+
|>Multi.run(:update_reaction_user_list,fn_,%{update_upvotes_count:article}->
4041
update_article_reaction_user_list(:upvot,article,from_user,:add)
4142
end)
4243
|>Multi.run(:add_achievement,fn_,_->
4344
achiever_id=article.author.user_id
4445
Accounts.achieve(%User{id:achiever_id},:inc,:upvote)
4546
end)
46-
|>Multi.run(:create_upvote,fn_,_->
47+
|>Multi.run(:create_upvote,fn_,%{update_reaction_user_list:article}->
4748
thread=thread|>to_string|>String.upcase()
4849
args=Map.put(%{user_id:user_id,thread:thread},info.foreign_key,article.id)
4950

5051
with{:ok,_}<-ORM.create(ArticleUpvote,args)do
51-
ORM.find(info.model,article.id)
52+
article|>done
53+
else
54+
_->{:error,[message:"viewer already upvoted",code:ecode(:already_upvoted)]}
5255
end
5356
end)
5457
|>Multi.run(:after_hooks,fn_,_->
@@ -64,17 +67,17 @@ defmodule GroupherServer.CMS.Delegate.ArticleUpvote do
6467
with{:ok,info}<-match(thread),
6568
{:ok,article}<-ORM.find(info.model,article_id)do
6669
Multi.new()
67-
|>Multi.run(:inc_article_upvotes_count,fn_,_->
70+
|>Multi.run(:update_upvotes_count,fn_,_->
6871
update_article_reactions_count(info,article,:upvotes_count,:dec)
6972
end)
70-
|>Multi.run(:update_article_reaction_user_list,fn_,_->
73+
|>Multi.run(:update_reaction_user_list,fn_,%{update_upvotes_count:article}->
7174
update_article_reaction_user_list(:upvot,article,from_user,:remove)
7275
end)
73-
|>Multi.run(:undo_upvote,fn_,_->
76+
|>Multi.run(:undo_upvote,fn_,%{update_reaction_user_list:article}->
7477
args=Map.put(%{user_id:user_id},info.foreign_key,article.id)
7578

7679
ORM.findby_delete(ArticleUpvote,args)
77-
ORM.find(info.model,article.id)
80+
article|>done
7881
end)
7982
|>Multi.run(:after_hooks,fn_,_->
8083
Later.run({Hooks.Notify,:handle,[:undo,:upvote,article,from_user]})

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

Lines changed: 18 additions & 9 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

@@ -169,7 +171,7 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
169171
|>Multi.run(:check_article_author_upvoted,fn_,%{inc_upvotes_count:comment}->
170172
update_article_author_upvoted_info(comment,user_id)
171173
end)
172-
|>Multi.run(:upvote_comment_done,fn_,%{check_article_author_upvoted:comment}->
174+
|>Multi.run(:viewer_states,fn_,%{check_article_author_upvoted:comment}->
173175
viewer_has_upvoted=Enum.member?(comment.meta.upvoted_user_ids,user_id)
174176
viewer_has_reported=Enum.member?(comment.meta.reported_user_ids,user_id)
175177

@@ -178,6 +180,9 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
178180
|>Map.merge(%{viewer_has_reported:viewer_has_reported})
179181
|>done
180182
end)
183+
|>Multi.run(:sync_embed_replies,fn_,%{viewer_states:comment}->
184+
sync_embed_replies(comment)
185+
end)
181186
|>Multi.run(:after_hooks,fn_,_->
182187
Later.run({Hooks.Notify,:handle,[:upvote,comment,from_user]})
183188
end)
@@ -204,12 +209,12 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
204209
{:ok,upvotes_count}=
205210
from(cinCommentUpvote,where:c.comment_id==^comment_id)|>ORM.count()
206211

207-
ORM.update(comment,%{upvotes_count:Enum.max([upvotes_count-1,0])})
212+
ORM.update(comment,%{upvotes_count:Enum.max([upvotes_count,0])})
208213
end)
209214
|>Multi.run(:check_article_author_upvoted,fn_,%{desc_upvotes_count:updated_comment}->
210215
update_article_author_upvoted_info(updated_comment,user_id)
211216
end)
212-
|>Multi.run(:upvote_comment_done,fn_,%{check_article_author_upvoted:comment}->
217+
|>Multi.run(:viewer_states,fn_,%{check_article_author_upvoted:comment}->
213218
viewer_has_upvoted=Enum.member?(comment.meta.upvoted_user_ids,user_id)
214219
viewer_has_reported=Enum.member?(comment.meta.reported_user_ids,user_id)
215220

@@ -218,6 +223,9 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
218223
|>Map.merge(%{viewer_has_reported:viewer_has_reported})
219224
|>done
220225
end)
226+
|>Multi.run(:sync_embed_replies,fn_,%{viewer_states:comment}->
227+
sync_embed_replies(comment)
228+
end)
221229
|>Multi.run(:after_hooks,fn_,_->
222230
Later.run({Hooks.Notify,:handle,[:undo,:upvote,comment,from_user]})
223231
end)
@@ -296,10 +304,7 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
296304
|>List.insert_at(length(replies),replyed_comment)
297305
|>Enum.slice(0,@max_parent_replies_count)
298306

299-
parent_comment
300-
|>Ecto.Changeset.change()
301-
|>Ecto.Changeset.put_embed(:replies,new_replies)
302-
|>Repo.update()
307+
ORM.update_embed(parent_comment,:replies,new_replies)
303308
end
304309

305310
# 如果已经有 @max_parent_replies_count 以上的回复了,直接忽略即可
@@ -385,7 +390,7 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
385390

386391
defpresult({:ok,%{create_comment:result}}),do:{:ok,result}
387392
defpresult({:ok,%{add_reply_to:result}}),do:{:ok,result}
388-
defpresult({:ok,%{upvote_comment_done:result}}),do:{:ok,result}
393+
defpresult({:ok,%{sync_embed_replies:result}}),do:{:ok,result}
389394
defpresult({:ok,%{update_comment_flag:result}}),do:{:ok,result}
390395
defpresult({:ok,%{delete_comment:result}}),do:{:ok,result}
391396
defpresult({:ok,%{fold_comment:result}}),do:{:ok,result}
@@ -394,6 +399,10 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
394399
raise_error(:create_comment,result)
395400
end
396401

402+
defpresult({:error,:create_comment_upvote,result,_steps})do
403+
raise_error(:comment_already_upvote,result)
404+
end
405+
397406
defpresult({:error,:add_participator,result,_steps})do
398407
{:error,result}
399408
end

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

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
77
importHelper.ErrorCode
88

99
importGroupherServer.CMS.Delegate.Helper,
10-
only:[mark_viewer_emotion_states:3,article_of:1,thread_of:1]
10+
only:[mark_viewer_emotion_states:2,article_of:1,thread_of:1,sync_embed_replies:1]
1111

1212
importGroupherServer.CMS.Helper.Matcher
1313
importShortMaps
@@ -144,6 +144,15 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
144144
|>done()
145145
end
146146

147+
defupdate_user_in_comments_participants(%User{login:login})do
148+
from(ainCMS.Model.Post,
149+
cross_join:cpinfragment("jsonb_array_elements(?)",a.comments_participants),
150+
where:fragment("?->>'login' = ?",cp,^login)
151+
)
152+
|>Repo.all()
153+
|>IO.inspect(label:"TODO")
154+
end
155+
147156
@doc"""
148157
creates a comment for article like psot, job ...
149158
"""
@@ -200,15 +209,33 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
200209
with{:ok,post}<-ORM.find(Post,comment.post_id),
201210
{:ok,parsed}<-Converter.Article.parse_body(body),
202211
{:ok,digest}<-Converter.Article.parse_digest(parsed.body_map)do
203-
%{body:body,body_html:body_html}=parsed
204-
post|>ORM.update(%{solution_digest:digest})
205-
comment|>ORM.update(%{body:body,body_html:body_html})
212+
Multi.new()
213+
|>Multi.run(:update_parent_post,fn_,_->
214+
ORM.update(post,%{solution_digest:digest})
215+
end)
216+
|>Multi.run(:update_comment,fn_,_->
217+
%{body:body,body_html:body_html}=parsed
218+
comment|>ORM.update(%{body:body,body_html:body_html})
219+
end)
220+
|>Multi.run(:sync_embed_replies,fn_,%{update_comment:comment}->
221+
sync_embed_replies(comment)
222+
end)
223+
|>Repo.transaction()
224+
|>result()
206225
end
207226
end
208227

209228
defupdate_comment(%Comment{}=comment,body)do
210229
with{:ok,%{body:body,body_html:body_html}}<-Converter.Article.parse_body(body)do
211-
comment|>ORM.update(%{body:body,body_html:body_html})
230+
Multi.new()
231+
|>Multi.run(:update_comment,fn_,_->
232+
ORM.update(comment,%{body:body,body_html:body_html})
233+
end)
234+
|>Multi.run(:sync_embed_replies,fn_,%{update_comment:comment}->
235+
sync_embed_replies(comment)
236+
end)
237+
|>Repo.transaction()
238+
|>result()
212239
end
213240
end
214241

@@ -245,6 +272,9 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
245272
|>Multi.run(:update_post_state,fn_,_->
246273
ORM.update(post,%{is_solved:is_solution,solution_digest:comment.body_html})
247274
end)
275+
|>Multi.run(:sync_embed_replies,fn_,%{mark_solution:comment}->
276+
sync_embed_replies(comment)
277+
end)
248278
|>Repo.transaction()
249279
|>result()
250280
else
@@ -378,7 +408,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
378408
|>QueryBuilder.filter_pack(Map.merge(filters,%{sort:sort}))
379409
|>ORM.paginator(~m(page size)a)
380410
|>add_pinned_comments_ifneed(thread,article_id,filters)
381-
|>mark_viewer_emotion_states(user,:comment)
411+
|>mark_viewer_emotion_states(user)
382412
|>mark_viewer_has_upvoted(user)
383413
|>done()
384414
end
@@ -394,7 +424,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
394424
|>where(^where_query)
395425
|>QueryBuilder.filter_pack(filters)
396426
|>ORM.paginator(~m(page size)a)
397-
|>mark_viewer_emotion_states(user,:comment)
427+
|>mark_viewer_emotion_states(user)
398428
|>mark_viewer_has_upvoted(user)
399429
|>done()
400430
end
@@ -457,7 +487,19 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
457487
entries=
458488
Enum.map(
459489
entries,
460-
&Map.merge(&1,%{viewer_has_upvoted:Enum.member?(&1.meta.upvoted_user_ids,user.id)})
490+
fncomment->
491+
replies=
492+
Enum.map(comment.replies,fnreply_comment->
493+
Map.merge(reply_comment,%{
494+
viewer_has_upvoted:Enum.member?(reply_comment.meta.upvoted_user_ids,user.id)
495+
})
496+
end)
497+
498+
Map.merge(comment,%{
499+
viewer_has_upvoted:Enum.member?(comment.meta.upvoted_user_ids,user.id),
500+
replies:replies
501+
})
502+
end
461503
)
462504

463505
Map.merge(paged_comments,%{entries:entries})
@@ -492,6 +534,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
492534
defpresult({:ok,%{set_question_flag_ifneed:result}}),do:{:ok,result}
493535
defpresult({:ok,%{delete_comment:result}}),do:{:ok,result}
494536
defpresult({:ok,%{mark_solution:result}}),do:{:ok,result}
537+
defpresult({:ok,%{sync_embed_replies:result}}),do:{:ok,result}
495538

496539
defpresult({:error,:create_comment,result,_steps})do
497540
raise_error(:create_comment,result)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp