@@ -10,6 +10,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
1010  alias  Helper.ORM 
1111
1212  alias  GroupherServer.CMS . { 
13+     ArticleMeta , 
1314    Community , 
1415    Post , 
1516    PostCommunityFlag , 
@@ -29,22 +30,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
2930  alias  GroupherServer.CMS.Repo ,  as:  CMSRepo 
3031  alias  GroupherServer.Repo 
3132
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- 
4833  def  pin_content ( % Post { id:  post_id } ,  % Community { id:  community_id } ,  topic )  do 
4934    with  { :ok ,  % { id:  topic_id } }  <-  ORM . find_by ( Topic ,  % { raw:  topic } ) , 
5035         { :ok ,  pined }  <- 
@@ -294,19 +279,32 @@ defmodule GroupherServer.CMS.Delegate.ArticleOperation do
294279  def  set_topic ( _topic ,  _thread ,  _content_id ) ,  do:  { :ok ,  :pass } 
295280
296281  @ 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 
297284  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:  % { } } ) 
299286  end 
300287
301288  def  set_meta ( _ ,  _ ) ,  do:  { :ok ,  :pass } 
302289
303290  @ 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 ( ) 
306298  end 
307299
300+   # for test or exsiting articles 
308301  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 ( ) 
310308  end 
311309
312310  def  update_meta ( content ,  _ ) ,  do:  { :ok ,  content } 
0 commit comments