]> Untitled Git - lemmy.git/commitdiff
Pass LocalUser to PostQuery etc, instead of separate params (#2413)
authorNutomic <me@nutomic.com>
Fri, 19 Aug 2022 14:27:39 +0000 (14:27 +0000)
committerGitHub <noreply@github.com>
Fri, 19 Aug 2022 14:27:39 +0000 (10:27 -0400)
crates/api/src/site/search.rs
crates/api_crud/src/comment/list.rs
crates/api_crud/src/community/list.rs
crates/api_crud/src/user/read.rs
crates/db_views/src/comment_view.rs
crates/db_views/src/post_view.rs
crates/db_views_actor/src/community_view.rs
crates/routes/src/feeds.rs

index bd04b053c2077b6af96ae5cc5a7b27e41e491cbb..4c27ef8930d63df78756d9d01449c2d00c593b11 100644 (file)
@@ -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()
index ca40d99c09205b0c8bb8efae40c7edc813ed02da..e7632cba2dd1fe441b5836862de9f923c929ca00 100644 (file)
@@ -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()
index c57e9eaa58e037c70289de5d47b4d60b668675f6..1aa4585f162fa1f31e1c338921e825e99217ad0b 100644 (file)
@@ -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()
index 15630924ae08f78cf5d4e81e07b9dceee48c809a..820905d826b83adfb1721313ac3212f504116218 100644 (file)
@@ -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)
index 2516b45e42ef7a542d160060ae8d7674fd967256..a7c96b17c5b1349f2ebd48669afe74950db455a7 100644 (file)
@@ -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<PostId>,
   parent_path: Option<Ltree>,
   creator_id: Option<PersonId>,
-  my_person_id: Option<PersonId>,
+  local_user: Option<&'a LocalUser>,
   search_term: Option<String>,
   saved_only: Option<bool>,
-  show_bot_accounts: Option<bool>,
   page: Option<i64>,
   limit: Option<i64>,
   max_depth: Option<i32>,
@@ -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();
index 761925a9cd36ec6ce534db8abcee9c93f897fbab..6d0bcb5daead62c87900f293745f9111967f08d3 100644 (file)
@@ -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<PersonId>,
   community_id: Option<CommunityId>,
   community_actor_id: Option<DbUrl>,
-  my_person_id: Option<PersonId>,
-  my_local_user_id: Option<LocalUserId>,
+  local_user: Option<&'a LocalUser>,
   search_term: Option<String>,
   url_search: Option<String>,
-  show_nsfw: Option<bool>,
-  show_bot_accounts: Option<bool>,
-  show_read_posts: Option<bool>,
   saved_only: Option<bool>,
   page: Option<i64>,
   limit: Option<i64>,
@@ -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();
index 69fae0c701d6ea800fee19078c74478761709f1a..0171e067f5007a9157a5b39611921bc04b377f6c 100644 (file)
@@ -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<ListingType>,
   sort: Option<SortType>,
-  my_person_id: Option<PersonId>,
-  show_nsfw: Option<bool>,
+  local_user: Option<&'a LocalUser>,
   search_term: Option<String>,
   page: Option<i64>,
   limit: Option<i64>,
@@ -109,7 +109,7 @@ pub struct CommunityQuery<'a> {
 impl<'a> CommunityQuery<'a> {
   pub fn list(self) -> Result<Vec<CommunityView>, 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));
       }
     }
index 5840687a0b63b5406b37b586260f5514a7c0e5bc..592aeb2ddac08f844efbd2f350822039ef612bda 100644 (file)
@@ -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()