@@ -63,66 +63,97 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
6363 {:error, %Ecto.Changeset{}}
6464
6565 """
66- def create_content (
67- % Community { id: community_id } ,
68- thread ,
69- attrs ,
70- % User { id: user_id }
71- ) do
72- with { :ok , author } <- ensure_author_exists ( % User { id: user_id } ) ,
66+ def create_content ( % Community { id: cid } , thread , attrs , % User { id: uid } ) do
67+ with { :ok , author } <- ensure_author_exists ( % User { id: uid } ) ,
7368 { :ok , action } <- match_action ( thread , :community ) ,
74- { :ok , community } <- ORM . find ( Community , community_id ) do
69+ { :ok , community } <- ORM . find ( Community , cid ) do
7570 Multi . new ( )
7671 |> Multi . run ( :create_content , fn _ , _ ->
77- action . target
78- |> struct ( )
79- |> action . target . changeset ( attrs )
80- |> Ecto.Changeset . put_change ( :author_id , author . id )
81- |> Ecto.Changeset . put_change ( :origial_community_id , integerfy ( community_id ) )
82- |> Repo . insert ( )
72+ exec_create_content ( action . target , attrs , author , community )
8373 end )
8474 |> Multi . run ( :set_community , fn _ , % { create_content: content } ->
8575 ArticleOperation . set_community ( community , thread , content . id )
8676 end )
8777 |> Multi . run ( :set_topic , fn _ , % { create_content: content } ->
88- topic_title =
89- case attrs |> Map . has_key? ( :topic ) do
90- true -> attrs . topic
91- false -> "posts"
92- end
93-
94- ArticleOperation . set_topic ( % Topic { title: topic_title } , thread , content . id )
78+ exec_set_topic ( thread , content . id , attrs )
9579 end )
9680 |> Multi . run ( :set_community_flag , fn _ , % { create_content: content } ->
97- # TODO: remove this judge, as content should have a flag
98- case action |> Map . has_key? ( :flag ) do
99- true ->
100- ArticleOperation . set_community_flags ( content , community . id , % {
101- trash: false
102- } )
103-
104- false ->
105- { :ok , :pass }
106- end
81+ exec_set_community_flag ( community , content , action )
10782 end )
10883 |> Multi . run ( :set_tag , fn _ , % { create_content: content } ->
109- case attrs |> Map . has_key? ( :tags ) do
110- true -> set_tags ( thread , content . id , attrs . tags )
111- false -> { :ok , :pass }
112- end
84+ exec_set_tag ( thread , content . id , attrs )
11385 end )
11486 |> Multi . run ( :mention_users , fn _ , % { create_content: content } ->
115- Delivery . mention_from_content ( community . raw , thread , content , attrs , % User { id: user_id } )
87+ Delivery . mention_from_content ( community . raw , thread , content , attrs , % User { id: uid } )
11688 { :ok , :pass }
11789 end )
11890 |> Multi . run ( :log_action , fn _ , _ ->
119- Statistics . log_publish_action ( % User { id: user_id } )
91+ Statistics . log_publish_action ( % User { id: uid } )
12092 end )
12193 |> Repo . transaction ( )
12294 |> create_content_result ( )
12395 end
12496 end
12597
98+ # for create content step in Multi.new
99+ defp exec_create_content ( target , attrs , % Author { id: aid } , % Community { id: cid } ) do
100+ target
101+ |> struct ( )
102+ |> target . changeset ( attrs )
103+ |> Ecto.Changeset . put_change ( :author_id , aid )
104+ |> Ecto.Changeset . put_change ( :origial_community_id , integerfy ( cid ) )
105+ |> Repo . insert ( )
106+ end
107+
108+ defp exec_set_topic ( thread , id , % { topic: topic } ) do
109+ ArticleOperation . set_topic ( % Topic { title: topic } , thread , id )
110+ end
111+
112+ # if topic is not provide, use posts as default
113+ defp exec_set_topic ( thread , id , _attrs ) do
114+ ArticleOperation . set_topic ( % Topic { title: "posts" } , thread , id )
115+ end
116+
117+ defp exec_set_tag ( thread , id , % { tags: tags } ) do
118+ try do
119+ Enum . each ( tags , fn tag ->
120+ { :ok , _ } = ArticleOperation . set_tag ( thread , % Tag { id: tag . id } , id )
121+ end )
122+
123+ { :ok , "psss" }
124+ rescue
125+ _ -> { :error , [ message: "set tag" , code: ecode ( :create_fails ) ] }
126+ end
127+ end
128+
129+ defp exec_set_tag ( _thread , _id , _attrs ) , do: { :ok , :pass }
130+
131+ # TODO: flag 逻辑似乎有问题
132+ defp exec_set_community_flag ( % Community { id: cid } , content , % { flag: _flag } ) do
133+ # TODO: 1. 参数改变一下顺序,把 community 放在前面
134+ # TODO: 2. 直接传 community 下去,免去 set_community_flags 函数再在内部查 community
135+ # TODO: 3. 该函数似乎逻辑似乎有点问题, 没有区分 action trash|..
136+ # 要考虑第二条是否符合现实?
137+ ArticleOperation . set_community_flags ( content , cid , % {
138+ trash: false
139+ } )
140+
141+ # TODO: remove this judge, as content should have a flag
142+ # case action |> Map.has_key?(:flag) do
143+ # true ->
144+ # ArticleOperation.set_community_flags(content, community.id, %{
145+ # trash: false
146+ # })
147+
148+ # false ->
149+ # {:ok, :pass}
150+ # end
151+ end
152+
153+ defp exec_set_community_flag ( _community , _content , _action ) do
154+ { :ok , :pass }
155+ end
156+
126157 @ doc """
127158 update a content(post/job ...)
128159 """
@@ -430,18 +461,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
430461 { :error , [ message: "log action" , code: ecode ( :create_fails ) ] }
431462 end
432463
433- defp set_tags ( thread , content_id , tags ) do
434- try do
435- Enum . each ( tags , fn tag ->
436- { :ok , _ } = ArticleOperation . set_tag ( thread , % Tag { id: tag . id } , content_id )
437- end )
438-
439- { :ok , "psss" }
440- rescue
441- _ -> { :error , [ message: "set tag" , code: ecode ( :create_fails ) ] }
442- end
443- end
444-
445464 defp update_tags ( _content , tags_ids ) when length ( tags_ids ) == 0 , do: { :ok , :pass }
446465
447466 # Job is special, the tags in job only represent city, so everytime update
0 commit comments