Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit 18858b0

Browse files
committed
refactor(article-comments): improve replies test
1 parent d5d316e commit 18858b0

File tree

5 files changed

+121
-5
lines changed

5 files changed

+121
-5
lines changed

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
107107

108108
defp do_list_comment_replies(comment_id, filters, user) do
109109
%{page: page, size: size} = filters
110+
query = from(c in ArticleComment, preload: [reply_to: :author])
110111

111112
where_query =
112113
dynamic([c], not c.is_reported and not c.is_folded and c.reply_to_id == ^comment_id)
113114

114-
ArticleComment
115+
query
115116
|> where(^where_query)
116117
|> QueryBuilder.filter_pack(filters)
117118
|> ORM.paginater(~m(page size)a)

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,14 @@ defmodule GroupherServerWeb.Resolvers.CMS do
361361
end
362362
end
363363

364+
def paged_comment_replies(_root, ~m(id filter)a, %{context: %{cur_user: user}}) do
365+
CMS.list_comment_replies(id, filter, user)
366+
end
367+
368+
def paged_comment_replies(_root, ~m(id filter)a, _info) do
369+
CMS.list_comment_replies(id, filter)
370+
end
371+
364372
def paged_comments(_root, ~m(id thread filter)a, _info) do
365373
CMS.list_comments(thread, id, filter)
366374
end

lib/groupher_server_web/schema/cms/cms_queries.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,15 @@ defmodule GroupherServerWeb.Schema.CMS.Queries do
171171
resolve(&R.CMS.paged_article_comments/3)
172172
end
173173

174+
@desc "get paged replies of a comment"
175+
field :paged_comment_replies, :paged_article_replies do
176+
arg(:id, non_null(:id))
177+
arg(:filter, :comments_filter)
178+
179+
middleware(M.PageSizeProof)
180+
resolve(&R.CMS.paged_comment_replies/3)
181+
end
182+
174183
@desc "get paged comments"
175184
field :paged_comments, :paged_comments do
176185
arg(:id, non_null(:id))

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,23 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
345345
field(:body_html, :string)
346346
field(:author, :user, resolve: dataloader(CMS, :author))
347347
field(:floor, :integer)
348+
field(:upvotes_count, :integer)
348349
field(:is_article_author, :boolean)
350+
field(:emotions, :article_comment_emotions)
351+
field(:meta, :article_comment_meta)
352+
field(:replies_count, :integer)
353+
field(:reply_to, :article_comment_reply)
354+
355+
field :viewer_has_upvoted, :boolean do
356+
arg(:viewer_did, :viewer_did_type, default_value: :viewer_did)
357+
358+
middleware(M.Authorize, :login)
359+
middleware(M.PutCurrentUser)
360+
resolve(dataloader(CMS, :upvotes))
361+
middleware(M.ViewerDidConvert)
362+
end
363+
364+
timestamp_fields()
349365
end
350366

351367
object :article_comment do
@@ -371,8 +387,6 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
371387
middleware(M.ViewerDidConvert)
372388
end
373389

374-
# replies ...
375-
376390
timestamp_fields()
377391
end
378392

@@ -422,11 +436,15 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
422436
end
423437

424438
object :paged_article_comments do
425-
# field(:id, :string)
426439
field(:entries, list_of(:article_comment))
427440
pagination_fields()
428441
end
429442

443+
object :paged_article_replies do
444+
field(:entries, list_of(:article_comment_reply))
445+
pagination_fields()
446+
end
447+
430448
object :paged_post_comments do
431449
field(:entries, list_of(:post_comment))
432450
pagination_fields()

test/groupher_server_web/query/cms/article_comment_test.exs

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,86 @@ defmodule GroupherServer.Test.Query.ArticleComment do
1717
{:ok, ~m(user_conn guest_conn post job user user2)a}
1818
end
1919

20+
describe "paged replies" do
21+
@query """
22+
query($id: ID!, $filter: CommentsFilter!) {
23+
pagedCommentReplies(id: $id, filter: $filter) {
24+
entries {
25+
id
26+
bodyHtml
27+
author {
28+
id
29+
nickname
30+
}
31+
upvotesCount
32+
emotions {
33+
downvoteCount
34+
latestDownvoteUsers {
35+
login
36+
nickname
37+
}
38+
viewerHasDownvoteed
39+
beerCount
40+
latestBeerUsers {
41+
login
42+
nickname
43+
}
44+
viewerHasBeered
45+
}
46+
isArticleAuthor
47+
meta {
48+
isArticleAuthorUpvoted
49+
}
50+
replyTo {
51+
id
52+
bodyHtml
53+
floor
54+
isArticleAuthor
55+
author {
56+
id
57+
login
58+
}
59+
}
60+
repliesCount
61+
viewerHasUpvoted
62+
}
63+
totalPages
64+
totalCount
65+
pageSize
66+
pageNumber
67+
}
68+
}
69+
"""
70+
@tag :wip2
71+
test "guest user can get paged replies", ~m(guest_conn post user user2)a do
72+
comment = "test comment"
73+
total_count = 2
74+
page_size = 10
75+
thread = :post
76+
77+
author_user = post.author.user
78+
{:ok, parent_comment} = CMS.create_article_comment(thread, post.id, comment, user)
79+
80+
Enum.reduce(1..total_count, [], fn i, acc ->
81+
{:ok, reply_comment} = CMS.reply_article_comment(parent_comment.id, "reply #{i}", user2)
82+
83+
acc ++ [reply_comment]
84+
end)
85+
86+
{:ok, author_reply_comment} =
87+
CMS.reply_article_comment(parent_comment.id, "author reply", author_user)
88+
89+
variables = %{id: parent_comment.id, filter: %{page: 1, size: page_size}}
90+
results = guest_conn |> query_result(@query, variables, "pagedCommentReplies")
91+
92+
author_reply_comment =
93+
Enum.find(results["entries"], &(&1["id"] == to_string(author_reply_comment.id)))
94+
95+
assert author_reply_comment["isArticleAuthor"]
96+
assert results["entries"] |> length == total_count + 1
97+
end
98+
end
99+
20100
describe "[baisc article post comment]" do
21101
@query """
22102
query($id: ID!) {
@@ -187,7 +267,7 @@ defmodule GroupherServer.Test.Query.ArticleComment do
187267
assert random_comment["repliesCount"] == 2
188268
end
189269

190-
@tag :wip2
270+
@tag :wip
191271
test "comment should have reply_to content if need", ~m(guest_conn post user user2)a do
192272
total_count = 2
193273
thread = :post

0 commit comments

Comments
 (0)