Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/groupher_server/cms/article_comment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ defmodule GroupherServer.CMS.ArticleComment do
@max_participator_count 5
@max_parent_replies_count 3

@supported_emotions [:downvote, :beer, :heart, :biceps, :orz, :confused, :pill]
# 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

@delete_hint "this comment is deleted"
Expand Down
31 changes: 22 additions & 9 deletions lib/groupher_server/cms/article_comment_user_emotion.ex
Original file line number Diff line number Diff line change
@@ -1,30 +1,43 @@
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
belongs_to(:article_comment, ArticleComment, foreign_key: :article_comment_id)
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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
7 changes: 6 additions & 1 deletion test/groupher_server/cms/article_comment_emotions_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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

Expand Down
7 changes: 6 additions & 1 deletion test/groupher_server_web/query/cms/article_comment_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ defmodule GroupherServer.Test.Query.ArticleComment do
nickname
}
viewerHasBeered

popcornCount
viewerHasPopcorned
}
isArticleAuthor
meta {
Expand Down Expand Up @@ -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
Expand All @@ -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"]
Expand Down