@ -147,275 +147,16 @@ impl CommentView {
}
}
mod join_types {
use crate ::schema ::{
comment ,
comment_aggregates ,
comment_alias_1 ,
comment_like ,
comment_saved ,
community ,
community_follower ,
community_user_ban ,
post ,
user_ ,
user_alias_1 ,
} ;
use diesel ::{
pg ::Pg ,
query_builder ::BoxedSelectStatement ,
query_source ::joins ::{ Inner , Join , JoinOn , LeftOuter } ,
sql_types ::* ,
} ;
// /// TODO awful, but necessary because of the boxed join
pub ( super ) type BoxedCommentJoin < ' a > = BoxedSelectStatement <
' a ,
(
(
Integer ,
Integer ,
Integer ,
Nullable < Integer > ,
Text ,
Bool ,
Bool ,
Timestamp ,
Nullable < Timestamp > ,
Bool ,
Text ,
Bool ,
) ,
(
Integer ,
Text ,
Nullable < Text > ,
Nullable < Text > ,
Bool ,
Bool ,
Timestamp ,
Nullable < Timestamp > ,
Nullable < Text > ,
Text ,
Nullable < Text > ,
Bool ,
Nullable < Text > ,
Bool ,
) ,
Nullable < (
Integer ,
Integer ,
Integer ,
Nullable < Integer > ,
Text ,
Bool ,
Bool ,
Timestamp ,
Nullable < Timestamp > ,
Bool ,
Text ,
Bool ,
) > ,
Nullable < (
Integer ,
Text ,
Nullable < Text > ,
Nullable < Text > ,
Bool ,
Bool ,
Timestamp ,
Nullable < Timestamp > ,
Nullable < Text > ,
Text ,
Nullable < Text > ,
Bool ,
Nullable < Text > ,
Bool ,
) > ,
(
Integer ,
Text ,
Nullable < Text > ,
Nullable < Text > ,
Integer ,
Integer ,
Bool ,
Bool ,
Timestamp ,
Nullable < Timestamp > ,
Bool ,
Bool ,
Bool ,
Nullable < Text > ,
Nullable < Text > ,
Nullable < Text > ,
Nullable < Text > ,
Text ,
Bool ,
) ,
(
Integer ,
Text ,
Text ,
Nullable < Text > ,
Integer ,
Integer ,
Bool ,
Timestamp ,
Nullable < Timestamp > ,
Bool ,
Bool ,
Text ,
Bool ,
Nullable < Text > ,
Nullable < Text > ,
) ,
( Integer , Integer , BigInt , BigInt , BigInt ) ,
Nullable < ( Integer , Integer , Integer , Timestamp ) > ,
Nullable < ( Integer , Integer , Integer , Timestamp , Nullable < Bool > ) > ,
Nullable < ( Integer , Integer , Integer , Timestamp ) > ,
Nullable < SmallInt > ,
) ,
JoinOn <
Join <
JoinOn <
Join <
JoinOn <
Join <
JoinOn <
Join <
JoinOn <
Join <
JoinOn <
Join <
JoinOn <
Join <
JoinOn <
Join <
JoinOn <
Join <
JoinOn <
Join < comment ::table , user_ ::table , Inner > ,
diesel ::expression ::operators ::Eq <
diesel ::expression ::nullable ::Nullable <
comment ::columns ::creator_id ,
> ,
diesel ::expression ::nullable ::Nullable <
user_ ::columns ::id ,
> ,
> ,
> ,
comment_alias_1 ::table ,
LeftOuter ,
> ,
diesel ::expression ::operators ::Eq <
diesel ::expression ::nullable ::Nullable <
comment_alias_1 ::columns ::id ,
> ,
comment ::columns ::parent_id ,
> ,
> ,
user_alias_1 ::table ,
LeftOuter ,
> ,
diesel ::expression ::operators ::Eq <
user_alias_1 ::columns ::id ,
comment_alias_1 ::columns ::creator_id ,
> ,
> ,
post ::table ,
Inner ,
> ,
diesel ::expression ::operators ::Eq <
diesel ::expression ::nullable ::Nullable < comment ::columns ::post_id > ,
diesel ::expression ::nullable ::Nullable < post ::columns ::id > ,
> ,
> ,
community ::table ,
Inner ,
> ,
diesel ::expression ::operators ::Eq <
post ::columns ::community_id ,
community ::columns ::id ,
> ,
> ,
comment_aggregates ::table ,
Inner ,
> ,
diesel ::expression ::operators ::Eq <
diesel ::expression ::nullable ::Nullable <
comment_aggregates ::columns ::comment_id ,
> ,
diesel ::expression ::nullable ::Nullable < comment ::columns ::id > ,
> ,
> ,
community_user_ban ::table ,
LeftOuter ,
> ,
diesel ::expression ::operators ::And <
diesel ::expression ::operators ::Eq <
community ::columns ::id ,
community_user_ban ::columns ::community_id ,
> ,
diesel ::expression ::operators ::Eq <
community_user_ban ::columns ::user_id ,
comment ::columns ::creator_id ,
> ,
> ,
> ,
community_follower ::table ,
LeftOuter ,
> ,
diesel ::expression ::operators ::And <
diesel ::expression ::operators ::Eq <
post ::columns ::community_id ,
community_follower ::columns ::community_id ,
> ,
diesel ::expression ::operators ::Eq <
community_follower ::columns ::user_id ,
diesel ::expression ::bound ::Bound < Integer , i32 > ,
> ,
> ,
> ,
comment_saved ::table ,
LeftOuter ,
> ,
diesel ::expression ::operators ::And <
diesel ::expression ::operators ::Eq <
comment ::columns ::id ,
comment_saved ::columns ::comment_id ,
> ,
diesel ::expression ::operators ::Eq <
comment_saved ::columns ::user_id ,
diesel ::expression ::bound ::Bound < Integer , i32 > ,
> ,
> ,
> ,
comment_like ::table ,
LeftOuter ,
> ,
diesel ::expression ::operators ::And <
diesel ::expression ::operators ::Eq < comment ::columns ::id , comment_like ::columns ::comment_id > ,
diesel ::expression ::operators ::Eq <
comment_like ::columns ::user_id ,
diesel ::expression ::bound ::Bound < Integer , i32 > ,
> ,
> ,
> ,
Pg ,
> ;
}
pub struct CommentQueryBuilder < ' a > {
conn : & ' a PgConnection ,
query : join_types ::BoxedCommentJoin < ' a > ,
listing_type : ListingType ,
sort : & ' a SortType ,
for_community_id : Option < i32 > ,
for_community_name : Option < String > ,
for_post_id : Option < i32 > ,
for_creator_id : Option < i32 > ,
for_recipient_id : Option < i32 > ,
community_id : Option < i32 > ,
community_name : Option < String > ,
post_id : Option < i32 > ,
creator_id : Option < i32 > ,
recipient_id : Option < i32 > ,
my_user_id : Option < i32 > ,
search_term : Option < String > ,
saved_only : bool ,
unread_only : bool ,
@ -424,71 +165,17 @@ pub struct CommentQueryBuilder<'a> {
}
impl < ' a > CommentQueryBuilder < ' a > {
pub fn create ( conn : & ' a PgConnection , my_user_id : Option < i32 > ) -> Self {
// The left join below will return None in this case
let user_id_join = my_user_id . unwrap_or ( - 1 ) ;
let query = comment ::table
. inner_join ( user_ ::table )
// recipient here
. left_join ( comment_alias_1 ::table . on ( comment_alias_1 ::id . nullable ( ) . eq ( comment ::parent_id ) ) )
. left_join ( user_alias_1 ::table . on ( user_alias_1 ::id . eq ( comment_alias_1 ::creator_id ) ) )
. inner_join ( post ::table )
. inner_join ( community ::table . on ( post ::community_id . eq ( community ::id ) ) )
. inner_join ( comment_aggregates ::table )
. left_join (
community_user_ban ::table . on (
community ::id
. eq ( community_user_ban ::community_id )
. and ( community_user_ban ::user_id . eq ( comment ::creator_id ) ) ,
) ,
)
. left_join (
community_follower ::table . on (
post ::community_id
. eq ( community_follower ::community_id )
. and ( community_follower ::user_id . eq ( user_id_join ) ) ,
) ,
)
. left_join (
comment_saved ::table . on (
comment ::id
. eq ( comment_saved ::comment_id )
. and ( comment_saved ::user_id . eq ( user_id_join ) ) ,
) ,
)
. left_join (
comment_like ::table . on (
comment ::id
. eq ( comment_like ::comment_id )
. and ( comment_like ::user_id . eq ( user_id_join ) ) ,
) ,
)
. select ( (
comment ::all_columns ,
User_ ::safe_columns_tuple ( ) ,
comment_alias_1 ::all_columns . nullable ( ) ,
UserAlias1 ::safe_columns_tuple ( ) . nullable ( ) ,
post ::all_columns ,
Community ::safe_columns_tuple ( ) ,
comment_aggregates ::all_columns ,
community_user_ban ::all_columns . nullable ( ) ,
community_follower ::all_columns . nullable ( ) ,
comment_saved ::all_columns . nullable ( ) ,
comment_like ::score . nullable ( ) ,
) )
. into_boxed ( ) ;
pub fn create ( conn : & ' a PgConnection ) -> Self {
CommentQueryBuilder {
conn ,
query ,
listing_type : ListingType ::All ,
sort : & SortType ::New ,
for_community_id : None ,
for_community_name : None ,
for_post_id : None ,
for_creator_id : None ,
for_recipient_id : None ,
community_id : None ,
community_name : None ,
post_id : None ,
creator_id : None ,
recipient_id : None ,
my_user_id : None ,
search_term : None ,
saved_only : false ,
unread_only : false ,
@ -507,28 +194,33 @@ impl<'a> CommentQueryBuilder<'a> {
self
}
pub fn for_post_id < T : MaybeOptional < i32 > > ( mut self , for_post_id : T ) -> Self {
self . for_post_id = for_post_id . get_optional ( ) ;
pub fn post_id < T : MaybeOptional < i32 > > ( mut self , post_id : T ) -> Self {
self . post_id = post_id . get_optional ( ) ;
self
}
pub fn creator_id < T : MaybeOptional < i32 > > ( mut self , creator_id : T ) -> Self {
self . creator_id = creator_id . get_optional ( ) ;
self
}
pub fn for_creator_id < T : MaybeOptional < i32 > > ( mut self , for_creator_id : T ) -> Self {
self . for_creator_id = for_creator_id . get_optional ( ) ;
pub fn recipient _id< T : MaybeOptional < i32 > > ( mut self , recipient _id: T ) -> Self {
self . recipient_id = recipient _id. get_optional ( ) ;
self
}
pub fn for_recipient _id< T : MaybeOptional < i32 > > ( mut self , for_recipient _id: T ) -> Self {
self . for_creator_id = for_recipient _id. get_optional ( ) ;
pub fn community _id< T : MaybeOptional < i32 > > ( mut self , community _id: T ) -> Self {
self . community_id = community _id. get_optional ( ) ;
self
}
pub fn for_co mmunit y_id< T : MaybeOptional < i32 > > ( mut self , for_co mmunit y_id: T ) -> Self {
self . for_co mmunit y_id = for_co mmunit y_id. get_optional ( ) ;
pub fn my_user _id< T : MaybeOptional < i32 > > ( mut self , my_user _id: T ) -> Self {
self . my_user _id = my_user _id. get_optional ( ) ;
self
}
pub fn for_ community_name< T : MaybeOptional < String > > ( mut self , for_ community_name: T ) -> Self {
self . for_ community_name = for_ community_name. get_optional ( ) ;
pub fn community_name< T : MaybeOptional < String > > ( mut self , community_name: T ) -> Self {
self . community_name = community_name. get_optional ( ) ;
self
}
@ -560,13 +252,65 @@ impl<'a> CommentQueryBuilder<'a> {
pub fn list ( self ) -> Result < Vec < CommentView > , Error > {
use diesel ::dsl ::* ;
let mut query = self . query ;
// The left join below will return None in this case
let user_id_join = self . my_user_id . unwrap_or ( - 1 ) ;
let mut query = comment ::table
. inner_join ( user_ ::table )
// recipient here
. left_join ( comment_alias_1 ::table . on ( comment_alias_1 ::id . nullable ( ) . eq ( comment ::parent_id ) ) )
. left_join ( user_alias_1 ::table . on ( user_alias_1 ::id . eq ( comment_alias_1 ::creator_id ) ) )
. inner_join ( post ::table )
. inner_join ( community ::table . on ( post ::community_id . eq ( community ::id ) ) )
. inner_join ( comment_aggregates ::table )
. left_join (
community_user_ban ::table . on (
community ::id
. eq ( community_user_ban ::community_id )
. and ( community_user_ban ::user_id . eq ( comment ::creator_id ) ) ,
) ,
)
. left_join (
community_follower ::table . on (
post ::community_id
. eq ( community_follower ::community_id )
. and ( community_follower ::user_id . eq ( user_id_join ) ) ,
) ,
)
. left_join (
comment_saved ::table . on (
comment ::id
. eq ( comment_saved ::comment_id )
. and ( comment_saved ::user_id . eq ( user_id_join ) ) ,
) ,
)
. left_join (
comment_like ::table . on (
comment ::id
. eq ( comment_like ::comment_id )
. and ( comment_like ::user_id . eq ( user_id_join ) ) ,
) ,
)
. select ( (
comment ::all_columns ,
User_ ::safe_columns_tuple ( ) ,
comment_alias_1 ::all_columns . nullable ( ) ,
UserAlias1 ::safe_columns_tuple ( ) . nullable ( ) ,
post ::all_columns ,
Community ::safe_columns_tuple ( ) ,
comment_aggregates ::all_columns ,
community_user_ban ::all_columns . nullable ( ) ,
community_follower ::all_columns . nullable ( ) ,
comment_saved ::all_columns . nullable ( ) ,
comment_like ::score . nullable ( ) ,
) )
. into_boxed ( ) ;
// The replies
if let Some ( for_recipient_id ) = self . for_recipient_id {
if let Some ( recipient_id) = self . recipient_id {
query = query
// TODO needs lots of testing
. filter ( user_alias_1 ::id . eq ( for_recipient_id ) )
. filter ( user_alias_1 ::id . eq ( recipient_id) )
. filter ( comment ::deleted . eq ( false ) )
. filter ( comment ::removed . eq ( false ) ) ;
}
@ -575,22 +319,22 @@ impl<'a> CommentQueryBuilder<'a> {
query = query . filter ( comment ::read . eq ( false ) ) ;
}
if let Some ( for_ creator_id) = self . for_ creator_id {
query = query . filter ( comment ::creator_id . eq ( for_ creator_id) ) ;
if let Some ( creator_id) = self . creator_id {
query = query . filter ( comment ::creator_id . eq ( creator_id) ) ;
} ;
if let Some ( for_ community_id) = self . for_ community_id {
query = query . filter ( post ::community_id . eq ( for_ community_id) ) ;
if let Some ( community_id) = self . community_id {
query = query . filter ( post ::community_id . eq ( community_id) ) ;
}
if let Some ( for_ community_name) = self . for_ community_name {
if let Some ( community_name) = self . community_name {
query = query
. filter ( community ::name . eq ( for_ community_name) )
. filter ( community ::name . eq ( community_name) )
. filter ( comment ::local . eq ( true ) ) ;
}
if let Some ( for_ post_id) = self . for_ post_id {
query = query . filter ( comment ::post_id . eq ( for_ post_id) ) ;
if let Some ( post_id) = self . post_id {
query = query . filter ( comment ::post_id . eq ( post_id) ) ;
} ;
if let Some ( search_term ) = self . search_term {
@ -861,13 +605,14 @@ mod tests {
let mut expected_comment_view_with_user = expected_comment_view_no_user . to_owned ( ) ;
expected_comment_view_with_user . my_vote = Some ( 1 ) ;
let read_comment_views_no_user = CommentQueryBuilder ::create ( & conn , None )
. for_ post_id( inserted_post . id )
let read_comment_views_no_user = CommentQueryBuilder ::create ( & conn )
. post_id( inserted_post . id )
. list ( )
. unwrap ( ) ;
let read_comment_views_with_user = CommentQueryBuilder ::create ( & conn , Some ( inserted_user . id ) )
. for_post_id ( inserted_post . id )
let read_comment_views_with_user = CommentQueryBuilder ::create ( & conn )
. post_id ( inserted_post . id )
. my_user_id ( inserted_user . id )
. list ( )
. unwrap ( ) ;