From b0eb6927604f8465c8ade745e83c4d1846f3bd1f Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 24 Jun 2022 17:21:58 +0200 Subject: [PATCH 1/2] WIP --- crates/collab/src/api.rs | 16 +++++++++ crates/collab/src/db.rs | 78 ++++++++++++++++++++++++++++++++-------- 2 files changed, 79 insertions(+), 15 deletions(-) diff --git a/crates/collab/src/api.rs b/crates/collab/src/api.rs index 6440e8cb3048b0709b5efe64f9b1089ed74930bf..bf35999b7fb08fa4b5f66c6db2d2baa53986710b 100644 --- a/crates/collab/src/api.rs +++ b/crates/collab/src/api.rs @@ -31,6 +31,7 @@ pub fn routes(rpc_server: &Arc, state: Arc) -> Router, + Extension(app): Extension>, +) -> Result>> { + Ok(Json( + app.db + .get_users_with_no_invites(params.invited_by_another_user) + .await?, + )) +} + #[derive(Debug, Deserialize)] struct Panic { version: String, diff --git a/crates/collab/src/db.rs b/crates/collab/src/db.rs index 5e8c45bf87ca435dc439dd16be722bb9479485e6..3224e9dbdb59044d29d6e404c9be38db181ab6ba 100644 --- a/crates/collab/src/db.rs +++ b/crates/collab/src/db.rs @@ -25,6 +25,7 @@ pub trait Db: Send + Sync { async fn fuzzy_search_users(&self, query: &str, limit: u32) -> Result>; async fn get_user_by_id(&self, id: UserId) -> Result>; async fn get_users_by_ids(&self, ids: Vec) -> Result>; + async fn get_users_with_no_invites(&self, invited_by_another_user: bool) -> Result>; async fn get_user_by_github_login(&self, github_login: &str) -> Result>; async fn set_user_is_admin(&self, id: UserId, is_admin: bool) -> Result<()>; async fn set_user_connected_once(&self, id: UserId, connected_once: bool) -> Result<()>; @@ -260,6 +261,18 @@ impl Db for PostgresDb { .await?) } + async fn get_users_with_no_invites(&self, invited_by_another_user: bool) -> Result> { + let query = " + SELECT users.* + FROM users + WHERE invite_count = 0 + "; + Ok(sqlx::query_as(query) + .bind(&ids) + .fetch_all(&self.pool) + .await?) + } + async fn get_user_by_github_login(&self, github_login: &str) -> Result> { let query = "SELECT * FROM users WHERE github_login = $1 LIMIT 1"; Ok(sqlx::query_as(query) @@ -1363,21 +1376,56 @@ pub mod tests { let user = db.create_user("user_1", None, false).await.unwrap(); let project = db.register_project(user).await.unwrap(); - db.update_worktree_extensions(project, 100, Default::default()).await.unwrap(); - db.update_worktree_extensions(project, 100, [("rs".to_string(), 5), ("md".to_string(), 3)].into_iter().collect()).await.unwrap(); - db.update_worktree_extensions(project, 100, [("rs".to_string(), 6), ("md".to_string(), 5)].into_iter().collect()).await.unwrap(); - db.update_worktree_extensions(project, 101, [("ts".to_string(), 2), ("md".to_string(), 1)].into_iter().collect()).await.unwrap(); - - assert_eq!(db.get_project_extensions(project).await.unwrap(), [ - (100, [ - ("rs".into(), 6), - ("md".into(), 5), - ].into_iter().collect::>()), - (101, [ - ("ts".into(), 2), - ("md".into(), 1), - ].into_iter().collect::>()) - ].into_iter().collect()); + db.update_worktree_extensions(project, 100, Default::default()) + .await + .unwrap(); + db.update_worktree_extensions( + project, + 100, + [("rs".to_string(), 5), ("md".to_string(), 3)] + .into_iter() + .collect(), + ) + .await + .unwrap(); + db.update_worktree_extensions( + project, + 100, + [("rs".to_string(), 6), ("md".to_string(), 5)] + .into_iter() + .collect(), + ) + .await + .unwrap(); + db.update_worktree_extensions( + project, + 101, + [("ts".to_string(), 2), ("md".to_string(), 1)] + .into_iter() + .collect(), + ) + .await + .unwrap(); + + assert_eq!( + db.get_project_extensions(project).await.unwrap(), + [ + ( + 100, + [("rs".into(), 6), ("md".into(), 5),] + .into_iter() + .collect::>() + ), + ( + 101, + [("ts".into(), 2), ("md".into(), 1),] + .into_iter() + .collect::>() + ) + ] + .into_iter() + .collect() + ); } #[tokio::test(flavor = "multi_thread")] From 4da3005b5c67ba94409c332d0dbca25837d84bbb Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 24 Jun 2022 09:57:52 -0600 Subject: [PATCH 2/2] Allow users with no invites to be fetched from the API --- crates/collab/src/api.rs | 1 + crates/collab/src/db.rs | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/crates/collab/src/api.rs b/crates/collab/src/api.rs index bf35999b7fb08fa4b5f66c6db2d2baa53986710b..0c91a5a0794020ba548014a242abce4352572175 100644 --- a/crates/collab/src/api.rs +++ b/crates/collab/src/api.rs @@ -229,6 +229,7 @@ async fn create_users( Ok(Json(users)) } +#[derive(Debug, Deserialize)] struct GetUsersWithNoInvites { invited_by_another_user: bool, } diff --git a/crates/collab/src/db.rs b/crates/collab/src/db.rs index 3224e9dbdb59044d29d6e404c9be38db181ab6ba..ce494db59bb6222b90533d0129f86c21e651e697 100644 --- a/crates/collab/src/db.rs +++ b/crates/collab/src/db.rs @@ -262,15 +262,17 @@ impl Db for PostgresDb { } async fn get_users_with_no_invites(&self, invited_by_another_user: bool) -> Result> { - let query = " + let query = format!( + " SELECT users.* FROM users WHERE invite_count = 0 - "; - Ok(sqlx::query_as(query) - .bind(&ids) - .fetch_all(&self.pool) - .await?) + AND inviter_id IS{} NULL + ", + if invited_by_another_user { " NOT" } else { "" } + ); + + Ok(sqlx::query_as(&query).fetch_all(&self.pool).await?) } async fn get_user_by_github_login(&self, github_login: &str) -> Result> { @@ -2188,6 +2190,10 @@ pub mod tests { Ok(ids.iter().filter_map(|id| users.get(id).cloned()).collect()) } + async fn get_users_with_no_invites(&self, _: bool) -> Result> { + unimplemented!() + } + async fn get_user_by_github_login(&self, github_login: &str) -> Result> { Ok(self .users