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

Commit 76bad31

Browse files
committed
refactor(article-comment): wip
1 parent 0a13869 commit 76bad31

File tree

6 files changed

+85
-7
lines changed

6 files changed

+85
-7
lines changed

lib/groupher_server/cms/delegates/article_comment.ex

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,28 +244,41 @@ defmodule GroupherServer.CMS.Delegate.ArticleComment do
244244
{:ok, %{user_list: emotioned_user_info_list, user_count: emotioned_user_count}}
245245
end)
246246
|> Multi.run(:update_comment_emotion, fn _, %{query_emotion_status: status} ->
247+
%{user_count: user_count, user_list: user_list} = status
248+
247249
updated_emotions =
248250
%{}
249-
|> Map.put(:"#{emotion}_count", status.user_count)
250-
|> Map.put(
251-
:"#{emotion}_user_logins",
252-
status.user_list |> Enum.map(& &1.login)
253-
)
251+
|> Map.put(:"#{emotion}_count", user_count)
252+
|> Map.put(:"#{emotion}_user_logins", user_list |> Enum.map(& &1.login))
254253
|> Map.put(
255254
:"latest_#{emotion}_users",
256-
Enum.slice(status.user_list, 0, @max_latest_emotion_users_count)
255+
Enum.slice(user_list, 0, @max_latest_emotion_users_count)
257256
)
258257

258+
viewer_has_emotioned = user.login in Map.get(updated_emotions, :"#{emotion}_user_logins")
259+
260+
updated_emotions =
261+
updated_emotions |> Map.put(:"viewer_has_#{emotion}ed", viewer_has_emotioned)
262+
259263
comment
260264
|> Ecto.Changeset.change()
261265
|> Ecto.Changeset.put_embed(:emotions, updated_emotions)
262266
|> Repo.update()
267+
# virtual field can not be updated
268+
|> add_viewer_emotioned_ifneed(updated_emotions)
263269
end)
264270
|> Repo.transaction()
265271
|> upsert_comment_result
266272
end
267273
end
268274

275+
defp add_viewer_emotioned_ifneed({:error, error}, _), do: {:error, error}
276+
277+
defp add_viewer_emotioned_ifneed({:ok, comment}, emotions) do
278+
# Map.merge(comment, %{emotion: emotions})
279+
{:ok, Map.merge(comment, %{emotion: emotions})}
280+
end
281+
269282
@doc """
270283
creates a comment for article like psot, job ...
271284
"""

lib/groupher_server_web/resolvers/cms_resolver.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ defmodule GroupherServerWeb.Resolvers.CMS do
322322
CMS.delete_article_comment(comment)
323323
end
324324

325+
def emotion_to_comment(_root, ~m(id emotion)a, %{context: %{cur_user: user}}) do
326+
CMS.emotion_to_comment(id, emotion, user)
327+
end
328+
325329
############
326330
############
327331
############

lib/groupher_server_web/schema/cms/cms_misc.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,11 @@ defmodule GroupherServerWeb.Schema.CMS.Misc do
152152
value(:grey)
153153
end
154154

155+
@desc "emotion options of comment"
156+
enum :article_comment_emotion do
157+
emotion_enum()
158+
end
159+
155160
@desc "the filter mode for list comments"
156161
enum :article_comments_mode do
157162
value(:replies)

lib/groupher_server_web/schema/cms/mutations/comment.ex

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,20 @@ defmodule GroupherServerWeb.Schema.CMS.Mutations.Comment do
4444
resolve(&R.CMS.delete_article_comment/3)
4545
end
4646

47+
@desc "emotion to a comment"
48+
field :emotion_to_comment, :article_comment do
49+
arg(:id, non_null(:id))
50+
arg(:emotion, non_null(:article_comment_emotion))
51+
52+
middleware(M.Authorize, :login)
53+
resolve(&R.CMS.emotion_to_comment/3)
54+
end
55+
56+
############################
57+
############################
58+
############################
59+
############################
60+
4761
@desc "create a comment"
4862
field :create_comment, :comment do
4963
# TODO use thread and force community pass-in

lib/groupher_server_web/schema/utils/helper.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,19 @@ defmodule GroupherServerWeb.Schema.Utils.Helper do
224224
end
225225
end
226226

227+
@doc """
228+
general emotion enum for comments
229+
#NOTE: xxx_user_logins field is not support for gq-endpoint
230+
"""
231+
defmacro emotion_enum() do
232+
@supported_emotions
233+
|> Enum.map(fn emotion ->
234+
quote do
235+
value(unquote(:"#{emotion}"))
236+
end
237+
end)
238+
end
239+
227240
@doc """
228241
general emotions for comments
229242
#NOTE: xxx_user_logins field is not support for gq-endpoint

test/groupher_server_web/mutation/cms/article_comment_test.exs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ defmodule GroupherServer.Test.Mutation.ArticleComment do
1515
{:ok, ~m(user_conn user guest_conn owner_conn community post)a}
1616
end
1717

18-
describe "[create article comment]" do
18+
describe "[article comment CURD]" do
1919
@write_comment_query """
2020
mutation($thread: CmsThread!, $id: ID!, $content: String!) {
2121
createArticleComment(thread: $thread,id: $id, content: $content) {
@@ -86,4 +86,33 @@ defmodule GroupherServer.Test.Mutation.ArticleComment do
8686
assert deleted["isDeleted"]
8787
end
8888
end
89+
90+
describe "[article comment emotion]" do
91+
@emotion_comment_query """
92+
mutation($id: ID!, $emotion: ArticleCommentEmotion!) {
93+
emotionToComment(id: $id, emotion: $emotion) {
94+
id
95+
emotions {
96+
beerCount
97+
viewerHasBeered
98+
latestBeerUsers {
99+
login
100+
nickname
101+
}
102+
}
103+
}
104+
}
105+
"""
106+
@tag :wip2
107+
test "login user can emotion to a comment", ~m(post user guest_conn user_conn owner_conn)a do
108+
{:ok, comment} = CMS.create_article_comment(:post, post.id, "post comment", user)
109+
variables = %{id: comment.id, emotion: "BEER"}
110+
111+
comment =
112+
user_conn |> mutation_result(@emotion_comment_query, variables, "emotionToComment")
113+
114+
assert comment |> get_in(["emotions", "beerCount"]) == 1
115+
assert get_in(comment, ["emotions", "viewerHasBeered"])
116+
end
117+
end
89118
end

0 commit comments

Comments
 (0)