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

Commit 83fffc4

Browse files
committed
feat(post-qa): gq workflow
1 parent bdc9257 commit 83fffc4

File tree

6 files changed

+115
-7
lines changed

6 files changed

+115
-7
lines changed

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
146146
article_comment |> ORM.update(%{body_html: content})
147147
end
148148

149-
def mark_comment_solution(article_comment_id, user) do
150-
with {:ok, article_comment} <- ORM.find(ArticleComment, article_comment_id),
149+
@doc """
150+
mark a comment as question post's best solution
151+
"""
152+
def mark_comment_solution(comment_id, user) do
153+
with {:ok, article_comment} <- ORM.find(ArticleComment, comment_id),
151154
{:ok, post} <- ORM.find(Post, article_comment.post_id, preload: [author: :user]) do
152155
# 确保只有一个最佳答案
153156
batch_update_solution_flag(post, false)
@@ -156,8 +159,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
156159
end
157160
end
158161

159-
def undo_mark_comment_solution(article_comment_id, user) do
160-
with {:ok, article_comment} <- ORM.find(ArticleComment, article_comment_id),
162+
@doc """
163+
undo mark a comment as question post's best solution
164+
"""
165+
def undo_mark_comment_solution(comment_id, user) do
166+
with {:ok, article_comment} <- ORM.find(ArticleComment, comment_id),
161167
{:ok, post} <- ORM.find(Post, article_comment.post_id, preload: [author: :user]) do
162168
do_mark_comment_solution(post, article_comment, user, false)
163169
end
@@ -168,7 +174,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
168174
with true <- user.id == post.author.user.id do
169175
Multi.new()
170176
|> Multi.run(:mark_solution, fn _, _ ->
171-
ORM.update(article_comment, %{is_solution: is_solution})
177+
ORM.update(article_comment, %{is_solution: is_solution, is_for_question: true})
172178
end)
173179
|> Multi.run(:update_post_state, fn _, _ ->
174180
ORM.update(post, %{is_solved: is_solution, solution_digest: article_comment.body_html})

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,14 @@ defmodule GroupherServerWeb.Resolvers.CMS do
320320
CMS.undo_emotion_to_comment(id, emotion, user)
321321
end
322322

323+
def mark_comment_solution(_root, ~m(id)a, %{context: %{cur_user: user}}) do
324+
CMS.mark_comment_solution(id, user)
325+
end
326+
327+
def undo_mark_comment_solution(_root, ~m(id)a, %{context: %{cur_user: user}}) do
328+
CMS.undo_mark_comment_solution(id, user)
329+
end
330+
323331
############
324332
############
325333
############

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
344344
field(:is_deleted, :boolean)
345345
field(:viewer_has_upvoted, :boolean)
346346

347+
field(:is_for_question, :boolean)
348+
field(:is_solution, :boolean)
349+
347350
timestamp_fields()
348351
end
349352

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,22 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Comment do
7373
resolve(&R.CMS.undo_emotion_to_comment/3)
7474
end
7575

76+
@desc "mark a comment as question post's best solution"
77+
field :mark_comment_solution, :article_comment do
78+
arg(:id, non_null(:id))
79+
80+
middleware(M.Authorize, :login)
81+
resolve(&R.CMS.mark_comment_solution/3)
82+
end
83+
84+
@desc "mark a comment as question post's best solution"
85+
field :undo_mark_comment_solution, :article_comment do
86+
arg(:id, non_null(:id))
87+
88+
middleware(M.Authorize, :login)
89+
resolve(&R.CMS.undo_mark_comment_solution/3)
90+
end
91+
7692
############################
7793
############################
7894
############################

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

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,79 @@ defmodule GroupherServer.Test.Mutation.Comments.PostComment do
219219
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
220220
end
221221
end
222+
223+
describe "[post only: article comment solution]" do
224+
@query """
225+
mutation($id: ID!) {
226+
markCommentSolution(id: $id) {
227+
id
228+
isForQuestion
229+
isSolution
230+
}
231+
}
232+
"""
233+
@tag :wip
234+
test "questioner can mark a post comment as solution", ~m(post)a do
235+
{:ok, post} = ORM.find(Post, post.id, preload: [author: :user])
236+
post_author = post.author.user
237+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "solution", post_author)
238+
239+
questioner_conn = simu_conn(:user, post_author)
240+
241+
variables = %{id: comment.id}
242+
243+
result = questioner_conn |> mutation_result(@query, variables, "markCommentSolution")
244+
245+
assert result["isForQuestion"]
246+
assert result["isSolution"]
247+
end
248+
249+
@tag :wip
250+
test "other user can not mark a post comment as solution", ~m(guest_conn user_conn post)a do
251+
{:ok, post} = ORM.find(Post, post.id, preload: [author: :user])
252+
post_author = post.author.user
253+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "solution", post_author)
254+
255+
variables = %{id: comment.id}
256+
assert user_conn |> mutation_get_error?(@query, variables, ecode(:require_questioner))
257+
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
258+
end
259+
260+
@query """
261+
mutation($id: ID!) {
262+
undoMarkCommentSolution(id: $id) {
263+
id
264+
isForQuestion
265+
isSolution
266+
}
267+
}
268+
"""
269+
@tag :wip
270+
test "questioner can undo mark a post comment as solution", ~m(post)a do
271+
{:ok, post} = ORM.find(Post, post.id, preload: [author: :user])
272+
post_author = post.author.user
273+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "solution", post_author)
274+
{:ok, comment} = CMS.mark_comment_solution(comment.id, post_author)
275+
276+
questioner_conn = simu_conn(:user, post_author)
277+
278+
variables = %{id: comment.id}
279+
result = questioner_conn |> mutation_result(@query, variables, "undoMarkCommentSolution")
280+
281+
assert result["isForQuestion"]
282+
assert not result["isSolution"]
283+
end
284+
285+
@tag :wip
286+
test "other user can not undo mark a post comment as solution",
287+
~m(guest_conn user_conn post)a do
288+
{:ok, post} = ORM.find(Post, post.id, preload: [author: :user])
289+
post_author = post.author.user
290+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "solution", post_author)
291+
292+
variables = %{id: comment.id}
293+
assert user_conn |> mutation_get_error?(@query, variables, ecode(:require_questioner))
294+
assert guest_conn |> mutation_get_error?(@query, variables, ecode(:account_login))
295+
end
296+
end
222297
end

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do
290290
post_author = post.author.user
291291

292292
{:ok, comment} = CMS.create_article_comment(thread, post.id, "pinned comment", user)
293-
{:ok, pinned_comment} = CMS.pin_article_comment(comment.id)
293+
{:ok, _pinned_comment} = CMS.pin_article_comment(comment.id)
294294

295295
Process.sleep(1000)
296296

@@ -299,7 +299,7 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do
299299

300300
Process.sleep(1000)
301301
{:ok, comment} = CMS.create_article_comment(thread, post.id, "pinned comment 2", user)
302-
{:ok, pinned_comment2} = CMS.pin_article_comment(comment.id)
302+
{:ok, _pinned_comment2} = CMS.pin_article_comment(comment.id)
303303

304304
variables = %{id: post.id, thread: "POST", filter: %{page: 1, size: 10}}
305305
results = guest_conn |> query_result(@query, variables, "pagedArticleComments")

0 commit comments

Comments
 (0)