diff --git a/crates/api/src/comment/distinguish.rs b/crates/api/src/comment/distinguish.rs index dfd850e89..0683af9a4 100644 --- a/crates/api/src/comment/distinguish.rs +++ b/crates/api/src/comment/distinguish.rs @@ -17,9 +17,13 @@ pub async fn distinguish_comment( context: Data, local_user_view: LocalUserView, ) -> LemmyResult> { - let orig_comment = CommentView::read(&mut context.pool(), data.comment_id, None) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let orig_comment = CommentView::read( + &mut context.pool(), + data.comment_id, + Some(&local_user_view.local_user), + ) + .await? + .ok_or(LemmyErrorType::CouldntFindComment)?; check_community_user_action( &local_user_view.person, @@ -54,7 +58,7 @@ pub async fn distinguish_comment( let comment_view = CommentView::read( &mut context.pool(), data.comment_id, - Some(local_user_view.person.id), + Some(&local_user_view.local_user), ) .await? .ok_or(LemmyErrorType::CouldntFindComment)?; diff --git a/crates/api/src/comment/like.rs b/crates/api/src/comment/like.rs index d0aa4a6c2..b8a1c6f76 100644 --- a/crates/api/src/comment/like.rs +++ b/crates/api/src/comment/like.rs @@ -35,9 +35,13 @@ pub async fn like_comment( check_bot_account(&local_user_view.person)?; let comment_id = data.comment_id; - let orig_comment = CommentView::read(&mut context.pool(), comment_id, None) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let orig_comment = CommentView::read( + &mut context.pool(), + comment_id, + Some(&local_user_view.local_user), + ) + .await? + .ok_or(LemmyErrorType::CouldntFindComment)?; check_community_user_action( &local_user_view.person, diff --git a/crates/api/src/comment/list_comment_likes.rs b/crates/api/src/comment/list_comment_likes.rs index 8c2c9dd32..4b2e1c8b3 100644 --- a/crates/api/src/comment/list_comment_likes.rs +++ b/crates/api/src/comment/list_comment_likes.rs @@ -17,7 +17,7 @@ pub async fn list_comment_likes( let comment_view = CommentView::read( &mut context.pool(), data.comment_id, - Some(local_user_view.person.id), + Some(&local_user_view.local_user), ) .await? .ok_or(LemmyErrorType::CouldntFindComment)?; diff --git a/crates/api/src/comment/save.rs b/crates/api/src/comment/save.rs index f9d649e48..67c2db331 100644 --- a/crates/api/src/comment/save.rs +++ b/crates/api/src/comment/save.rs @@ -32,10 +32,13 @@ pub async fn save_comment( } let comment_id = data.comment_id; - let person_id = local_user_view.person.id; - let comment_view = CommentView::read(&mut context.pool(), comment_id, Some(person_id)) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let comment_view = CommentView::read( + &mut context.pool(), + comment_id, + Some(&local_user_view.local_user), + ) + .await? + .ok_or(LemmyErrorType::CouldntFindComment)?; Ok(Json(CommentResponse { comment_view, diff --git a/crates/api/src/comment_report/create.rs b/crates/api/src/comment_report/create.rs index c008d1df2..a269df07f 100644 --- a/crates/api/src/comment_report/create.rs +++ b/crates/api/src/comment_report/create.rs @@ -35,9 +35,13 @@ pub async fn create_comment_report( let person_id = local_user_view.person.id; let comment_id = data.comment_id; - let comment_view = CommentView::read(&mut context.pool(), comment_id, None) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let comment_view = CommentView::read( + &mut context.pool(), + comment_id, + Some(&local_user_view.local_user), + ) + .await? + .ok_or(LemmyErrorType::CouldntFindComment)?; check_community_user_action( &local_user_view.person, diff --git a/crates/api/src/community/block.rs b/crates/api/src/community/block.rs index 449addf32..ad31548ea 100644 --- a/crates/api/src/community/block.rs +++ b/crates/api/src/community/block.rs @@ -50,10 +50,14 @@ pub async fn block_community( .with_lemmy_type(LemmyErrorType::CommunityBlockAlreadyExists)?; } - let community_view = - CommunityView::read(&mut context.pool(), community_id, Some(person_id), false) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + let community_view = CommunityView::read( + &mut context.pool(), + community_id, + Some(&local_user_view.local_user), + false, + ) + .await? + .ok_or(LemmyErrorType::CouldntFindCommunity)?; ActivityChannel::submit_activity( SendActivityData::FollowCommunity( diff --git a/crates/api/src/community/follow.rs b/crates/api/src/community/follow.rs index 853cfde14..2236fa5bc 100644 --- a/crates/api/src/community/follow.rs +++ b/crates/api/src/community/follow.rs @@ -62,11 +62,14 @@ pub async fn follow_community( } let community_id = data.community_id; - let person_id = local_user_view.person.id; - let community_view = - CommunityView::read(&mut context.pool(), community_id, Some(person_id), false) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + let community_view = CommunityView::read( + &mut context.pool(), + community_id, + Some(&local_user_view.local_user), + false, + ) + .await? + .ok_or(LemmyErrorType::CouldntFindCommunity)?; let discussion_languages = CommunityLanguage::read(&mut context.pool(), community_id).await?; diff --git a/crates/api/src/community/transfer.rs b/crates/api/src/community/transfer.rs index 5f3a6032e..a32c069b1 100644 --- a/crates/api/src/community/transfer.rs +++ b/crates/api/src/community/transfer.rs @@ -76,11 +76,14 @@ pub async fn transfer_community( ModTransferCommunity::create(&mut context.pool(), &form).await?; let community_id = data.community_id; - let person_id = local_user_view.person.id; - let community_view = - CommunityView::read(&mut context.pool(), community_id, Some(person_id), false) - .await? - .ok_or(LemmyErrorType::CouldntFindCommunity)?; + let community_view = CommunityView::read( + &mut context.pool(), + community_id, + Some(&local_user_view.local_user), + false, + ) + .await? + .ok_or(LemmyErrorType::CouldntFindCommunity)?; let community_id = data.community_id; let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id) diff --git a/crates/api/src/post/feature.rs b/crates/api/src/post/feature.rs index 40cbf6794..ec99a3345 100644 --- a/crates/api/src/post/feature.rs +++ b/crates/api/src/post/feature.rs @@ -72,11 +72,5 @@ pub async fn feature_post( ) .await?; - build_post_response( - &context, - orig_post.community_id, - &local_user_view.person, - post_id, - ) - .await + build_post_response(&context, orig_post.community_id, local_user_view, post_id).await } diff --git a/crates/api/src/post/like.rs b/crates/api/src/post/like.rs index fccd9f8df..e6903fb3c 100644 --- a/crates/api/src/post/like.rs +++ b/crates/api/src/post/like.rs @@ -85,11 +85,5 @@ pub async fn like_post( ) .await?; - build_post_response( - context.deref(), - post.community_id, - &local_user_view.person, - post_id, - ) - .await + build_post_response(context.deref(), post.community_id, local_user_view, post_id).await } diff --git a/crates/api/src/post/lock.rs b/crates/api/src/post/lock.rs index 05db8ebbb..36f9c2a33 100644 --- a/crates/api/src/post/lock.rs +++ b/crates/api/src/post/lock.rs @@ -63,11 +63,5 @@ pub async fn lock_post( ) .await?; - build_post_response( - &context, - orig_post.community_id, - &local_user_view.person, - post_id, - ) - .await + build_post_response(&context, orig_post.community_id, local_user_view, post_id).await } diff --git a/crates/api/src/post/save.rs b/crates/api/src/post/save.rs index 96dd85579..85dfc11e3 100644 --- a/crates/api/src/post/save.rs +++ b/crates/api/src/post/save.rs @@ -34,9 +34,14 @@ pub async fn save_post( let post_id = data.post_id; let person_id = local_user_view.person.id; - let post_view = PostView::read(&mut context.pool(), post_id, Some(person_id), false) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + let post_view = PostView::read( + &mut context.pool(), + post_id, + Some(&local_user_view.local_user), + false, + ) + .await? + .ok_or(LemmyErrorType::CouldntFindPost)?; mark_post_as_read(person_id, post_id, &mut context.pool()).await?; diff --git a/crates/api/src/site/purge/comment.rs b/crates/api/src/site/purge/comment.rs index 70d95e160..f99dfc689 100644 --- a/crates/api/src/site/purge/comment.rs +++ b/crates/api/src/site/purge/comment.rs @@ -29,9 +29,13 @@ pub async fn purge_comment( let comment_id = data.comment_id; // Read the comment to get the post_id and community - let comment_view = CommentView::read(&mut context.pool(), comment_id, None) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let comment_view = CommentView::read( + &mut context.pool(), + comment_id, + Some(&local_user_view.local_user), + ) + .await? + .ok_or(LemmyErrorType::CouldntFindComment)?; let post_id = comment_view.comment.post_id; diff --git a/crates/api_common/src/build_response.rs b/crates/api_common/src/build_response.rs index 85cf065eb..200284b00 100644 --- a/crates/api_common/src/build_response.rs +++ b/crates/api_common/src/build_response.rs @@ -36,8 +36,8 @@ pub async fn build_comment_response( local_user_view: Option, recipient_ids: Vec, ) -> LemmyResult { - let person_id = local_user_view.map(|l| l.person.id); - let comment_view = CommentView::read(&mut context.pool(), comment_id, person_id) + let local_user = local_user_view.map(|l| l.local_user); + let comment_view = CommentView::read(&mut context.pool(), comment_id, local_user.as_ref()) .await? .ok_or(LemmyErrorType::CouldntFindComment)?; Ok(CommentResponse { @@ -54,11 +54,11 @@ pub async fn build_community_response( let is_mod_or_admin = is_mod_or_admin(&mut context.pool(), &local_user_view.person, community_id) .await .is_ok(); - let person_id = local_user_view.person.id; + let local_user = local_user_view.local_user; let community_view = CommunityView::read( &mut context.pool(), community_id, - Some(person_id), + Some(&local_user), is_mod_or_admin, ) .await? @@ -74,16 +74,17 @@ pub async fn build_community_response( pub async fn build_post_response( context: &LemmyContext, community_id: CommunityId, - person: &Person, + local_user_view: LocalUserView, post_id: PostId, ) -> LemmyResult> { - let is_mod_or_admin = is_mod_or_admin(&mut context.pool(), person, community_id) + let local_user = local_user_view.local_user; + let is_mod_or_admin = is_mod_or_admin(&mut context.pool(), &local_user_view.person, community_id) .await .is_ok(); let post_view = PostView::read( &mut context.pool(), post_id, - Some(person.id), + Some(&local_user), is_mod_or_admin, ) .await? @@ -103,6 +104,7 @@ pub async fn send_local_notifs( let mut recipient_ids = Vec::new(); let inbox_link = format!("{}/inbox", context.settings().get_protocol_and_hostname()); + // let person = my_local_user.person; // Read the comment view to get extra info let comment_view = CommentView::read(&mut context.pool(), comment_id, None) .await? diff --git a/crates/api_crud/src/comment/create.rs b/crates/api_crud/src/comment/create.rs index ff3efd946..b7c65740c 100644 --- a/crates/api_crud/src/comment/create.rs +++ b/crates/api_crud/src/comment/create.rs @@ -54,7 +54,7 @@ pub async fn create_comment( let post_view = PostView::read( &mut context.pool(), post_id, - Some(local_user_view.person.id), + Some(&local_user_view.local_user), true, ) .await? diff --git a/crates/api_crud/src/comment/delete.rs b/crates/api_crud/src/comment/delete.rs index 2b1a20f89..29706d365 100644 --- a/crates/api_crud/src/comment/delete.rs +++ b/crates/api_crud/src/comment/delete.rs @@ -21,9 +21,13 @@ pub async fn delete_comment( local_user_view: LocalUserView, ) -> LemmyResult> { let comment_id = data.comment_id; - let orig_comment = CommentView::read(&mut context.pool(), comment_id, None) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let orig_comment = CommentView::read( + &mut context.pool(), + comment_id, + Some(&local_user_view.local_user), + ) + .await? + .ok_or(LemmyErrorType::CouldntFindComment)?; // Dont delete it if its already been deleted. if orig_comment.comment.deleted == data.deleted { diff --git a/crates/api_crud/src/comment/remove.rs b/crates/api_crud/src/comment/remove.rs index 926472b94..83ef82e3a 100644 --- a/crates/api_crud/src/comment/remove.rs +++ b/crates/api_crud/src/comment/remove.rs @@ -25,9 +25,13 @@ pub async fn remove_comment( local_user_view: LocalUserView, ) -> LemmyResult> { let comment_id = data.comment_id; - let orig_comment = CommentView::read(&mut context.pool(), comment_id, None) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let orig_comment = CommentView::read( + &mut context.pool(), + comment_id, + Some(&local_user_view.local_user), + ) + .await? + .ok_or(LemmyErrorType::CouldntFindComment)?; check_community_mod_action( &local_user_view.person, @@ -68,14 +72,8 @@ pub async fn remove_comment( }; ModRemoveComment::create(&mut context.pool(), &form).await?; - let recipient_ids = send_local_notifs( - vec![], - comment_id, - &local_user_view.person.clone(), - false, - &context, - ) - .await?; + let recipient_ids = + send_local_notifs(vec![], comment_id, &local_user_view.person, false, &context).await?; let updated_comment_id = updated_comment.id; ActivityChannel::submit_activity( diff --git a/crates/api_crud/src/comment/update.rs b/crates/api_crud/src/comment/update.rs index 4c8cf9436..32bd08240 100644 --- a/crates/api_crud/src/comment/update.rs +++ b/crates/api_crud/src/comment/update.rs @@ -36,9 +36,13 @@ pub async fn update_comment( let local_site = LocalSite::read(&mut context.pool()).await?; let comment_id = data.comment_id; - let orig_comment = CommentView::read(&mut context.pool(), comment_id, None) - .await? - .ok_or(LemmyErrorType::CouldntFindComment)?; + let orig_comment = CommentView::read( + &mut context.pool(), + comment_id, + Some(&local_user_view.local_user), + ) + .await? + .ok_or(LemmyErrorType::CouldntFindComment)?; check_community_user_action( &local_user_view.person, diff --git a/crates/api_crud/src/post/create.rs b/crates/api_crud/src/post/create.rs index 39ef8ce09..0f6abc2aa 100644 --- a/crates/api_crud/src/post/create.rs +++ b/crates/api_crud/src/post/create.rs @@ -188,5 +188,5 @@ pub async fn create_post( } }; - build_post_response(&context, community_id, &local_user_view.person, post_id).await + build_post_response(&context, community_id, local_user_view, post_id).await } diff --git a/crates/api_crud/src/post/delete.rs b/crates/api_crud/src/post/delete.rs index 696566c8e..6834030ac 100644 --- a/crates/api_crud/src/post/delete.rs +++ b/crates/api_crud/src/post/delete.rs @@ -62,7 +62,7 @@ pub async fn delete_post( build_post_response( &context, orig_post.community_id, - &local_user_view.person, + local_user_view, data.post_id, ) .await diff --git a/crates/api_crud/src/post/read.rs b/crates/api_crud/src/post/read.rs index 60b5609a9..ebf8940a2 100644 --- a/crates/api_crud/src/post/read.rs +++ b/crates/api_crud/src/post/read.rs @@ -55,9 +55,15 @@ pub async fn get_post( .await .is_ok(); - let post_view = PostView::read(&mut context.pool(), post_id, person_id, is_mod_or_admin) - .await? - .ok_or(LemmyErrorType::CouldntFindPost)?; + let local_user = local_user_view.map(|l| l.local_user); + let post_view = PostView::read( + &mut context.pool(), + post_id, + local_user.as_ref(), + is_mod_or_admin, + ) + .await? + .ok_or(LemmyErrorType::CouldntFindPost)?; let post_id = post_view.post.id; if let Some(person_id) = person_id { @@ -76,20 +82,19 @@ pub async fn get_post( let community_view = CommunityView::read( &mut context.pool(), community_id, - person_id, + local_user.as_ref(), is_mod_or_admin, ) .await? .ok_or(LemmyErrorType::CouldntFindCommunity)?; let moderators = CommunityModeratorView::for_community(&mut context.pool(), community_id).await?; - let local_user = local_user_view.as_ref().map(|u| &u.local_user); // Fetch the cross_posts let cross_posts = if let Some(url) = &post_view.post.url { let mut x_posts = PostQuery { url_search: Some(url.inner().as_str().into()), - local_user, + local_user: local_user.as_ref(), ..Default::default() } .list(&local_site.site, &mut context.pool()) diff --git a/crates/api_crud/src/post/remove.rs b/crates/api_crud/src/post/remove.rs index 682ed75d3..4ca15c90f 100644 --- a/crates/api_crud/src/post/remove.rs +++ b/crates/api_crud/src/post/remove.rs @@ -73,11 +73,5 @@ pub async fn remove_post( ) .await?; - build_post_response( - &context, - orig_post.community_id, - &local_user_view.person, - post_id, - ) - .await + build_post_response(&context, orig_post.community_id, local_user_view, post_id).await } diff --git a/crates/api_crud/src/post/update.rs b/crates/api_crud/src/post/update.rs index 9e665aed6..cd4b2d41b 100644 --- a/crates/api_crud/src/post/update.rs +++ b/crates/api_crud/src/post/update.rs @@ -137,7 +137,7 @@ pub async fn update_post( build_post_response( context.deref(), orig_post.community_id, - &local_user_view.person, + local_user_view, post_id, ) .await diff --git a/crates/apub/src/api/read_community.rs b/crates/apub/src/api/read_community.rs index dae7719ae..62fd6ec0b 100644 --- a/crates/apub/src/api/read_community.rs +++ b/crates/apub/src/api/read_community.rs @@ -29,7 +29,7 @@ pub async fn get_community( check_private_instance(&local_user_view, &local_site)?; - let person_id = local_user_view.as_ref().map(|u| u.person.id); + let local_user = local_user_view.as_ref().map(|u| &u.local_user); let community_id = match data.id { Some(id) => id, @@ -53,7 +53,7 @@ pub async fn get_community( let community_view = CommunityView::read( &mut context.pool(), community_id, - person_id, + local_user, is_mod_or_admin, ) .await? diff --git a/crates/apub/src/api/resolve_object.rs b/crates/apub/src/api/resolve_object.rs index 47f6c5d06..3f2591241 100644 --- a/crates/apub/src/api/resolve_object.rs +++ b/crates/apub/src/api/resolve_object.rs @@ -10,7 +10,7 @@ use lemmy_api_common::{ site::{ResolveObject, ResolveObjectResponse}, utils::check_private_instance, }; -use lemmy_db_schema::{newtypes::PersonId, source::local_site::LocalSite, utils::DbPool}; +use lemmy_db_schema::{source::local_site::LocalSite, utils::DbPool}; use lemmy_db_views::structs::{CommentView, LocalUserView, PostView}; use lemmy_db_views_actor::structs::{CommunityView, PersonView}; use lemmy_utils::error::{LemmyErrorExt2, LemmyErrorType, LemmyResult}; @@ -23,10 +23,9 @@ pub async fn resolve_object( ) -> LemmyResult> { let local_site = LocalSite::read(&mut context.pool()).await?; check_private_instance(&local_user_view, &local_site)?; - let person_id = local_user_view.map(|v| v.person.id); // If we get a valid personId back we can safely assume that the user is authenticated, // if there's no personId then the JWT was missing or invalid. - let is_authenticated = person_id.is_some(); + let is_authenticated = local_user_view.is_some(); let res = if is_authenticated { // user is fully authenticated; allow remote lookups as well. @@ -37,24 +36,26 @@ pub async fn resolve_object( } .with_lemmy_type(LemmyErrorType::CouldntFindObject)?; - convert_response(res, person_id, &mut context.pool()) + convert_response(res, local_user_view, &mut context.pool()) .await .with_lemmy_type(LemmyErrorType::CouldntFindObject) } async fn convert_response( object: SearchableObjects, - user_id: Option, + local_user_view: Option, pool: &mut DbPool<'_>, ) -> LemmyResult> { use SearchableObjects::*; let removed_or_deleted; let mut res = ResolveObjectResponse::default(); + let local_user = local_user_view.map(|l| l.local_user); + match object { Post(p) => { removed_or_deleted = p.deleted || p.removed; res.post = Some( - PostView::read(pool, p.id, user_id, false) + PostView::read(pool, p.id, local_user.as_ref(), false) .await? .ok_or(LemmyErrorType::CouldntFindPost)?, ) @@ -62,7 +63,7 @@ async fn convert_response( Comment(c) => { removed_or_deleted = c.deleted || c.removed; res.comment = Some( - CommentView::read(pool, c.id, user_id) + CommentView::read(pool, c.id, local_user.as_ref()) .await? .ok_or(LemmyErrorType::CouldntFindComment)?, ) @@ -79,7 +80,7 @@ async fn convert_response( UserOrCommunity::Community(c) => { removed_or_deleted = c.deleted || c.removed; res.community = Some( - CommunityView::read(pool, c.id, user_id, false) + CommunityView::read(pool, c.id, local_user.as_ref(), false) .await? .ok_or(LemmyErrorType::CouldntFindCommunity)?, ) diff --git a/crates/db_schema/src/impls/local_user.rs b/crates/db_schema/src/impls/local_user.rs index 9b59e07ba..694979ade 100644 --- a/crates/db_schema/src/impls/local_user.rs +++ b/crates/db_schema/src/impls/local_user.rs @@ -1,6 +1,6 @@ use crate::{ newtypes::{DbUrl, LanguageId, LocalUserId, PersonId}, - schema::{local_user, person, registration_application}, + schema::{community, local_user, person, registration_application}, source::{ actor_language::LocalUserLanguage, local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm}, @@ -13,6 +13,7 @@ use crate::{ now, DbPool, }, + CommunityVisibility, }; use bcrypt::{hash, DEFAULT_COST}; use diesel::{ @@ -225,6 +226,12 @@ pub trait LocalUserOptionHelper { fn show_read_posts(&self) -> bool; fn is_admin(&self) -> bool; fn show_nsfw(&self, site: &Site) -> bool; + fn visible_communities_only(&self, query: Q) -> Q + where + Q: diesel::query_dsl::methods::FilterDsl< + diesel::dsl::Eq, + Output = Q, + >; } impl LocalUserOptionHelper for Option<&LocalUser> { @@ -253,6 +260,20 @@ impl LocalUserOptionHelper for Option<&LocalUser> { .map(|l| l.show_nsfw) .unwrap_or(site.content_warning.is_some()) } + + fn visible_communities_only(&self, query: Q) -> Q + where + Q: diesel::query_dsl::methods::FilterDsl< + diesel::dsl::Eq, + Output = Q, + >, + { + if self.is_none() { + query.filter(community::visibility.eq(CommunityVisibility::Public)) + } else { + query + } + } } impl LocalUserInsertForm { diff --git a/crates/db_schema/src/utils.rs b/crates/db_schema/src/utils.rs index cca449b0e..ec9f9ded7 100644 --- a/crates/db_schema/src/utils.rs +++ b/crates/db_schema/src/utils.rs @@ -1,15 +1,7 @@ -use crate::{ - diesel::ExpressionMethods, - newtypes::{DbUrl, PersonId}, - schema::community, - CommentSortType, - CommunityVisibility, - SortType, -}; +use crate::{newtypes::DbUrl, CommentSortType, SortType}; use chrono::{DateTime, TimeDelta, Utc}; use deadpool::Runtime; use diesel::{ - dsl, helper_types::AsExprOf, pg::Pg, query_builder::{Query, QueryFragment}, @@ -592,20 +584,6 @@ impl Queries { } } -pub fn visible_communities_only(my_person_id: Option, query: Q) -> Q -where - Q: diesel::query_dsl::methods::FilterDsl< - dsl::Eq, - Output = Q, - >, -{ - if my_person_id.is_none() { - query.filter(community::visibility.eq(CommunityVisibility::Public)) - } else { - query - } -} - #[cfg(test)] #[allow(clippy::indexing_slicing)] mod tests { diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index cd7560a00..0e476b3b6 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -36,22 +36,13 @@ use lemmy_db_schema::{ post, }, source::local_user::LocalUser, - utils::{ - fuzzy_search, - limit_and_offset, - visible_communities_only, - DbConn, - DbPool, - ListFn, - Queries, - ReadFn, - }, + utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, CommentSortType, ListingType, }; fn queries<'a>() -> Queries< - impl ReadFn<'a, CommentView, (CommentId, Option)>, + impl ReadFn<'a, CommentView, (CommentId, Option<&'a LocalUser>)>, impl ListFn<'a, CommentView, CommentQuery<'a>>, > { let is_creator_banned_from_community = exists( @@ -182,9 +173,12 @@ fn queries<'a>() -> Queries< }; let read = move |mut conn: DbConn<'a>, - (comment_id, my_person_id): (CommentId, Option)| async move { - let mut query = all_joins(comment::table.find(comment_id).into_boxed(), my_person_id); - query = visible_communities_only(my_person_id, query); + (comment_id, my_local_user): (CommentId, Option<&'a LocalUser>)| async move { + let mut query = all_joins( + comment::table.find(comment_id).into_boxed(), + my_local_user.person_id(), + ); + query = my_local_user.visible_communities_only(query); query.first(&mut conn).await }; @@ -301,7 +295,7 @@ fn queries<'a>() -> Queries< query = query.filter(not(is_creator_blocked(person_id_join))); }; - query = visible_communities_only(options.local_user.person_id(), query); + query = options.local_user.visible_communities_only(query); // A Max depth given means its a tree fetch let (limit, offset) = if let Some(max_depth) = options.max_depth { @@ -366,16 +360,16 @@ fn queries<'a>() -> Queries< } impl CommentView { - pub async fn read( + pub async fn read<'a>( pool: &mut DbPool<'_>, comment_id: CommentId, - my_person_id: Option, + my_local_user: Option<&'a LocalUser>, ) -> Result, Error> { // If a person is given, then my_vote (res.9), if None, should be 0, not null // Necessary to differentiate between other person's votes - if let Ok(Some(res)) = queries().read(pool, (comment_id, my_person_id)).await { + if let Ok(Some(res)) = queries().read(pool, (comment_id, my_local_user)).await { let mut new_view = res.clone(); - if my_person_id.is_some() && res.my_vote.is_none() { + if my_local_user.is_some() && res.my_vote.is_none() { new_view.my_vote = Some(0); } if res.comment.deleted || res.comment.removed { @@ -676,7 +670,7 @@ mod tests { let read_comment_from_blocked_person = CommentView::read( pool, data.inserted_comment_1.id, - Some(data.timmy_local_user_view.person.id), + Some(&data.timmy_local_user_view.local_user), ) .await? .ok_or(LemmyErrorType::CouldntFindComment)?; @@ -1171,7 +1165,7 @@ mod tests { let authenticated_comment = CommentView::read( pool, data.inserted_comment_0.id, - Some(data.timmy_local_user_view.person.id), + Some(&data.timmy_local_user_view.local_user), ) .await; assert!(authenticated_comment.is_ok()); @@ -1211,7 +1205,7 @@ mod tests { let comment_view = CommentView::read( pool, data.inserted_comment_0.id, - Some(inserted_banned_from_comm_local_user.person_id), + Some(&inserted_banned_from_comm_local_user), ) .await? .ok_or(LemmyErrorType::CouldntFindComment)?; @@ -1232,7 +1226,7 @@ mod tests { let comment_view = CommentView::read( pool, data.inserted_comment_0.id, - Some(data.timmy_local_user_view.person.id), + Some(&data.timmy_local_user_view.local_user), ) .await? .ok_or(LemmyErrorType::CouldntFindComment)?; diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 0e22f689b..588bb017b 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -49,7 +49,6 @@ use lemmy_db_schema::{ get_conn, limit_and_offset, now, - visible_communities_only, Commented, DbConn, DbPool, @@ -64,7 +63,7 @@ use lemmy_db_schema::{ use tracing::debug; fn queries<'a>() -> Queries< - impl ReadFn<'a, PostView, (PostId, Option, bool)>, + impl ReadFn<'a, PostView, (PostId, Option<&'a LocalUser>, bool)>, impl ListFn<'a, PostView, (PostQuery<'a>, &'a Site)>, > { let is_creator_banned_from_community = exists( @@ -142,6 +141,7 @@ fn queries<'a>() -> Queries< .single_value() }; + // TODO maybe this should go to localuser also let all_joins = move |query: post_aggregates::BoxedQuery<'a, Pg>, my_person_id: Option| { let is_local_user_banned_from_community_selection: Box< @@ -250,52 +250,56 @@ fn queries<'a>() -> Queries< )) }; - let read = - move |mut conn: DbConn<'a>, - (post_id, my_person_id, is_mod_or_admin): (PostId, Option, bool)| async move { - // The left join below will return None in this case - let person_id_join = my_person_id.unwrap_or(PersonId(-1)); + let read = move |mut conn: DbConn<'a>, + (post_id, my_local_user, is_mod_or_admin): ( + PostId, + Option<&'a LocalUser>, + bool, + )| async move { + // The left join below will return None in this case + let my_person_id = my_local_user.person_id(); + let person_id_join = my_person_id.unwrap_or(PersonId(-1)); - let mut query = all_joins( - post_aggregates::table - .filter(post_aggregates::post_id.eq(post_id)) - .into_boxed(), - my_person_id, - ); + let mut query = all_joins( + post_aggregates::table + .filter(post_aggregates::post_id.eq(post_id)) + .into_boxed(), + my_person_id, + ); - // Hide deleted and removed for non-admins or mods - if !is_mod_or_admin { - query = query - .filter( - community::removed - .eq(false) - .or(post::creator_id.eq(person_id_join)), - ) - .filter( - post::removed - .eq(false) - .or(post::creator_id.eq(person_id_join)), - ) - // users can see their own deleted posts - .filter( - community::deleted - .eq(false) - .or(post::creator_id.eq(person_id_join)), - ) - .filter( - post::deleted - .eq(false) - .or(post::creator_id.eq(person_id_join)), - ); - } + // Hide deleted and removed for non-admins or mods + if !is_mod_or_admin { + query = query + .filter( + community::removed + .eq(false) + .or(post::creator_id.eq(person_id_join)), + ) + .filter( + post::removed + .eq(false) + .or(post::creator_id.eq(person_id_join)), + ) + // users can see their own deleted posts + .filter( + community::deleted + .eq(false) + .or(post::creator_id.eq(person_id_join)), + ) + .filter( + post::deleted + .eq(false) + .or(post::creator_id.eq(person_id_join)), + ); + } - query = visible_communities_only(my_person_id, query); + query = my_local_user.visible_communities_only(query); - Commented::new(query) - .text("PostView::read") - .first(&mut conn) - .await - }; + Commented::new(query) + .text("PostView::read") + .first(&mut conn) + .await + }; let list = move |mut conn: DbConn<'a>, (options, site): (PostQuery<'a>, &'a Site)| async move { // The left join below will return None in this case @@ -437,7 +441,7 @@ fn queries<'a>() -> Queries< } }; - query = visible_communities_only(options.local_user.person_id(), query); + query = options.local_user.visible_communities_only(query); // Dont filter blocks or missing languages for moderator view type if let (Some(person_id), false) = ( @@ -552,14 +556,14 @@ fn queries<'a>() -> Queries< } impl PostView { - pub async fn read( + pub async fn read<'a>( pool: &mut DbPool<'_>, post_id: PostId, - my_person_id: Option, + my_local_user: Option<&'a LocalUser>, is_mod_or_admin: bool, ) -> Result, Error> { queries() - .read(pool, (post_id, my_person_id, is_mod_or_admin)) + .read(pool, (post_id, my_local_user, is_mod_or_admin)) .await } } @@ -938,7 +942,7 @@ mod tests { let post_listing_single_with_person = PostView::read( pool, data.inserted_post.id, - Some(data.local_user_view.person.id), + Some(&data.local_user_view.local_user), false, ) .await? @@ -1067,7 +1071,7 @@ mod tests { let post_listing_single_with_person = PostView::read( pool, data.inserted_post.id, - Some(data.local_user_view.person.id), + Some(&data.local_user_view.local_user), false, ) .await? @@ -1755,7 +1759,7 @@ mod tests { let authenticated_post = PostView::read( pool, data.inserted_post.id, - Some(data.local_user_view.person.id), + Some(&data.local_user_view.local_user), false, ) .await; @@ -1797,7 +1801,7 @@ mod tests { let post_view = PostView::read( pool, data.inserted_post.id, - Some(inserted_banned_from_comm_local_user.person_id), + Some(&inserted_banned_from_comm_local_user), false, ) .await? @@ -1819,7 +1823,7 @@ mod tests { let post_view = PostView::read( pool, data.inserted_post.id, - Some(data.local_user_view.person.id), + Some(&data.local_user_view.local_user), false, ) .await? diff --git a/crates/db_views_actor/src/community_view.rs b/crates/db_views_actor/src/community_view.rs index 25e76c7b3..7d8a7277f 100644 --- a/crates/db_views_actor/src/community_view.rs +++ b/crates/db_views_actor/src/community_view.rs @@ -22,27 +22,18 @@ use lemmy_db_schema::{ instance_block, }, source::{community::CommunityFollower, local_user::LocalUser, site::Site}, - utils::{ - fuzzy_search, - limit_and_offset, - visible_communities_only, - DbConn, - DbPool, - ListFn, - Queries, - ReadFn, - }, + utils::{fuzzy_search, limit_and_offset, DbConn, DbPool, ListFn, Queries, ReadFn}, ListingType, SortType, }; fn queries<'a>() -> Queries< - impl ReadFn<'a, CommunityView, (CommunityId, Option, bool)>, + impl ReadFn<'a, CommunityView, (CommunityId, Option<&'a LocalUser>, bool)>, impl ListFn<'a, CommunityView, (CommunityQuery<'a>, &'a Site)>, > { - let all_joins = |query: community::BoxedQuery<'a, Pg>, my_person_id: Option| { + let all_joins = |query: community::BoxedQuery<'a, Pg>, my_local_user: Option<&'a LocalUser>| { // The left join below will return None in this case - let person_id_join = my_person_id.unwrap_or(PersonId(-1)); + let person_id_join = my_local_user.person_id().unwrap_or(PersonId(-1)); query .inner_join(community_aggregates::table) @@ -89,14 +80,14 @@ fn queries<'a>() -> Queries< .and(community::deleted.eq(false)); let read = move |mut conn: DbConn<'a>, - (community_id, my_person_id, is_mod_or_admin): ( + (community_id, my_local_user, is_mod_or_admin): ( CommunityId, - Option, + Option<&'a LocalUser>, bool, )| async move { let mut query = all_joins( community::table.find(community_id).into_boxed(), - my_person_id, + my_local_user, ) .select(selection); @@ -105,7 +96,7 @@ fn queries<'a>() -> Queries< query = query.filter(not_removed_or_deleted); } - query = visible_communities_only(my_person_id, query); + query = my_local_user.visible_communities_only(query); query.first(&mut conn).await }; @@ -116,11 +107,7 @@ fn queries<'a>() -> Queries< // The left join below will return None in this case let person_id_join = options.local_user.person_id().unwrap_or(PersonId(-1)); - let mut query = all_joins( - community::table.into_boxed(), - options.local_user.person_id(), - ) - .select(selection); + let mut query = all_joins(community::table.into_boxed(), options.local_user).select(selection); if let Some(search_term) = options.search_term { let searcher = fuzzy_search(&search_term); @@ -173,7 +160,7 @@ fn queries<'a>() -> Queries< query = query.filter(community::nsfw.eq(false)); } - query = visible_communities_only(options.local_user.person_id(), query); + query = options.local_user.visible_communities_only(query); let (limit, offset) = limit_and_offset(options.page, options.limit)?; query @@ -187,14 +174,14 @@ fn queries<'a>() -> Queries< } impl CommunityView { - pub async fn read( + pub async fn read<'a>( pool: &mut DbPool<'_>, community_id: CommunityId, - my_person_id: Option, + my_local_user: Option<&'a LocalUser>, is_mod_or_admin: bool, ) -> Result, Error> { queries() - .read(pool, (community_id, my_person_id, is_mod_or_admin)) + .read(pool, (community_id, my_local_user, is_mod_or_admin)) .await } @@ -388,7 +375,7 @@ mod tests { let authenticated_community = CommunityView::read( pool, data.inserted_community.id, - Some(data.local_user.person_id), + Some(&data.local_user), false, ) .await;