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.

Commit3663e56

Browse files
committed
feat(comments): add commentsState api
1 parentd6cf9c9 commit3663e56

File tree

7 files changed

+133
-2
lines changed

7 files changed

+133
-2
lines changed

‎lib/groupher_server/cms/cms.ex‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ defmodule GroupherServer.CMS do
152152

153153
# Comment CURD
154154

155+
defdelegatecomments_state(thread,article_id),to:CommentCurd
156+
defdelegatecomments_state(thread,article_id,user),to:CommentCurd
155157
defdelegateone_comment(id),to:CommentCurd
156158
defdelegateone_comment(id,user),to:CommentCurd
157159

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,44 @@ defmodule GroupherServer.CMS.Delegate.CommentCurd do
3333

3434
@archive_thresholdget_config(:article,:archive_threshold)
3535

36+
defcomments_state(thread,article_id)do
37+
filter=%{page:1,size:20}
38+
39+
with{:ok,thread_query}<-match(thread,:query,article_id),
40+
{:ok,info}<-match(thread),
41+
{:ok,article}<-ORM.find(info.model,article_id),
42+
{:ok,paged_participants}<-do_paged_comments_participants(thread_query,filter)do
43+
%{
44+
total_count:article.comments_count,
45+
participants_count:article.comments_participants_count,
46+
participants:paged_participants.entries,
47+
is_viewer_joined:false
48+
}
49+
|>done
50+
end
51+
end
52+
53+
defcomments_state(thread,article_id,user)do
54+
with{:ok,thread_query}<-match(thread,:query,article_id),
55+
{:ok,state}<-comments_state(thread,article_id)do
56+
user_joined=
57+
casestate.participants|>Enum.any?(&(&1.id==user.id))do
58+
true->
59+
true
60+
61+
false->
62+
from(cinComment)
63+
|>where(^thread_query)
64+
|>where([c],c.author_id==^user.id)
65+
|>Repo.all()
66+
|>length
67+
|>Kernel.>(0)
68+
end
69+
70+
state|>Map.merge(%{is_viewer_joined:user_joined})|>done
71+
end
72+
end
73+
3674
@doc"""
3775
get spec comment by id
3876
"""

‎lib/groupher_server_web/resolvers/cms_resolver.ex‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,14 @@ defmodule GroupherServerWeb.Resolvers.CMS do
308308
# #######################
309309
# comemnts ..
310310
# #######################
311+
defcomments_state(_root,~m(thread id)a,%{context:%{cur_user:user}})do
312+
CMS.comments_state(thread,id,user)
313+
end
314+
315+
defcomments_state(_root,~m(thread id)a,_)do
316+
CMS.comments_state(thread,id)
317+
end
318+
311319
defone_comment(_root,~m(id)a,%{context:%{cur_user:user}})do
312320
CMS.one_comment(id,user)
313321
end

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do
8787
resolve(&R.CMS.paged_article_tags/3)
8888
end
8989

90+
@desc"got basic commnets state"
91+
field:comments_state,:comments_list_statedo
92+
arg(:id,non_null(:id))
93+
arg(:thread,:thread,default_value::post)
94+
95+
resolve(&R.CMS.comments_state/3)
96+
end
97+
98+
@desc"got spec commnet by id"
9099
field:one_comment,:commentdo
91100
arg(:id,non_null(:id))
92101

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,13 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
324324
field(:is_solution,:boolean)
325325
end
326326

327+
object:comments_list_statedo
328+
field(:total_count,:integer)
329+
field(:participants_count,:integer)
330+
field(:participants,list_of(:common_user))
331+
field(:is_viewer_joined,:boolean)
332+
end
333+
327334
####### reports
328335
object:abuse_report_casedo
329336
field(:reason,:string)

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,45 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do
2626
{:ok,~m(community user user2 user3 post)a}
2727
end
2828

29+
describe"[comments state]"do
30+
test"can get basic state",~m(user post)ado
31+
{:ok,_}=CMS.create_comment(:post,post.id,mock_comment(),user)
32+
{:ok,_}=CMS.create_comment(:post,post.id,mock_comment(),user)
33+
34+
{:ok,state}=CMS.comments_state(:post,post.id)
35+
36+
assertstate.participants_count==1
37+
assertstate.total_count==2
38+
39+
assertstate.participants|>length==1
40+
assertnotstate.is_viewer_joined
41+
end
42+
43+
test"can get viewer joined state",~m(user post)ado
44+
{:ok,_}=CMS.create_comment(:post,post.id,mock_comment(),user)
45+
{:ok,_}=CMS.create_comment(:post,post.id,mock_comment(),user)
46+
47+
{:ok,state}=CMS.comments_state(:post,post.id,user)
48+
49+
assertstate.participants_count==1
50+
assertstate.total_count==2
51+
assertstate.participants|>length==1
52+
assertstate.is_viewer_joined
53+
end
54+
55+
test"can get viewer joined state 2",~m(user user2 user3 post)ado
56+
{:ok,_}=CMS.create_comment(:post,post.id,mock_comment(),user2)
57+
{:ok,_}=CMS.create_comment(:post,post.id,mock_comment(),user3)
58+
59+
{:ok,state}=CMS.comments_state(:post,post.id,user)
60+
61+
assertstate.participants_count==2
62+
assertstate.total_count==2
63+
assertstate.participants|>length==2
64+
assertnotstate.is_viewer_joined
65+
end
66+
end
67+
2968
describe"[basic article comment]"do
3069
test"post are supported by article comment.",~m(user post)ado
3170
{:ok,post_comment_1}=CMS.create_comment(:post,post.id,mock_comment(),user)
@@ -157,7 +196,6 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do
157196
assertList.first(comment.upvotes).user_id==user.id
158197
end
159198

160-
@tag:wip
161199
test"user can upvote a post comment twice is fine",~m(user post)ado
162200
{:ok,comment}=CMS.create_comment(:post,post.id,mock_comment(),user)
163201

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,36 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do
1919
{:ok,~m(user_conn guest_conn community post user user2)a}
2020
end
2121

22+
@query"""
23+
query($id: ID!, $thread: Thread) {
24+
commentsState(id: $id, thread: $thread) {
25+
totalCount
26+
isViewerJoined
27+
participantsCount
28+
29+
participants {
30+
login
31+
nickname
32+
avatar
33+
}
34+
}
35+
}
36+
"""
37+
test"can get basic comments state",~m(guest_conn user_conn post user user2)ado
38+
{:ok,comment}=CMS.create_comment(:post,post.id,mock_comment(),user)
39+
40+
variables=%{id:post.id,thread:"POST"}
41+
results=guest_conn|>query_result(@query,variables,"commentsState")
42+
43+
assertresults["participantsCount"]==1
44+
assertresults["totalCount"]==1
45+
assertnotresults["isViewerJoined"]
46+
assertuser_exist_in?(user,results["participants"])
47+
48+
results=user_conn|>query_result(@query,variables,"commentsState")
49+
assertresults["isViewerJoined"]
50+
end
51+
2252
@query"""
2353
query($id: ID!) {
2454
oneComment(id: $id) {
@@ -190,7 +220,6 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do
190220
}
191221
}
192222
"""
193-
@tag:wip
194223
test"list comments with default replies-mode",~m(guest_conn post user user2)ado
195224
total_count=3
196225
page_size=20

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp