|
|
|
@ -35,7 +35,7 @@ use lemmy_db_schema::{
|
|
|
|
|
person_block,
|
|
|
|
|
post,
|
|
|
|
|
},
|
|
|
|
|
source::local_user::LocalUser,
|
|
|
|
|
source::{local_user::LocalUser, site::Site},
|
|
|
|
|
utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn},
|
|
|
|
|
CommentSortType,
|
|
|
|
|
ListingType,
|
|
|
|
@ -43,7 +43,7 @@ use lemmy_db_schema::{
|
|
|
|
|
|
|
|
|
|
fn queries<'a>() -> Queries<
|
|
|
|
|
impl ReadFn<'a, CommentView, (CommentId, Option<&'a LocalUser>)>,
|
|
|
|
|
impl ListFn<'a, CommentView, CommentQuery<'a>>,
|
|
|
|
|
impl ListFn<'a, CommentView, (CommentQuery<'a>, &'a Site)>,
|
|
|
|
|
> {
|
|
|
|
|
let is_creator_banned_from_community = exists(
|
|
|
|
|
community_person_ban::table.filter(
|
|
|
|
@ -182,7 +182,7 @@ fn queries<'a>() -> Queries<
|
|
|
|
|
query.first(&mut conn).await
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let list = move |mut conn: DbConn<'a>, options: CommentQuery<'a>| async move {
|
|
|
|
|
let list = move |mut conn: DbConn<'a>, (options, site): (CommentQuery<'a>, &'a Site)| async move {
|
|
|
|
|
// The left join below will return None in this case
|
|
|
|
|
let person_id_join = options.local_user.person_id().unwrap_or(PersonId(-1));
|
|
|
|
|
let local_user_id_join = options
|
|
|
|
@ -295,6 +295,12 @@ fn queries<'a>() -> Queries<
|
|
|
|
|
query = query.filter(not(is_creator_blocked(person_id_join)));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if !options.local_user.show_nsfw(site) {
|
|
|
|
|
query = query
|
|
|
|
|
.filter(post::nsfw.eq(false))
|
|
|
|
|
.filter(community::nsfw.eq(false));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
query = options.local_user.visible_communities_only(query);
|
|
|
|
|
|
|
|
|
|
// A Max depth given means its a tree fetch
|
|
|
|
@ -398,10 +404,10 @@ pub struct CommentQuery<'a> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a> CommentQuery<'a> {
|
|
|
|
|
pub async fn list(self, pool: &mut DbPool<'_>) -> Result<Vec<CommentView>, Error> {
|
|
|
|
|
pub async fn list(self, site: &Site, pool: &mut DbPool<'_>) -> Result<Vec<CommentView>, Error> {
|
|
|
|
|
Ok(
|
|
|
|
|
queries()
|
|
|
|
|
.list(pool, self)
|
|
|
|
|
.list(pool, (self, site))
|
|
|
|
|
.await?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|mut c| {
|
|
|
|
@ -455,7 +461,8 @@ mod tests {
|
|
|
|
|
local_user_vote_display_mode::LocalUserVoteDisplayMode,
|
|
|
|
|
person::{Person, PersonInsertForm},
|
|
|
|
|
person_block::{PersonBlock, PersonBlockForm},
|
|
|
|
|
post::{Post, PostInsertForm},
|
|
|
|
|
post::{Post, PostInsertForm, PostUpdateForm},
|
|
|
|
|
site::{Site, SiteInsertForm},
|
|
|
|
|
},
|
|
|
|
|
traits::{Bannable, Blockable, Crud, Joinable, Likeable, Saveable},
|
|
|
|
|
utils::{build_db_pool_for_tests, RANK_DEFAULT},
|
|
|
|
@ -475,6 +482,7 @@ mod tests {
|
|
|
|
|
timmy_local_user_view: LocalUserView,
|
|
|
|
|
inserted_sara_person: Person,
|
|
|
|
|
inserted_community: Community,
|
|
|
|
|
site: Site,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn init_data(pool: &mut DbPool<'_>) -> LemmyResult<Data> {
|
|
|
|
@ -611,6 +619,11 @@ mod tests {
|
|
|
|
|
person: inserted_timmy_person.clone(),
|
|
|
|
|
counts: Default::default(),
|
|
|
|
|
};
|
|
|
|
|
let site_form = SiteInsertForm::builder()
|
|
|
|
|
.name("test site".to_string())
|
|
|
|
|
.instance_id(inserted_instance.id)
|
|
|
|
|
.build();
|
|
|
|
|
let site = Site::create(pool, &site_form).await?;
|
|
|
|
|
Ok(Data {
|
|
|
|
|
inserted_instance,
|
|
|
|
|
inserted_comment_0,
|
|
|
|
@ -620,6 +633,7 @@ mod tests {
|
|
|
|
|
timmy_local_user_view,
|
|
|
|
|
inserted_sara_person,
|
|
|
|
|
inserted_community,
|
|
|
|
|
site,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -640,7 +654,7 @@ mod tests {
|
|
|
|
|
post_id: (Some(data.inserted_post.id)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
@ -654,7 +668,7 @@ mod tests {
|
|
|
|
|
local_user: (Some(&data.timmy_local_user_view.local_user)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
@ -706,7 +720,7 @@ mod tests {
|
|
|
|
|
liked_only: Some(true),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(|c| c.comment.content)
|
|
|
|
@ -722,7 +736,7 @@ mod tests {
|
|
|
|
|
disliked_only: Some(true),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
assert!(read_disliked_comment_views.is_empty());
|
|
|
|
@ -743,7 +757,7 @@ mod tests {
|
|
|
|
|
parent_path: (Some(top_path)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
let child_path = data.inserted_comment_1.path.clone();
|
|
|
|
@ -752,7 +766,7 @@ mod tests {
|
|
|
|
|
parent_path: (Some(child_path)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
// Make sure the comment parent-limited fetch is correct
|
|
|
|
@ -772,7 +786,7 @@ mod tests {
|
|
|
|
|
max_depth: (Some(1)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
// Make sure a depth limited one only has the top comment
|
|
|
|
@ -790,7 +804,7 @@ mod tests {
|
|
|
|
|
sort: (Some(CommentSortType::New)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
// Make sure a depth limited one, and given child comment 1, has 3
|
|
|
|
@ -816,7 +830,7 @@ mod tests {
|
|
|
|
|
local_user: (Some(&data.timmy_local_user_view.local_user)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
assert_length!(5, all_languages);
|
|
|
|
|
|
|
|
|
@ -834,7 +848,7 @@ mod tests {
|
|
|
|
|
local_user: (Some(&data.timmy_local_user_view.local_user)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
assert_length!(2, finnish_comments);
|
|
|
|
|
let finnish_comment = finnish_comments
|
|
|
|
@ -857,7 +871,7 @@ mod tests {
|
|
|
|
|
local_user: (Some(&data.timmy_local_user_view.local_user)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
assert_length!(1, undetermined_comment);
|
|
|
|
|
|
|
|
|
@ -881,7 +895,7 @@ mod tests {
|
|
|
|
|
post_id: Some(data.inserted_comment_2.post_id),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
assert_eq!(comments[0].comment.id, data.inserted_comment_2.id);
|
|
|
|
|
assert!(comments[0].comment.distinguished);
|
|
|
|
@ -910,7 +924,7 @@ mod tests {
|
|
|
|
|
sort: (Some(CommentSortType::Old)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
assert_eq!(comments[1].creator.name, "sara");
|
|
|
|
@ -931,7 +945,7 @@ mod tests {
|
|
|
|
|
sort: (Some(CommentSortType::Old)),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
// Timmy is an admin, and make sure that field is true
|
|
|
|
@ -971,7 +985,7 @@ mod tests {
|
|
|
|
|
saved_only: Some(true),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
// There should only be two comments
|
|
|
|
@ -1001,6 +1015,7 @@ mod tests {
|
|
|
|
|
LocalUser::delete(pool, data.timmy_local_user_view.local_user.id).await?;
|
|
|
|
|
Person::delete(pool, data.inserted_sara_person.id).await?;
|
|
|
|
|
Instance::delete(pool, data.inserted_instance.id).await?;
|
|
|
|
|
Site::delete(pool, data.site.id).await?;
|
|
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
@ -1139,7 +1154,7 @@ mod tests {
|
|
|
|
|
let unauthenticated_query = CommentQuery {
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
assert_eq!(0, unauthenticated_query.len());
|
|
|
|
|
|
|
|
|
@ -1147,7 +1162,7 @@ mod tests {
|
|
|
|
|
local_user: Some(&data.timmy_local_user_view.local_user),
|
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.list(pool)
|
|
|
|
|
.list(&data.site, pool)
|
|
|
|
|
.await?;
|
|
|
|
|
assert_eq!(5, authenticated_query.len());
|
|
|
|
|
|
|
|
|
@ -1225,4 +1240,33 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
cleanup(data, pool).await
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
#[serial]
|
|
|
|
|
async fn comment_listings_hide_nsfw() -> LemmyResult<()> {
|
|
|
|
|
let pool = &build_db_pool_for_tests().await;
|
|
|
|
|
let pool = &mut pool.into();
|
|
|
|
|
let data = init_data(pool).await?;
|
|
|
|
|
|
|
|
|
|
// Mark a post as nsfw
|
|
|
|
|
let update_form = PostUpdateForm {
|
|
|
|
|
nsfw: Some(true),
|
|
|
|
|
..Default::default()
|
|
|
|
|
};
|
|
|
|
|
Post::update(pool, data.inserted_post.id, &update_form).await?;
|
|
|
|
|
|
|
|
|
|
// Make sure comments of this post are not returned
|
|
|
|
|
let comments = CommentQuery::default().list(&data.site, pool).await?;
|
|
|
|
|
assert_eq!(0, comments.len());
|
|
|
|
|
|
|
|
|
|
// Mark site as nsfw
|
|
|
|
|
let mut site = data.site.clone();
|
|
|
|
|
site.content_warning = Some("nsfw".to_string());
|
|
|
|
|
|
|
|
|
|
// Now comments of nsfw post are returned
|
|
|
|
|
let comments = CommentQuery::default().list(&site, pool).await?;
|
|
|
|
|
assert_eq!(6, comments.len());
|
|
|
|
|
|
|
|
|
|
cleanup(data, pool).await
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|