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.

Commit14e3bcb

Browse files
authored
refactor: debug new comments (#430)
* fix(article): enhance seed & comment fields* fix(article): comments_participants_count logic* fix(article): comments_participants_count logic* fix: seed threads sort* fix(comments): adjust fields* chore: clean up
1 parentb07f1f9 commit14e3bcb

File tree

16 files changed

+136
-148
lines changed

16 files changed

+136
-148
lines changed

‎lib/groupher_server/cms/delegates/Seeds/seeds.ex‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,15 @@ defmodule GroupherServer.CMS.Delegate.Seeds do
2121
aliasHelper.ORM
2222
aliasGroupherServer.CMS
2323

24-
aliasCMS.Model.{Community,Category,Post}
24+
aliasCMS.Model.{Community,Category,Post,Comment}
2525
aliasCMS.Delegate.Seeds
2626
aliasSeeds.Domain
2727

2828
@article_threadsget_config(:article,:threads)
2929
# categories
3030
@community_types[:pl,:framework,:editor,:database,:devops,:city]
3131

32+
@comment_emotionsget_config(:article,:comment_emotions)
3233
# seed community
3334

3435
@doc"""
@@ -88,6 +89,7 @@ defmodule GroupherServer.CMS.Delegate.Seeds do
8889
attrs=mock_attrs(thread,%{community_id:community.id})
8990
{:ok,article}=CMS.create_article(community,thread,attrs,user)
9091
seed_tags(tags,thread,article.id)
92+
seed_comments(thread,article.id,user)
9193
seed_upvotes(thread,article.id)
9294
end)
9395
end
@@ -117,6 +119,36 @@ defmodule GroupherServer.CMS.Delegate.Seeds do
117119
tags.entries|>Enum.map(&&1.id)|>Enum.shuffle()|>Enum.take(1)
118120
end
119121

122+
defpseed_comments(thread,article_id,user)do
123+
0..Enum.random(1..5)
124+
|>Enum.each(fn_->
125+
text=Faker.Lorem.sentence(%Range{first:30,last:80})
126+
{:ok,comment}=CMS.create_comment(thread,article_id,mock_comment(text),user)
127+
seed_comment_emotions(comment)
128+
seed_comment_replies(comment)
129+
end)
130+
end
131+
132+
defpseed_comment_replies(%Comment{}=comment)do
133+
with{:ok,users}<-db_insert_multi(:user,Enum.random(1..5))do
134+
users
135+
|>Enum.each(fnuser->
136+
text=Faker.Lorem.sentence(%Range{first:30,last:80})
137+
{:ok,_}=CMS.reply_comment(comment.id,mock_comment(text),user)
138+
end)
139+
end
140+
end
141+
142+
defpseed_comment_emotions(%Comment{}=comment)do
143+
with{:ok,users}<-db_insert_multi(:user,Enum.random(1..5))do
144+
users
145+
|>Enum.each(fnuser->
146+
emotion=@comment_emotions|>Enum.random()
147+
{:ok,_}=CMS.emotion_to_comment(comment.id,emotion,user)
148+
end)
149+
end
150+
end
151+
120152
# clean up
121153

122154
defclean_up(:all)do

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

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
33
CURD and operations for article comments
44
"""
55
importEcto.Query,warn:false
6-
importHelper.Utils,only:[done:1,ensure:2,get_config:2]
6+
importHelper.Utils,only:[done:1,ensure:2,strip_struct:1,get_config:2]
77
importHelper.ErrorCode
88

99
importGroupherServer.CMS.Delegate.Helper,
@@ -110,24 +110,40 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
110110
@specpaged_comments_participants(T.article_thread(),Integer.t(),T.paged_filter())::
111111
{:ok,T.paged_users()}
112112
defpaged_comments_participants(thread,article_id,filters)do
113-
%{page:page,size:size}=filters
113+
with{:ok,thread_query}<-match(thread,:query,article_id),
114+
{:ok,info}<-match(thread),
115+
{:ok,article}<-ORM.find(info.model,article_id),
116+
{:ok,paged_data}<-do_paged_comments_participants(thread_query,filters)do
117+
# check participants_count if history data do not match
118+
casearticle.comments_participants_count!==paged_data.total_countdo
119+
true->
120+
article|>ORM.update(%{comments_participants_count:paged_data.total_count})
121+
122+
false->
123+
{:ok,:pass}
124+
end
114125

115-
with{:ok,thread_query}<-match(thread,:query,article_id)do
116-
Comment
117-
|>where(^thread_query)
118-
|>QueryBuilder.filter_pack(Map.merge(filters,%{sort::desc_inserted}))
119-
|>join(:inner,[c],ainassoc(c,:author))
120-
|>distinct([c,a],a.id)
121-
# group_by
122-
|>group_by([c,a],a.id)
123-
|>group_by([c,a],c.inserted_at)
124-
|>group_by([c,a],c.id)
125-
|>select([c,a],a)
126-
|>ORM.paginator(~m(page size)a)
127-
|>done()
126+
paged_data|>done
128127
end
129128
end
130129

130+
defpdo_paged_comments_participants(query,filters)do
131+
%{page:page,size:size}=filters
132+
133+
Comment
134+
|>where(^query)
135+
|>QueryBuilder.filter_pack(Map.merge(filters,%{sort::desc_inserted}))
136+
|>join(:inner,[c],ainassoc(c,:author))
137+
|>distinct([c,a],a.id)
138+
# group_by
139+
|>group_by([c,a],a.id)
140+
|>group_by([c,a],c.inserted_at)
141+
|>group_by([c,a],c.id)
142+
|>select([c,a],a)
143+
|>ORM.paginator(~m(page size)a)
144+
|>done()
145+
end
146+
131147
@doc"""
132148
creates a comment for article like psot, job ...
133149
"""
@@ -273,15 +289,19 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
273289

274290
# add participator to article-like(Post, Job ...) and update count
275291
defadd_participant_to_article(%{comments_participants:participants}=article,%User{}=user)do
276-
total_participants=participants|>List.insert_at(0,user)|>Enum.uniq_by(&&1.id)
292+
cur_participants=participants|>List.insert_at(0,user)|>Enum.uniq_by(&&1.id)
293+
294+
meta=article.meta|>strip_struct
295+
cur_participants_ids=(meta.comments_participant_user_ids++[user.id])|>Enum.uniq()
296+
meta=Map.merge(meta,%{comments_participant_user_ids:cur_participants_ids})
277297

278-
latest_participants=total_participants|>Enum.slice(0,@max_participator_count)
279-
total_participants_count=length(total_participants)
298+
latest_participants=cur_participants|>Enum.slice(0,@max_participator_count)
280299

281300
article
282301
|>Ecto.Changeset.change()
283-
|>Ecto.Changeset.put_change(:comments_participants_count,total_participants_count)
302+
|>Ecto.Changeset.put_change(:comments_participants_count,cur_participants_ids|>length)
284303
|>Ecto.Changeset.put_embed(:comments_participants,latest_participants)
304+
|>Ecto.Changeset.put_embed(:meta,meta)
285305
|>Repo.update()
286306
end
287307

‎lib/groupher_server/cms/models/embeds/article_meta.ex‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ defmodule GroupherServer.CMS.Model.Embeds.ArticleMeta do
88

99
aliasGroupherServer.CMS.Model.Embeds
1010

11-
@optional_fields~w(thread is_edited is_comment_locked upvoted_user_ids collected_user_ids viewed_user_ids reported_user_ids reported_count is_sinked can_undo_sink last_active_at)a
11+
@optional_fields~w(thread is_edited is_comment_locked upvoted_user_ids collected_user_ids viewed_user_idscomments_participant_user_idsreported_user_ids reported_count is_sinked can_undo_sink last_active_at)a
1212

1313
@doc"for test usage"
1414
defdefault_meta()do
@@ -21,6 +21,7 @@ defmodule GroupherServer.CMS.Model.Embeds.ArticleMeta do
2121
collected_user_ids:[],
2222
viewed_user_ids:[],
2323
reported_user_ids:[],
24+
comments_participant_user_ids:[],
2425
reported_count:0,
2526
is_sinked:false,
2627
can_undo_sink:true,
@@ -44,6 +45,8 @@ defmodule GroupherServer.CMS.Model.Embeds.ArticleMeta do
4445
field(:reported_user_ids,{:array,:integer},default:[])
4546
field(:reported_count,:integer,default:0)
4647

48+
field(:comments_participant_user_ids,{:array,:integer},default:[])
49+
4750
field(:is_sinked,:boolean,default:false)
4851
field(:can_undo_sink,:boolean,default:false)
4952
# if undo_sink, can recover last active_at from here

‎lib/groupher_server_web/schema/Helper/fields.ex‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,32 @@ defmodule GroupherServerWeb.Schema.Helper.Fields do
151151
end
152152
end
153153

154+
defmacrocomment_general_fieldsdo
155+
quotedo
156+
field(:id,:id)
157+
field(:body,:string)
158+
field(:body_html,:string)
159+
field(:author,:user,resolve:dataloader(CMS,:author))
160+
field(:is_pinned,:boolean)
161+
field(:floor,:integer)
162+
field(:upvotes_count,:integer)
163+
field(:is_article_author,:boolean)
164+
field(:emotions,:comment_emotions)
165+
field(:meta,:comment_meta)
166+
field(:replies_count,:integer)
167+
field(:thread,:string)
168+
field(:viewer_has_upvoted,:boolean)
169+
field(:thread,:string)
170+
field(:replies_count,:integer)
171+
172+
field(:is_deleted,:boolean)
173+
field(:is_archived,:boolean)
174+
field(:archived_at,:datetime)
175+
176+
timestamp_fields()
177+
end
178+
end
179+
154180
# see: https://github.com/absinthe-graphql/absinthe/issues/363
155181
defmacropagination_argsdo
156182
quotedo

‎lib/groupher_server_web/schema/cms/cms_types.ex‎

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -302,49 +302,18 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
302302
end
303303

304304
object:comment_replydo
305-
field(:id,:id)
306-
field(:body,:string)
307-
field(:body_html,:string)
308-
field(:author,:user,resolve:dataloader(CMS,:author))
309-
field(:floor,:integer)
310-
field(:upvotes_count,:integer)
311-
field(:is_article_author,:boolean)
312-
field(:emotions,:comment_emotions)
313-
field(:meta,:comment_meta)
314-
field(:replies_count,:integer)
315-
field(:reply_to,:comment_reply)
316-
field(:viewer_has_upvoted,:boolean)
317-
field(:thread,:string)
318-
319-
timestamp_fields()
305+
comment_general_fields()
320306
end
321307

322308
object:commentdo
323-
field(:id,:id)
324-
field(:body_html,:string)
325-
field(:author,:user,resolve:dataloader(CMS,:author))
326-
field(:is_pinned,:boolean)
327-
field(:floor,:integer)
328-
field(:upvotes_count,:integer)
329-
field(:emotions,:comment_emotions)
330-
field(:is_article_author,:boolean)
331-
field(:meta,:comment_meta)
309+
comment_general_fields()
332310
field(:reply_to,:comment_reply)
311+
333312
field(:replies,list_of(:comment_reply))
334-
field(:replies_count,:integer)
335-
field(:thread,:string)
336313
field(:article,:common_article)
337314

338-
field(:is_deleted,:boolean)
339-
field(:viewer_has_upvoted,:boolean)
340-
341315
field(:is_for_question,:boolean)
342316
field(:is_solution,:boolean)
343-
344-
field(:is_archived,:boolean)
345-
field(:archived_at,:datetime)
346-
347-
timestamp_fields()
348317
end
349318

350319
####### reports

‎test/groupher_server/seeds/articles_seed_test.exs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ defmodule GroupherServer.Test.Seeds.Articles do
2929
posts.entries|>Enum.map(&&1.original_community_id)|>Enum.uniq()
3030

3131
assertoriginal_community_ids===[community.id]
32+
33+
{:ok,paged_comments}=
34+
CMS.paged_comments(:post,ramdom_post.id,%{page:1,size:10},:timeline)
35+
36+
# IO.inspect(paged_comments, label: "paged_comments -> ")
37+
assertpaged_comments.total_count!==0
3238
end
3339

3440
test"can seed jobs"do
@@ -45,6 +51,11 @@ defmodule GroupherServer.Test.Seeds.Articles do
4551
original_community_ids=jobs.entries|>Enum.map(&&1.original_community_id)|>Enum.uniq()
4652

4753
assertoriginal_community_ids===[community.id]
54+
55+
{:ok,paged_comments}=
56+
CMS.paged_comments(:job,ramdom_job.id,%{page:1,size:20},:timeline)
57+
58+
assertpaged_comments.total_count!==0
4859
end
4960

5061
test"can seed radars"do
@@ -62,6 +73,11 @@ defmodule GroupherServer.Test.Seeds.Articles do
6273
radars.entries|>Enum.map(&&1.original_community_id)|>Enum.uniq()
6374

6475
assertoriginal_community_ids===[community.id]
76+
77+
{:ok,paged_comments}=
78+
CMS.paged_comments(:radar,ramdom_radar.id,%{page:1,size:20},:timeline)
79+
80+
assertpaged_comments.total_count!==0
6581
end
6682

6783
test"can seed blogs"do
@@ -78,6 +94,11 @@ defmodule GroupherServer.Test.Seeds.Articles do
7894
blogs.entries|>Enum.map(&&1.original_community_id)|>Enum.uniq()
7995

8096
assertoriginal_community_ids===[community.id]
97+
98+
{:ok,paged_comments}=
99+
CMS.paged_comments(:blog,ramdom_blog.id,%{page:1,size:20},:timeline)
100+
101+
assertpaged_comments.total_count!==0
81102
end
82103

83104
test"can seed works"do
@@ -95,6 +116,11 @@ defmodule GroupherServer.Test.Seeds.Articles do
95116
works.entries|>Enum.map(&&1.original_community_id)|>Enum.uniq()
96117

97118
assertoriginal_community_ids===[community.id]
119+
120+
{:ok,paged_comments}=
121+
CMS.paged_comments(:works,ramdom_works.id,%{page:1,size:20},:timeline)
122+
123+
assertpaged_comments.total_count!==0
98124
end
99125
end
100126
end

‎test/groupher_server/seeds/community_seed_test.exs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ defmodule GroupherServer.Test.Seeds.CommunitySeed do
4242
assertfound.threads|>length==3
4343

4444
threads=found.threads|>Enum.map(&&1.thread.title)
45-
assertthreads==["帖子","看板","分布"]
45+
assert"帖子"inthreads
46+
assert"看板"inthreads
47+
assert"分布"inthreads
4648
end
4749

4850
# Makers, 广告墙, 求助,外包合作

‎test/groupher_server_web/query/cms/comments/blog_comment_test.exs‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -605,16 +605,6 @@ defmodule GroupherServer.Test.Query.Comments.BlogComment do
605605
meta {
606606
isArticleAuthorUpvoted
607607
}
608-
replyTo {
609-
id
610-
bodyHtml
611-
floor
612-
isArticleAuthor
613-
author {
614-
id
615-
login
616-
}
617-
}
618608
repliesCount
619609
viewerHasUpvoted
620610
}

‎test/groupher_server_web/query/cms/comments/drink_comment_test.exs‎

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -614,16 +614,6 @@ defmodule GroupherServer.Test.Query.Comments.DrinkComment do
614614
meta {
615615
isArticleAuthorUpvoted
616616
}
617-
replyTo {
618-
id
619-
bodyHtml
620-
floor
621-
isArticleAuthor
622-
author {
623-
id
624-
login
625-
}
626-
}
627617
repliesCount
628618
viewerHasUpvoted
629619
}

‎test/groupher_server_web/query/cms/comments/guide_comment_test.exs‎

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ defmodule GroupherServer.Test.Query.Comments.GuideComment do
3030
}
3131
}
3232
"""
33-
33+
@tag:wip
3434
test"guest user can get comment participants after comment created",
3535
~m(guest_conn guide user user2)ado
3636
total_count=5
@@ -614,16 +614,6 @@ defmodule GroupherServer.Test.Query.Comments.GuideComment do
614614
meta {
615615
isArticleAuthorUpvoted
616616
}
617-
replyTo {
618-
id
619-
bodyHtml
620-
floor
621-
isArticleAuthor
622-
author {
623-
id
624-
login
625-
}
626-
}
627617
repliesCount
628618
viewerHasUpvoted
629619
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp