@@ -8,7 +8,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
88
99 import Helper.ErrorCode
1010 import ShortMaps
11- import Helper.Utils , only: [ strip_struct: 1 , integerfy: 1 ]
11+ import Helper.Utils , only: [ strip_struct: 1 , integerfy: 1 , done: 1 ]
1212 import GroupherServer.CMS.Helper.Matcher2
1313
1414 alias Helper.Types , as: T
@@ -29,6 +29,8 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
2929 alias GroupherServer.CMS.Repo , as: CMSRepo
3030 alias GroupherServer.Repo
3131
32+ alias Ecto.Multi
33+
3234 @ max_pinned_article_count_per_thread Community . max_pinned_article_count_per_thread ( )
3335
3436 @ spec pin_article ( T . article_thread ( ) , Integer . t ( ) , Integer . t ( ) ) :: { :ok , PinnedArticle . t ( ) }
@@ -103,7 +105,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
103105 end
104106
105107 @ doc """
106- set content to diffent community
108+ mirror article to other community
107109 """
108110 def set_community ( thread , article_id , community_id ) do
109111 with { :ok , info } <- match ( thread ) ,
@@ -116,6 +118,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
116118 end
117119 end
118120
121+ @ doc """
122+ unmirror article to a community
123+ """
119124 def unset_community ( thread , article_id , community_id ) do
120125 with { :ok , info } <- match ( thread ) ,
121126 { :ok , article } <-
@@ -135,18 +140,33 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
135140 end
136141
137142 @ doc """
138- set article original community in meta
143+ move article original community to other community
139144 """
140145 def move_article ( thread , article_id , community_id ) do
141146 with { :ok , info } <- match ( thread ) ,
142- { :ok , article } <- ORM . find ( info . model , article_id ) do
143- article
144- |> Ecto.Changeset . change ( )
145- |> Ecto.Changeset . put_change ( :original_community_id , integerfy ( community_id ) )
146- |> Repo . update ( )
147+ { :ok , community } <- ORM . find ( Community , community_id ) ,
148+ { :ok , article } <- ORM . find ( info . model , article_id , preload: [ :communities ] ) do
149+ Multi . new ( )
150+ |> Multi . run ( :change_original_community , fn _ , _ ->
151+ article
152+ |> Ecto.Changeset . change ( )
153+ |> Ecto.Changeset . put_change ( :original_community_id , community . id )
154+ |> Repo . update ( )
155+ end )
156+ |> Multi . run ( :unmirror_community , fn _ , % { change_original_community: article } ->
157+ article
158+ |> Ecto.Changeset . change ( )
159+ |> Ecto.Changeset . put_assoc ( :communities , article . communities -- [ community ] )
160+ |> Repo . update ( )
161+ end )
162+ |> Repo . transaction ( )
163+ |> result ( )
147164 end
148165 end
149166
167+ defp result ( { :ok , % { change_original_community: result } } ) , do: result |> done ( )
168+ defp result ( { :error , _ , result , _steps } ) , do: { :error , result }
169+
150170 @ doc """
151171 set general tag for post / tuts ...
152172 """
0 commit comments