From 64d1ad3db6ec8b3c208ad6b664acaef4eb8f0906 Mon Sep 17 00:00:00 2001 From: mydearxym Date: Fri, 7 May 2021 14:33:55 +0800 Subject: [PATCH 1/2] refactor(comment): add popcorn emotion --- lib/groupher_server/cms/article_comment.ex | 2 +- .../cms/article_comment_user_emotion.ex | 31 +++++++++++++------ ...7063145_add_popcorn_emotion_to_comment.exs | 9 ++++++ .../cms/article_comment_emotions_test.exs | 7 ++++- .../query/cms/article_comment_test.exs | 7 ++++- 5 files changed, 44 insertions(+), 12 deletions(-) create mode 100644 priv/repo/migrations/20210507063145_add_popcorn_emotion_to_comment.exs diff --git a/lib/groupher_server/cms/article_comment.ex b/lib/groupher_server/cms/article_comment.ex index b846e66d4..501f52ef3 100644 --- a/lib/groupher_server/cms/article_comment.ex +++ b/lib/groupher_server/cms/article_comment.ex @@ -25,7 +25,7 @@ defmodule GroupherServer.CMS.ArticleComment do @max_participator_count 5 @max_parent_replies_count 3 - @supported_emotions [:downvote, :beer, :heart, :biceps, :orz, :confused, :pill] + @supported_emotions [:downvote, :beer, :heart, :biceps, :orz, :confused, :pill, :popcorn] @max_latest_emotion_users_count 5 @delete_hint "this comment is deleted" diff --git a/lib/groupher_server/cms/article_comment_user_emotion.ex b/lib/groupher_server/cms/article_comment_user_emotion.ex index 73bf62bfe..fb41bf993 100644 --- a/lib/groupher_server/cms/article_comment_user_emotion.ex +++ b/lib/groupher_server/cms/article_comment_user_emotion.ex @@ -1,15 +1,35 @@ +defmodule GroupherServer.CMS.ArticleCommentUserEmotion.Macros do + alias GroupherServer.CMS + alias CMS.ArticleComment + + @supported_emotions ArticleComment.supported_emotions() + + defmacro emotion_fields() do + @supported_emotions + |> Enum.map(fn emotion -> + quote do + field(unquote(:"#{emotion}"), :boolean, default: false) + end + end) + end +end + defmodule GroupherServer.CMS.ArticleCommentUserEmotion do @moduledoc false alias __MODULE__ use Ecto.Schema import Ecto.Changeset + import GroupherServer.CMS.ArticleCommentUserEmotion.Macros alias GroupherServer.{Accounts, CMS} alias CMS.ArticleComment + @supported_emotions ArticleComment.supported_emotions() + @required_fields ~w(article_comment_id user_id recived_user_id)a - @optional_fields ~w(downvote beer heart biceps orz confused pill)a + # @optional_fields ~w(downvote beer heart biceps orz confused pill)a + @optional_fields Enum.map(@supported_emotions, &:"#{&1}") @type t :: %ArticleCommentUserEmotion{} schema "articles_comments_users_emotions" do @@ -17,14 +37,7 @@ defmodule GroupherServer.CMS.ArticleCommentUserEmotion do belongs_to(:recived_user, Accounts.User, foreign_key: :recived_user_id) belongs_to(:user, Accounts.User, foreign_key: :user_id) - field(:downvote, :boolean, default: false) - field(:beer, :boolean, default: false) - field(:heart, :boolean, default: false) - field(:biceps, :boolean, default: false) - field(:orz, :boolean, default: false) - field(:confused, :boolean, default: false) - field(:pill, :boolean, default: false) - + emotion_fields() timestamps(type: :utc_datetime) end diff --git a/priv/repo/migrations/20210507063145_add_popcorn_emotion_to_comment.exs b/priv/repo/migrations/20210507063145_add_popcorn_emotion_to_comment.exs new file mode 100644 index 000000000..01c37902b --- /dev/null +++ b/priv/repo/migrations/20210507063145_add_popcorn_emotion_to_comment.exs @@ -0,0 +1,9 @@ +defmodule GroupherServer.Repo.Migrations.AddPopcornEmotionToComment do + use Ecto.Migration + + def change do + alter table(:articles_comments_users_emotions) do + add(:popcorn, :boolean, default: false) + end + end +end diff --git a/test/groupher_server/cms/article_comment_emotions_test.exs b/test/groupher_server/cms/article_comment_emotions_test.exs index e0becac5c..d27508dd7 100644 --- a/test/groupher_server/cms/article_comment_emotions_test.exs +++ b/test/groupher_server/cms/article_comment_emotions_test.exs @@ -22,7 +22,7 @@ defmodule GroupherServer.Test.CMS.ArticleCommentEmotions do end describe "[emotion in paged article comment]" do - @tag :wip + @tag :wip2 test "login user should got viewer has emotioned status", ~m(post user)a do total_count = 0 page_number = 10 @@ -38,6 +38,7 @@ defmodule GroupherServer.Test.CMS.ArticleCommentEmotions do {:ok, _} = CMS.make_emotion(first_comment.id, :downvote, user) {:ok, _} = CMS.make_emotion(first_comment.id, :beer, user) + {:ok, _} = CMS.make_emotion(first_comment.id, :popcorn, user) {:ok, paged_comments} = CMS.list_article_comments( @@ -57,6 +58,10 @@ defmodule GroupherServer.Test.CMS.ArticleCommentEmotions do assert target.emotions.beer_count == 1 assert user_exist_in?(user, target.emotions.latest_beer_users) assert target.emotions.viewer_has_beered + + assert target.emotions.popcorn_count == 1 + assert user_exist_in?(user, target.emotions.latest_popcorn_users) + assert target.emotions.viewer_has_popcorned end end diff --git a/test/groupher_server_web/query/cms/article_comment_test.exs b/test/groupher_server_web/query/cms/article_comment_test.exs index 392aef6d7..81311a64b 100644 --- a/test/groupher_server_web/query/cms/article_comment_test.exs +++ b/test/groupher_server_web/query/cms/article_comment_test.exs @@ -85,6 +85,9 @@ defmodule GroupherServer.Test.Query.ArticleComment do nickname } viewerHasBeered + + popcornCount + viewerHasPopcorned } isArticleAuthor meta { @@ -392,7 +395,7 @@ defmodule GroupherServer.Test.Query.ArticleComment do assert the_random_comment |> get_in(["meta", "isArticleAuthorUpvoted"]) end - @tag :wip + @tag :wip2 test "guest user can get paged comment with emotions info", ~m(guest_conn post user user2)a do total_count = 2 @@ -419,6 +422,8 @@ defmodule GroupherServer.Test.Query.ArticleComment do comment_emotion = Enum.find(results["entries"], &(&1["id"] == to_string(comment.id))) |> Map.get("emotions") + assert comment_emotion["popcornCount"] == 0 + assert comment_emotion["downvoteCount"] == 2 assert comment_emotion["latestDownvoteUsers"] |> length == 2 assert not comment_emotion["viewerHasDownvoteed"] From 27bcd1f9b16edc6ca3de909a81ffbde669b8630e Mon Sep 17 00:00:00 2001 From: mydearxym Date: Fri, 7 May 2021 14:38:48 +0800 Subject: [PATCH 2/2] refactor(comment): add doc --- lib/groupher_server/cms/article_comment.ex | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/groupher_server/cms/article_comment.ex b/lib/groupher_server/cms/article_comment.ex index 501f52ef3..61bead05c 100644 --- a/lib/groupher_server/cms/article_comment.ex +++ b/lib/groupher_server/cms/article_comment.ex @@ -25,6 +25,8 @@ defmodule GroupherServer.CMS.ArticleComment do @max_participator_count 5 @max_parent_replies_count 3 + # NOTE: if you want to add/remove emotion, just edit the list below + # and migrate the field to table "articles_comments_users_emotions" @supported_emotions [:downvote, :beer, :heart, :biceps, :orz, :confused, :pill, :popcorn] @max_latest_emotion_users_count 5