Skip to content

Commit

Permalink
finish tomorrow; get some sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
callmeclover committed Apr 25, 2024
1 parent 1d15377 commit 0ad1fe7
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 32 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ ammonia = "4.0.0"
kuchikiki = "0.8.2"
js-sys = "0.3.69"
uuid = { version = "1.8.0", features = ["v4", "fast-rng", "serde"] }
sea-orm = { version = "0.12", features = ["sqlx-postgres", "runtime-tokio-rustls", "macros", "with-uuid", "with-json"] }
postgres = { version = "0.19.7", features = ["with-uuid-1", "with-serde_json-1"] }

7 changes: 3 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static USER_ID: Lazy<Arc<Mutex<i32>>> = Lazy::new(|| Arc::new(Mutex::new(0)));
lazy_static::lazy_static! {
static ref DB_CLIENT: Arc<Mutex<DatabaseConnectix>> = {
tokio::runtime::Runtime::new().unwrap().block_on(async {
return Arc::new(Mutex::new(DatabaseConnectix::default().await));
return Arc::new(Mutex::new(DatabaseConnectix::default()));
})
};
}
Expand Down Expand Up @@ -93,7 +93,6 @@ async fn ws_handler(
async fn handle_socket(socket: WebSocket, _who: SocketAddr, state: Arc<AppState>) {
let (mut sender, mut receiver) = socket.split();
*USER_ID.lock().unwrap() += 1;
let user_id = USER_ID.lock().unwrap().clone();

let user = Arc::new(Mutex::new(User::new("".into())));

Expand All @@ -102,7 +101,7 @@ async fn handle_socket(socket: WebSocket, _who: SocketAddr, state: Arc<AppState>
let mut rx = state.tx.subscribe();

// Now send the "joined" message to all subscribers.
let msg = format!("user with id {0} connected.", (*user.lock().unwrap()).id);
let msg = format!("user with uuid {0} connected.", user.lock().unwrap().uuid);
tracing::debug!("{msg}");

let msg_vec = (*MESSAGES.lock().unwrap().clone()).to_vec();
Expand Down Expand Up @@ -169,7 +168,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(
let _ = tx.send(
serde_json::to_string(&(UserJoin { user: user_recv.lock().unwrap().name.clone() })).expect("")
);
continue;
Expand Down
21 changes: 11 additions & 10 deletions src/user/auth.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use std::error::Error;
use sea_orm::{*, prelude::*, entity::*, error::*};
use postgres::{Client, NoTls};

use crate::user::model::{Entity as ModelEntity, Column as ModelColumn};
use super::model::Model;

pub struct DatabaseConnectix {
connection: DatabaseConnection
connection: Client
}

impl Default for DatabaseConnectix {
fn default() -> Self {
tokio::runtime::Runtime::new().unwrap().block_on(async {
let uri = std::env::var("DB_URL").unwrap();
let db: DatabaseConnection = Database::connect(uri).await.expect("couldn't connect to database!");
let mut client = Client::connect(&uri, NoTls)?;

return Self {
connection: db
connection: client
};
})

Expand All @@ -24,21 +24,22 @@ impl Default for DatabaseConnectix {
impl DatabaseConnectix {
pub fn new(uri: &str) -> Self {
tokio::runtime::Runtime::new().unwrap().block_on(async {
let db: DatabaseConnection = Database::connect(uri).await.expect("couldn't connect to database!");
let mut client = Client::connect(uri, NoTls)?;

return Self {
connection: db
connection: client
};
})
}

/// 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.contains(name)).one(&self.connection).await? {
todo!("am eepy must finish tomorrow");
/*if let Some(res) = ModelEntity::find().expr(Expr::col("id").max()).filter(ModelColumn::Name.contains(name)).one(&self.connection).await? {
if res.id == 9999 { return Err("username is taken".into()); }
Ok(res.id+1)
} else {
Ok(1)
}
}*/
}
}
20 changes: 3 additions & 17 deletions src/user/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use std::error::Error;
use rustrict::{Censor, Type};
use serde::{Serialize, Deserialize};
use uuid::Uuid;
use sea_orm::entity::prelude::*;

/// What am I?
/// A class meant to hold all the values the server uses to compute messages.
Expand Down Expand Up @@ -39,32 +38,19 @@ 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)]
#[sea_orm(table_name = "users")]
#[derive(Serialize, Deserialize, Clone)]
pub struct Model {
#[sea_orm(primary_key)]
pub user_number: i32,
#[sea_orm(column_name = "id", enum_name = "Id")]
pub id: i32,
#[sea_orm(column_name = "name", enum_name = "Name")]
pub name: String,
#[sea_orm(column_name = "uuid", enum_name = "UUID")]
pub uuid: Uuid,
#[sea_orm(column_name = "password", enum_name = "Password")]
pub password: String,
#[sea_orm(column_name = "email", enum_name = "Email")]
pub email: String,
#[sea_orm(column_name = "mod", enum_name = "Mod")]
/// This is just the DB equivalent of `glass`.
/// It's in JSON format.
pub moderation_stats: String
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}

/// What am I?
/// A stripped down version of the `User` struct so that you can send something to the other clients.
#[derive(Serialize, Deserialize, Clone)]
Expand Down Expand Up @@ -93,8 +79,8 @@ pub struct GlassModeration {

impl GlassModeration {
/// Runs the given text through a censoring filter.
/// This will add reports if it finds Type::OFFENSIVE, returning an error.
/// If it finds no Type::OFFENSIVE, but Type::EVASIVE, it will instead warn the user.
/// This will add reports if it finds `Type::OFFENSIVE`, returning an error.
/// If it finds no `Type::OFFENSIVE`, but `Type::EVASIVE`, it will instead warn the user.
/// If the user is muted, it returns an error.
pub fn process(&mut self, input: &str) -> Result<String, Box<dyn Error>> {
if self.is_muted { return Err("User is muted".into()); }
Expand Down

0 comments on commit 0ad1fe7

Please sign in to comment.