@ -18,16 +18,19 @@ use lemmy_db_schema::{
comment_like ,
comment_like ,
comment_saved ,
comment_saved ,
community ,
community ,
community_block ,
community_follower ,
community_follower ,
community_person_ban ,
community_person_ban ,
person ,
person ,
person_alias_1 ,
person_alias_1 ,
person_block ,
post ,
post ,
} ,
} ,
source ::{
source ::{
comment ::{ Comment , CommentAlias1 , CommentSaved } ,
comment ::{ Comment , CommentAlias1 , CommentSaved } ,
community ::{ Community , CommunityFollower , CommunityPersonBan , CommunitySafe } ,
community ::{ Community , CommunityFollower , CommunityPersonBan , CommunitySafe } ,
person ::{ Person , PersonAlias1 , PersonSafe , PersonSafeAlias1 } ,
person ::{ Person , PersonAlias1 , PersonSafe , PersonSafeAlias1 } ,
person_block ::PersonBlock ,
post ::Post ,
post ::Post ,
} ,
} ,
CommentId ,
CommentId ,
@ -49,6 +52,7 @@ pub struct CommentView {
pub creator_banned_from_community : bool , // Left Join to CommunityPersonBan
pub creator_banned_from_community : bool , // Left Join to CommunityPersonBan
pub subscribed : bool , // Left join to CommunityFollower
pub subscribed : bool , // Left join to CommunityFollower
pub saved : bool , // Left join to CommentSaved
pub saved : bool , // Left join to CommentSaved
pub creator_blocked : bool , // Left join to PersonBlock
pub my_vote : Option < i16 > , // Left join to CommentLike
pub my_vote : Option < i16 > , // Left join to CommentLike
}
}
@ -63,6 +67,7 @@ type CommentViewTuple = (
Option < CommunityPersonBan > ,
Option < CommunityPersonBan > ,
Option < CommunityFollower > ,
Option < CommunityFollower > ,
Option < CommentSaved > ,
Option < CommentSaved > ,
Option < PersonBlock > ,
Option < i16 > ,
Option < i16 > ,
) ;
) ;
@ -86,6 +91,7 @@ impl CommentView {
creator_banned_from_community ,
creator_banned_from_community ,
subscribed ,
subscribed ,
saved ,
saved ,
creator_blocked ,
comment_like ,
comment_like ,
) = comment ::table
) = comment ::table
. find ( comment_id )
. find ( comment_id )
@ -117,6 +123,13 @@ impl CommentView {
. and ( comment_saved ::person_id . eq ( person_id_join ) ) ,
. and ( comment_saved ::person_id . eq ( person_id_join ) ) ,
) ,
) ,
)
)
. left_join (
person_block ::table . on (
comment ::creator_id
. eq ( person_block ::target_id )
. and ( person_block ::person_id . eq ( person_id_join ) ) ,
) ,
)
. left_join (
. left_join (
comment_like ::table . on (
comment_like ::table . on (
comment ::id
comment ::id
@ -135,6 +148,7 @@ impl CommentView {
community_person_ban ::all_columns . nullable ( ) ,
community_person_ban ::all_columns . nullable ( ) ,
community_follower ::all_columns . nullable ( ) ,
community_follower ::all_columns . nullable ( ) ,
comment_saved ::all_columns . nullable ( ) ,
comment_saved ::all_columns . nullable ( ) ,
person_block ::all_columns . nullable ( ) ,
comment_like ::score . nullable ( ) ,
comment_like ::score . nullable ( ) ,
) )
) )
. first ::< CommentViewTuple > ( conn ) ? ;
. first ::< CommentViewTuple > ( conn ) ? ;
@ -157,6 +171,7 @@ impl CommentView {
creator_banned_from_community : creator_banned_from_community . is_some ( ) ,
creator_banned_from_community : creator_banned_from_community . is_some ( ) ,
subscribed : subscribed . is_some ( ) ,
subscribed : subscribed . is_some ( ) ,
saved : saved . is_some ( ) ,
saved : saved . is_some ( ) ,
creator_blocked : creator_blocked . is_some ( ) ,
my_vote ,
my_vote ,
} )
} )
}
}
@ -315,6 +330,20 @@ impl<'a> CommentQueryBuilder<'a> {
. and ( comment_saved ::person_id . eq ( person_id_join ) ) ,
. and ( comment_saved ::person_id . eq ( person_id_join ) ) ,
) ,
) ,
)
)
. left_join (
person_block ::table . on (
comment ::creator_id
. eq ( person_block ::target_id )
. and ( person_block ::person_id . eq ( person_id_join ) ) ,
) ,
)
. left_join (
community_block ::table . on (
community ::id
. eq ( community_block ::community_id )
. and ( community_block ::person_id . eq ( person_id_join ) ) ,
) ,
)
. left_join (
. left_join (
comment_like ::table . on (
comment_like ::table . on (
comment ::id
comment ::id
@ -333,6 +362,7 @@ impl<'a> CommentQueryBuilder<'a> {
community_person_ban ::all_columns . nullable ( ) ,
community_person_ban ::all_columns . nullable ( ) ,
community_follower ::all_columns . nullable ( ) ,
community_follower ::all_columns . nullable ( ) ,
comment_saved ::all_columns . nullable ( ) ,
comment_saved ::all_columns . nullable ( ) ,
person_block ::all_columns . nullable ( ) ,
comment_like ::score . nullable ( ) ,
comment_like ::score . nullable ( ) ,
) )
) )
. into_boxed ( ) ;
. into_boxed ( ) ;
@ -413,6 +443,12 @@ impl<'a> CommentQueryBuilder<'a> {
. order_by ( comment_aggregates ::score . desc ( ) ) ,
. order_by ( comment_aggregates ::score . desc ( ) ) ,
} ;
} ;
// Don't show blocked communities or persons
if self . my_person_id . is_some ( ) {
query = query . filter ( community_block ::person_id . is_null ( ) ) ;
query = query . filter ( person_block ::person_id . is_null ( ) ) ;
}
let ( limit , offset ) = limit_and_offset ( self . page , self . limit ) ;
let ( limit , offset ) = limit_and_offset ( self . page , self . limit ) ;
// Note: deleted and removed comments are done on the front side
// Note: deleted and removed comments are done on the front side
@ -440,7 +476,8 @@ impl ViewToVec for CommentView {
creator_banned_from_community : a . 7. is_some ( ) ,
creator_banned_from_community : a . 7. is_some ( ) ,
subscribed : a . 8. is_some ( ) ,
subscribed : a . 8. is_some ( ) ,
saved : a . 9. is_some ( ) ,
saved : a . 9. is_some ( ) ,
my_vote : a . 10 ,
creator_blocked : a . 10. is_some ( ) ,
my_vote : a . 11 ,
} )
} )
. collect ::< Vec < Self > > ( )
. collect ::< Vec < Self > > ( )
}
}
@ -452,10 +489,17 @@ mod tests {
use lemmy_db_queries ::{
use lemmy_db_queries ::{
aggregates ::comment_aggregates ::CommentAggregates ,
aggregates ::comment_aggregates ::CommentAggregates ,
establish_unpooled_connection ,
establish_unpooled_connection ,
Blockable ,
Crud ,
Crud ,
Likeable ,
Likeable ,
} ;
} ;
use lemmy_db_schema ::source ::{ comment ::* , community ::* , person ::* , post ::* } ;
use lemmy_db_schema ::source ::{
comment ::* ,
community ::* ,
person ::* ,
person_block ::PersonBlockForm ,
post ::* ,
} ;
use serial_test ::serial ;
use serial_test ::serial ;
#[ test ]
#[ test ]
@ -470,6 +514,13 @@ mod tests {
let inserted_person = Person ::create ( & conn , & new_person ) . unwrap ( ) ;
let inserted_person = Person ::create ( & conn , & new_person ) . unwrap ( ) ;
let new_person_2 = PersonForm {
name : "sara" . into ( ) ,
.. PersonForm ::default ( )
} ;
let inserted_person_2 = Person ::create ( & conn , & new_person_2 ) . unwrap ( ) ;
let new_community = CommunityForm {
let new_community = CommunityForm {
name : "test community 5" . to_string ( ) ,
name : "test community 5" . to_string ( ) ,
title : "nada" . to_owned ( ) ,
title : "nada" . to_owned ( ) ,
@ -496,6 +547,32 @@ mod tests {
let inserted_comment = Comment ::create ( & conn , & comment_form ) . unwrap ( ) ;
let inserted_comment = Comment ::create ( & conn , & comment_form ) . unwrap ( ) ;
let comment_form_2 = CommentForm {
content : "A test blocked comment" . into ( ) ,
creator_id : inserted_person_2 . id ,
post_id : inserted_post . id ,
parent_id : Some ( inserted_comment . id ) ,
.. CommentForm ::default ( )
} ;
let inserted_comment_2 = Comment ::create ( & conn , & comment_form_2 ) . unwrap ( ) ;
let timmy_blocks_sara_form = PersonBlockForm {
person_id : inserted_person . id ,
target_id : inserted_person_2 . id ,
} ;
let inserted_block = PersonBlock ::block ( & conn , & timmy_blocks_sara_form ) . unwrap ( ) ;
let expected_block = PersonBlock {
id : inserted_block . id ,
person_id : inserted_person . id ,
target_id : inserted_person_2 . id ,
published : inserted_block . published ,
} ;
assert_eq! ( expected_block , inserted_block ) ;
let comment_like_form = CommentLikeForm {
let comment_like_form = CommentLikeForm {
comment_id : inserted_comment . id ,
comment_id : inserted_comment . id ,
post_id : inserted_post . id ,
post_id : inserted_post . id ,
@ -512,6 +589,7 @@ mod tests {
my_vote : None ,
my_vote : None ,
subscribed : false ,
subscribed : false ,
saved : false ,
saved : false ,
creator_blocked : false ,
comment : Comment {
comment : Comment {
id : inserted_comment . id ,
id : inserted_comment . id ,
content : "A test comment 32" . into ( ) ,
content : "A test comment 32" . into ( ) ,
@ -606,20 +684,32 @@ mod tests {
. list ( )
. list ( )
. unwrap ( ) ;
. unwrap ( ) ;
let read_comment_from_blocked_person =
CommentView ::read ( & conn , inserted_comment_2 . id , Some ( inserted_person . id ) ) . unwrap ( ) ;
let like_removed = CommentLike ::remove ( & conn , inserted_person . id , inserted_comment . id ) . unwrap ( ) ;
let like_removed = CommentLike ::remove ( & conn , inserted_person . id , inserted_comment . id ) . unwrap ( ) ;
let num_deleted = Comment ::delete ( & conn , inserted_comment . id ) . unwrap ( ) ;
let num_deleted = Comment ::delete ( & conn , inserted_comment . id ) . unwrap ( ) ;
Comment ::delete ( & conn , inserted_comment_2 . id ) . unwrap ( ) ;
Post ::delete ( & conn , inserted_post . id ) . unwrap ( ) ;
Post ::delete ( & conn , inserted_post . id ) . unwrap ( ) ;
Community ::delete ( & conn , inserted_community . id ) . unwrap ( ) ;
Community ::delete ( & conn , inserted_community . id ) . unwrap ( ) ;
Person ::delete ( & conn , inserted_person . id ) . unwrap ( ) ;
Person ::delete ( & conn , inserted_person . id ) . unwrap ( ) ;
Person ::delete ( & conn , inserted_person_2 . id ) . unwrap ( ) ;
// Make sure its 1, not showing the blocked comment
assert_eq! ( 1 , read_comment_views_with_person . len ( ) ) ;
assert_eq! (
assert_eq! (
expected_comment_view_no_person ,
expected_comment_view_no_person ,
read_comment_views_no_person [ 0 ]
read_comment_views_no_person [ 1 ]
) ;
) ;
assert_eq! (
assert_eq! (
expected_comment_view_with_person ,
expected_comment_view_with_person ,
read_comment_views_with_person [ 0 ]
read_comment_views_with_person [ 0 ]
) ;
) ;
// Make sure block set the creator blocked
assert_eq! ( true , read_comment_from_blocked_person . creator_blocked ) ;
assert_eq! ( 1 , num_deleted ) ;
assert_eq! ( 1 , num_deleted ) ;
assert_eq! ( 1 , like_removed ) ;
assert_eq! ( 1 , like_removed ) ;
}
}