@@ -6,14 +6,7 @@ defmodule GroupherServer.CMS.Helper.Loader do
66
77 alias GroupherServer . { CMS , Repo }
88
9- alias CMS.Model . {
10- Author ,
11- CommunityThread ,
12- PostComment ,
13- PostCommentLike ,
14- PostCommentReply
15- }
16-
9+ alias CMS.Model . { Author , CommunityThread }
1710 alias Helper.QueryBuilder
1811
1912 def data , do: Dataloader.Ecto . new ( Repo , query: & query / 2 )
@@ -31,96 +24,6 @@ defmodule GroupherServer.CMS.Helper.Loader do
3124 )
3225 end
3326
34- # ------- post comments ------
35- @ doc """
36- get unique participators join in comments
37- """
38- # NOTE: this is NOT the right solution
39- # should use WINDOW function
40- # see https://github.com/coderplanets/coderplanets_server/issues/16
41- def query ( { "posts_comments" , PostComment } , % { filter: _filter , unique: true } ) do
42- PostComment
43- |> join ( :inner , [ c ] , a in assoc ( c , :author ) )
44- # NOTE: this distinct not working in production env, so the uniq logic is move to
45- # cut_participators.ex middleware, when the data is large, will cause performace issue
46- # |> distinct([c, a], a.id)
47- |> select ( [ c , a ] , a )
48- end
49-
50- def query ( { "posts_comments" , PostComment } , % { count: _ , unique: true } ) do
51- PostComment
52- |> join ( :inner , [ c ] , a in assoc ( c , :author ) )
53- |> distinct ( [ c , a ] , a . id )
54- |> group_by ( [ c , a ] , a . id )
55- |> group_by ( [ c , a ] , c . post_id )
56- |> select ( [ c , a ] , count ( c . id ) )
57- end
58-
59- def query ( { "posts_comments" , PostComment } , % { count: _ } ) do
60- PostComment
61- |> group_by ( [ c ] , c . post_id )
62- |> select ( [ c ] , count ( c . id ) )
63- end
64-
65- def query ( { "posts_comments" , PostComment } , % { filter: filter } ) do
66- PostComment
67- |> QueryBuilder . filter_pack ( filter )
68- end
69-
70- def query ( { "posts_comments_replies" , PostCommentReply } , % { count: _ } ) do
71- PostCommentReply
72- |> group_by ( [ c ] , c . post_comment_id )
73- |> select ( [ c ] , count ( c . id ) )
74- end
75-
76- def query ( { "posts_comments_replies" , PostCommentReply } , % { filter: filter } ) do
77- PostCommentReply
78- |> load_inner_replies ( filter )
79- end
80-
81- @ doc """
82- load replies of the given comment
83- TODO: remove
84- """
85- defp load_inner_replies ( queryable , filter ) do
86- queryable
87- |> QueryBuilder . filter_pack ( filter )
88- |> join ( :inner , [ c ] , r in assoc ( c , :reply ) )
89- |> select ( [ c , r ] , r )
90- end
91-
92- def query ( { "posts_comments_replies" , PostCommentReply } , % { reply_to: _ } ) do
93- PostCommentReply
94- |> join ( :inner , [ c ] , r in assoc ( c , :post_comment ) )
95- |> select ( [ c , r ] , r )
96- end
97-
98- def query ( { "posts_comments_likes" , PostCommentLike } , % { count: _ } ) do
99- PostCommentLike
100- |> group_by ( [ f ] , f . post_comment_id )
101- |> select ( [ f ] , count ( f . id ) )
102- end
103-
104- def query ( { "posts_comments_likes" , PostCommentLike } , % { viewer_did: _ , cur_user: cur_user } ) do
105- PostCommentLike |> where ( [ f ] , f . user_id == ^ cur_user . id )
106- end
107-
108- def query ( { "posts_comments_likes" , PostCommentLike } , % { filter: _filter } = args ) do
109- PostCommentLike |> members_pack ( args )
110- end
111-
112- # TODO: remove it
113- def members_pack ( queryable , % { filter: filter } ) do
114- queryable |> QueryBuilder . load_inner_users ( filter )
115- end
116-
117- # def query({"articles_comments_upvotes", ArticleCommentUpvote}, %{
118- # viewer_did: _,
119- # cur_user: cur_user
120- # }) do
121- # ArticleCommentUpvote |> where([f], f.user_id == ^cur_user.id)
122- # end
123-
12427 # default loader
12528 def query ( queryable , _args ) do
12629 queryable
0 commit comments