Accept an optional email address when creating new users
This commit is contained in:
parent
b751156cd7
commit
7a8ff5abd7
4 changed files with 88 additions and 34 deletions
|
@ -1,4 +1,5 @@
|
||||||
ALTER TABLE users
|
ALTER TABLE users
|
||||||
|
ADD email_address VARCHAR(255) DEFAULT NULL,
|
||||||
ADD invite_code VARCHAR(64),
|
ADD invite_code VARCHAR(64),
|
||||||
ADD invite_count INTEGER NOT NULL DEFAULT 0,
|
ADD invite_count INTEGER NOT NULL DEFAULT 0,
|
||||||
ADD inviter_id INTEGER REFERENCES users (id),
|
ADD inviter_id INTEGER REFERENCES users (id),
|
||||||
|
|
|
@ -35,7 +35,6 @@ pub fn routes(rpc_server: &Arc<crate::rpc::Server>, state: Arc<AppState>) -> Rou
|
||||||
.layer(Extension(rpc_server.clone()))
|
.layer(Extension(rpc_server.clone()))
|
||||||
.layer(middleware::from_fn(validate_api_token)),
|
.layer(middleware::from_fn(validate_api_token)),
|
||||||
)
|
)
|
||||||
// TODO: Compression on API routes?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn validate_api_token<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse {
|
pub async fn validate_api_token<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse {
|
||||||
|
@ -74,10 +73,11 @@ async fn get_users(Extension(app): Extension<Arc<AppState>>) -> Result<Json<Vec<
|
||||||
Ok(Json(users))
|
Ok(Json(users))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize, Debug)]
|
||||||
struct CreateUserParams {
|
struct CreateUserParams {
|
||||||
github_login: String,
|
github_login: String,
|
||||||
invite_code: Option<String>,
|
invite_code: Option<String>,
|
||||||
|
email_address: Option<String>,
|
||||||
admin: bool,
|
admin: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,10 +86,16 @@ async fn create_user(
|
||||||
Extension(app): Extension<Arc<AppState>>,
|
Extension(app): Extension<Arc<AppState>>,
|
||||||
Extension(rpc_server): Extension<Arc<crate::rpc::Server>>,
|
Extension(rpc_server): Extension<Arc<crate::rpc::Server>>,
|
||||||
) -> Result<Json<User>> {
|
) -> Result<Json<User>> {
|
||||||
|
println!("{:?}", params);
|
||||||
|
|
||||||
let user_id = if let Some(invite_code) = params.invite_code {
|
let user_id = if let Some(invite_code) = params.invite_code {
|
||||||
let invitee_id = app
|
let invitee_id = app
|
||||||
.db
|
.db
|
||||||
.redeem_invite_code(&invite_code, ¶ms.github_login)
|
.redeem_invite_code(
|
||||||
|
&invite_code,
|
||||||
|
¶ms.github_login,
|
||||||
|
params.email_address.as_deref(),
|
||||||
|
)
|
||||||
.await?;
|
.await?;
|
||||||
rpc_server
|
rpc_server
|
||||||
.invite_code_redeemed(&invite_code, invitee_id)
|
.invite_code_redeemed(&invite_code, invitee_id)
|
||||||
|
@ -98,7 +104,11 @@ async fn create_user(
|
||||||
invitee_id
|
invitee_id
|
||||||
} else {
|
} else {
|
||||||
app.db
|
app.db
|
||||||
.create_user(¶ms.github_login, params.admin)
|
.create_user(
|
||||||
|
¶ms.github_login,
|
||||||
|
params.email_address.as_deref(),
|
||||||
|
params.admin,
|
||||||
|
)
|
||||||
.await?
|
.await?
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,12 @@ use time::OffsetDateTime;
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
pub trait Db: Send + Sync {
|
pub trait Db: Send + Sync {
|
||||||
async fn create_user(&self, github_login: &str, admin: bool) -> Result<UserId>;
|
async fn create_user(
|
||||||
|
&self,
|
||||||
|
github_login: &str,
|
||||||
|
email_address: Option<&str>,
|
||||||
|
admin: bool,
|
||||||
|
) -> Result<UserId>;
|
||||||
async fn get_all_users(&self) -> Result<Vec<User>>;
|
async fn get_all_users(&self) -> Result<Vec<User>>;
|
||||||
async fn fuzzy_search_users(&self, query: &str, limit: u32) -> Result<Vec<User>>;
|
async fn fuzzy_search_users(&self, query: &str, limit: u32) -> Result<Vec<User>>;
|
||||||
async fn get_user_by_id(&self, id: UserId) -> Result<Option<User>>;
|
async fn get_user_by_id(&self, id: UserId) -> Result<Option<User>>;
|
||||||
|
@ -24,7 +29,12 @@ pub trait Db: Send + Sync {
|
||||||
async fn set_invite_count(&self, id: UserId, count: u32) -> Result<()>;
|
async fn set_invite_count(&self, id: UserId, count: u32) -> Result<()>;
|
||||||
async fn get_invite_code_for_user(&self, id: UserId) -> Result<Option<(String, u32)>>;
|
async fn get_invite_code_for_user(&self, id: UserId) -> Result<Option<(String, u32)>>;
|
||||||
async fn get_user_for_invite_code(&self, code: &str) -> Result<User>;
|
async fn get_user_for_invite_code(&self, code: &str) -> Result<User>;
|
||||||
async fn redeem_invite_code(&self, code: &str, login: &str) -> Result<UserId>;
|
async fn redeem_invite_code(
|
||||||
|
&self,
|
||||||
|
code: &str,
|
||||||
|
login: &str,
|
||||||
|
email_address: Option<&str>,
|
||||||
|
) -> Result<UserId>;
|
||||||
|
|
||||||
async fn get_contacts(&self, id: UserId) -> Result<Vec<Contact>>;
|
async fn get_contacts(&self, id: UserId) -> Result<Vec<Contact>>;
|
||||||
async fn has_contact(&self, user_id_a: UserId, user_id_b: UserId) -> Result<bool>;
|
async fn has_contact(&self, user_id_a: UserId, user_id_b: UserId) -> Result<bool>;
|
||||||
|
@ -110,15 +120,21 @@ impl PostgresDb {
|
||||||
impl Db for PostgresDb {
|
impl Db for PostgresDb {
|
||||||
// users
|
// users
|
||||||
|
|
||||||
async fn create_user(&self, github_login: &str, admin: bool) -> Result<UserId> {
|
async fn create_user(
|
||||||
|
&self,
|
||||||
|
github_login: &str,
|
||||||
|
email_address: Option<&str>,
|
||||||
|
admin: bool,
|
||||||
|
) -> Result<UserId> {
|
||||||
let query = "
|
let query = "
|
||||||
INSERT INTO users (github_login, admin)
|
INSERT INTO users (github_login, email_address, admin)
|
||||||
VALUES ($1, $2)
|
VALUES ($1, $2, $3)
|
||||||
ON CONFLICT (github_login) DO UPDATE SET github_login = excluded.github_login
|
ON CONFLICT (github_login) DO UPDATE SET github_login = excluded.github_login
|
||||||
RETURNING id
|
RETURNING id
|
||||||
";
|
";
|
||||||
Ok(sqlx::query_scalar(query)
|
Ok(sqlx::query_scalar(query)
|
||||||
.bind(github_login)
|
.bind(github_login)
|
||||||
|
.bind(email_address)
|
||||||
.bind(admin)
|
.bind(admin)
|
||||||
.fetch_one(&self.pool)
|
.fetch_one(&self.pool)
|
||||||
.await
|
.await
|
||||||
|
@ -275,7 +291,12 @@ impl Db for PostgresDb {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn redeem_invite_code(&self, code: &str, login: &str) -> Result<UserId> {
|
async fn redeem_invite_code(
|
||||||
|
&self,
|
||||||
|
code: &str,
|
||||||
|
login: &str,
|
||||||
|
email_address: Option<&str>,
|
||||||
|
) -> Result<UserId> {
|
||||||
let mut tx = self.pool.begin().await?;
|
let mut tx = self.pool.begin().await?;
|
||||||
|
|
||||||
let inviter_id: Option<UserId> = sqlx::query_scalar(
|
let inviter_id: Option<UserId> = sqlx::query_scalar(
|
||||||
|
@ -317,13 +338,14 @@ impl Db for PostgresDb {
|
||||||
let invitee_id = sqlx::query_scalar(
|
let invitee_id = sqlx::query_scalar(
|
||||||
"
|
"
|
||||||
INSERT INTO users
|
INSERT INTO users
|
||||||
(github_login, admin, inviter_id)
|
(github_login, email_address, admin, inviter_id)
|
||||||
VALUES
|
VALUES
|
||||||
($1, 'f', $2)
|
($1, $2, 'f', $3)
|
||||||
RETURNING id
|
RETURNING id
|
||||||
",
|
",
|
||||||
)
|
)
|
||||||
.bind(login)
|
.bind(login)
|
||||||
|
.bind(email_address)
|
||||||
.bind(inviter_id)
|
.bind(inviter_id)
|
||||||
.fetch_one(&mut tx)
|
.fetch_one(&mut tx)
|
||||||
.await
|
.await
|
||||||
|
@ -855,6 +877,7 @@ id_type!(UserId);
|
||||||
pub struct User {
|
pub struct User {
|
||||||
pub id: UserId,
|
pub id: UserId,
|
||||||
pub github_login: String,
|
pub github_login: String,
|
||||||
|
pub email_address: Option<String>,
|
||||||
pub admin: bool,
|
pub admin: bool,
|
||||||
pub invite_code: Option<String>,
|
pub invite_code: Option<String>,
|
||||||
pub invite_count: i32,
|
pub invite_count: i32,
|
||||||
|
@ -956,10 +979,10 @@ pub mod tests {
|
||||||
] {
|
] {
|
||||||
let db = test_db.db();
|
let db = test_db.db();
|
||||||
|
|
||||||
let user = db.create_user("user", false).await.unwrap();
|
let user = db.create_user("user", None, false).await.unwrap();
|
||||||
let friend1 = db.create_user("friend-1", false).await.unwrap();
|
let friend1 = db.create_user("friend-1", None, false).await.unwrap();
|
||||||
let friend2 = db.create_user("friend-2", false).await.unwrap();
|
let friend2 = db.create_user("friend-2", None, false).await.unwrap();
|
||||||
let friend3 = db.create_user("friend-3", false).await.unwrap();
|
let friend3 = db.create_user("friend-3", None, false).await.unwrap();
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
db.get_users_by_ids(vec![user, friend1, friend2, friend3])
|
db.get_users_by_ids(vec![user, friend1, friend2, friend3])
|
||||||
|
@ -1002,7 +1025,7 @@ pub mod tests {
|
||||||
TestDb::fake(Arc::new(gpui::executor::Background::new())),
|
TestDb::fake(Arc::new(gpui::executor::Background::new())),
|
||||||
] {
|
] {
|
||||||
let db = test_db.db();
|
let db = test_db.db();
|
||||||
let user = db.create_user("user", false).await.unwrap();
|
let user = db.create_user("user", None, false).await.unwrap();
|
||||||
let org = db.create_org("org", "org").await.unwrap();
|
let org = db.create_org("org", "org").await.unwrap();
|
||||||
let channel = db.create_org_channel(org, "channel").await.unwrap();
|
let channel = db.create_org_channel(org, "channel").await.unwrap();
|
||||||
for i in 0..10 {
|
for i in 0..10 {
|
||||||
|
@ -1041,7 +1064,7 @@ pub mod tests {
|
||||||
TestDb::fake(Arc::new(gpui::executor::Background::new())),
|
TestDb::fake(Arc::new(gpui::executor::Background::new())),
|
||||||
] {
|
] {
|
||||||
let db = test_db.db();
|
let db = test_db.db();
|
||||||
let user = db.create_user("user", false).await.unwrap();
|
let user = db.create_user("user", None, false).await.unwrap();
|
||||||
let org = db.create_org("org", "org").await.unwrap();
|
let org = db.create_org("org", "org").await.unwrap();
|
||||||
let channel = db.create_org_channel(org, "channel").await.unwrap();
|
let channel = db.create_org_channel(org, "channel").await.unwrap();
|
||||||
|
|
||||||
|
@ -1072,7 +1095,7 @@ pub mod tests {
|
||||||
async fn test_create_access_tokens() {
|
async fn test_create_access_tokens() {
|
||||||
let test_db = TestDb::postgres().await;
|
let test_db = TestDb::postgres().await;
|
||||||
let db = test_db.db();
|
let db = test_db.db();
|
||||||
let user = db.create_user("the-user", false).await.unwrap();
|
let user = db.create_user("the-user", None, false).await.unwrap();
|
||||||
|
|
||||||
db.create_access_token_hash(user, "h1", 3).await.unwrap();
|
db.create_access_token_hash(user, "h1", 3).await.unwrap();
|
||||||
db.create_access_token_hash(user, "h2", 3).await.unwrap();
|
db.create_access_token_hash(user, "h2", 3).await.unwrap();
|
||||||
|
@ -1120,7 +1143,7 @@ pub mod tests {
|
||||||
"delaware",
|
"delaware",
|
||||||
"rhode-island",
|
"rhode-island",
|
||||||
] {
|
] {
|
||||||
db.create_user(github_login, false).await.unwrap();
|
db.create_user(github_login, None, false).await.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -1150,9 +1173,9 @@ pub mod tests {
|
||||||
] {
|
] {
|
||||||
let db = test_db.db();
|
let db = test_db.db();
|
||||||
|
|
||||||
let user_1 = db.create_user("user1", false).await.unwrap();
|
let user_1 = db.create_user("user1", None, false).await.unwrap();
|
||||||
let user_2 = db.create_user("user2", false).await.unwrap();
|
let user_2 = db.create_user("user2", None, false).await.unwrap();
|
||||||
let user_3 = db.create_user("user3", false).await.unwrap();
|
let user_3 = db.create_user("user3", None, false).await.unwrap();
|
||||||
|
|
||||||
// User starts with no contacts
|
// User starts with no contacts
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -1366,7 +1389,7 @@ pub mod tests {
|
||||||
async fn test_invite_codes() {
|
async fn test_invite_codes() {
|
||||||
let postgres = TestDb::postgres().await;
|
let postgres = TestDb::postgres().await;
|
||||||
let db = postgres.db();
|
let db = postgres.db();
|
||||||
let user1 = db.create_user("user-1", false).await.unwrap();
|
let user1 = db.create_user("user-1", None, false).await.unwrap();
|
||||||
|
|
||||||
// Initially, user 1 has no invite code
|
// Initially, user 1 has no invite code
|
||||||
assert_eq!(db.get_invite_code_for_user(user1).await.unwrap(), None);
|
assert_eq!(db.get_invite_code_for_user(user1).await.unwrap(), None);
|
||||||
|
@ -1378,7 +1401,10 @@ pub mod tests {
|
||||||
assert_eq!(invite_count, 2);
|
assert_eq!(invite_count, 2);
|
||||||
|
|
||||||
// User 2 redeems the invite code and becomes a contact of user 1.
|
// User 2 redeems the invite code and becomes a contact of user 1.
|
||||||
let user2 = db.redeem_invite_code(&invite_code, "user-2").await.unwrap();
|
let user2 = db
|
||||||
|
.redeem_invite_code(&invite_code, "user-2", None)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
|
let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
|
||||||
assert_eq!(invite_count, 1);
|
assert_eq!(invite_count, 1);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -1409,7 +1435,10 @@ pub mod tests {
|
||||||
);
|
);
|
||||||
|
|
||||||
// User 3 redeems the invite code and becomes a contact of user 1.
|
// User 3 redeems the invite code and becomes a contact of user 1.
|
||||||
let user3 = db.redeem_invite_code(&invite_code, "user-3").await.unwrap();
|
let user3 = db
|
||||||
|
.redeem_invite_code(&invite_code, "user-3", None)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
|
let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
|
||||||
assert_eq!(invite_count, 0);
|
assert_eq!(invite_count, 0);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -1444,7 +1473,7 @@ pub mod tests {
|
||||||
);
|
);
|
||||||
|
|
||||||
// Trying to reedem the code for the third time results in an error.
|
// Trying to reedem the code for the third time results in an error.
|
||||||
db.redeem_invite_code(&invite_code, "user-4")
|
db.redeem_invite_code(&invite_code, "user-4", None)
|
||||||
.await
|
.await
|
||||||
.unwrap_err();
|
.unwrap_err();
|
||||||
|
|
||||||
|
@ -1456,7 +1485,10 @@ pub mod tests {
|
||||||
assert_eq!(invite_count, 2);
|
assert_eq!(invite_count, 2);
|
||||||
|
|
||||||
// User 4 can now redeem the invite code and becomes a contact of user 1.
|
// User 4 can now redeem the invite code and becomes a contact of user 1.
|
||||||
let user4 = db.redeem_invite_code(&invite_code, "user-4").await.unwrap();
|
let user4 = db
|
||||||
|
.redeem_invite_code(&invite_code, "user-4", None)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
|
let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
|
||||||
assert_eq!(invite_count, 1);
|
assert_eq!(invite_count, 1);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
@ -1495,7 +1527,7 @@ pub mod tests {
|
||||||
);
|
);
|
||||||
|
|
||||||
// An existing user cannot redeem invite codes.
|
// An existing user cannot redeem invite codes.
|
||||||
db.redeem_invite_code(&invite_code, "user-2")
|
db.redeem_invite_code(&invite_code, "user-2", None)
|
||||||
.await
|
.await
|
||||||
.unwrap_err();
|
.unwrap_err();
|
||||||
let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
|
let (_, invite_count) = db.get_invite_code_for_user(user1).await.unwrap().unwrap();
|
||||||
|
@ -1594,7 +1626,12 @@ pub mod tests {
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl Db for FakeDb {
|
impl Db for FakeDb {
|
||||||
async fn create_user(&self, github_login: &str, admin: bool) -> Result<UserId> {
|
async fn create_user(
|
||||||
|
&self,
|
||||||
|
github_login: &str,
|
||||||
|
email_address: Option<&str>,
|
||||||
|
admin: bool,
|
||||||
|
) -> Result<UserId> {
|
||||||
self.background.simulate_random_delay().await;
|
self.background.simulate_random_delay().await;
|
||||||
|
|
||||||
let mut users = self.users.lock();
|
let mut users = self.users.lock();
|
||||||
|
@ -1610,6 +1647,7 @@ pub mod tests {
|
||||||
User {
|
User {
|
||||||
id: user_id,
|
id: user_id,
|
||||||
github_login: github_login.to_string(),
|
github_login: github_login.to_string(),
|
||||||
|
email_address: email_address.map(str::to_string),
|
||||||
admin,
|
admin,
|
||||||
invite_code: None,
|
invite_code: None,
|
||||||
invite_count: 0,
|
invite_count: 0,
|
||||||
|
@ -1679,7 +1717,12 @@ pub mod tests {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn redeem_invite_code(&self, _code: &str, _login: &str) -> Result<UserId> {
|
async fn redeem_invite_code(
|
||||||
|
&self,
|
||||||
|
_code: &str,
|
||||||
|
_login: &str,
|
||||||
|
_email_address: Option<&str>,
|
||||||
|
) -> Result<UserId> {
|
||||||
unimplemented!()
|
unimplemented!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6065,9 +6065,9 @@ mod tests {
|
||||||
|
|
||||||
let mut server = TestServer::start(cx.foreground(), cx.background()).await;
|
let mut server = TestServer::start(cx.foreground(), cx.background()).await;
|
||||||
let db = server.app_state.db.clone();
|
let db = server.app_state.db.clone();
|
||||||
let host_user_id = db.create_user("host", false).await.unwrap();
|
let host_user_id = db.create_user("host", None, false).await.unwrap();
|
||||||
for username in ["guest-1", "guest-2", "guest-3", "guest-4"] {
|
for username in ["guest-1", "guest-2", "guest-3", "guest-4"] {
|
||||||
let guest_user_id = db.create_user(username, false).await.unwrap();
|
let guest_user_id = db.create_user(username, None, false).await.unwrap();
|
||||||
server
|
server
|
||||||
.app_state
|
.app_state
|
||||||
.db
|
.db
|
||||||
|
@ -6582,7 +6582,7 @@ mod tests {
|
||||||
if let Ok(Some(user)) = self.app_state.db.get_user_by_github_login(name).await {
|
if let Ok(Some(user)) = self.app_state.db.get_user_by_github_login(name).await {
|
||||||
user.id
|
user.id
|
||||||
} else {
|
} else {
|
||||||
self.app_state.db.create_user(name, false).await.unwrap()
|
self.app_state.db.create_user(name, None, false).await.unwrap()
|
||||||
};
|
};
|
||||||
let client_name = name.to_string();
|
let client_name = name.to_string();
|
||||||
let mut client = Client::new(http.clone());
|
let mut client = Client::new(http.clone());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue