@@ -63,60 +63,32 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
63
63
{:error, %Ecto.Changeset{}}
64
64
65
65
"""
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 } ) ,
73
68
{ :ok , action } <- match_action ( thread , :community ) ,
74
- { :ok , community } <- ORM . find ( Community , community_id ) do
69
+ { :ok , community } <- ORM . find ( Community , cid ) do
75
70
Multi . new ( )
76
71
|> 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 )
83
73
end )
84
74
|> Multi . run ( :set_community , fn _ , % { create_content: content } ->
85
75
ArticleOperation . set_community ( community , thread , content . id )
86
76
end )
87
77
|> 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 )
95
79
end )
96
80
|> 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 )
107
82
end )
108
83
|> 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 )
113
85
end )
114
86
|> 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 } )
116
88
{ :ok , :pass }
117
89
end )
118
90
|> Multi . run ( :log_action , fn _ , _ ->
119
- Statistics . log_publish_action ( % User { id: user_id } )
91
+ Statistics . log_publish_action ( % User { id: uid } )
120
92
end )
121
93
|> Repo . transaction ( )
122
94
|> create_content_result ( )
@@ -132,7 +104,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
132
104
ORM . update ( content , args )
133
105
end )
134
106
|> Multi . run ( :update_tag , fn _ , _ ->
135
- update_tags ( content , args . tags )
107
+ exec_update_tags ( content , args . tags )
136
108
end )
137
109
|> Repo . transaction ( )
138
110
|> update_content_result ( )
@@ -368,37 +340,33 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
368
340
369
341
defp should_add_pin? ( _filter ) , do: { :error , :pass }
370
342
343
+ defp concat_contents ( % { total_count: 0 } , normal_contents ) , do: { :ok , normal_contents }
344
+
371
345
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
398
366
end
399
367
400
368
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 ] } )
402
370
{ :ok , result }
403
371
end
404
372
@@ -430,10 +398,41 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
430
398
{ :error , [ message: "log action" , code: ecode ( :create_fails ) ] }
431
399
end
432
400
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
434
433
try do
435
434
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 )
437
436
end )
438
437
439
438
{ :ok , "psss" }
@@ -442,62 +441,44 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
442
441
end
443
442
end
444
443
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 }
446
445
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
+ } )
454
451
end
455
452
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 }
460
455
end
461
456
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 }
470
458
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 )
475
464
476
- case tag . title == "refined" do
477
- true ->
478
- acc
465
+ case tag . title == "refined" do
466
+ true ->
467
+ acc
479
468
480
- false ->
481
- acc ++ [ tag ]
482
- end
483
- end )
469
+ false ->
470
+ acc ++ [ tag ]
471
+ end
472
+ end )
484
473
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
489
479
end
490
480
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
501
482
target = result . __struct__
502
483
preload = [ :origial_community , author: :user ]
503
484