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

test(article-comment): missing gq test for upvote #393

Merged
merged 1 commit into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions lib/groupher_server/cms/delegates/article_comment_action.ex
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
def upvote_article_comment(comment_id, %User{id: user_id}) do
with {:ok, comment} <- ORM.find(ArticleComment, comment_id),
false <- comment.is_deleted do
# TODO: is user upvoted before?
Multi.new()
|> Multi.run(:create_comment_upvote, fn _, _ ->
ORM.create(ArticleCommentUpvote, %{article_comment_id: comment.id, user_id: user_id})
Expand All @@ -176,6 +175,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
|> Multi.run(:check_article_author_upvoted, fn _, %{inc_upvotes_count: comment} ->
update_article_author_upvoted_info(comment, user_id)
end)
|> Multi.run(:upvote_comment_done, fn _, %{check_article_author_upvoted: comment} ->
viewer_has_upvoted = Enum.member?(comment.meta.upvoted_user_ids, user_id)
viewer_has_reported = Enum.member?(comment.meta.reported_user_ids, user_id)

comment
|> Map.merge(%{viewer_has_upvoted: viewer_has_upvoted})
|> Map.merge(%{viewer_has_reported: viewer_has_reported})
|> done
end)
|> Repo.transaction()
|> result()
end
Expand Down Expand Up @@ -204,6 +212,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
|> Multi.run(:check_article_author_upvoted, fn _, %{desc_upvotes_count: updated_comment} ->
update_article_author_upvoted_info(updated_comment, user_id)
end)
|> Multi.run(:upvote_comment_done, fn _, %{check_article_author_upvoted: comment} ->
viewer_has_upvoted = Enum.member?(comment.meta.upvoted_user_ids, user_id)
viewer_has_reported = Enum.member?(comment.meta.reported_user_ids, user_id)

comment
|> Map.merge(%{viewer_has_upvoted: viewer_has_upvoted})
|> Map.merge(%{viewer_has_reported: viewer_has_reported})
|> done
end)
|> Repo.transaction()
|> result()
end
Expand Down Expand Up @@ -234,20 +251,13 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
defp update_article_author_upvoted_info(%ArticleComment{} = comment, user_id) do
with {:ok, article} = get_full_comment(comment.id) do
is_article_author_upvoted = article.author.id == user_id

new_meta =
comment.meta
|> Map.from_struct()
|> Map.merge(%{is_article_author_upvoted: is_article_author_upvoted})

comment |> ORM.update(%{meta: new_meta})
meta = comment.meta |> Map.put(:is_article_author_upvoted, is_article_author_upvoted)
comment |> ORM.update_meta(meta)
end
end

# 设计盖楼只保留一个层级,回复楼中的评论都会被放到顶楼的 replies 中
defp get_parent_comment(%ArticleComment{reply_to_id: nil} = comment) do
comment
end
defp get_parent_comment(%ArticleComment{reply_to_id: nil} = comment), do: comment

defp get_parent_comment(%ArticleComment{reply_to_id: reply_to_id} = comment)
when not is_nil(reply_to_id) do
Expand Down Expand Up @@ -355,7 +365,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do

defp result({:ok, %{create_article_comment: result}}), do: {:ok, result}
defp result({:ok, %{add_reply_to: result}}), do: {:ok, result}
defp result({:ok, %{check_article_author_upvoted: result}}), do: {:ok, result}
defp result({:ok, %{upvote_comment_done: result}}), do: {:ok, result}
defp result({:ok, %{fold_comment_report_too_many: result}}), do: {:ok, result}
defp result({:ok, %{update_comment_flag: result}}), do: {:ok, result}
defp result({:ok, %{delete_article_comment: result}}), do: {:ok, result}
Expand Down
1 change: 1 addition & 0 deletions lib/groupher_server/cms/models/article_comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ defmodule GroupherServer.CMS.Model.ArticleComment do
# 是否置顶
field(:is_pinned, :boolean, default: false)
field(:viewer_has_upvoted, :boolean, default: false, virtual: true)
field(:viewer_has_reported, :boolean, default: false, virtual: true)

belongs_to(:reply_to, ArticleComment, foreign_key: :reply_to_id)

Expand Down
8 changes: 8 additions & 0 deletions lib/groupher_server_web/resolvers/cms_resolver.ex
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,14 @@ defmodule GroupherServerWeb.Resolvers.CMS do
CMS.reply_article_comment(id, content, user)
end

def upvote_article_comment(_root, ~m(id)a, %{context: %{cur_user: user}}) do
CMS.upvote_article_comment(id, user)
end

def undo_upvote_article_comment(_root, ~m(id)a, %{context: %{cur_user: user}}) do
CMS.undo_upvote_article_comment(id, user)
end

def emotion_to_comment(_root, ~m(id emotion)a, %{context: %{cur_user: user}}) do
CMS.emotion_to_comment(id, emotion, user)
end
Expand Down
16 changes: 16 additions & 0 deletions lib/groupher_server_web/schema/cms/mutations/comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Comment do
middleware(M.Statistics.MakeContribute, for: :user)
end

@desc "upvote to a comment"
field :upvote_article_comment, :article_comment do
arg(:id, non_null(:id))

middleware(M.Authorize, :login)
resolve(&R.CMS.upvote_article_comment/3)
end

@desc "undo upvote to a comment"
field :undo_upvote_article_comment, :article_comment do
arg(:id, non_null(:id))

middleware(M.Authorize, :login)
resolve(&R.CMS.undo_upvote_article_comment/3)
end

@desc "emotion to a comment"
field :emotion_to_comment, :article_comment do
arg(:id, non_null(:id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,59 @@ defmodule GroupherServer.Test.Mutation.Comments.BlogComment do
end
end

describe "[article comment upvote]" do
@upvote_comment_query """
mutation($id: ID!) {
upvoteArticleComment(id: $id) {
id
upvotesCount
viewerHasUpvoted
}
}
"""
@tag :wip
test "login user can upvote a exsit blog comment", ~m(blog user guest_conn user_conn)a do
{:ok, comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user)
variables = %{id: comment.id}

assert guest_conn
|> mutation_get_error?(@upvote_comment_query, variables, ecode(:account_login))

result =
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")

assert result["id"] == to_string(comment.id)
assert result["upvotesCount"] == 1
assert result["viewerHasUpvoted"]
end

@undo_upvote_comment_query """
mutation($id: ID!) {
undoUpvoteArticleComment(id: $id) {
id
upvotesCount
viewerHasUpvoted
}
}
"""
@tag :wip
test "login user can undo upvote a exsit blog comment", ~m(blog user guest_conn user_conn)a do
{:ok, comment} = CMS.create_article_comment(:blog, blog.id, "blog comment", user)
variables = %{id: comment.id}
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")

assert guest_conn
|> mutation_get_error?(@undo_upvote_comment_query, variables, ecode(:account_login))

result =
user_conn
|> mutation_result(@undo_upvote_comment_query, variables, "undoUpvoteArticleComment")

assert result["upvotesCount"] == 0
assert not result["viewerHasUpvoted"]
end
end

describe "[article comment emotion]" do
@emotion_comment_query """
mutation($id: ID!, $emotion: ArticleCommentEmotion!) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,59 @@ defmodule GroupherServer.Test.Mutation.Comments.JobComment do
end
end

describe "[article comment upvote]" do
@upvote_comment_query """
mutation($id: ID!) {
upvoteArticleComment(id: $id) {
id
upvotesCount
viewerHasUpvoted
}
}
"""
@tag :wip
test "login user can upvote a exsit job comment", ~m(job user guest_conn user_conn)a do
{:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user)
variables = %{id: comment.id}

assert guest_conn
|> mutation_get_error?(@upvote_comment_query, variables, ecode(:account_login))

result =
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")

assert result["id"] == to_string(comment.id)
assert result["upvotesCount"] == 1
assert result["viewerHasUpvoted"]
end

@undo_upvote_comment_query """
mutation($id: ID!) {
undoUpvoteArticleComment(id: $id) {
id
upvotesCount
viewerHasUpvoted
}
}
"""
@tag :wip
test "login user can undo upvote a exsit job comment", ~m(job user guest_conn user_conn)a do
{:ok, comment} = CMS.create_article_comment(:job, job.id, "job comment", user)
variables = %{id: comment.id}
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")

assert guest_conn
|> mutation_get_error?(@undo_upvote_comment_query, variables, ecode(:account_login))

result =
user_conn
|> mutation_result(@undo_upvote_comment_query, variables, "undoUpvoteArticleComment")

assert result["upvotesCount"] == 0
assert not result["viewerHasUpvoted"]
end
end

describe "[article comment emotion]" do
@emotion_comment_query """
mutation($id: ID!, $emotion: ArticleCommentEmotion!) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,59 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do
end
end

describe "[article comment upvote]" do
@upvote_comment_query """
mutation($id: ID!) {
upvoteArticleComment(id: $id) {
id
upvotesCount
viewerHasUpvoted
}
}
"""
@tag :wip
test "login user can upvote a exsit post comment", ~m(post user guest_conn user_conn)a do
{:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user)
variables = %{id: comment.id}

assert guest_conn
|> mutation_get_error?(@upvote_comment_query, variables, ecode(:account_login))

result =
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")

assert result["id"] == to_string(comment.id)
assert result["upvotesCount"] == 1
assert result["viewerHasUpvoted"]
end

@undo_upvote_comment_query """
mutation($id: ID!) {
undoUpvoteArticleComment(id: $id) {
id
upvotesCount
viewerHasUpvoted
}
}
"""
@tag :wip
test "login user can undo upvote a exsit post comment", ~m(post user guest_conn user_conn)a do
{:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user)
variables = %{id: comment.id}
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")

assert guest_conn
|> mutation_get_error?(@undo_upvote_comment_query, variables, ecode(:account_login))

result =
user_conn
|> mutation_result(@undo_upvote_comment_query, variables, "undoUpvoteArticleComment")

assert result["upvotesCount"] == 0
assert not result["viewerHasUpvoted"]
end
end

describe "[article comment emotion]" do
@emotion_comment_query """
mutation($id: ID!, $emotion: ArticleCommentEmotion!) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,59 @@ defmodule GroupherServer.Test.Mutation.Comments.RepoComment do
end
end

describe "[article comment upvote]" do
@upvote_comment_query """
mutation($id: ID!) {
upvoteArticleComment(id: $id) {
id
upvotesCount
viewerHasUpvoted
}
}
"""
@tag :wip
test "login user can upvote a exsit repo comment", ~m(repo user guest_conn user_conn)a do
{:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user)
variables = %{id: comment.id}

assert guest_conn
|> mutation_get_error?(@upvote_comment_query, variables, ecode(:account_login))

result =
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")

assert result["id"] == to_string(comment.id)
assert result["upvotesCount"] == 1
assert result["viewerHasUpvoted"]
end

@undo_upvote_comment_query """
mutation($id: ID!) {
undoUpvoteArticleComment(id: $id) {
id
upvotesCount
viewerHasUpvoted
}
}
"""
@tag :wip
test "login user can undo upvote a exsit repo comment", ~m(repo user guest_conn user_conn)a do
{:ok, comment} = CMS.create_article_comment(:repo, repo.id, "repo comment", user)
variables = %{id: comment.id}
user_conn |> mutation_result(@upvote_comment_query, variables, "upvoteArticleComment")

assert guest_conn
|> mutation_get_error?(@undo_upvote_comment_query, variables, ecode(:account_login))

result =
user_conn
|> mutation_result(@undo_upvote_comment_query, variables, "undoUpvoteArticleComment")

assert result["upvotesCount"] == 0
assert not result["viewerHasUpvoted"]
end
end

describe "[article comment emotion]" do
@emotion_comment_query """
mutation($id: ID!, $emotion: ArticleCommentEmotion!) {
Expand Down