@@ -63,60 +63,32 @@ 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 ( )
@@ -132,7 +104,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
132104 ORM . update ( content , args )
133105 end )
134106 |> Multi . run ( :update_tag , fn _ , _ ->
135- update_tags ( content , args . tags )
107+ exec_update_tags ( content , args . tags )
136108 end )
137109 |> Repo . transaction ( )
138110 |> update_content_result ( )
@@ -368,37 +340,33 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
368340
369341 defp should_add_pin? ( _filter ) , do: { :error , :pass }
370342
343+ defp concat_contents ( % { total_count: 0 } , normal_contents ) , do: { :ok , normal_contents }
344+
371345 defp concat_contents ( pined_content , normal_contents ) do
372- case pined_content |> Map . get ( :total_count ) do
373- 0 ->
374- { :ok , normal_contents }
375-
376- _ ->
377- pind_entries =
378- pined_content
379- |> Map . get ( :entries )
380- |> Enum . map ( & struct ( & 1 , % { pin: true } ) )
381-
382- normal_entries = normal_contents |> Map . get ( :entries )
383-
384- # pind_count = pined_content |> Map.get(:total_count)
385- normal_count = normal_contents |> Map . get ( :total_count )
386-
387- # remote the pined content from normal_entries (if have)
388- pind_ids = pick_by ( pind_entries , :id )
389- normal_entries = Enum . reject ( normal_entries , & ( & 1 . id in pind_ids ) )
390-
391- normal_contents
392- |> Map . put ( :entries , pind_entries ++ normal_entries )
393- # those two are equals
394- # |> Map.put(:total_count, pind_count + normal_count - pind_count)
395- |> Map . put ( :total_count , normal_count )
396- |> done
397- end
346+ pind_entries =
347+ pined_content
348+ |> Map . get ( :entries )
349+ |> Enum . map ( & struct ( & 1 , % { pin: true } ) )
350+
351+ normal_entries = normal_contents |> Map . get ( :entries )
352+
353+ # pind_count = pined_content |> Map.get(:total_count)
354+ normal_count = normal_contents |> Map . get ( :total_count )
355+
356+ # remote the pined content from normal_entries (if have)
357+ pind_ids = pick_by ( pind_entries , :id )
358+ normal_entries = Enum . reject ( normal_entries , & ( & 1 . id in pind_ids ) )
359+
360+ normal_contents
361+ |> Map . put ( :entries , pind_entries ++ normal_entries )
362+ # those two are equals
363+ # |> Map.put(:total_count, pind_count + normal_count - pind_count)
364+ |> Map . put ( :total_count , normal_count )
365+ |> done
398366 end
399367
400368 defp create_content_result ( { :ok , % { create_content: result } } ) do
401- Later . exec ( { __MODULE__ , :nofify_admin_new_content , [ result ] } )
369+ Later . exec ( { __MODULE__ , :notify_admin_new_content , [ result ] } )
402370 { :ok , result }
403371 end
404372
@@ -430,10 +398,41 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
430398 { :error , [ message: "log action" , code: ecode ( :create_fails ) ] }
431399 end
432400
433- defp set_tags ( thread , content_id , tags ) do
401+ # except Job, other content will just pass, should use set_tag function instead
402+ # defp exec_update_tags(_, _tags_ids), do: {:ok, :pass}
403+
404+ defp update_content_result ( { :ok , % { update_content: result } } ) , do: { :ok , result }
405+ defp update_content_result ( { :error , :update_content , result , _steps } ) , do: { :error , result }
406+ defp update_content_result ( { :error , :update_tag , result , _steps } ) , do: { :error , result }
407+
408+ defp content_id ( :post , id ) , do: % { post_id: id }
409+ defp content_id ( :job , id ) , do: % { job_id: id }
410+ defp content_id ( :repo , id ) , do: % { repo_id: id }
411+ defp content_id ( :video , id ) , do: % { video_id: id }
412+
413+ # for create content step in Multi.new
414+ defp exec_create_content ( target , attrs , % Author { id: aid } , % Community { id: cid } ) do
415+ target
416+ |> struct ( )
417+ |> target . changeset ( attrs )
418+ |> Ecto.Changeset . put_change ( :author_id , aid )
419+ |> Ecto.Changeset . put_change ( :origial_community_id , integerfy ( cid ) )
420+ |> Repo . insert ( )
421+ end
422+
423+ defp exec_set_topic ( thread , id , % { topic: topic } ) do
424+ ArticleOperation . set_topic ( % Topic { title: topic } , thread , id )
425+ end
426+
427+ # if topic is not provide, use posts as default
428+ defp exec_set_topic ( thread , id , _attrs ) do
429+ ArticleOperation . set_topic ( % Topic { title: "posts" } , thread , id )
430+ end
431+
432+ defp exec_set_tag ( thread , id , % { tags: tags } ) do
434433 try do
435434 Enum . each ( tags , fn tag ->
436- { :ok , _ } = ArticleOperation . set_tag ( thread , % Tag { id: tag . id } , content_id )
435+ { :ok , _ } = ArticleOperation . set_tag ( thread , % Tag { id: tag . id } , id )
437436 end )
438437
439438 { :ok , "psss" }
@@ -442,62 +441,44 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
442441 end
443442 end
444443
445- defp update_tags ( _content , tags_ids ) when length ( tags_ids ) == 0 , do: { :ok , :pass }
444+ defp exec_set_tag ( _thread , _id , _attrs ) , do: { :ok , :pass }
446445
447- # Job is special, the tags in job only represent city, so everytime update
448- # tags on job content, should be override the old ones, in this way, every
449- # communiies contains this job will have the same city info
450- defp update_tags ( % CMS.Job { } = content , tags_ids ) do
451- with { :ok , content } <- ORM . find ( CMS.Job , content . id , preload: :tags ) do
452- concat_tags ( content , tags_ids )
453- end
446+ # TODO: flag 逻辑似乎有问题
447+ defp exec_set_community_flag ( % Community { } = community , content , % { flag: _flag } ) do
448+ ArticleOperation . set_community_flags ( community , content , % {
449+ trash: false
450+ } )
454451 end
455452
456- defp update_tags ( % CMS.Post { } = content , tags_ids ) do
457- with { :ok , content } <- ORM . find ( CMS.Post , content . id , preload: :tags ) do
458- concat_tags ( content , tags_ids )
459- end
453+ defp exec_set_community_flag ( _community , _content , _action ) do
454+ { :ok , :pass }
460455 end
461456
462- defp update_tags ( % CMS.Video { } = content , tags_ids ) do
463- with { :ok , content } <- ORM . find ( CMS.Video , content . id , preload: :tags ) do
464- concat_tags ( content , tags_ids )
465- end
466- end
467-
468- # except Job, other content will just pass, should use set_tag function instead
469- defp update_tags ( _ , _tags_ids ) , do: { :ok , :pass }
457+ defp exec_update_tags ( _content , tags_ids ) when length ( tags_ids ) == 0 , do: { :ok , :pass }
470458
471- defp concat_tags ( content , tags_ids ) do
472- tags =
473- Enum . reduce ( tags_ids , [ ] , fn t , acc ->
474- { :ok , tag } = ORM . find ( Tag , t . id )
459+ defp exec_update_tags ( content , tags_ids ) do
460+ with { :ok , content } <- ORM . find ( content . __struct__ , content . id , preload: :tags ) do
461+ tags =
462+ Enum . reduce ( tags_ids , [ ] , fn t , acc ->
463+ { :ok , tag } = ORM . find ( Tag , t . id )
475464
476- case tag . title == "refined" do
477- true ->
478- acc
465+ case tag . title == "refined" do
466+ true ->
467+ acc
479468
480- false ->
481- acc ++ [ tag ]
482- end
483- end )
469+ false ->
470+ acc ++ [ tag ]
471+ end
472+ end )
484473
485- content
486- |> Ecto.Changeset . change ( )
487- |> Ecto.Changeset . put_assoc ( :tags , tags )
488- |> Repo . update ( )
474+ content
475+ |> Ecto.Changeset . change ( )
476+ |> Ecto.Changeset . put_assoc ( :tags , tags )
477+ |> Repo . update ( )
478+ end
489479 end
490480
491- defp update_content_result ( { :ok , % { update_content: result } } ) , do: { :ok , result }
492- defp update_content_result ( { :error , :update_content , result , _steps } ) , do: { :error , result }
493- defp update_content_result ( { :error , :update_tag , result , _steps } ) , do: { :error , result }
494-
495- defp content_id ( :post , id ) , do: % { post_id: id }
496- defp content_id ( :job , id ) , do: % { job_id: id }
497- defp content_id ( :repo , id ) , do: % { repo_id: id }
498- defp content_id ( :video , id ) , do: % { video_id: id }
499-
500- defp nofify_admin_new_content ( % { id: id } = result ) do
481+ defp notify_admin_new_content ( % { id: id } = result ) do
501482 target = result . __struct__
502483 preload = [ :origial_community , author: :user ]
503484
0 commit comments