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

Commit 7a4f65e

Browse files
committed
refactor(viewer-upvote): gq-workflow done
1 parent 32316c4 commit 7a4f65e

File tree

7 files changed

+65
-45
lines changed

7 files changed

+65
-45
lines changed

lib/groupher_server/cms/article_comment.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ defmodule GroupherServer.CMS.ArticleComment do
6969

7070
# 是否置顶
7171
field(:is_pinned, :boolean, default: false)
72+
field(:viewer_has_upvoted, :boolean, default: false, virtual: true)
7273

7374
belongs_to(:author, Accounts.User, foreign_key: :author_id)
7475
belongs_to(:post, Post, foreign_key: :post_id)

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,14 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
364364
with {:ok, comment} <- ORM.find(ArticleComment, comment_id),
365365
false <- comment.is_deleted do
366366
# TODO: is user upvoted before?
367-
# IO.inspect(comment, label: "the comment")
368367
Multi.new()
369368
|> Multi.run(:create_comment_upvote, fn _, _ ->
370369
ORM.create(ArticleCommentUpvote, %{article_comment_id: comment.id, user_id: user_id})
371370
end)
372371
|> Multi.run(:add_upvoted_user, fn _, _ ->
373372
update_upvoted_user_list(comment, user_id, :add)
374373
end)
375-
|> Multi.run(:inc_upvotes_count, fn _, _ ->
374+
|> Multi.run(:inc_upvotes_count, fn _, %{add_upvoted_user: comment} ->
376375
count_query = from(c in ArticleCommentUpvote, where: c.article_comment_id == ^comment.id)
377376
upvotes_count = Repo.aggregate(count_query, :count)
378377
ORM.update(comment, %{upvotes_count: upvotes_count})
@@ -385,20 +384,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
385384
end
386385
end
387386

388-
defp update_upvoted_user_list(comment, user_id, opt) do
389-
IO.inspect(comment.meta, label: "update_upvoted_user_list meta")
390-
cur_user_ids = get_in(comment, [:meta, :upvoted_user_ids])
391-
392-
user_ids =
393-
case opt do
394-
:add -> [user_id] ++ cur_user_ids
395-
:remove -> cur_user_ids -- [user_id]
396-
end
397-
398-
meta = comment.meta |> Map.merge(%{upvoted_user_ids: user_ids}) |> strip_struct
399-
ORM.update_meta(comment, meta)
400-
end
401-
402387
@doc "upvote a comment"
403388
def undo_upvote_article_comment(comment_id, %User{id: user_id}) do
404389
with {:ok, comment} <- ORM.find(ArticleComment, comment_id),
@@ -410,7 +395,10 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
410395
user_id: user_id
411396
})
412397
end)
413-
|> Multi.run(:desc_upvotes_count, fn _, _ ->
398+
|> Multi.run(:remove_upvoted_user, fn _, _ ->
399+
update_upvoted_user_list(comment, user_id, :remove)
400+
end)
401+
|> Multi.run(:desc_upvotes_count, fn _, %{remove_upvoted_user: comment} ->
414402
count_query = from(c in ArticleCommentUpvote, where: c.article_comment_id == ^comment_id)
415403
upvotes_count = Repo.aggregate(count_query, :count)
416404

@@ -437,6 +425,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
437425
|> ORM.paginater(~m(page size)a)
438426
|> set_viewer_emotion_ifneed(user)
439427
|> add_pined_comments_ifneed(thread, article_id, filters)
428+
|> mark_viewer_has_upvoted(user)
440429
|> done()
441430
end
442431
end
@@ -453,6 +442,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
453442
|> QueryBuilder.filter_pack(filters)
454443
|> ORM.paginater(~m(page size)a)
455444
|> set_viewer_emotion_ifneed(user)
445+
|> mark_viewer_has_upvoted(user)
456446
|> done()
457447
end
458448

@@ -625,6 +615,18 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
625615
%{paged_comments | entries: new_entries}
626616
end
627617

618+
defp mark_viewer_has_upvoted(paged_comments, nil), do: paged_comments
619+
620+
defp mark_viewer_has_upvoted(%{entries: entries} = paged_comments, %User{} = user) do
621+
entries =
622+
Enum.map(
623+
entries,
624+
&Map.merge(&1, %{viewer_has_upvoted: Enum.member?(&1.meta.upvoted_user_ids, user.id)})
625+
)
626+
627+
Map.merge(paged_comments, %{entries: entries})
628+
end
629+
628630
defp get_article(%ArticleComment{post_id: post_id} = comment) when not is_nil(post_id) do
629631
with {:ok, article} <- ORM.find(Post, comment.post_id, preload: [author: :user]) do
630632
{:post, article}
@@ -692,6 +694,19 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
692694
end
693695
end
694696

697+
defp update_upvoted_user_list(comment, user_id, opt) do
698+
cur_user_ids = get_in(comment, [:meta, :upvoted_user_ids])
699+
700+
user_ids =
701+
case opt do
702+
:add -> [user_id] ++ cur_user_ids
703+
:remove -> cur_user_ids -- [user_id]
704+
end
705+
706+
meta = comment.meta |> Map.merge(%{upvoted_user_ids: user_ids}) |> strip_struct
707+
ORM.update_meta(comment, meta)
708+
end
709+
695710
defp upsert_comment_result({:ok, %{create_article_comment: result}}), do: {:ok, result}
696711
defp upsert_comment_result({:ok, %{add_reply_to: result}}), do: {:ok, result}
697712
defp upsert_comment_result({:ok, %{check_article_author_upvoted: result}}), do: {:ok, result}

lib/groupher_server/cms/embeds/article_comment_meta.ex

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ defmodule GroupherServer.CMS.Embeds.ArticleCommentMeta do
33
general article comment meta info
44
"""
55
use Ecto.Schema
6+
use Accessible
7+
68
import Ecto.Changeset
79

810
@optional_fields ~w(is_article_author_upvoted is_solution report_count is_reply_to_others)a

lib/groupher_server/cms/utils/loader.ex

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ defmodule GroupherServer.CMS.Utils.Loader do
99

1010
alias CMS.{
1111
Author,
12-
ArticleCommentUpvote,
1312
CommunityEditor,
1413
CommunitySubscriber,
1514
CommunityThread,
@@ -165,12 +164,12 @@ defmodule GroupherServer.CMS.Utils.Loader do
165164
|> QueryBuilder.members_pack(args)
166165
end
167166

168-
def query({"articles_comments_upvotes", ArticleCommentUpvote}, %{
169-
viewer_did: _,
170-
cur_user: cur_user
171-
}) do
172-
ArticleCommentUpvote |> where([f], f.user_id == ^cur_user.id)
173-
end
167+
# def query({"articles_comments_upvotes", ArticleCommentUpvote}, %{
168+
# viewer_did: _,
169+
# cur_user: cur_user
170+
# }) do
171+
# ArticleCommentUpvote |> where([f], f.user_id == ^cur_user.id)
172+
# end
174173

175174
# default loader
176175
def query(queryable, _args) do

lib/groupher_server_web/schema/cms/cms_types.ex

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -339,15 +339,7 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
339339
field(:meta, :article_comment_meta)
340340
field(:replies_count, :integer)
341341
field(:reply_to, :article_comment_reply)
342-
343-
field :viewer_has_upvoted, :boolean do
344-
arg(:viewer_did, :viewer_did_type, default_value: :viewer_did)
345-
346-
middleware(M.Authorize, :login)
347-
middleware(M.PutCurrentUser)
348-
resolve(dataloader(CMS, :upvotes))
349-
middleware(M.ViewerDidConvert)
350-
end
342+
field(:viewer_has_upvoted, :boolean)
351343

352344
timestamp_fields()
353345
end
@@ -365,15 +357,8 @@ defmodule GroupherServerWeb.Schema.CMS.Types do
365357
field(:reply_to, :article_comment_reply)
366358
field(:replies, list_of(:article_comment_reply))
367359
field(:replies_count, :integer)
368-
# field(:viewer_has_upvoted, :boolean, resolve: dataloader(CMS, :))
369-
field :viewer_has_upvoted, :boolean do
370-
arg(:viewer_did, :viewer_did_type, default_value: :viewer_did)
371360

372-
middleware(M.Authorize, :login)
373-
middleware(M.PutCurrentUser)
374-
resolve(dataloader(CMS, :upvotes))
375-
middleware(M.ViewerDidConvert)
376-
end
361+
field(:viewer_has_upvoted, :boolean)
377362

378363
timestamp_fields()
379364
end

test/groupher_server/cms/article_comment_test.exs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,29 @@ defmodule GroupherServer.Test.CMS.ArticleComment do
131131
end
132132

133133
@tag :wip2
134-
test "article author upvote post comment will update upvoted_user_ids", ~m(post user)a do
134+
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)
137-
CMS.upvote_article_comment(comment.id, user)
138-
# IO.inspect(hello, label: "the hello")
137+
{:ok, comment} = CMS.upvote_article_comment(comment.id, user)
138+
139+
assert user.id in comment.meta.upvoted_user_ids
140+
end
141+
142+
@tag :wip2
143+
test "user undo upvote post comment will remove id from upvoted_user_ids",
144+
~m(post user user2)a do
145+
comment = "post_comment"
146+
{:ok, comment} = CMS.create_article_comment(:post, post.id, comment, user)
147+
{:ok, _comment} = CMS.upvote_article_comment(comment.id, user)
148+
{:ok, comment} = CMS.upvote_article_comment(comment.id, user2)
149+
150+
assert user2.id in comment.meta.upvoted_user_ids
151+
assert user.id in comment.meta.upvoted_user_ids
152+
153+
{:ok, comment} = CMS.undo_upvote_article_comment(comment.id, user2)
154+
155+
assert user.id in comment.meta.upvoted_user_ids
156+
assert user2.id not in comment.meta.upvoted_user_ids
139157
end
140158

141159
@tag :wip

test/groupher_server_web/query/cms/article_comment_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ defmodule GroupherServer.Test.Query.ArticleComment do
473473
|> get_in(["emotions", "viewerHasDownvoteed"])
474474
end
475475

476-
@tag :wip
476+
@tag :wip2
477477
test "comment should have viewer has upvoted flag", ~m(user_conn post user)a do
478478
total_count = 10
479479
page_size = 12

0 commit comments

Comments
 (0)