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

Commit 9b5139b

Browse files
authored
refactor(article-comments): support sort in filters (#347)
* refactor(article-comments): support sort in filters * refactor(article-comments): update job test * refactor(article-comments): fix test * refactor(article-comments): fix test
1 parent 45da7fd commit 9b5139b

38 files changed

+257
-143
lines changed

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,16 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
204204

205205
defp do_list_article_comment(thread, article_id, filters, where_query, user) do
206206
%{page: page, size: size} = filters
207+
sort = Map.get(filters, :sort, :asc_inserted)
207208

208209
with {:ok, thread_query} <- match(thread, :query, article_id) do
209210
query = from(c in ArticleComment, preload: [reply_to: :author])
210211

211212
query
212213
|> where(^thread_query)
213214
|> where(^where_query)
214-
|> QueryBuilder.filter_pack(Map.merge(filters, %{sort: :asc_inserted}))
215+
# |> QueryBuilder.filter_pack(Map.merge(filters, %{sort: :asc_inserted}))
216+
|> QueryBuilder.filter_pack(Map.merge(filters, %{sort: sort}))
215217
|> ORM.paginater(~m(page size)a)
216218
|> set_viewer_emotion_ifneed(user)
217219
|> add_pined_comments_ifneed(thread, article_id, filters)

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -286,21 +286,15 @@ defmodule GroupherServerWeb.Resolvers.CMS do
286286
# #######################
287287
def paged_article_comments(_root, ~m(id thread filter mode)a, %{context: %{cur_user: user}}) do
288288
case mode do
289-
:replies ->
290-
CMS.list_article_comments(thread, id, filter, :replies, user)
291-
292-
:timeline ->
293-
CMS.list_article_comments(thread, id, filter, :timeline, user)
289+
:replies -> CMS.list_article_comments(thread, id, filter, :replies, user)
290+
:timeline -> CMS.list_article_comments(thread, id, filter, :timeline, user)
294291
end
295292
end
296293

297294
def paged_article_comments(_root, ~m(id thread filter mode)a, _info) do
298295
case mode do
299-
:replies ->
300-
CMS.list_article_comments(thread, id, filter, :replies)
301-
302-
:timeline ->
303-
CMS.list_article_comments(thread, id, filter, :timeline)
296+
:replies -> CMS.list_article_comments(thread, id, filter, :replies)
297+
:timeline -> CMS.list_article_comments(thread, id, filter, :timeline)
304298
end
305299
end
306300

lib/groupher_server_web/schema/Helper/metrics.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ defmodule GroupherServerWeb.Schema.Helper.Metrics do
3333

3434
input_object :common_paged_filter do
3535
pagination_args()
36-
field(:sort, :comment_sort_enum, default_value: :desc_inserted)
36+
field(:sort, :inserted_sort_enum, default_value: :desc_inserted)
3737
end
3838
end

lib/groupher_server_web/schema/cms/cms_misc.ex

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,9 @@ defmodule GroupherServerWeb.Schema.CMS.Misc do
9595
value(:this_year)
9696
end
9797

98-
enum :comment_sort_enum do
98+
enum :inserted_sort_enum do
9999
value(:asc_inserted)
100100
value(:desc_inserted)
101-
value(:most_likes)
102-
value(:most_dislikes)
103101
end
104102

105103
enum :thread_sort_enum do
@@ -173,7 +171,7 @@ defmodule GroupherServerWeb.Schema.CMS.Misc do
173171

174172
input_object :comments_filter do
175173
pagination_args()
176-
field(:sort, :comment_sort_enum, default_value: :asc_inserted)
174+
field(:sort, :inserted_sort_enum, default_value: :asc_inserted)
177175
end
178176

179177
input_object :communities_filter do

test/groupher_server/cms/comments/job_comment_emotions_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do
2121
end
2222

2323
describe "[emotion in paged article comment]" do
24-
@tag :wip2
24+
@tag :wip3
2525
test "login user should got viewer has emotioned status", ~m(job user)a do
2626
total_count = 0
2727
page_number = 10
@@ -76,7 +76,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do
7676
assert @default_emotions == emotions
7777
end
7878

79-
@tag :wip2
79+
@tag :wip3
8080
test "can make emotion to comment", ~m(job user user2)a do
8181
parent_content = "parent comment"
8282
{:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user)
@@ -91,7 +91,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do
9191
assert user_exist_in?(user2, emotions.latest_downvote_users)
9292
end
9393

94-
@tag :wip2
94+
@tag :wip3
9595
test "can undo emotion to comment", ~m(job user user2)a do
9696
parent_content = "parent comment"
9797
{:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user)
@@ -114,7 +114,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do
114114
assert not user_exist_in?(user2, emotions.latest_downvote_users)
115115
end
116116

117-
@tag :wip2
117+
@tag :wip3
118118
test "same user make same emotion to same comment.", ~m(job user)a do
119119
parent_content = "parent comment"
120120
{:ok, parent_comment} = CMS.create_article_comment(:job, job.id, parent_content, user)
@@ -128,7 +128,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do
128128
assert user_exist_in?(user, parent_comment.emotions.latest_downvote_users)
129129
end
130130

131-
@tag :wip2
131+
@tag :wip3
132132
test "same user same emotion to same comment only have one user_emotion record",
133133
~m(job user)a do
134134
parent_content = "parent comment"
@@ -152,7 +152,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentEmotions do
152152
assert record.heart
153153
end
154154

155-
@tag :wip2
155+
@tag :wip3
156156
test "different user can make same emotions on same comment", ~m(job user user2 user3)a do
157157
{:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent comment", user)
158158

test/groupher_server/cms/comments/job_comment_replies_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do
3333
assert exist_in?(replyed_comment, parent_comment.replies)
3434
end
3535

36-
@tag :wip2
36+
@tag :wip3
3737
test "deleted comment can not be reply", ~m(job user user2)a do
3838
parent_content = "parent comment"
3939
reply_content = "reply comment"
@@ -64,7 +64,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do
6464
assert exist_in?(replyed_comment_2, parent_comment.replies)
6565
end
6666

67-
@tag :wip2
67+
@tag :wip3
6868
test "reply to reply inside a comment should belong same parent comment",
6969
~m(job user user2)a do
7070
{:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent comment", user)
@@ -90,7 +90,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do
9090
assert replyed_comment_3.reply_to_id == replyed_comment_2.id
9191
end
9292

93-
@tag :wip2
93+
@tag :wip3
9494
test "reply to reply inside a comment should have is_reply_to_others flag in meta",
9595
~m(job user user2)a do
9696
{:ok, parent_comment} = CMS.create_article_comment(:job, job.id, "parent comment", user)
@@ -110,7 +110,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobCommentReplies do
110110
assert replyed_comment_3.meta.is_reply_to_others
111111
end
112112

113-
@tag :wip2
113+
@tag :wip3
114114
test "comment replies only contains @max_parent_replies_count replies", ~m(job user)a do
115115
total_reply_count = @max_parent_replies_count + 1
116116

test/groupher_server/cms/comments/job_comment_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do
3939
assert comment.meta |> Map.from_struct() |> Map.delete(:id) == @default_comment_meta
4040
end
4141

42-
@tag :wip2
42+
@tag :wip3
4343
test "comment can be updated", ~m(job user)a do
4444
{:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user)
4545

@@ -130,7 +130,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do
130130
assert comment.meta.is_article_author_upvoted
131131
end
132132

133-
@tag :wip2
133+
@tag :wip3
134134
test "user upvote job comment will add id to upvoted_user_ids", ~m(job user)a do
135135
comment = "job_comment"
136136
{:ok, comment} = CMS.create_article_comment(:job, job.id, comment, user)
@@ -139,7 +139,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do
139139
assert user.id in comment.meta.upvoted_user_ids
140140
end
141141

142-
@tag :wip2
142+
@tag :wip3
143143
test "user undo upvote job comment will remove id from upvoted_user_ids",
144144
~m(job user user2)a do
145145
comment = "job_comment"
@@ -543,7 +543,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do
543543
end
544544

545545
describe "[article comment delete]" do
546-
@tag :wip2
546+
@tag :wip3
547547
test "delete comment still exsit in paged list and content is gone", ~m(user job)a do
548548
total_count = 10
549549
page_number = 1
@@ -568,7 +568,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do
568568
assert deleted_comment.body_html == @delete_hint
569569
end
570570

571-
@tag :wip2
571+
@tag :wip3
572572
test "delete comment still update article's comments_count field", ~m(user job)a do
573573
{:ok, _comment} = CMS.create_article_comment(:job, job.id, "commment", user)
574574
{:ok, _comment} = CMS.create_article_comment(:job, job.id, "commment", user)
@@ -586,7 +586,7 @@ defmodule GroupherServer.Test.CMS.Comments.JobComment do
586586
assert job.article_comments_count == 4
587587
end
588588

589-
@tag :wip2
589+
@tag :wip3
590590
test "delete comment still delete pined record if needed", ~m(user job)a do
591591
total_count = 10
592592

test/groupher_server/cms/comments/post_comment_emotions_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do
2121
end
2222

2323
describe "[emotion in paged article comment]" do
24-
@tag :wip2
24+
@tag :wip3
2525
test "login user should got viewer has emotioned status", ~m(post user)a do
2626
total_count = 0
2727
page_number = 10
@@ -76,7 +76,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do
7676
assert @default_emotions == emotions
7777
end
7878

79-
@tag :wip2
79+
@tag :wip3
8080
test "can make emotion to comment", ~m(post user user2)a do
8181
parent_content = "parent comment"
8282
{:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user)
@@ -91,7 +91,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do
9191
assert user_exist_in?(user2, emotions.latest_downvote_users)
9292
end
9393

94-
@tag :wip2
94+
@tag :wip3
9595
test "can undo emotion to comment", ~m(post user user2)a do
9696
parent_content = "parent comment"
9797
{:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user)
@@ -114,7 +114,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do
114114
assert not user_exist_in?(user2, emotions.latest_downvote_users)
115115
end
116116

117-
@tag :wip2
117+
@tag :wip3
118118
test "same user make same emotion to same comment.", ~m(post user)a do
119119
parent_content = "parent comment"
120120
{:ok, parent_comment} = CMS.create_article_comment(:post, post.id, parent_content, user)
@@ -128,7 +128,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do
128128
assert user_exist_in?(user, parent_comment.emotions.latest_downvote_users)
129129
end
130130

131-
@tag :wip2
131+
@tag :wip3
132132
test "same user same emotion to same comment only have one user_emotion record",
133133
~m(post user)a do
134134
parent_content = "parent comment"
@@ -152,7 +152,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentEmotions do
152152
assert record.heart
153153
end
154154

155-
@tag :wip2
155+
@tag :wip3
156156
test "different user can make same emotions on same comment", ~m(post user user2 user3)a do
157157
{:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent comment", user)
158158

test/groupher_server/cms/comments/post_comment_replies_test.exs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do
3333
assert exist_in?(replyed_comment, parent_comment.replies)
3434
end
3535

36-
@tag :wip2
36+
@tag :wip3
3737
test "deleted comment can not be reply", ~m(post user user2)a do
3838
parent_content = "parent comment"
3939
reply_content = "reply comment"
@@ -64,7 +64,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do
6464
assert exist_in?(replyed_comment_2, parent_comment.replies)
6565
end
6666

67-
@tag :wip2
67+
@tag :wip3
6868
test "reply to reply inside a comment should belong same parent comment",
6969
~m(post user user2)a do
7070
{:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent comment", user)
@@ -90,7 +90,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do
9090
assert replyed_comment_3.reply_to_id == replyed_comment_2.id
9191
end
9292

93-
@tag :wip2
93+
@tag :wip3
9494
test "reply to reply inside a comment should have is_reply_to_others flag in meta",
9595
~m(post user user2)a do
9696
{:ok, parent_comment} = CMS.create_article_comment(:post, post.id, "parent comment", user)
@@ -110,7 +110,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do
110110
assert replyed_comment_3.meta.is_reply_to_others
111111
end
112112

113-
@tag :wip2
113+
@tag :wip3
114114
test "comment replies only contains @max_parent_replies_count replies", ~m(post user)a do
115115
total_reply_count = @max_parent_replies_count + 1
116116

test/groupher_server/cms/comments/post_comment_test.exs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do
3939
assert comment.meta |> Map.from_struct() |> Map.delete(:id) == @default_comment_meta
4040
end
4141

42-
@tag :wip2
42+
@tag :wip3
4343
test "comment can be updated", ~m(post user)a do
4444
{:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user)
4545

@@ -130,7 +130,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do
130130
assert comment.meta.is_article_author_upvoted
131131
end
132132

133-
@tag :wip2
133+
@tag :wip3
134134
test "user upvote post comment will add id to upvoted_user_ids", ~m(post user)a do
135135
comment = "post_comment"
136136
{:ok, comment} = CMS.create_article_comment(:post, post.id, comment, user)
@@ -139,7 +139,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do
139139
assert user.id in comment.meta.upvoted_user_ids
140140
end
141141

142-
@tag :wip2
142+
@tag :wip3
143143
test "user undo upvote post comment will remove id from upvoted_user_ids",
144144
~m(post user user2)a do
145145
comment = "post_comment"
@@ -543,7 +543,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do
543543
end
544544

545545
describe "[article comment delete]" do
546-
@tag :wip2
546+
@tag :wip3
547547
test "delete comment still exsit in paged list and content is gone", ~m(user post)a do
548548
total_count = 10
549549
page_number = 1
@@ -568,7 +568,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do
568568
assert deleted_comment.body_html == @delete_hint
569569
end
570570

571-
@tag :wip2
571+
@tag :wip3
572572
test "delete comment still update article's comments_count field", ~m(user post)a do
573573
{:ok, _comment} = CMS.create_article_comment(:post, post.id, "commment", user)
574574
{:ok, _comment} = CMS.create_article_comment(:post, post.id, "commment", user)
@@ -586,7 +586,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do
586586
assert post.article_comments_count == 4
587587
end
588588

589-
@tag :wip2
589+
@tag :wip3
590590
test "delete comment still delete pined record if needed", ~m(user post)a do
591591
total_count = 10
592592

0 commit comments

Comments
 (0)