X-Git-Url: http://these/git/?a=blobdiff_plain;f=crates%2Fdb_views%2Fsrc%2Fpost_view.rs;h=f7add4cec60b194645898123bb92902510bd1889;hb=c8063f3267cf2b3622f1fdc69128c6b55feefbbc;hp=7d461d14d99bb6e2db73e3f6e68e7ea82699f9d6;hpb=9b710a2ed3df7411f97e873ae445e96fa5a8bd56;p=lemmy.git diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 7d461d14..f7add4ce 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -63,7 +63,7 @@ type PostViewTuple = ( sql_function!(fn coalesce(x: sql_types::Nullable, y: sql_types::BigInt) -> sql_types::BigInt); fn queries<'a>() -> Queries< - impl ReadFn<'a, PostView, (PostId, Option, Option)>, + impl ReadFn<'a, PostView, (PostId, Option, bool)>, impl ListFn<'a, PostView, PostQuery<'a>>, > { let all_joins = |query: post_aggregates::BoxedQuery<'a, Pg>, my_person_id: Option| { @@ -149,43 +149,40 @@ fn queries<'a>() -> Queries< ), ); - let read = move |mut conn: DbConn<'a>, - (post_id, my_person_id, is_mod_or_admin): ( - PostId, - Option, - Option, - )| async move { - // The left join below will return None in this case - 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, - ) - .select(selection); + 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 mut query = all_joins( + post_aggregates::table + .filter(post_aggregates::post_id.eq(post_id)) + .into_boxed(), + my_person_id, + ) + .select(selection); - // Hide deleted and removed for non-admins or mods - if !is_mod_or_admin.unwrap_or(false) { - query = query - .filter(community::removed.eq(false)) - .filter(post::removed.eq(false)) - // 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)) + .filter(post::removed.eq(false)) + // 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.first::(&mut conn).await - }; + query.first::(&mut conn).await + }; let list = move |mut conn: DbConn<'a>, options: PostQuery<'a>| async move { let person_id = options.local_user.map(|l| l.person.id); @@ -291,11 +288,11 @@ fn queries<'a>() -> Queries< query = query.filter(person::bot_account.eq(false)); }; - if options.saved_only.unwrap_or(false) { + if options.saved_only { query = query.filter(post_saved::id.is_not_null()); } - if options.moderator_view.unwrap_or(false) { + if options.moderator_view { query = query.filter(community_moderator::person_id.is_not_null()); } // Only hide the read posts, if the saved_only is false. Otherwise ppl with the hide_read @@ -311,9 +308,9 @@ fn queries<'a>() -> Queries< } } - if options.liked_only.unwrap_or_default() { + if options.liked_only { query = query.filter(post_like::score.eq(1)); - } else if options.disliked_only.unwrap_or_default() { + } else if options.disliked_only { query = query.filter(post_like::score.eq(-1)); } @@ -323,7 +320,7 @@ fn queries<'a>() -> Queries< // Don't show blocked communities or persons query = query.filter(community_block::person_id.is_null()); - if !options.moderator_view.unwrap_or(false) { + if !options.moderator_view { query = query.filter(person_block::person_id.is_null()); } } @@ -404,7 +401,7 @@ impl PostView { pool: &mut DbPool<'_>, post_id: PostId, my_person_id: Option, - is_mod_or_admin: Option, + is_mod_or_admin: bool, ) -> Result { let mut res = queries() .read(pool, (post_id, my_person_id, is_mod_or_admin)) @@ -429,10 +426,10 @@ pub struct PostQuery<'a> { pub local_user: Option<&'a LocalUserView>, pub search_term: Option, pub url_search: Option, - pub saved_only: Option, - pub liked_only: Option, - pub disliked_only: Option, - pub moderator_view: Option, + pub saved_only: bool, + pub liked_only: bool, + pub disliked_only: bool, + pub moderator_view: bool, pub is_profile_view: bool, pub page: Option, pub limit: Option, @@ -632,7 +629,7 @@ mod tests { pool, data.inserted_post.id, Some(data.local_user_view.person.id), - None, + false, ) .await .unwrap(); @@ -691,7 +688,7 @@ mod tests { .unwrap(); let read_post_listing_single_no_person = - PostView::read(pool, data.inserted_post.id, None, None) + PostView::read(pool, data.inserted_post.id, None, false) .await .unwrap(); @@ -771,7 +768,7 @@ mod tests { pool, data.inserted_post.id, Some(data.local_user_view.person.id), - None, + false, ) .await .unwrap(); @@ -808,7 +805,7 @@ mod tests { let read_liked_post_listing = PostQuery { community_id: (Some(data.inserted_community.id)), local_user: (Some(&data.local_user_view)), - liked_only: (Some(true)), + liked_only: (true), ..Default::default() } .list(pool) @@ -819,7 +816,7 @@ mod tests { let read_disliked_post_listing = PostQuery { community_id: (Some(data.inserted_community.id)), local_user: (Some(&data.local_user_view)), - disliked_only: (Some(true)), + disliked_only: (true), ..Default::default() } .list(pool)