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

Commit cea7bcd

Browse files
committed
feat(post-qa): pin solution logic
1 parent 9f99109 commit cea7bcd

File tree

5 files changed

+81
-16
lines changed

5 files changed

+81
-16
lines changed

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
107107
|> Multi.run(:set_question_flag_ifneed, fn _, %{create_article_comment: comment} ->
108108
set_question_flag_ifneed(article, comment)
109109
end)
110-
|> Multi.run(:add_participator, fn _, _ ->
111-
add_participator_to_article(article, user)
112-
end)
110+
|> Multi.run(:add_participator, fn _, _ -> add_participator_to_article(article, user) end)
113111
|> Multi.run(:update_article_active_timestamp, fn _, %{create_article_comment: comment} ->
114112
case comment.author_id == article.author.user.id do
115113
true -> {:ok, :pass}
@@ -144,6 +142,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
144142
{:ok, post} <- ORM.find(CMS.Post, article_comment.post_id, preload: [author: :user]) do
145143
# 确保只有一个最佳答案
146144
batch_update_solution_flag(post, false)
145+
CMS.pin_article_comment(article_comment.id)
147146
do_mark_comment_solution(post, article_comment, user, true)
148147
end
149148
end
@@ -209,7 +208,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
209208
%{article_comments_participators: article_comments_participators} = article,
210209
%User{} = user
211210
) do
212-
total_participators = article_comments_participators |> List.insert_at(0, user) |> Enum.uniq()
211+
total_participators =
212+
article_comments_participators |> List.insert_at(0, user) |> Enum.uniq_by(& &1.id)
213+
213214
new_comment_participators = total_participators |> Enum.slice(0, @max_participator_count)
214215
total_participators_count = length(total_participators)
215216

@@ -312,6 +313,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
312313
join: c in ArticleComment,
313314
on: p.article_comment_id == c.id,
314315
where: field(p, ^info.foreign_key) == ^article_id,
316+
order_by: [desc: p.inserted_at],
315317
select: c
316318
),
317319
{:ok, pined_comments} <- Repo.all(query) |> done() do
@@ -323,6 +325,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
323325
preloaded_pined_comments =
324326
Enum.slice(pined_comments, 0, @pinned_comment_limit)
325327
|> Repo.preload(reply_to: :author)
328+
|> sort_solution_to_front
326329

327330
entries = Enum.concat(preloaded_pined_comments, entries)
328331
pined_comment_count = length(pined_comments)
@@ -335,6 +338,19 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
335338

336339
defp add_pined_comments_ifneed(paged_comments, _thread, _article_id, _), do: paged_comments
337340

341+
defp sort_solution_to_front(pined_comments) do
342+
solution_index = Enum.find_index(pined_comments, & &1.is_solution)
343+
344+
case is_nil(solution_index) do
345+
true ->
346+
pined_comments
347+
348+
false ->
349+
{solution_comment, rest_comments} = List.pop_at(pined_comments, solution_index)
350+
[solution_comment] ++ rest_comments
351+
end
352+
end
353+
338354
defp mark_viewer_has_upvoted(paged_comments, nil), do: paged_comments
339355

340356
defp mark_viewer_has_upvoted(%{entries: entries} = paged_comments, %User{} = user) do

test/groupher_server/cms/comments/post_comment_test.exs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do
714714
assert not comment3.is_for_question
715715
end
716716

717-
@tag :wip
717+
@tag :wip2
718718
test "can mark a comment as solution", ~m(user community)a do
719719
post_attrs = mock_attrs(:post, %{community_id: community.id, is_question: true})
720720
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)
@@ -732,7 +732,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do
732732
assert post.solution_digest == comment.body_html
733733
end
734734

735-
@tag :wip
735+
@tag :wip2
736736
test "non-post-author can not mark a comment as solution", ~m(user community)a do
737737
post_attrs = mock_attrs(:post, %{community_id: community.id, is_question: true})
738738
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)
@@ -747,7 +747,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do
747747
reason |> is_error?(:require_questioner)
748748
end
749749

750-
@tag :wip
750+
@tag :wip2
751751
test "can undo mark a comment as solution", ~m(user community)a do
752752
post_attrs = mock_attrs(:post, %{community_id: community.id, is_question: true})
753753
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)
@@ -765,7 +765,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do
765765
assert not post.is_solved
766766
end
767767

768-
@tag :wip
768+
@tag :wip2
769769
test "non-post-author can not undo mark a comment as solution", ~m(user community)a do
770770
post_attrs = mock_attrs(:post, %{community_id: community.id, is_question: true})
771771
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)
@@ -780,7 +780,7 @@ defmodule GroupherServer.Test.CMS.Comments.PostComment do
780780
reason |> is_error?(:require_questioner)
781781
end
782782

783-
@tag :wip
783+
@tag :wip2
784784
test "can only mark one best comment as solution", ~m(user community)a do
785785
post_attrs = mock_attrs(:post, %{community_id: community.id, is_question: true})
786786
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do
240240
assert results["totalCount"] == total_count
241241
end
242242

243+
@tag :wip
243244
test "guest user can get paged comment with pinned comment in it",
244245
~m(guest_conn job user)a do
245246
total_count = 20
@@ -254,14 +255,16 @@ defmodule GroupherServer.Test.Query.Comments.JobComment do
254255
{:ok, comment} = CMS.create_article_comment(thread, job.id, "pinned comment", user)
255256
{:ok, pinned_comment} = CMS.pin_article_comment(comment.id)
256257

258+
Process.sleep(1000)
259+
257260
{:ok, comment} = CMS.create_article_comment(thread, job.id, "pinned comment 2", user)
258261
{:ok, pinned_comment2} = CMS.pin_article_comment(comment.id)
259262

260263
variables = %{id: job.id, thread: "JOB", filter: %{page: 1, size: 10}}
261264
results = guest_conn |> query_result(@query, variables, "pagedArticleComments")
262265

263-
assert results["entries"] |> List.first() |> Map.get("id") == to_string(pinned_comment.id)
264-
assert results["entries"] |> Enum.at(1) |> Map.get("id") == to_string(pinned_comment2.id)
266+
assert results["entries"] |> List.first() |> Map.get("id") == to_string(pinned_comment2.id)
267+
assert results["entries"] |> Enum.at(1) |> Map.get("id") == to_string(pinned_comment.id)
265268

266269
assert results["totalCount"] == total_count + 2
267270
end

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

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do
44
use GroupherServer.TestTools
55

66
alias GroupherServer.CMS
7+
alias Helper.ORM
78

89
setup do
910
{:ok, post} = db_insert(:post)
1011
{:ok, user} = db_insert(:user)
1112
{:ok, user2} = db_insert(:user)
13+
{:ok, community} = db_insert(:community)
1214

1315
guest_conn = simu_conn(:guest)
1416
user_conn = simu_conn(:user, user)
1517

16-
{:ok, ~m(user_conn guest_conn post user user2)a}
18+
{:ok, ~m(user_conn guest_conn community post user user2)a}
1719
end
1820

1921
describe "[baisc article post comment]" do
@@ -240,6 +242,7 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do
240242
assert results["totalCount"] == total_count
241243
end
242244

245+
@tag :wip
243246
test "guest user can get paged comment with pinned comment in it",
244247
~m(guest_conn post user)a do
245248
total_count = 20
@@ -254,18 +257,58 @@ defmodule GroupherServer.Test.Query.Comments.PostComment do
254257
{:ok, comment} = CMS.create_article_comment(thread, post.id, "pinned comment", user)
255258
{:ok, pinned_comment} = CMS.pin_article_comment(comment.id)
256259

260+
Process.sleep(1000)
261+
257262
{:ok, comment} = CMS.create_article_comment(thread, post.id, "pinned comment 2", user)
258263
{:ok, pinned_comment2} = CMS.pin_article_comment(comment.id)
259264

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

263-
assert results["entries"] |> List.first() |> Map.get("id") == to_string(pinned_comment.id)
264-
assert results["entries"] |> Enum.at(1) |> Map.get("id") == to_string(pinned_comment2.id)
268+
assert results["entries"] |> List.first() |> Map.get("id") == to_string(pinned_comment2.id)
269+
assert results["entries"] |> Enum.at(1) |> Map.get("id") == to_string(pinned_comment.id)
265270

266271
assert results["totalCount"] == total_count + 2
267272
end
268273

274+
# post only
275+
@tag :wip
276+
test "if solution in pinned comments, solution should always on top",
277+
~m(guest_conn community user)a do
278+
post_attrs = mock_attrs(:post, %{community_id: community.id, is_question: true})
279+
{:ok, post} = CMS.create_article(community, :post, post_attrs, user)
280+
281+
total_count = 20
282+
thread = :post
283+
284+
Enum.reduce(1..total_count, [], fn _, acc ->
285+
{:ok, comment} = CMS.create_article_comment(thread, post.id, "test comment", user)
286+
287+
acc ++ [comment]
288+
end)
289+
290+
{:ok, post} = ORM.find(CMS.Post, post.id, preload: [author: :user])
291+
post_author = post.author.user
292+
293+
{:ok, comment} = CMS.create_article_comment(thread, post.id, "pinned comment", user)
294+
{:ok, pinned_comment} = CMS.pin_article_comment(comment.id)
295+
296+
Process.sleep(1000)
297+
298+
{:ok, comment} = CMS.create_article_comment(thread, post.id, "solution", post_author)
299+
{:ok, solution_comment} = CMS.mark_comment_solution(comment.id, post_author)
300+
301+
Process.sleep(1000)
302+
{:ok, comment} = CMS.create_article_comment(thread, post.id, "pinned comment 2", user)
303+
{:ok, pinned_comment2} = CMS.pin_article_comment(comment.id)
304+
305+
variables = %{id: post.id, thread: "POST", filter: %{page: 1, size: 10}}
306+
results = guest_conn |> query_result(@query, variables, "pagedArticleComments")
307+
308+
assert results["entries"] |> List.first() |> Map.get("id") == to_string(solution_comment.id)
309+
assert results["totalCount"] == total_count + 3
310+
end
311+
269312
test "guest user can get paged comment with floor it", ~m(guest_conn post user)a do
270313
total_count = 5
271314
thread = :post

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do
238238
assert results["totalCount"] == total_count
239239
end
240240

241+
@tag :wip
241242
test "guest user can get paged comment with pinned comment in it",
242243
~m(guest_conn repo user)a do
243244
total_count = 20
@@ -252,14 +253,16 @@ defmodule GroupherServer.Test.Query.Comments.RepoComment do
252253
{:ok, comment} = CMS.create_article_comment(thread, repo.id, "pinned comment", user)
253254
{:ok, pinned_comment} = CMS.pin_article_comment(comment.id)
254255

256+
Process.sleep(1000)
257+
255258
{:ok, comment} = CMS.create_article_comment(thread, repo.id, "pinned comment 2", user)
256259
{:ok, pinned_comment2} = CMS.pin_article_comment(comment.id)
257260

258261
variables = %{id: repo.id, thread: "REPO", filter: %{page: 1, size: 10}}
259262
results = guest_conn |> query_result(@query, variables, "pagedArticleComments")
260263

261-
assert results["entries"] |> List.first() |> Map.get("id") == to_string(pinned_comment.id)
262-
assert results["entries"] |> Enum.at(1) |> Map.get("id") == to_string(pinned_comment2.id)
264+
assert results["entries"] |> List.first() |> Map.get("id") == to_string(pinned_comment2.id)
265+
assert results["entries"] |> Enum.at(1) |> Map.get("id") == to_string(pinned_comment.id)
263266

264267
assert results["totalCount"] == total_count + 2
265268
end

0 commit comments

Comments
 (0)