@@ -4,7 +4,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
4
4
"""
5
5
import Ecto.Query , warn: false
6
6
7
- import GroupherServer.CMS.Helper.Matcher2
7
+ import GroupherServer.CMS.Helper.Matcher
8
8
9
9
import Helper.Utils ,
10
10
only: [ done: 1 , pick_by: 2 , integerfy: 1 , strip_struct: 1 , module_to_thread: 1 ]
@@ -19,7 +19,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
19
19
alias Accounts.User
20
20
alias CMS . { Author , Community , PinnedArticle , Embeds , Delegate }
21
21
22
- alias Delegate . { ArticleCommunity , ArticleTag }
22
+ alias Delegate . { ArticleCommunity , ArticleTag , CommunityCURD }
23
23
24
24
alias Ecto.Multi
25
25
@@ -47,8 +47,19 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
47
47
|> Multi . run ( :add_viewed_user , fn _ , % { inc_views: article } ->
48
48
update_viewed_user_list ( article , user_id )
49
49
end )
50
+ |> Multi . run ( :set_viewer_has_states , fn _ , % { inc_views: article } ->
51
+ article_meta = if is_nil ( article . meta ) , do: @ default_article_meta , else: article . meta
52
+
53
+ viewer_has_states = % {
54
+ viewer_has_collected: user_id in article_meta . collected_user_ids ,
55
+ viewer_has_upvoted: user_id in article_meta . upvoted_user_ids ,
56
+ viewer_has_reported: user_id in article_meta . reported_user_ids
57
+ }
58
+
59
+ { :ok , Map . merge ( article , viewer_has_states ) }
60
+ end )
50
61
|> Repo . transaction ( )
51
- |> read_result ( )
62
+ |> result ( )
52
63
end
53
64
end
54
65
@@ -135,6 +146,9 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
135
146
|> Multi . run ( :set_article_tags , fn _ , % { create_article: article } ->
136
147
ArticleTag . set_article_tags ( community , thread , article , attrs )
137
148
end )
149
+ |> Multi . run ( :update_community_article_count , fn _ , _ ->
150
+ CommunityCURD . update_community_count_field ( community , thread )
151
+ end )
138
152
|> Multi . run ( :mention_users , fn _ , % { create_article: article } ->
139
153
Delivery . mention_from_content ( community . raw , thread , article , attrs , % User { id: uid } )
140
154
{ :ok , :pass }
@@ -183,18 +197,42 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
183
197
ArticleCommunity . update_edit_status ( update_article )
184
198
end )
185
199
|> Repo . transaction ( )
186
- |> update_article_result ( )
200
+ |> result ( )
187
201
end
188
202
203
+ @ doc """
204
+ mark delete falst for an anticle
205
+ """
189
206
def mark_delete_article ( thread , id ) do
190
- with { :ok , info } <- match ( thread ) do
191
- ORM . find_update ( info . model , % { id: id , mark_delete: true } )
207
+ with { :ok , info } <- match ( thread ) ,
208
+ { :ok , article } <- ORM . find ( info . model , id , preload: :communities ) do
209
+ Multi . new ( )
210
+ |> Multi . run ( :update_article , fn _ , _ ->
211
+ ORM . update ( article , % { mark_delete: true } )
212
+ end )
213
+ |> Multi . run ( :update_community_article_count , fn _ , _ ->
214
+ CommunityCURD . update_community_count_field ( article . communities , thread )
215
+ end )
216
+ |> Repo . transaction ( )
217
+ |> result ( )
192
218
end
193
219
end
194
220
221
+ @ doc """
222
+ undo mark delete falst for an anticle
223
+ """
195
224
def undo_mark_delete_article ( thread , id ) do
196
- with { :ok , info } <- match ( thread ) do
197
- ORM . find_update ( info . model , % { id: id , mark_delete: false } )
225
+ with { :ok , info } <- match ( thread ) ,
226
+ { :ok , article } <- ORM . find ( info . model , id , preload: :communities ) do
227
+ Multi . new ( )
228
+ |> Multi . run ( :update_article , fn _ , _ ->
229
+ ORM . update ( article , % { mark_delete: false } )
230
+ end )
231
+ |> Multi . run ( :update_community_article_count , fn _ , _ ->
232
+ CommunityCURD . update_community_count_field ( article . communities , thread )
233
+ end )
234
+ |> Repo . transaction ( )
235
+ |> result ( )
198
236
end
199
237
end
200
238
@@ -406,12 +444,11 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
406
444
end
407
445
end
408
446
409
- defp update_article_result ( { :ok , % { update_edit_status: result } } ) , do: { :ok , result }
410
- defp update_article_result ( { :error , :update_article , result , _steps } ) , do: { :error , result }
411
-
412
- defp read_result ( { :ok , % { inc_views: result } } ) , do: result |> done ( )
447
+ defp result ( { :ok , % { update_edit_status: result } } ) , do: { :ok , result }
448
+ defp result ( { :ok , % { update_article: result } } ) , do: { :ok , result }
449
+ defp result ( { :ok , % { set_viewer_has_states: result } } ) , do: result |> done ( )
413
450
414
- defp read_result ( { :error , _ , result , _steps } ) do
451
+ defp result ( { :error , _ , result , _steps } ) do
415
452
{ :error , result }
416
453
end
417
454
end
0 commit comments