Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
callmeclover committed Apr 24, 2024
1 parent 72d06a0 commit 474c1fd
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ async fn handle_socket(socket: WebSocket, _who: SocketAddr, state: Arc<AppState>
*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.
Expand Down Expand Up @@ -170,7 +170,7 @@ async fn handle_socket(socket: WebSocket, _who: SocketAddr, state: Arc<AppState>
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;
}
Expand All @@ -191,7 +191,7 @@ async fn handle_socket(socket: WebSocket, _who: SocketAddr, state: Arc<AppState>
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;
Expand Down
13 changes: 6 additions & 7 deletions src/user/auth.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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
Expand All @@ -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<Output = Self> {
let db: DatabaseConnection = Database::connect(uri).await.expect("couldn't connect to database!");

return Self {
connection: db
Expand All @@ -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<i32, Box<dyn Error>> {
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)
}
}
}
11 changes: 7 additions & 4 deletions src/user/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
}

Expand All @@ -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")]
Expand Down

0 comments on commit 474c1fd

Please sign in to comment.