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

Commit e4e266b

Browse files
authored
refactor(article-comment): missing pin/unpin gq endpoint & test (#398)
1 parent cd519cf commit e4e266b

File tree

9 files changed

+262
-21
lines changed

9 files changed

+262
-21
lines changed

lib/groupher_server/cms/delegates/comment_action.ex

+2-9
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,7 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
2121
alias GroupherServer.{Accounts, CMS, Repo}
2222

2323
alias Accounts.Model.User
24-
25-
alias CMS.Model.{
26-
Comment,
27-
PinnedComment,
28-
CommentUpvote,
29-
CommentReply,
30-
Embeds
31-
}
24+
alias CMS.Model.{Comment, PinnedComment, CommentUpvote, CommentReply, Embeds}
3225

3326
alias Ecto.Multi
3427

@@ -38,8 +31,8 @@ defmodule GroupherServer.CMS.Delegate.CommentAction do
3831
@max_parent_replies_count Comment.max_parent_replies_count()
3932
@pinned_comment_limit Comment.pinned_comment_limit()
4033

41-
@spec pin_comment(Integer.t()) :: {:ok, Comment.t()}
4234
@doc "pin a comment"
35+
@spec pin_comment(Integer.t()) :: {:ok, Comment.t()}
4336
def pin_comment(comment_id) do
4437
with {:ok, comment} <- ORM.find(Comment, comment_id),
4538
{:ok, full_comment} <- get_full_comment(comment.id),

lib/groupher_server/cms/helper/macros.ex

+2-10
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,9 @@ defmodule GroupherServer.CMS.Helper.Macros do
55
import Helper.Utils, only: [get_config: 2]
66

77
alias GroupherServer.{CMS, Accounts}
8-
alias Accounts.Model.User
98

10-
alias CMS.Model.{
11-
Embeds,
12-
Author,
13-
Community,
14-
Comment,
15-
ArticleTag,
16-
ArticleUpvote,
17-
ArticleCollect
18-
}
9+
alias Accounts.Model.User
10+
alias CMS.Model.{Embeds, Author, Community, Comment, ArticleTag, ArticleUpvote, ArticleCollect}
1911

2012
@article_threads get_config(:article, :threads)
2113

lib/groupher_server_web/resolvers/cms_resolver.ex

+4-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ defmodule GroupherServerWeb.Resolvers.CMS do
7676
def delete_article(_root, %{passport_source: content}, _info), do: ORM.delete(content)
7777

7878
# #######################
79-
# content flag ..
79+
# article actions
8080
# #######################
8181
def pin_article(_root, ~m(id community_id thread)a, _info) do
8282
CMS.pin_article(thread, id, community_id)
@@ -336,6 +336,9 @@ defmodule GroupherServerWeb.Resolvers.CMS do
336336
CMS.undo_mark_comment_solution(id, user)
337337
end
338338

339+
def pin_comment(_root, ~m(id)a, _info), do: CMS.pin_comment(id)
340+
def undo_pin_comment(_root, ~m(id)a, _info), do: CMS.undo_pin_comment(id)
341+
339342
############
340343
############
341344
############

lib/groupher_server_web/schema/cms/mutations/comment.ex

+22
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,27 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Comment do
104104
middleware(M.Authorize, :login)
105105
resolve(&R.CMS.undo_mark_comment_solution/3)
106106
end
107+
108+
@desc "pin a comment"
109+
field :pin_comment, :comment do
110+
arg(:id, non_null(:id))
111+
112+
middleware(M.Authorize, :login)
113+
middleware(M.PassportLoader, source: :comment)
114+
middleware(M.Passport, claim: "owner")
115+
116+
resolve(&R.CMS.pin_comment/3)
117+
end
118+
119+
@desc "undo pin a comment"
120+
field :undo_pin_comment, :comment do
121+
arg(:id, non_null(:id))
122+
123+
middleware(M.Authorize, :login)
124+
middleware(M.PassportLoader, source: :comment)
125+
middleware(M.Passport, claim: "owner")
126+
127+
resolve(&R.CMS.undo_pin_comment/3)
128+
end
107129
end
108130
end

test/groupher_server/cms/comments/post_comment_replies_test.exs

-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ defmodule GroupherServer.Test.CMS.Comments.PostCommentReplies do
119119
assert not exist_in?(List.last(reply_comment_list), parent_comment.replies)
120120
end
121121

122-
@tag :wip
123122
test "replyed user should appear in article comment participants", ~m(post user user2)a do
124123
{:ok, parent_comment} = CMS.create_comment(:post, post.id, mock_comment(), user)
125124
{:ok, _} = CMS.reply_comment(parent_comment.id, mock_comment(), user2)

test/groupher_server_web/mutation/cms/comments/blog_comment_test.exs

+58
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,62 @@ defmodule GroupherServer.Test.Mutation.Comments.BlogComment do
269269
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
270270
end
271271
end
272+
273+
describe "[article comment pin/unPin]" do
274+
@query """
275+
mutation($id: ID!){
276+
pinComment(id: $id) {
277+
id
278+
isPinned
279+
}
280+
}
281+
"""
282+
@tag :wip
283+
test "can pin a blog's comment", ~m(owner_conn blog user)a do
284+
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user)
285+
286+
variables = %{id: comment.id}
287+
result = owner_conn |> mutation_result(@query, variables, "pinComment")
288+
289+
assert result["id"] == to_string(comment.id)
290+
assert result["isPinned"]
291+
end
292+
293+
@tag :wip
294+
test "unauth user fails.", ~m(guest_conn blog user)a do
295+
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user)
296+
variables = %{id: comment.id}
297+
298+
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
299+
end
300+
301+
@query """
302+
mutation($id: ID!){
303+
undoPinComment(id: $id) {
304+
id
305+
isPinned
306+
}
307+
}
308+
"""
309+
@tag :wip
310+
test "can undo pin a blog's comment", ~m(owner_conn blog user)a do
311+
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user)
312+
{:ok, _} = CMS.pin_comment(comment.id)
313+
314+
variables = %{id: comment.id}
315+
result = owner_conn |> mutation_result(@query, variables, "undoPinComment")
316+
317+
assert result["id"] == to_string(comment.id)
318+
assert not result["isPinned"]
319+
end
320+
321+
@tag :wip
322+
test "unauth user undo fails.", ~m(guest_conn blog user)a do
323+
{:ok, comment} = CMS.create_comment(:blog, blog.id, mock_comment(), user)
324+
{:ok, _} = CMS.pin_comment(comment.id)
325+
variables = %{id: comment.id}
326+
327+
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
328+
end
329+
end
272330
end

test/groupher_server_web/mutation/cms/comments/job_comment_test.exs

+58
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,62 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do
269269
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
270270
end
271271
end
272+
273+
describe "[article comment pin/unPin]" do
274+
@query """
275+
mutation($id: ID!){
276+
pinComment(id: $id) {
277+
id
278+
isPinned
279+
}
280+
}
281+
"""
282+
@tag :wip
283+
test "can pin a job's comment", ~m(owner_conn job user)a do
284+
{:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user)
285+
286+
variables = %{id: comment.id}
287+
result = owner_conn |> mutation_result(@query, variables, "pinComment")
288+
289+
assert result["id"] == to_string(comment.id)
290+
assert result["isPinned"]
291+
end
292+
293+
@tag :wip
294+
test "unauth user fails.", ~m(guest_conn job user)a do
295+
{:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user)
296+
variables = %{id: comment.id}
297+
298+
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
299+
end
300+
301+
@query """
302+
mutation($id: ID!){
303+
undoPinComment(id: $id) {
304+
id
305+
isPinned
306+
}
307+
}
308+
"""
309+
@tag :wip
310+
test "can undo pin a job's comment", ~m(owner_conn job user)a do
311+
{:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user)
312+
{:ok, _} = CMS.pin_comment(comment.id)
313+
314+
variables = %{id: comment.id}
315+
result = owner_conn |> mutation_result(@query, variables, "undoPinComment")
316+
317+
assert result["id"] == to_string(comment.id)
318+
assert not result["isPinned"]
319+
end
320+
321+
@tag :wip
322+
test "unauth user undo fails.", ~m(guest_conn job user)a do
323+
{:ok, comment} = CMS.create_comment(:job, job.id, mock_comment(), user)
324+
{:ok, _} = CMS.pin_comment(comment.id)
325+
variables = %{id: comment.id}
326+
327+
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
328+
end
329+
end
272330
end

test/groupher_server_web/mutation/cms/comments/post_comment_test.exs

+58
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,64 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do
271271
end
272272
end
273273

274+
describe "[article comment pin/unPin]" do
275+
@query """
276+
mutation($id: ID!){
277+
pinComment(id: $id) {
278+
id
279+
isPinned
280+
}
281+
}
282+
"""
283+
@tag :wip
284+
test "can pin a post's comment", ~m(owner_conn post user)a do
285+
{:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user)
286+
287+
variables = %{id: comment.id}
288+
result = owner_conn |> mutation_result(@query, variables, "pinComment")
289+
290+
assert result["id"] == to_string(comment.id)
291+
assert result["isPinned"]
292+
end
293+
294+
@tag :wip
295+
test "unauth user fails.", ~m(guest_conn post user)a do
296+
{:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user)
297+
variables = %{id: comment.id}
298+
299+
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
300+
end
301+
302+
@query """
303+
mutation($id: ID!){
304+
undoPinComment(id: $id) {
305+
id
306+
isPinned
307+
}
308+
}
309+
"""
310+
@tag :wip
311+
test "can undo pin a post's comment", ~m(owner_conn post user)a do
312+
{:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user)
313+
{:ok, _} = CMS.pin_comment(comment.id)
314+
315+
variables = %{id: comment.id}
316+
result = owner_conn |> mutation_result(@query, variables, "undoPinComment")
317+
318+
assert result["id"] == to_string(comment.id)
319+
assert not result["isPinned"]
320+
end
321+
322+
@tag :wip
323+
test "unauth user undo fails.", ~m(guest_conn post user)a do
324+
{:ok, comment} = CMS.create_comment(:post, post.id, mock_comment(), user)
325+
{:ok, _} = CMS.pin_comment(comment.id)
326+
variables = %{id: comment.id}
327+
328+
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
329+
end
330+
end
331+
274332
describe "[post only: article comment solution]" do
275333
@query """
276334
mutation($id: ID!) {

test/groupher_server_web/mutation/cms/comments/repo_comment_test.exs

+58
Original file line numberDiff line numberDiff line change
@@ -269,4 +269,62 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do
269269
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
270270
end
271271
end
272+
273+
describe "[article comment pin/unPin]" do
274+
@query """
275+
mutation($id: ID!){
276+
pinComment(id: $id) {
277+
id
278+
isPinned
279+
}
280+
}
281+
"""
282+
@tag :wip
283+
test "can pin a repo's comment", ~m(owner_conn repo user)a do
284+
{:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user)
285+
286+
variables = %{id: comment.id}
287+
result = owner_conn |> mutation_result(@query, variables, "pinComment")
288+
289+
assert result["id"] == to_string(comment.id)
290+
assert result["isPinned"]
291+
end
292+
293+
@tag :wip
294+
test "unauth user fails.", ~m(guest_conn repo user)a do
295+
{:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user)
296+
variables = %{id: comment.id}
297+
298+
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
299+
end
300+
301+
@query """
302+
mutation($id: ID!){
303+
undoPinComment(id: $id) {
304+
id
305+
isPinned
306+
}
307+
}
308+
"""
309+
@tag :wip
310+
test "can undo pin a repo's comment", ~m(owner_conn repo user)a do
311+
{:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user)
312+
{:ok, _} = CMS.pin_comment(comment.id)
313+
314+
variables = %{id: comment.id}
315+
result = owner_conn |> mutation_result(@query, variables, "undoPinComment")
316+
317+
assert result["id"] == to_string(comment.id)
318+
assert not result["isPinned"]
319+
end
320+
321+
@tag :wip
322+
test "unauth user undo fails.", ~m(guest_conn repo user)a do
323+
{:ok, comment} = CMS.create_comment(:repo, repo.id, mock_comment(), user)
324+
{:ok, _} = CMS.pin_comment(comment.id)
325+
variables = %{id: comment.id}
326+
327+
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
328+
end
329+
end
272330
end

0 commit comments

Comments
 (0)