@@ -16,17 +16,7 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
1616 alias GroupherServer . { Accounts , CMS , Repo }
1717
1818 alias Accounts.User
19-
20- alias CMS . {
21- ArticleComment ,
22- ArticlePinnedComment ,
23- ArticleCommentUpvote ,
24- ArticleCommentReply ,
25- Community ,
26- # TODO: remove spec type
27- Post ,
28- Job
29- }
19+ alias CMS . { ArticleComment , ArticlePinnedComment , ArticleCommentUpvote , ArticleCommentReply }
3020
3121 alias Ecto.Multi
3222
@@ -250,45 +240,25 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
250240 { :ok , parent_comment }
251241 end
252242
253- defp get_article ( % ArticleComment { post_id: post_id } = comment ) when not is_nil ( post_id ) do
254- with { :ok , article } <- ORM . find ( Post , comment . post_id , preload: [ author: :user ] ) do
255- { :post , article }
256- end
257- end
258-
259- defp get_article ( % ArticleComment { job_id: job_id } = comment ) when not is_nil ( job_id ) do
260- with { :ok , article } <- ORM . find ( Job , comment . job_id , preload: [ author: :user ] ) do
261- { :job , article }
262- end
263- end
264-
265- defp get_article ( % ArticleComment { repo_id: repo_id } = comment ) when not is_nil ( repo_id ) do
266- with { :ok , article } <- ORM . find ( CMS.Repo , comment . repo_id , preload: [ author: :user ] ) do
267- { :repo , article }
243+ defp get_article ( % ArticleComment { } = comment ) do
244+ with article_thread <- find_comment_article_thread ( comment ) ,
245+ { :ok , info } <- match ( article_thread ) ,
246+ article_id <- Map . get ( comment , info . foreign_key ) ,
247+ { :ok , article } <- ORM . find ( info . model , article_id , preload: [ author: :user ] ) do
248+ { article_thread , article }
268249 end
269250 end
270251
271252 @ spec get_full_comment ( String . t ( ) ) :: { :ok , T . article_info ( ) } | { :error , nil }
272253 defp get_full_comment ( comment_id ) do
273254 query = from ( c in ArticleComment , where: c . id == ^ comment_id , preload: ^ @ article_threads )
274255
275- with { :ok , comment } <- Repo . one ( query ) |> done ( ) do
276- extract_article_info ( comment )
256+ with { :ok , comment } <- Repo . one ( query ) |> done ( ) ,
257+ article_thread <- find_comment_article_thread ( comment ) do
258+ do_extract_article_info ( article_thread , Map . get ( comment , article_thread ) )
277259 end
278260 end
279261
280- defp extract_article_info ( % ArticleComment { post: % Post { } = post } ) when not is_nil ( post ) do
281- do_extract_article_info ( :post , post )
282- end
283-
284- defp extract_article_info ( % ArticleComment { job: % Job { } = job } ) when not is_nil ( job ) do
285- do_extract_article_info ( :job , job )
286- end
287-
288- defp extract_article_info ( % ArticleComment { repo: % CMS.Repo { } = repo } ) when not is_nil ( repo ) do
289- do_extract_article_info ( :repo , repo )
290- end
291-
292262 @ spec do_extract_article_info ( T . article_thread ( ) , T . article_common ( ) ) :: { :ok , T . article_info ( ) }
293263 defp do_extract_article_info ( thread , article ) do
294264 with { :ok , article_with_author } <- Repo . preload ( article , author: :user ) |> done ( ) ,
@@ -306,6 +276,12 @@ defmodule GroupherServer.CMS.Delegate.ArticleCommentAction do
306276 end
307277 end
308278
279+ defp find_comment_article_thread ( % ArticleComment { } = comment ) do
280+ @ article_threads
281+ |> Enum . filter ( & Map . get ( comment , :"#{ & 1 } _id" ) )
282+ |> List . first ( )
283+ end
284+
309285 # used in replies mode, for those reply to other user in replies box (for frontend)
310286 # 用于回复模式,指代这条回复是回复“回复列表其他人的” (方便前端展示)
311287 defp update_reply_to_others_state ( parent_comment , replying_comment , replyed_comment ) do
0 commit comments