From 08b01a377d60b5e8e806c1b14f791513c57b285b Mon Sep 17 00:00:00 2001 From: Nutomic Date: Fri, 1 Mar 2024 17:53:20 +0100 Subject: [PATCH] Support listing type for person (fixes #4146) (#4487) * Support listing type for person (fixes #4146) * add test --- Cargo.lock | 1 + crates/apub/src/api/search.rs | 2 + crates/db_views_actor/Cargo.toml | 1 + crates/db_views_actor/src/person_view.rs | 119 ++++++++++++++--------- 4 files changed, 77 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 82c9afa6f..a5194f393 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2755,6 +2755,7 @@ dependencies = [ "diesel", "diesel-async", "lemmy_db_schema", + "lemmy_utils", "pretty_assertions", "serde", "serde_with", diff --git a/crates/apub/src/api/search.rs b/crates/apub/src/api/search.rs index dff2dffeb..32128a3c1 100644 --- a/crates/apub/src/api/search.rs +++ b/crates/apub/src/api/search.rs @@ -104,6 +104,7 @@ pub async fn search( users = PersonQuery { sort, search_term: (Some(q)), + listing_type: (listing_type), page: (page), limit: (limit), } @@ -174,6 +175,7 @@ pub async fn search( PersonQuery { sort, search_term: (Some(q)), + listing_type: (listing_type), page: (page), limit: (limit), } diff --git a/crates/db_views_actor/Cargo.toml b/crates/db_views_actor/Cargo.toml index dd2b1aeb6..1892055d1 100644 --- a/crates/db_views_actor/Cargo.toml +++ b/crates/db_views_actor/Cargo.toml @@ -40,6 +40,7 @@ serial_test = { workspace = true } tokio = { workspace = true } pretty_assertions = { workspace = true } url.workspace = true +lemmy_utils.workspace = true [package.metadata.cargo-machete] ignored = ["strum"] diff --git a/crates/db_views_actor/src/person_view.rs b/crates/db_views_actor/src/person_view.rs index 08ad2880f..aee56748e 100644 --- a/crates/db_views_actor/src/person_view.rs +++ b/crates/db_views_actor/src/person_view.rs @@ -23,6 +23,7 @@ use lemmy_db_schema::{ Queries, ReadFn, }, + ListingType, SortType, }; use serde::{Deserialize, Serialize}; @@ -115,6 +116,15 @@ fn queries<'a>( let (limit, offset) = limit_and_offset(options.page, options.limit)?; query = query.limit(limit).offset(offset); + + if let Some(listing_type) = options.listing_type { + query = match listing_type { + // return nothing as its not possible to follow users + ListingType::Subscribed => query.limit(0), + ListingType::Local => query.filter(person::local.eq(true)), + _ => query, + }; + } } } query.load::(&mut conn).await @@ -141,6 +151,7 @@ impl PersonView { pub struct PersonQuery { pub sort: Option, pub search_term: Option, + pub listing_type: Option, pub page: Option, pub limit: Option, } @@ -168,6 +179,7 @@ mod tests { traits::Crud, utils::build_db_pool_for_tests, }; + use lemmy_utils::error::LemmyResult; use pretty_assertions::assert_eq; use serial_test::serial; @@ -178,64 +190,59 @@ mod tests { bob_local_user: LocalUser, } - async fn init_data(pool: &mut DbPool<'_>) -> Data { - let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()) - .await - .unwrap(); + async fn init_data(pool: &mut DbPool<'_>) -> LemmyResult { + let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string()).await?; let alice_form = PersonInsertForm::builder() .name("alice".to_string()) .public_key("pubkey".to_string()) .instance_id(inserted_instance.id) + .local(Some(true)) .build(); - let alice = Person::create(pool, &alice_form).await.unwrap(); + let alice = Person::create(pool, &alice_form).await?; let alice_local_user_form = LocalUserInsertForm::builder() .person_id(alice.id) .password_encrypted(String::new()) .build(); - let alice_local_user = LocalUser::create(pool, &alice_local_user_form) - .await - .unwrap(); + let alice_local_user = LocalUser::create(pool, &alice_local_user_form).await?; let bob_form = PersonInsertForm::builder() .name("bob".to_string()) .bot_account(Some(true)) .public_key("pubkey".to_string()) .instance_id(inserted_instance.id) + .local(Some(false)) .build(); - let bob = Person::create(pool, &bob_form).await.unwrap(); + let bob = Person::create(pool, &bob_form).await?; let bob_local_user_form = LocalUserInsertForm::builder() .person_id(bob.id) .password_encrypted(String::new()) .build(); - let bob_local_user = LocalUser::create(pool, &bob_local_user_form).await.unwrap(); + let bob_local_user = LocalUser::create(pool, &bob_local_user_form).await?; - Data { + Ok(Data { alice, alice_local_user, bob, bob_local_user, - } + }) } - async fn cleanup(data: Data, pool: &mut DbPool<'_>) { - LocalUser::delete(pool, data.alice_local_user.id) - .await - .unwrap(); - LocalUser::delete(pool, data.bob_local_user.id) - .await - .unwrap(); - Person::delete(pool, data.alice.id).await.unwrap(); - Person::delete(pool, data.bob.id).await.unwrap(); - Instance::delete(pool, data.bob.instance_id).await.unwrap(); + async fn cleanup(data: Data, pool: &mut DbPool<'_>) -> LemmyResult<()> { + LocalUser::delete(pool, data.alice_local_user.id).await?; + LocalUser::delete(pool, data.bob_local_user.id).await?; + Person::delete(pool, data.alice.id).await?; + Person::delete(pool, data.bob.id).await?; + Instance::delete(pool, data.bob.instance_id).await?; + Ok(()) } #[tokio::test] #[serial] - async fn exclude_deleted() { + async fn exclude_deleted() -> LemmyResult<()> { let pool = &build_db_pool_for_tests().await; let pool = &mut pool.into(); - let data = init_data(pool).await; + let data = init_data(pool).await?; Person::update( pool, @@ -245,8 +252,7 @@ mod tests { ..Default::default() }, ) - .await - .unwrap(); + .await?; let read = PersonView::read(pool, data.alice.id).await; assert_eq!(read.err(), Some(NotFound)); @@ -256,20 +262,19 @@ mod tests { ..Default::default() } .list(pool) - .await - .unwrap(); + .await?; assert_length!(1, list); assert_eq!(list[0].person.id, data.bob.id); - cleanup(data, pool).await; + cleanup(data, pool).await } #[tokio::test] #[serial] - async fn list_banned() { + async fn list_banned() -> LemmyResult<()> { let pool = &build_db_pool_for_tests().await; let pool = &mut pool.into(); - let data = init_data(pool).await; + let data = init_data(pool).await?; Person::update( pool, @@ -279,22 +284,21 @@ mod tests { ..Default::default() }, ) - .await - .unwrap(); + .await?; - let list = PersonView::banned(pool).await.unwrap(); + let list = PersonView::banned(pool).await?; assert_length!(1, list); assert_eq!(list[0].person.id, data.alice.id); - cleanup(data, pool).await; + cleanup(data, pool).await } #[tokio::test] #[serial] - async fn list_admins() { + async fn list_admins() -> LemmyResult<()> { let pool = &build_db_pool_for_tests().await; let pool = &mut pool.into(); - let data = init_data(pool).await; + let data = init_data(pool).await?; LocalUser::update( pool, @@ -304,22 +308,45 @@ mod tests { ..Default::default() }, ) - .await - .unwrap(); + .await?; - let list = PersonView::admins(pool).await.unwrap(); + let list = PersonView::admins(pool).await?; assert_length!(1, list); assert_eq!(list[0].person.id, data.alice.id); - let is_admin = PersonView::read(pool, data.alice.id) - .await - .unwrap() - .is_admin; + let is_admin = PersonView::read(pool, data.alice.id).await?.is_admin; assert!(is_admin); - let is_admin = PersonView::read(pool, data.bob.id).await.unwrap().is_admin; + let is_admin = PersonView::read(pool, data.bob.id).await?.is_admin; assert!(!is_admin); - cleanup(data, pool).await; + cleanup(data, pool).await + } + + #[tokio::test] + #[serial] + async fn listing_type() -> LemmyResult<()> { + let pool = &build_db_pool_for_tests().await; + let pool = &mut pool.into(); + let data = init_data(pool).await?; + + let list = PersonQuery { + listing_type: Some(ListingType::Local), + ..Default::default() + } + .list(pool) + .await?; + assert_length!(1, list); + assert_eq!(list[0].person.id, data.alice.id); + + let list = PersonQuery { + listing_type: Some(ListingType::All), + ..Default::default() + } + .list(pool) + .await?; + assert_length!(2, list); + + cleanup(data, pool).await } }