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

Commit 73cf641

Browse files
authored
refactor(comment): add popcorn emotion (#336)
* refactor(comment): add popcorn emotion * refactor(comment): add doc
1 parent dcd2cca commit 73cf641

File tree

5 files changed

+46
-12
lines changed

5 files changed

+46
-12
lines changed

lib/groupher_server/cms/article_comment.ex

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ defmodule GroupherServer.CMS.ArticleComment do
2525
@max_participator_count 5
2626
@max_parent_replies_count 3
2727

28-
@supported_emotions [:downvote, :beer, :heart, :biceps, :orz, :confused, :pill]
28+
# NOTE: if you want to add/remove emotion, just edit the list below
29+
# and migrate the field to table "articles_comments_users_emotions"
30+
@supported_emotions [:downvote, :beer, :heart, :biceps, :orz, :confused, :pill, :popcorn]
2931
@max_latest_emotion_users_count 5
3032

3133
@delete_hint "this comment is deleted"

lib/groupher_server/cms/article_comment_user_emotion.ex

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
1+
defmodule GroupherServer.CMS.ArticleCommentUserEmotion.Macros do
2+
alias GroupherServer.CMS
3+
alias CMS.ArticleComment
4+
5+
@supported_emotions ArticleComment.supported_emotions()
6+
7+
defmacro emotion_fields() do
8+
@supported_emotions
9+
|> Enum.map(fn emotion ->
10+
quote do
11+
field(unquote(:"#{emotion}"), :boolean, default: false)
12+
end
13+
end)
14+
end
15+
end
16+
117
defmodule GroupherServer.CMS.ArticleCommentUserEmotion do
218
@moduledoc false
319
alias __MODULE__
420

521
use Ecto.Schema
622
import Ecto.Changeset
23+
import GroupherServer.CMS.ArticleCommentUserEmotion.Macros
724

825
alias GroupherServer.{Accounts, CMS}
926
alias CMS.ArticleComment
1027

28+
@supported_emotions ArticleComment.supported_emotions()
29+
1130
@required_fields ~w(article_comment_id user_id recived_user_id)a
12-
@optional_fields ~w(downvote beer heart biceps orz confused pill)a
31+
# @optional_fields ~w(downvote beer heart biceps orz confused pill)a
32+
@optional_fields Enum.map(@supported_emotions, &:"#{&1}")
1333

1434
@type t :: %ArticleCommentUserEmotion{}
1535
schema "articles_comments_users_emotions" do
1636
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
1737
belongs_to(:recived_user, Accounts.User, foreign_key: :recived_user_id)
1838
belongs_to(:user, Accounts.User, foreign_key: :user_id)
1939

20-
field(:downvote, :boolean, default: false)
21-
field(:beer, :boolean, default: false)
22-
field(:heart, :boolean, default: false)
23-
field(:biceps, :boolean, default: false)
24-
field(:orz, :boolean, default: false)
25-
field(:confused, :boolean, default: false)
26-
field(:pill, :boolean, default: false)
27-
40+
emotion_fields()
2841
timestamps(type: :utc_datetime)
2942
end
3043

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
defmodule GroupherServer.Repo.Migrations.AddPopcornEmotionToComment do
2+
use Ecto.Migration
3+
4+
def change do
5+
alter table(:articles_comments_users_emotions) do
6+
add(:popcorn, :boolean, default: false)
7+
end
8+
end
9+
end

test/groupher_server/cms/article_comment_emotions_test.exs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ defmodule GroupherServer.Test.CMS.ArticleCommentEmotions do
2222
end
2323

2424
describe "[emotion in paged article comment]" do
25-
@tag :wip
25+
@tag :wip2
2626
test "login user should got viewer has emotioned status", ~m(post user)a do
2727
total_count = 0
2828
page_number = 10
@@ -38,6 +38,7 @@ defmodule GroupherServer.Test.CMS.ArticleCommentEmotions do
3838

3939
{:ok, _} = CMS.make_emotion(first_comment.id, :downvote, user)
4040
{:ok, _} = CMS.make_emotion(first_comment.id, :beer, user)
41+
{:ok, _} = CMS.make_emotion(first_comment.id, :popcorn, user)
4142

4243
{:ok, paged_comments} =
4344
CMS.list_article_comments(
@@ -57,6 +58,10 @@ defmodule GroupherServer.Test.CMS.ArticleCommentEmotions do
5758
assert target.emotions.beer_count == 1
5859
assert user_exist_in?(user, target.emotions.latest_beer_users)
5960
assert target.emotions.viewer_has_beered
61+
62+
assert target.emotions.popcorn_count == 1
63+
assert user_exist_in?(user, target.emotions.latest_popcorn_users)
64+
assert target.emotions.viewer_has_popcorned
6065
end
6166
end
6267

test/groupher_server_web/query/cms/article_comment_test.exs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ defmodule GroupherServer.Test.Query.ArticleComment do
8585
nickname
8686
}
8787
viewerHasBeered
88+
89+
popcornCount
90+
viewerHasPopcorned
8891
}
8992
isArticleAuthor
9093
meta {
@@ -392,7 +395,7 @@ defmodule GroupherServer.Test.Query.ArticleComment do
392395
assert the_random_comment |> get_in(["meta", "isArticleAuthorUpvoted"])
393396
end
394397

395-
@tag :wip
398+
@tag :wip2
396399
test "guest user can get paged comment with emotions info",
397400
~m(guest_conn post user user2)a do
398401
total_count = 2
@@ -419,6 +422,8 @@ defmodule GroupherServer.Test.Query.ArticleComment do
419422
comment_emotion =
420423
Enum.find(results["entries"], &(&1["id"] == to_string(comment.id))) |> Map.get("emotions")
421424

425+
assert comment_emotion["popcornCount"] == 0
426+
422427
assert comment_emotion["downvoteCount"] == 2
423428
assert comment_emotion["latestDownvoteUsers"] |> length == 2
424429
assert not comment_emotion["viewerHasDownvoteed"]

0 commit comments

Comments
 (0)