From a6dc6804aabbbd8966f802bb1a78a4dc20246955 Mon Sep 17 00:00:00 2001 From: Nutomic Date: Fri, 19 Aug 2022 14:27:39 +0000 Subject: [PATCH] Pass LocalUser to PostQuery etc, instead of separate params (#2413) --- crates/api/src/site/search.rs | 22 ++--- crates/api_crud/src/comment/list.rs | 10 +-- crates/api_crud/src/community/list.rs | 10 +-- crates/api_crud/src/user/read.rs | 10 +-- crates/db_views/src/comment_view.rs | 29 ++++-- crates/db_views/src/post_view.rs | 98 ++++++++++----------- crates/db_views_actor/src/community_view.rs | 10 +-- crates/routes/src/feeds.rs | 4 +- 8 files changed, 90 insertions(+), 103 deletions(-) diff --git a/crates/api/src/site/search.rs b/crates/api/src/site/search.rs index bd04b053..4c27ef89 100644 --- a/crates/api/src/site/search.rs +++ b/crates/api/src/site/search.rs @@ -31,14 +31,10 @@ impl Perform for Search { let local_user_view = get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret()) .await?; - check_private_instance(&local_user_view, context.pool()).await?; - let show_bot_accounts = local_user_view - .as_ref() - .map(|t| t.local_user.show_bot_accounts); - let person_id = local_user_view.as_ref().map(|u| u.person.id); + let local_user = local_user_view.map(|l| l.local_user); let mut posts = Vec::new(); let mut comments = Vec::new(); @@ -73,7 +69,7 @@ impl Perform for Search { .community_id(community_id) .community_actor_id(community_actor_id) .creator_id(creator_id) - .my_person_id(person_id) + .local_user(local_user.as_ref()) .search_term(Some(q)) .page(page) .limit(limit) @@ -89,11 +85,10 @@ impl Perform for Search { .sort(sort.map(post_to_comment_sort_type)) .listing_type(listing_type) .search_term(Some(q)) - .show_bot_accounts(show_bot_accounts) .community_id(community_id) .community_actor_id(community_actor_id) .creator_id(creator_id) - .my_person_id(person_id) + .local_user(local_user.as_ref()) .page(page) .limit(limit) .build() @@ -108,7 +103,7 @@ impl Perform for Search { .sort(sort) .listing_type(listing_type) .search_term(Some(q)) - .my_person_id(person_id) + .local_user(local_user.as_ref()) .page(page) .limit(limit) .build() @@ -135,6 +130,7 @@ impl Perform for Search { data.community_id.is_some() || data.community_name.is_some() || data.creator_id.is_some(); let community_actor_id_2 = community_actor_id.to_owned(); + let local_user_ = local_user.clone(); posts = blocking(context.pool(), move |conn| { PostQuery::builder() .conn(conn) @@ -143,7 +139,7 @@ impl Perform for Search { .community_id(community_id) .community_actor_id(community_actor_id_2) .creator_id(creator_id) - .my_person_id(person_id) + .local_user(local_user_.as_ref()) .search_term(Some(q)) .page(page) .limit(limit) @@ -155,17 +151,17 @@ impl Perform for Search { let q = data.q.to_owned(); let community_actor_id = community_actor_id.to_owned(); + let local_user_ = local_user.clone(); comments = blocking(context.pool(), move |conn| { CommentQuery::builder() .conn(conn) .sort(sort.map(post_to_comment_sort_type)) .listing_type(listing_type) .search_term(Some(q)) - .show_bot_accounts(show_bot_accounts) .community_id(community_id) .community_actor_id(community_actor_id) .creator_id(creator_id) - .my_person_id(person_id) + .local_user(local_user_.as_ref()) .page(page) .limit(limit) .build() @@ -184,7 +180,7 @@ impl Perform for Search { .sort(sort) .listing_type(listing_type) .search_term(Some(q)) - .my_person_id(person_id) + .local_user(local_user.as_ref()) .page(page) .limit(limit) .build() diff --git a/crates/api_crud/src/comment/list.rs b/crates/api_crud/src/comment/list.rs index ca40d99c..e7632cba 100644 --- a/crates/api_crud/src/comment/list.rs +++ b/crates/api_crud/src/comment/list.rs @@ -32,14 +32,8 @@ impl PerformCrud for GetComments { let local_user_view = get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret()) .await?; - check_private_instance(&local_user_view, context.pool()).await?; - let show_bot_accounts = local_user_view - .as_ref() - .map(|t| t.local_user.show_bot_accounts); - let person_id = local_user_view.map(|u| u.person.id); - let community_id = data.community_id; let listing_type = listing_type_with_site_default(data.type_, context.pool()).await?; @@ -70,6 +64,7 @@ impl PerformCrud for GetComments { }; let post_id = data.post_id; + let local_user = local_user_view.map(|l| l.local_user); let mut comments = blocking(context.pool(), move |conn| { CommentQuery::builder() .conn(conn) @@ -81,8 +76,7 @@ impl PerformCrud for GetComments { .community_actor_id(community_actor_id) .parent_path(parent_path) .post_id(post_id) - .my_person_id(person_id) - .show_bot_accounts(show_bot_accounts) + .local_user(local_user.as_ref()) .page(page) .limit(limit) .build() diff --git a/crates/api_crud/src/community/list.rs b/crates/api_crud/src/community/list.rs index c57e9eaa..1aa4585f 100644 --- a/crates/api_crud/src/community/list.rs +++ b/crates/api_crud/src/community/list.rs @@ -28,23 +28,17 @@ impl PerformCrud for ListCommunities { let person_id = local_user_view.to_owned().map(|l| l.person.id); - // Don't show NSFW by default - let show_nsfw = match &local_user_view { - Some(uv) => uv.local_user.show_nsfw, - None => false, - }; - let sort = data.sort; let listing_type = data.type_; let page = data.page; let limit = data.limit; + let local_user = local_user_view.map(|l| l.local_user); let mut communities = blocking(context.pool(), move |conn| { CommunityQuery::builder() .conn(conn) .listing_type(listing_type) .sort(sort) - .show_nsfw(Some(show_nsfw)) - .my_person_id(person_id) + .local_user(local_user.as_ref()) .page(page) .limit(limit) .build() diff --git a/crates/api_crud/src/user/read.rs b/crates/api_crud/src/user/read.rs index 15630924..820905d8 100644 --- a/crates/api_crud/src/user/read.rs +++ b/crates/api_crud/src/user/read.rs @@ -31,13 +31,8 @@ impl PerformCrud for GetPersonDetails { let local_user_view = get_local_user_view_from_jwt_opt(data.auth.as_ref(), context.pool(), context.secret()) .await?; - check_private_instance(&local_user_view, context.pool()).await?; - let show_bot_accounts = local_user_view - .as_ref() - .map(|t| t.local_user.show_bot_accounts); - let person_details_id = match data.person_id { Some(id) => id, None => { @@ -76,11 +71,10 @@ impl PerformCrud for GetPersonDetails { .page(page) .limit(limit); - let person_id = local_user_view.map(|uv| uv.person.id); + let local_user = local_user_view.map(|l| l.local_user); let comments_query = CommentQuery::builder() .conn(conn) - .my_person_id(person_id) - .show_bot_accounts(show_bot_accounts) + .local_user(local_user.as_ref()) .sort(sort.map(post_to_comment_sort_type)) .saved_only(saved_only) .community_id(community_id) diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 2516b45e..a7c96b17 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -20,6 +20,7 @@ use lemmy_db_schema::{ source::{ comment::{Comment, CommentSaved}, community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe}, + local_user::LocalUser, person::{Person, PersonSafe}, person_block::PersonBlock, post::Post, @@ -159,10 +160,9 @@ pub struct CommentQuery<'a> { post_id: Option, parent_path: Option, creator_id: Option, - my_person_id: Option, + local_user: Option<&'a LocalUser>, search_term: Option, saved_only: Option, - show_bot_accounts: Option, page: Option, limit: Option, max_depth: Option, @@ -173,7 +173,7 @@ impl<'a> CommentQuery<'a> { use diesel::dsl::*; // The left join below will return None in this case - let person_id_join = self.my_person_id.unwrap_or(PersonId(-1)); + let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1)); let mut query = comment::table .inner_join(person::table) @@ -291,12 +291,12 @@ impl<'a> CommentQuery<'a> { query = query.filter(comment_saved::id.is_not_null()); } - if !self.show_bot_accounts.unwrap_or(true) { + if !self.local_user.map(|l| l.show_bot_accounts).unwrap_or(true) { query = query.filter(person::bot_account.eq(false)); }; // Don't show blocked communities or persons - if self.my_person_id.is_some() { + if self.local_user.is_some() { query = query.filter(community_block::person_id.is_null()); query = query.filter(person_block::person_id.is_null()); } @@ -373,7 +373,14 @@ mod tests { use crate::comment_view::*; use lemmy_db_schema::{ aggregates::structs::CommentAggregates, - source::{comment::*, community::*, person::*, person_block::PersonBlockForm, post::*}, + source::{ + comment::*, + community::*, + local_user::LocalUserForm, + person::*, + person_block::PersonBlockForm, + post::*, + }, traits::{Blockable, Crud, Likeable}, utils::establish_unpooled_connection, SubscribedType, @@ -390,15 +397,19 @@ mod tests { public_key: Some("pubkey".to_string()), ..PersonForm::default() }; - let inserted_person = Person::create(&conn, &new_person).unwrap(); + let local_user_form = LocalUserForm { + person_id: Some(inserted_person.id), + password_encrypted: Some("".to_string()), + ..Default::default() + }; + let inserted_local_user = LocalUser::create(&conn, &local_user_form).unwrap(); let new_person_2 = PersonForm { name: "sara".into(), public_key: Some("pubkey".to_string()), ..PersonForm::default() }; - let inserted_person_2 = Person::create(&conn, &new_person_2).unwrap(); let new_community = CommunityForm { @@ -622,7 +633,7 @@ mod tests { let read_comment_views_with_person = CommentQuery::builder() .conn(&conn) .post_id(Some(inserted_post.id)) - .my_person_id(Some(inserted_person.id)) + .local_user(Some(&inserted_local_user)) .build() .list() .unwrap(); diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 761925a9..6d0bcb5d 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -21,6 +21,7 @@ use lemmy_db_schema::{ source::{ community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe}, language::Language, + local_user::LocalUser, person::{Person, PersonSafe}, person_block::PersonBlock, post::{Post, PostRead, PostSaved}, @@ -169,13 +170,9 @@ pub struct PostQuery<'a> { creator_id: Option, community_id: Option, community_actor_id: Option, - my_person_id: Option, - my_local_user_id: Option, + local_user: Option<&'a LocalUser>, search_term: Option, url_search: Option, - show_nsfw: Option, - show_bot_accounts: Option, - show_read_posts: Option, saved_only: Option, page: Option, limit: Option, @@ -186,8 +183,8 @@ impl<'a> PostQuery<'a> { use diesel::dsl::*; // The left join below will return None in this case - let person_id_join = self.my_person_id.unwrap_or(PersonId(-1)); - let local_user_id_join = self.my_local_user_id.unwrap_or(LocalUserId(-1)); + let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1)); + let local_user_id_join = self.local_user.map(|l| l.id).unwrap_or(LocalUserId(-1)); let mut query = post::table .inner_join(person::table) @@ -322,13 +319,13 @@ impl<'a> PostQuery<'a> { query = query.filter(post::creator_id.eq(creator_id)); } - if !self.show_nsfw.unwrap_or(false) { + if !self.local_user.map(|l| l.show_nsfw).unwrap_or(false) { query = query .filter(post::nsfw.eq(false)) .filter(community::nsfw.eq(false)); }; - if !self.show_bot_accounts.unwrap_or(true) { + if !self.local_user.map(|l| l.show_bot_accounts).unwrap_or(true) { query = query.filter(person::bot_account.eq(false)); }; @@ -337,17 +334,15 @@ impl<'a> PostQuery<'a> { } // Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read // setting wont be able to see saved posts. - else if !self.show_read_posts.unwrap_or(true) { + else if !self.local_user.map(|l| l.show_read_posts).unwrap_or(true) { query = query.filter(post_read::id.is_null()); } - // Filter out the rows with missing languages - if self.my_local_user_id.is_some() { + if self.local_user.is_some() { + // Filter out the rows with missing languages query = query.filter(local_user_language::id.is_not_null()); - } - // Don't show blocked communities or persons - if self.my_person_id.is_some() { + // Don't show blocked communities or persons query = query.filter(community_block::person_id.is_null()); query = query.filter(person_block::person_id.is_null()); } @@ -458,6 +453,7 @@ mod tests { struct Data { inserted_person: Person, + inserted_local_user: LocalUser, inserted_blocked_person: Person, inserted_bot: Person, inserted_community: Community, @@ -475,6 +471,15 @@ mod tests { let inserted_person = Person::create(conn, &new_person).unwrap(); + let local_user_form = LocalUserForm { + person_id: Some(inserted_person.id), + password_encrypted: Some("".to_string()), + ..Default::default() + }; + let inserted_local_user = LocalUser::create(conn, &local_user_form).unwrap(); + // update user languages to all + LocalUserLanguage::update_user_languages(conn, None, inserted_local_user.id).unwrap(); + let new_bot = PersonForm { name: "mybot".to_string(), bot_account: Some(true), @@ -542,6 +547,7 @@ mod tests { Data { inserted_person, + inserted_local_user, inserted_blocked_person, inserted_bot, inserted_community, @@ -658,12 +664,18 @@ mod tests { let conn = establish_unpooled_connection(); let data = init_data(&conn); + let local_user_form = LocalUserForm { + show_bot_accounts: Some(false), + ..Default::default() + }; + let inserted_local_user = + LocalUser::update(&conn, data.inserted_local_user.id, &local_user_form).unwrap(); + let read_post_listing = PostQuery::builder() .conn(&conn) .sort(Some(SortType::New)) .community_id(Some(data.inserted_community.id)) - .show_bot_accounts(Some(false)) - .my_person_id(Some(data.inserted_person.id)) + .local_user(Some(&inserted_local_user)) .build() .list() .unwrap(); @@ -683,12 +695,18 @@ mod tests { post_listing_single_with_person ); + let local_user_form = LocalUserForm { + show_bot_accounts: Some(true), + ..Default::default() + }; + let inserted_local_user = + LocalUser::update(&conn, data.inserted_local_user.id, &local_user_form).unwrap(); + let post_listings_with_bots = PostQuery::builder() .conn(&conn) .sort(Some(SortType::New)) .community_id(Some(data.inserted_community.id)) - .show_bot_accounts(Some(true)) - .my_person_id(Some(data.inserted_person.id)) + .local_user(Some(&inserted_local_user)) .build() .list() .unwrap(); @@ -748,8 +766,7 @@ mod tests { .conn(&conn) .sort(Some(SortType::New)) .community_id(Some(data.inserted_community.id)) - .show_bot_accounts(Some(true)) - .my_person_id(Some(data.inserted_person.id)) + .local_user(Some(&data.inserted_local_user)) .build() .list() .unwrap(); @@ -806,44 +823,29 @@ mod tests { Post::create(&conn, &post_spanish).unwrap(); - let my_person_form = PersonForm { - name: "Reverie Toiba".to_string(), - public_key: Some("pubkey".to_string()), - ..PersonForm::default() - }; - let my_person = Person::create(&conn, &my_person_form).unwrap(); - let local_user_form = LocalUserForm { - person_id: Some(my_person.id), - password_encrypted: Some("".to_string()), - ..Default::default() - }; - let local_user = LocalUser::create(&conn, &local_user_form).unwrap(); - - // Update the users languages to all - LocalUserLanguage::update_user_languages(&conn, None, local_user.id).unwrap(); - let post_listings_all = PostQuery::builder() .conn(&conn) .sort(Some(SortType::New)) - .show_bot_accounts(Some(true)) - .my_person_id(Some(my_person.id)) - .my_local_user_id(Some(local_user.id)) + .local_user(Some(&data.inserted_local_user)) .build() .list() .unwrap(); // no language filters specified, all posts should be returned - assert_eq!(4, post_listings_all.len()); + assert_eq!(3, post_listings_all.len()); let french_id = Language::read_id_from_code(&conn, "fr").unwrap(); - LocalUserLanguage::update_user_languages(&conn, Some(vec![french_id]), local_user.id).unwrap(); + LocalUserLanguage::update_user_languages( + &conn, + Some(vec![french_id]), + data.inserted_local_user.id, + ) + .unwrap(); let post_listing_french = PostQuery::builder() .conn(&conn) .sort(Some(SortType::New)) - .show_bot_accounts(Some(true)) - .my_person_id(Some(my_person.id)) - .my_local_user_id(Some(local_user.id)) + .local_user(Some(&data.inserted_local_user)) .build() .list() .unwrap(); @@ -856,15 +858,13 @@ mod tests { LocalUserLanguage::update_user_languages( &conn, Some(vec![french_id, undetermined_id]), - local_user.id, + data.inserted_local_user.id, ) .unwrap(); let post_listings_french_und = PostQuery::builder() .conn(&conn) .sort(Some(SortType::New)) - .show_bot_accounts(Some(true)) - .my_person_id(Some(my_person.id)) - .my_local_user_id(Some(local_user.id)) + .local_user(Some(&data.inserted_local_user)) .build() .list() .unwrap(); diff --git a/crates/db_views_actor/src/community_view.rs b/crates/db_views_actor/src/community_view.rs index 69fae0c7..0171e067 100644 --- a/crates/db_views_actor/src/community_view.rs +++ b/crates/db_views_actor/src/community_view.rs @@ -7,6 +7,7 @@ use lemmy_db_schema::{ source::{ community::{Community, CommunityFollower, CommunitySafe}, community_block::CommunityBlock, + local_user::LocalUser, }, traits::{ToSafe, ViewToVec}, utils::{functions::hot_rank, fuzzy_search, limit_and_offset}, @@ -99,8 +100,7 @@ pub struct CommunityQuery<'a> { conn: &'a PgConnection, listing_type: Option, sort: Option, - my_person_id: Option, - show_nsfw: Option, + local_user: Option<&'a LocalUser>, search_term: Option, page: Option, limit: Option, @@ -109,7 +109,7 @@ pub struct CommunityQuery<'a> { impl<'a> CommunityQuery<'a> { pub fn list(self) -> Result, Error> { // The left join below will return None in this case - let person_id_join = self.my_person_id.unwrap_or(PersonId(-1)); + let person_id_join = self.local_user.map(|l| l.person_id).unwrap_or(PersonId(-1)); let mut query = community::table .inner_join(community_aggregates::table) @@ -188,12 +188,12 @@ impl<'a> CommunityQuery<'a> { } // Don't show blocked communities or nsfw communities if not enabled in profile - if self.my_person_id.is_some() { + if self.local_user.is_some() { query = query.filter(community_block::person_id.is_null()); query = query.filter(community::nsfw.eq(false).or(local_user::show_nsfw.eq(true))); } else { // No person in request, only show nsfw communities if show_nsfw passed into request - if !self.show_nsfw.unwrap_or(false) { + if !self.local_user.map(|l| l.show_nsfw).unwrap_or(false) { query = query.filter(community::nsfw.eq(false)); } } diff --git a/crates/routes/src/feeds.rs b/crates/routes/src/feeds.rs index 5840687a..592aeb2d 100644 --- a/crates/routes/src/feeds.rs +++ b/crates/routes/src/feeds.rs @@ -256,9 +256,7 @@ fn get_feed_front( let posts = PostQuery::builder() .conn(conn) .listing_type(Some(ListingType::Subscribed)) - .my_person_id(Some(local_user.person_id)) - .show_bot_accounts(Some(local_user.show_bot_accounts)) - .show_read_posts(Some(local_user.show_read_posts)) + .local_user(Some(&local_user)) .sort(Some(*sort_type)) .limit(Some(RSS_FETCH_LIMIT)) .build() -- 2.44.1