@@ -7,7 +7,15 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
77 import GroupherServer.CMS.Helper.Matcher
88
99 import Helper.Utils ,
10- only: [ done: 1 , pick_by: 2 , module_to_atom: 1 , get_config: 2 , ensure: 2 , module_to_upcase: 1 ]
10+ only: [
11+ done: 1 ,
12+ pick_by: 2 ,
13+ module_to_atom: 1 ,
14+ get_config: 2 ,
15+ ensure: 2 ,
16+ module_to_upcase: 1 ,
17+ thread_of_article: 1
18+ ]
1119
1220 import GroupherServer.CMS.Delegate.Helper , only: [ mark_viewer_emotion_states: 2 ]
1321 import Helper.ErrorCode
@@ -328,26 +336,28 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
328336 @ doc """
329337 remove article forever
330338 """
331- def remove_article ( thread , id , reason \\ @ remove_article_hint ) do
332- with { :ok , info } <- match ( thread ) ,
333- { :ok , article } <- ORM . find ( info . model , id , preload: [ :communities , [ author: :user ] ] ) do
334- Multi . new ( )
335- |> Multi . run ( :remove_article , fn _ , _ ->
336- article |> ORM . delete ( )
337- end )
338- |> Multi . run ( :update_community_article_count , fn _ , _ ->
339- CommunityCURD . update_community_count_field ( article . communities , thread )
340- end )
341- |> Multi . run ( :update_user_published_meta , fn _ , _ ->
342- Accounts . update_published_states ( article . author . user . id , thread )
343- end )
344- |> Multi . run ( :delete_document , fn _ , _ ->
345- Document . remove ( thread , id )
346- end )
347- # TODO: notify author
348- |> Repo . transaction ( )
349- |> result ( )
350- end
339+ def delete_article ( article , reason \\ @ remove_article_hint ) do
340+ article = Repo . preload ( article , [ :communities , [ author: :user ] ] )
341+ { :ok , thread } = thread_of_article ( article )
342+
343+ Multi . new ( )
344+ |> Multi . run ( :delete_article , fn _ , _ ->
345+ article |> ORM . delete ( )
346+ end )
347+ |> Multi . run ( :update_community_article_count , fn _ , _ ->
348+ CommunityCURD . update_community_count_field ( article . communities , thread )
349+ end )
350+ |> Multi . run ( :update_user_published_meta , fn _ , _ ->
351+ Accounts . update_published_states ( article . author . user . id , thread )
352+ end )
353+ |> Multi . run ( :delete_document , fn _ , _ ->
354+ Document . remove ( thread , article . id )
355+ # for those history & test setup case
356+ { :ok , :pass }
357+ end )
358+ # TODO: notify author
359+ |> Repo . transaction ( )
360+ |> result ( )
351361 end
352362
353363 @ spec ensure_author_exists ( User . t ( ) ) :: { :ok , User . t ( ) }
@@ -497,7 +507,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
497507
498508 defp result ( { :ok , % { update_edit_status: result } } ) , do: { :ok , result }
499509 defp result ( { :ok , % { update_article: result } } ) , do: { :ok , result }
500- defp result ( { :ok , % { remove_article : result } } ) , do: { :ok , result }
510+ defp result ( { :ok , % { delete_article : result } } ) , do: { :ok , result }
501511 # NOTE: for read article, order is import
502512 defp result ( { :ok , % { set_viewer_has_states: result } } ) , do: result |> done ( )
503513 defp result ( { :ok , % { update_article_meta: result } } ) , do: { :ok , result }
0 commit comments