Fake auth

This commit is contained in:
2025-09-19 18:47:46 +02:00
parent 3ddc25f224
commit 68e6e538bd
8 changed files with 353 additions and 18 deletions
+12 -4
View File
@@ -100,13 +100,13 @@ pub mod handshake {
}
}
pub struct Client(pub TcpStream);
pub struct Client(TcpStream, Option<u32>);
impl Client {
/// Create a client with no timeouts
pub fn new(mut stream: TcpStream) -> crate::Result<Self> {
handshake::handle_websocket_handshake(&mut stream)?;
Ok(Client(stream))
Ok(Client(stream, None))
}
/// Create a client and set read/write timeouts (useful in prod)
@@ -122,7 +122,7 @@ impl Client {
stream.set_write_timeout(Some(t))?;
}
handshake::handle_websocket_handshake(&mut stream)?;
Ok(Client(stream))
Ok(Client(stream, None))
}
/// Send a close frame and flush. `code` is a WebSocket close code (e.g., 1000 normal).
@@ -323,11 +323,19 @@ impl Client {
pub fn read(&self) -> crate::Result<Option<WsMessage<ClientMessage>>> {
self.read_t()
}
pub fn get_uuid(&self) -> crate::Result<u32> {
self.1.ok_or(anyhow!("Invalid UUID"))
}
pub fn set_uuid(&mut self, uuid: u32) {
self.1 = Some(uuid)
}
}
impl Clone for Client {
fn clone(&self) -> Self {
Client(self.0.try_clone().expect("failed to clone TcpStream"))
Client(self.0.try_clone().expect("failed to clone TcpStream"), None)
}
}
+26 -5
View File
@@ -1,7 +1,7 @@
use crate::{ServerConfig, types::data::Message};
use rusqlite::{Connection, Result, params};
pub struct Database(pub Connection);
pub struct Database(Connection);
// General use case
impl Database {
@@ -30,13 +30,13 @@ impl Database {
pub fn insert_message(
&self,
channel_id: &str,
user_id: &str,
user_id: u32,
contents: &str,
timestamp: i64,
) -> Result<Message> {
self.0.execute(
"INSERT INTO chat (channel_id, user_id, contents, timestamp)
VALUES (?1, ?2, ?3, ?4)",
VALUES (?1, ?2, ?3, ?4)",
params![channel_id, user_id, contents, timestamp],
)?;
@@ -45,12 +45,33 @@ impl Database {
Ok(Message {
id,
channel_id: channel_id.to_string(),
from: user_id.to_string(),
from: user_id,
contents: contents.to_string(),
timestamp,
})
}
/// Delete a message from the DB
pub fn delete_message(&self, message_id: usize) -> Result<()> {
self.0
.execute("DELETE FROM chat WHERE id = ?1;", params![message_id])?;
Ok(())
}
/// Delete a message from the DB
pub fn edit_message(&self, message_id: usize, contents: &str) -> Result<()> {
self.0.execute(
"UPDATE table_name
SET contents = ?2
WHERE id = ?1;
",
params![message_id, contents],
)?;
Ok(())
}
/// Get a message by its ID
pub fn get_message_by_id(&self, message_id: usize) -> Result<Option<Message>> {
let mut stmt = self.0.prepare(
@@ -63,7 +84,7 @@ impl Database {
Ok((
row.get::<_, i64>(0)?, // id
row.get::<_, String>(1)?, // channel_id
row.get::<_, String>(2)?, // user_id
row.get::<_, u32>(2)?, // user_id
row.get::<_, String>(3)?, // contents
row.get::<_, i64>(4)?, // timestamp
))