@@ -4,6 +4,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
44 """
55 import Ecto.Query , warn: false
66
7+ import GroupherServer.CMS.Delegate.Helper , only: [ update_emotions_field: 4 ]
8+
79 alias Helper.ORM
810 alias GroupherServer . { Accounts , CMS , Repo }
911
@@ -15,8 +17,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
1517 @ type t_user_list :: [ % { login: String . t ( ) } ]
1618 @ type t_mention_status :: % { user_list: t_user_list , user_count: Integer . t ( ) }
1719
18- @ max_latest_emotion_users_count ArticleComment . max_latest_emotion_users_count ( )
19-
2020 @ doc "make emotion to a comment"
2121 def emotion_to_comment ( comment_id , emotion , % User { } = user ) do
2222 with { :ok , comment } <- ORM . find ( ArticleComment , comment_id , preload: :author ) do
@@ -31,18 +31,18 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
3131 args = Map . put ( target , :"#{ emotion } " , true )
3232
3333 case ORM . find_by ( ArticleCommentUserEmotion , target ) do
34- { :ok , article_comment_user_emotion } -> article_comment_user_emotion |> ORM . update ( args )
35- { :error , _ } -> ArticleCommentUserEmotion |> ORM . create ( args )
34+ { :ok , article_comment_user_emotion } -> ORM . update ( article_comment_user_emotion , args )
35+ { :error , _ } -> ORM . create ( ArticleCommentUserEmotion , args )
3636 end
3737 end )
38- |> Multi . run ( :query_emotion_status , fn _ , _ ->
39- query_emotion_status ( comment , emotion )
38+ |> Multi . run ( :query_emotion_states , fn _ , _ ->
39+ query_emotion_states ( comment , emotion )
4040 end )
41- |> Multi . run ( :update_comment_emotion , fn _ , % { query_emotion_status : status } ->
42- update_comment_emotion ( comment , emotion , status , user )
41+ |> Multi . run ( :update_emotions_field , fn _ , % { query_emotion_states : status } ->
42+ update_emotions_field ( comment , emotion , status , user )
4343 end )
4444 |> Repo . transaction ( )
45- |> update_emotion_result
45+ |> update_emotions_field_result
4646 end
4747 end
4848
@@ -56,23 +56,28 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
5656 user_id: user . id
5757 }
5858
59- { :ok , article_comment_user_emotion } = ORM . find_by ( ArticleCommentUserEmotion , target )
60- args = Map . put ( target , :"#{ emotion } " , false )
61- article_comment_user_emotion |> ORM . update ( args )
59+ case ORM . find_by ( ArticleCommentUserEmotion , target ) do
60+ { :ok , article_comment_user_emotion } ->
61+ args = Map . put ( target , :"#{ emotion } " , false )
62+ article_comment_user_emotion |> ORM . update ( args )
63+
64+ { :error , _ } ->
65+ ORM . create ( ArticleCommentUserEmotion , target )
66+ end
6267 end )
63- |> Multi . run ( :query_emotion_status , fn _ , _ ->
64- query_emotion_status ( comment , emotion )
68+ |> Multi . run ( :query_emotion_states , fn _ , _ ->
69+ query_emotion_states ( comment , emotion )
6570 end )
66- |> Multi . run ( :update_comment_emotion , fn _ , % { query_emotion_status : status } ->
67- update_comment_emotion ( comment , emotion , status , user )
71+ |> Multi . run ( :update_emotions_field , fn _ , % { query_emotion_states : status } ->
72+ update_emotions_field ( comment , emotion , status , user )
6873 end )
6974 |> Repo . transaction ( )
70- |> update_emotion_result
75+ |> update_emotions_field_result
7176 end
7277 end
7378
74- @ spec query_emotion_status ( ArticleComment . t ( ) , Atom . t ( ) ) :: { :ok , t_mention_status }
75- defp query_emotion_status ( comment , emotion ) do
79+ @ spec query_emotion_states ( ArticleComment . t ( ) , Atom . t ( ) ) :: { :ok , t_mention_status }
80+ defp query_emotion_states ( comment , emotion ) do
7681 # 每次被 emotion 动作触发后重新查询,主要原因
7782 # 1.并发下保证数据准确,类似 views 阅读数的统计
7883 # 2. 前端使用 nickname 而非 login 展示,如果用户改了 nickname, 可以"自动纠正"
@@ -91,41 +96,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentEmotion do
9196 { :ok , % { user_list: emotioned_user_info_list , user_count: emotioned_user_count } }
9297 end
9398
94- @ spec update_comment_emotion ( ArticleComment . t ( ) , Atom . t ( ) , t_mention_status , User . t ( ) ) ::
95- { :ok , ArticleComment . t ( ) } | { :error , any }
96- defp update_comment_emotion ( comment , emotion , status , user ) do
97- % { user_count: user_count , user_list: user_list } = status
98-
99- emotions =
100- % { }
101- |> Map . put ( :"#{ emotion } _count" , user_count )
102- |> Map . put ( :"#{ emotion } _user_logins" , user_list |> Enum . map ( & & 1 . login ) )
103- |> Map . put (
104- :"latest_#{ emotion } _users" ,
105- Enum . slice ( user_list , 0 , @ max_latest_emotion_users_count )
106- )
107-
108- viewer_has_emotioned = user . login in Map . get ( emotions , :"#{ emotion } _user_logins" )
109- emotions = emotions |> Map . put ( :"viewer_has_#{ emotion } ed" , viewer_has_emotioned )
110-
111- comment
112- |> Ecto.Changeset . change ( )
113- |> Ecto.Changeset . put_embed ( :emotions , emotions )
114- |> Repo . update ( )
115- # virtual field can not be updated
116- |> add_viewer_emotioned_ifneed ( emotions )
117- end
118-
119- defp add_viewer_emotioned_ifneed ( { :error , error } , _ ) , do: { :error , error }
120-
121- defp add_viewer_emotioned_ifneed ( { :ok , comment } , emotions ) do
122- # Map.merge(comment, %{emotion: emotions})
123- { :ok , Map . merge ( comment , % { emotion: emotions } ) }
124- end
125-
126- defp update_emotion_result ( { :ok , % { update_comment_emotion: result } } ) , do: { :ok , result }
99+ defp update_emotions_field_result ( { :ok , % { update_emotions_field: result } } ) , do: { :ok , result }
127100
128- defp update_emotion_result ( { :error , _ , result , _steps } ) do
101+ defp update_emotions_field_result ( { :error , _ , result , _steps } ) do
129102 { :error , result }
130103 end
131104end
0 commit comments