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

Commit fee3ed7

Browse files
committed
refactor(article-comments): add list_article_comments_participators
1 parent 04d3680 commit fee3ed7

File tree

8 files changed

+181
-63
lines changed

8 files changed

+181
-63
lines changed

lib/groupher_server/cms/cms.ex

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ defmodule GroupherServer.CMS do
121121

122122
defdelegate list_comment_replies(comment_id, filters), to: ArticleComment
123123
defdelegate list_comment_replies(comment_id, filters, user), to: ArticleComment
124-
125-
defdelegate list_comments(thread, content_id, filters), to: CommentCURD
126-
defdelegate list_comments_participators(thread, content_id, filters), to: CommentCURD
124+
defdelegate list_article_comments_participators(thread, content_id, filters), to: ArticleComment
127125

128126
defdelegate create_article_comment(thread, article_id, args, user), to: ArticleComment
129127
defdelegate upvote_article_comment(comment_id, user), to: ArticleComment
@@ -146,6 +144,9 @@ defmodule GroupherServer.CMS do
146144
defdelegate list_replies(thread, comment, user), to: CommentCURD
147145
defdelegate reply_comment(thread, comment, args, user), to: CommentCURD
148146

147+
defdelegate list_comments(thread, content_id, filters), to: CommentCURD
148+
defdelegate list_comments_participators(thread, content_id, filters), to: CommentCURD
149+
149150
# report
150151
defdelegate create_report(type, content_id, args, user), to: AbuseReport
151152

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 78 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -88,73 +88,26 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
8888
do_list_comment_replies(comment_id, filters, user)
8989
end
9090

91-
defp do_list_article_comment(thread, article_id, filters, where_query, user) do
91+
@spec list_article_comments_participators(T.comment_thread(), Integer.t(), T.paged_filter()) ::
92+
{:ok, T.paged_users()}
93+
def list_article_comments_participators(thread, article_id, filters) do
9294
%{page: page, size: size} = filters
9395

9496
with {:ok, thread_query} <- match(thread, :query, article_id) do
95-
query = from(c in ArticleComment, preload: [reply_to: :author])
96-
97-
query
97+
ArticleComment
9898
|> where(^thread_query)
99-
|> where(^where_query)
100-
|> QueryBuilder.filter_pack(Map.merge(filters, %{sort: :asc_inserted}))
99+
|> QueryBuilder.filter_pack(Map.merge(filters, %{sort: :desc_inserted}))
100+
|> join(:inner, [c], a in assoc(c, :author))
101+
|> distinct([c, a], a.id)
102+
|> group_by([c, a], a.id)
103+
|> group_by([c, a], c.inserted_at)
104+
|> select([c, a], a)
101105
|> ORM.paginater(~m(page size)a)
102-
|> set_viewer_emotion_ifneed(user)
103-
|> add_pined_comments_ifneed(thread, article_id, filters)
104106
|> done()
105107
end
106108
end
107109

108-
defp do_list_comment_replies(comment_id, filters, user) do
109-
%{page: page, size: size} = filters
110-
query = from(c in ArticleComment, preload: [reply_to: :author])
111-
112-
where_query =
113-
dynamic([c], not c.is_reported and not c.is_folded and c.reply_to_id == ^comment_id)
114-
115-
query
116-
|> where(^where_query)
117-
|> QueryBuilder.filter_pack(filters)
118-
|> ORM.paginater(~m(page size)a)
119-
|> set_viewer_emotion_ifneed(user)
120-
|> done()
121-
end
122-
123-
defp add_pined_comments_ifneed(%{entries: entries} = paged_comments, thread, article_id, %{
124-
page: 1
125-
}) do
126-
with {:ok, info} <- match(thread),
127-
query <-
128-
from(p in ArticlePinedComment,
129-
join: c in ArticleComment,
130-
on: p.article_comment_id == c.id,
131-
where: field(p, ^info.foreign_key) == ^article_id,
132-
select: c
133-
),
134-
{:ok, pined_comments} <- Repo.all(query) |> done() do
135-
case pined_comments do
136-
[] ->
137-
paged_comments
138-
139-
_ ->
140-
preloaded_pined_comments =
141-
Enum.slice(pined_comments, 0, @pined_comment_limit)
142-
|> Repo.preload(reply_to: :author)
143-
144-
updated_entries = Enum.concat(preloaded_pined_comments, entries)
145-
146-
pined_comment_count = length(pined_comments)
147-
148-
Map.merge(paged_comments, %{
149-
entries: updated_entries,
150-
total_count: paged_comments.total_count + pined_comment_count
151-
})
152-
end
153-
end
154-
end
155-
156-
defp add_pined_comments_ifneed(paged_comments, _thread, _article_id, _), do: paged_comments
157-
110+
@spec pin_article_comment(Integer.t()) :: {:ok, ArticleComment.t()}
158111
@doc "pin a comment"
159112
def pin_article_comment(comment_id) do
160113
with {:ok, comment} <- ORM.find(ArticleComment, comment_id),
@@ -452,6 +405,73 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
452405
end
453406
end
454407

408+
defp do_list_article_comment(thread, article_id, filters, where_query, user) do
409+
%{page: page, size: size} = filters
410+
411+
with {:ok, thread_query} <- match(thread, :query, article_id) do
412+
query = from(c in ArticleComment, preload: [reply_to: :author])
413+
414+
query
415+
|> where(^thread_query)
416+
|> where(^where_query)
417+
|> QueryBuilder.filter_pack(Map.merge(filters, %{sort: :asc_inserted}))
418+
|> ORM.paginater(~m(page size)a)
419+
|> set_viewer_emotion_ifneed(user)
420+
|> add_pined_comments_ifneed(thread, article_id, filters)
421+
|> done()
422+
end
423+
end
424+
425+
defp do_list_comment_replies(comment_id, filters, user) do
426+
%{page: page, size: size} = filters
427+
query = from(c in ArticleComment, preload: [reply_to: :author])
428+
429+
where_query =
430+
dynamic([c], not c.is_reported and not c.is_folded and c.reply_to_id == ^comment_id)
431+
432+
query
433+
|> where(^where_query)
434+
|> QueryBuilder.filter_pack(filters)
435+
|> ORM.paginater(~m(page size)a)
436+
|> set_viewer_emotion_ifneed(user)
437+
|> done()
438+
end
439+
440+
defp add_pined_comments_ifneed(%{entries: entries} = paged_comments, thread, article_id, %{
441+
page: 1
442+
}) do
443+
with {:ok, info} <- match(thread),
444+
query <-
445+
from(p in ArticlePinedComment,
446+
join: c in ArticleComment,
447+
on: p.article_comment_id == c.id,
448+
where: field(p, ^info.foreign_key) == ^article_id,
449+
select: c
450+
),
451+
{:ok, pined_comments} <- Repo.all(query) |> done() do
452+
case pined_comments do
453+
[] ->
454+
paged_comments
455+
456+
_ ->
457+
preloaded_pined_comments =
458+
Enum.slice(pined_comments, 0, @pined_comment_limit)
459+
|> Repo.preload(reply_to: :author)
460+
461+
updated_entries = Enum.concat(preloaded_pined_comments, entries)
462+
463+
pined_comment_count = length(pined_comments)
464+
465+
Map.merge(paged_comments, %{
466+
entries: updated_entries,
467+
total_count: paged_comments.total_count + pined_comment_count
468+
})
469+
end
470+
end
471+
end
472+
473+
defp add_pined_comments_ifneed(paged_comments, _thread, _article_id, _), do: paged_comments
474+
455475
defp update_article_author_upvoted_info(%ArticleComment{} = comment, user_id) do
456476
with {:ok, article} = get_full_comment(comment.id) do
457477
is_article_author_upvoted = article.author.id == user_id

lib/groupher_server_web/resolvers/cms_resolver.ex

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

364+
def paged_article_comments_participators(_root, ~m(id thread filter)a, _info) do
365+
CMS.list_article_comments_participators(thread, id, filter)
366+
end
367+
364368
def paged_comment_replies(_root, ~m(id filter)a, %{context: %{cur_user: user}}) do
365369
CMS.list_comment_replies(id, filter, user)
366370
end

lib/groupher_server_web/schema/cms/cms_queries.ex

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

174+
@desc "get paged article comments participators"
175+
field :paged_article_comments_participators, :paged_users do
176+
arg(:id, non_null(:id))
177+
arg(:thread, :cms_thread, default_value: :post)
178+
arg(:filter, :paged_filter)
179+
180+
middleware(M.PageSizeProof)
181+
resolve(&R.CMS.paged_article_comments_participators/3)
182+
end
183+
174184
@desc "get paged replies of a comment"
175185
field :paged_comment_replies, :paged_article_replies do
176186
arg(:id, non_null(:id))

lib/helper/types.ex

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ defmodule Helper.Types do
33
custom @types
44
"""
55

6+
alias GroupherServer.{Accounts}
7+
alias Accounts.User
8+
69
@typedoc """
710
Type GraphQL flavor the error format
811
"""
@@ -29,6 +32,22 @@ defmodule Helper.Types do
2932
}
3033

3134
@type article_thread :: :post | :job
35+
@type comment_thread :: :post | :job
36+
37+
@type paged_filter :: %{
38+
page: Integer.t(),
39+
size: Integer.t(),
40+
sort: :desc_inserted | :asc_inserted
41+
}
42+
43+
@type paged_users :: %{
44+
entries: [User.t()],
45+
page_number: Integer.t(),
46+
page_size: Integer.t(),
47+
total_count: Integer.t(),
48+
total_pages: Integer.t()
49+
}
50+
3251
@type article_common :: %{
3352
title: String.t()
3453
}

test/groupher_server/cms/article_comment_replies_test.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ defmodule GroupherServer.Test.CMS.ArticleCommentReplies do
6565
assert exist_in?(replyed_comment_2, parent_comment.replies)
6666
end
6767

68-
@tag :wip2
68+
@tag :wip
6969
test "reply to reply inside a comment should belong same parent comment",
7070
~m(post user user2)a do
7171
{:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent comment", user)
@@ -91,7 +91,7 @@ defmodule GroupherServer.Test.CMS.ArticleCommentReplies do
9191
assert replyed_comment_3.reply_to_id == replyed_comment_2.id
9292
end
9393

94-
@tag :wip2
94+
@tag :wip
9595
test "reply to reply inside a comment should have is_reply_to_others flag in meta",
9696
~m(post user user2)a do
9797
{:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent comment", user)

test/groupher_server/cms/article_comment_test.exs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,30 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
318318
end
319319

320320
describe "paged article comments" do
321+
@tag :wip2
322+
test "can load paged comments participators of a article", ~m(user post)a do
323+
total_count = 30
324+
page_number = 1
325+
page_size = 10
326+
thread = :post
327+
328+
Enum.reduce(1..total_count, [], fn _, acc ->
329+
{:ok, new_user} = db_insert(:user)
330+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", new_user)
331+
332+
acc ++ [comment]
333+
end)
334+
335+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
336+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
337+
338+
{:ok, results} =
339+
CMS.list_article_comments_participators(thread, post.id, %{page: 1, size: page_size})
340+
341+
assert results |> is_valid_pagination?(:raw)
342+
assert results.total_count == total_count + 1
343+
end
344+
321345
@tag :wip
322346
test "paged article comments folded flag should be false", ~m(user post)a do
323347
total_count = 30

test/groupher_server_web/query/cms/article_comment_test.exs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,46 @@ defmodule GroupherServer.Test.Query.ArticleComment do
494494
end
495495
end
496496

497+
describe "paged paticipators" do
498+
@query """
499+
query($id: ID!, $filter: PagedFilter!) {
500+
pagedArticleCommentsParticipators(id: $id, filter: $filter) {
501+
entries {
502+
id
503+
nickname
504+
}
505+
totalPages
506+
totalCount
507+
pageSize
508+
pageNumber
509+
}
510+
}
511+
"""
512+
@tag :wip2
513+
test "guest user can get paged participators", ~m(guest_conn post user)a do
514+
total_count = 30
515+
page_size = 10
516+
thread = "POST"
517+
518+
Enum.reduce(1..total_count, [], fn _, acc ->
519+
{:ok, new_user} = db_insert(:user)
520+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", new_user)
521+
522+
acc ++ [comment]
523+
end)
524+
525+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
526+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "commment", user)
527+
528+
variables = %{id: post.id, thread: thread, filter: %{page: 1, size: page_size}}
529+
530+
results = guest_conn |> query_result(@query, variables, "pagedArticleCommentsParticipators")
531+
532+
assert results |> is_valid_pagination?
533+
assert results["totalCount"] == total_count + 1
534+
end
535+
end
536+
497537
describe "paged replies" do
498538
@query """
499539
query($id: ID!, $filter: CommentsFilter!) {

0 commit comments

Comments
 (0)