@@ -161,14 +161,14 @@ defmodule GroupherServer.CMS.Delegate.CiteTasks do
161161  defp  parse_cited_info_per_block ( content ,  % { "id"  =>  block_id ,  "data"  =>  % { "text"  =>  text } } )  do 
162162    links  =  Floki . find ( text ,  "a[href]" ) 
163163
164-     do_parse_cited_info ( content ,  block_id ,  links ) 
164+     do_parse_cited_info_per_block ( content ,  block_id ,  links ) 
165165  end 
166166
167167  # links Floki parsed fmt 
168168  # content means both article and comment 
169169  # e.g: 
170170  # [{"a", [{"href", "https://coderplanets.com/post/195675"}], []},] 
171-   defp  do_parse_cited_info ( content ,  block_id ,  links )  do 
171+   defp  do_parse_cited_info_per_block ( content ,  block_id ,  links )  do 
172172    Enum . reduce ( links ,  [ ] ,  fn  link ,  acc  -> 
173173      case  parse_valid_cited ( content . id ,  link )  do 
174174        { :ok ,  cited }  ->  List . insert_at ( acc ,  0 ,  shape_cited ( content ,  cited ,  block_id ) ) 
@@ -189,6 +189,75 @@ defmodule GroupherServer.CMS.Delegate.CiteTasks do
189189    end 
190190  end 
191191
192+   # return fmt: %{type: :comment | :article, content: %Comment{} | Article} 
193+   # 要考虑是否有 comment_id 的情况,如果有,那么 就应该 load comment 而不是 article 
194+   defp  parse_cited ( { "a" ,  attrs ,  _ } )  do 
195+     with  { :ok ,  link }  <-  parse_link ( attrs ) , 
196+          true  <-  is_site_article_link? ( link )  do 
197+       # IO.inspect(link, label: "parse link") 
198+       # IO.inspect(is_comment_link?(link), label: "is_comment_link") 
199+ 
200+       case  is_comment_link? ( link )  do 
201+         true  ->  load_cited_comment_from_url ( link ) 
202+         false  ->  load_cited_article_from_url ( link ) 
203+       end 
204+     end 
205+   end 
206+ 
207+   @ doc  """ 
208+   parse link from Floki parse result 
209+ 
210+   e.g: 
211+   [{"href", "https://coderplanets.com/post/190220", "bla", "bla"}] -> 
212+   {:ok, "https://coderplanets.com/post/190220"} 
213+   """ 
214+   defp  parse_link ( attrs )  do 
215+     with  { "href" ,  link }  <-  Enum . find ( attrs ,  fn  { a ,  _v }  ->  a  ==  "href"  end )  do 
216+       { :ok ,  link } 
217+     else 
218+       _  ->  { :error ,  "invalid fmt" } 
219+     end 
220+   end 
221+ 
222+   # 检测是否是站内文章的链接 
223+   defp  is_site_article_link? ( url )  do 
224+     Enum . any? ( @ valid_article_prefix ,  & String . starts_with? ( url ,  & 1 ) ) 
225+   end 
226+ 
227+   defp  is_comment_link? ( url )  do 
228+     with  % { query:  query }  <-  URI . parse ( url )  do 
229+       not  is_nil ( query )  and  String . starts_with? ( query ,  "comment_id=" ) 
230+     end 
231+   end 
232+ 
233+   defp  load_cited_comment_from_url ( url )  do 
234+     % { query:  query }  =  URI . parse ( url ) 
235+ 
236+     try  do 
237+       comment_id  =  URI . decode_query ( query )  |>  Map . get ( "comment_id" ) 
238+ 
239+       with  { :ok ,  comment }  <-  ORM . find ( Comment ,  comment_id )  do 
240+         { :ok ,  % { type:  :comment ,  content:  comment } } 
241+       end 
242+     rescue 
243+       _  ->  { :error ,  "load comment error" } 
244+     end 
245+   end 
246+ 
247+   # get cited article from url 
248+   # e.g: https://coderplanets.com/post/189993 -> ORM.find(Post, 189993) 
249+   defp  load_cited_article_from_url ( url )  do 
250+     % { path:  path }  =  URI . parse ( url ) 
251+     path_list  =  path  |>  String . split ( "/" ) 
252+     thread  =  path_list  |>  Enum . at ( 1 )  |>  String . downcase ( )  |>  String . to_atom ( ) 
253+     article_id  =  path_list  |>  Enum . at ( 2 ) 
254+ 
255+     with  { :ok ,  info }  <-  match ( thread ) , 
256+          { :ok ,  article }  <-  ORM . find ( info . model ,  article_id )  do 
257+       { :ok ,  % { type:  :article ,  content:  article } } 
258+     end 
259+   end 
260+ 
192261  # cite article in comment 
193262  # 在评论中引用文章 
194263  defp  shape_cited ( % Comment { }  =  comment ,  % { type:  :article ,  content:  cited } ,  block_id )  do 
@@ -263,74 +332,6 @@ defmodule GroupherServer.CMS.Delegate.CiteTasks do
263332    |>  Map . put ( info . foreign_key ,  article . id ) 
264333  end 
265334
266-   # 要考虑是否有 comment_id 的情况,如果有,那么 就应该 load comment 而不是 article 
267-   defp  parse_cited ( { "a" ,  attrs ,  _ } )  do 
268-     with  { :ok ,  link }  <-  parse_link ( attrs ) , 
269-          true  <-  is_site_article_link? ( link )  do 
270-       # IO.inspect(link, label: "parse link") 
271-       # IO.inspect(is_comment_link?(link), label: "is_comment_link") 
272- 
273-       case  is_comment_link? ( link )  do 
274-         true  ->  load_cited_comment_from_url ( link ) 
275-         false  ->  load_cited_article_from_url ( link ) 
276-       end 
277-     end 
278-   end 
279- 
280-   @ doc  """ 
281-   parse link from Floki parse result 
282- 
283-   e.g: 
284-   [{"href", "https://coderplanets.com/post/190220", "bla", "bla"}] -> 
285-   {:ok, "https://coderplanets.com/post/190220"} 
286-   """ 
287-   defp  parse_link ( attrs )  do 
288-     with  { "href" ,  link }  <-  Enum . find ( attrs ,  fn  { a ,  _v }  ->  a  ==  "href"  end )  do 
289-       { :ok ,  link } 
290-     else 
291-       _  ->  { :error ,  "invalid fmt" } 
292-     end 
293-   end 
294- 
295-   # 检测是否是站内文章的链接 
296-   defp  is_site_article_link? ( url )  do 
297-     Enum . any? ( @ valid_article_prefix ,  & String . starts_with? ( url ,  & 1 ) ) 
298-   end 
299- 
300-   defp  is_comment_link? ( url )  do 
301-     with  % { query:  query }  <-  URI . parse ( url )  do 
302-       not  is_nil ( query )  and  String . starts_with? ( query ,  "comment_id=" ) 
303-     end 
304-   end 
305- 
306-   defp  load_cited_comment_from_url ( url )  do 
307-     % { query:  query }  =  URI . parse ( url ) 
308- 
309-     try  do 
310-       comment_id  =  URI . decode_query ( query )  |>  Map . get ( "comment_id" ) 
311- 
312-       with  { :ok ,  comment }  <-  ORM . find ( Comment ,  comment_id )  do 
313-         { :ok ,  % { type:  :comment ,  content:  comment } } 
314-       end 
315-     rescue 
316-       _  ->  { :error ,  "load comment error" } 
317-     end 
318-   end 
319- 
320-   # get cited article from url 
321-   # e.g: https://coderplanets.com/post/189993 -> ORM.find(Post, 189993) 
322-   defp  load_cited_article_from_url ( url )  do 
323-     % { path:  path }  =  URI . parse ( url ) 
324-     path_list  =  path  |>  String . split ( "/" ) 
325-     thread  =  path_list  |>  Enum . at ( 1 )  |>  String . downcase ( )  |>  String . to_atom ( ) 
326-     article_id  =  path_list  |>  Enum . at ( 2 ) 
327- 
328-     with  { :ok ,  info }  <-  match ( thread ) , 
329-          { :ok ,  article }  <-  ORM . find ( info . model ,  article_id )  do 
330-       { :ok ,  % { type:  :article ,  content:  article } } 
331-     end 
332-   end 
333- 
334335  defp  result ( { :ok ,  % { update_cited_info:  result } } ) ,  do:  { :ok ,  result } 
335336
336337  defp  result ( { :error ,  :update_cited_info ,  _result ,  _steps } )  do 
0 commit comments