diff --git a/src/main.rs b/src/main.rs index 747c225a..8d618397 100644 --- a/src/main.rs +++ b/src/main.rs @@ -95,7 +95,7 @@ async fn handle_socket(socket: WebSocket, _who: SocketAddr, state: Arc *USER_ID.lock().unwrap() += 1; let user_id = USER_ID.lock().unwrap().clone(); - let user = Arc::new(Mutex::new(User::new("".into(), user_id))); + let user = Arc::new(Mutex::new(User::new("".into()))); // We subscribe *before* sending the "joined" message, so that we will also // display it to our client. @@ -170,7 +170,7 @@ async fn handle_socket(socket: WebSocket, _who: SocketAddr, state: Arc MessageTypes::UserJoin(request) => { user_recv.lock().unwrap().name = request.user; let _ = state.tx.send( - serde_json::to_string(&(UserJoin { userjoin: user_recv.lock().unwrap().name.clone() })).expect("") + serde_json::to_string(&(UserJoin { user: user_recv.lock().unwrap().name.clone() })).expect("") ); continue; } @@ -191,7 +191,7 @@ async fn handle_socket(socket: WebSocket, _who: SocketAddr, state: Arc let msg = format!("{0} left.", user.lock().unwrap().name); tracing::debug!("{msg}"); let _ = state.tx.send( - serde_json::to_string(&(UserLeft { userleft: user.lock().unwrap().name.clone() })).expect("") + serde_json::to_string(&(UserLeft { user: user.lock().unwrap().name.clone() })).expect("") ); *USER_ID.lock().unwrap() -= 1; diff --git a/src/user/auth.rs b/src/user/auth.rs index 9206166f..d507f202 100644 --- a/src/user/auth.rs +++ b/src/user/auth.rs @@ -1,4 +1,3 @@ -use lazy_static::lazy_static; use std::error::Error; use sea_orm::*; use crate::user::model::{Model, Entity as ModelEntity, Column as ModelColumn}; @@ -10,7 +9,7 @@ pub struct DatabaseConnectix { impl Default for DatabaseConnectix { pub async fn default() -> Self { let uri = std::env::var("DB_URL").unwrap(); - let db: DatabaseConnection = Database::connect(uri).await?; + let db: DatabaseConnection = Database::connect(uri).await.expect("couldn't connect to database!"); return Self { connection: db @@ -19,8 +18,8 @@ impl Default for DatabaseConnectix { } impl DatabaseConnectix { - pub async fn new(uri: &str) -> Self { - let db: DatabaseConnection = Database::connect(uri).await?; + pub async fn new(uri: &str) -> impl Future { + let db: DatabaseConnection = Database::connect(uri).await.expect("couldn't connect to database!"); return Self { connection: db @@ -29,11 +28,11 @@ impl DatabaseConnectix { /// Gets a possible user id (if one exists) for a username. pub async fn get_user_id(&self, name: &str) -> Result> { - if let Some(res) = ModelEntity::find().expr(Expr::col("id").max()).filter(ModelColumn::Name.eq(name)).one(self.connection).await? { - if res.id == 9999 { return Box::new(Err("username is taken")); } + if let Some(res) = ModelEntity::find().expr(Expr::col("id").max()).filter(ModelColumn::Name.eq(name)).one(&self.connection).await? { + if res.id == 9999 { return Err("username is taken".into()); } Ok(res.id+1) } else { - Ok(0001) + Ok(1) } } } \ No newline at end of file diff --git a/src/user/model.rs b/src/user/model.rs index 564132b9..c9b04597 100644 --- a/src/user/model.rs +++ b/src/user/model.rs @@ -12,7 +12,7 @@ pub struct User { /// Why is the user id (the number after the @) not stored here? /// Because we can simplify this! Use the method `get_name_split()`. pub name: String, - pub uuid: Uuid + pub uuid: Uuid, pub glass: GlassModeration, pub sendable_user: SendableUser, // pub password: String, @@ -21,11 +21,12 @@ pub struct User { impl User { pub fn new(name: String) -> Self { + let uuid = Uuid::new_v4(); Self { name, - uuid: Uuid::new_v4(), + uuid, glass: GlassModeration::default(), - sendable_user: SendableUser::new(name, self.uuid) + sendable_user: SendableUser::new(name, uuid) } } @@ -38,9 +39,11 @@ impl User { /// What am I? /// A struct so that we can save user data in the database. -#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Debug, DeriveEntityModel)] +#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Debug, Self::DeriveEntityModel)] #[sea_orm(table_name = "users")] pub struct Model { + #[sea_orm(primary_key, auto_increment)] + pub user_number: i32, #[sea_orm(column_name = "id", enum_name = "Id")] pub id: i32, #[sea_orm(column_name = "name", enum_name = "Name")]