@@ -10,6 +10,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
10
10
alias Helper.ORM
11
11
12
12
alias GroupherServer.CMS . {
13
+ ArticleMeta ,
13
14
Community ,
14
15
Post ,
15
16
PostCommunityFlag ,
@@ -29,22 +30,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
29
30
alias GroupherServer.CMS.Repo , as: CMSRepo
30
31
alias GroupherServer.Repo
31
32
32
- @ default_article_meta % {
33
- isEdited: false ,
34
- forbidComment: false ,
35
- isReported: false
36
- # linkedPostsCount: 0,
37
- # linkedJobsCount: 0,
38
- # linkedWorksCount: 0,
39
- # reaction: %{
40
- # rocketCount: 0,
41
- # heartCount: 0,
42
- # }
43
- }
44
-
45
- @ doc "for test usage"
46
- def default_article_meta ( ) , do: @ default_article_meta
47
-
48
33
def pin_content ( % Post { id: post_id } , % Community { id: community_id } , topic ) do
49
34
with { :ok , % { id: topic_id } } <- ORM . find_by ( Topic , % { raw: topic } ) ,
50
35
{ :ok , pined } <-
@@ -294,19 +279,32 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
294
279
def set_topic ( _topic , _thread , _content_id ) , do: { :ok , :pass }
295
280
296
281
@ doc "set meta info"
282
+ # embeds_one do not have default option, so we init it with empty map mannully
283
+ # see: https://github.com/elixir-ecto/ecto/issues/2634
297
284
def set_meta ( :post , content_id ) do
298
- ORM . update_by ( Post , [ id: content_id ] , % { meta: @ default_article_meta } )
285
+ ORM . update_by ( Post , [ id: content_id ] , % { meta: % { } } )
299
286
end
300
287
301
288
def set_meta ( _ , _ ) , do: { :ok , :pass }
302
289
303
290
@ doc "update isEdited meta label if needed"
304
- def update_meta ( % Post { meta: % { "isEdited" => false } = meta } = content , :is_edited ) do
305
- ORM . update ( content , % { meta: Map . merge ( meta , % { "isEdited" => true } ) } )
291
+ def update_meta ( % Post { meta: % ArticleMeta { is_edited: false } = meta } = content , :is_edited ) do
292
+ new_meta = meta |> Map . from_struct ( ) |> Map . delete ( :id ) |> Map . merge ( % { is_edited: true } )
293
+
294
+ content
295
+ |> Ecto.Changeset . change ( )
296
+ |> Ecto.Changeset . put_embed ( :meta , new_meta )
297
+ |> Repo . update ( )
306
298
end
307
299
300
+ # for test or exsiting articles
308
301
def update_meta ( % Post { meta: nil } = content , :is_edited ) do
309
- ORM . update ( content , % { meta: Map . merge ( @ default_article_meta , % { "isEdited" => true } ) } )
302
+ new_meta = ArticleMeta . default_meta ( ) |> Map . merge ( % { is_edited: true } )
303
+
304
+ content
305
+ |> Ecto.Changeset . change ( )
306
+ |> Ecto.Changeset . put_embed ( :meta , new_meta )
307
+ |> Repo . update ( )
310
308
end
311
309
312
310
def update_meta ( content , _ ) , do: { :ok , content }