diff options
-rw-r--r-- | Cargo.lock | 2 | ||||
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/database.rs | 15 |
3 files changed, 16 insertions, 3 deletions
@@ -1143,7 +1143,7 @@ dependencies = [ [[package]] name = "rocket_test" -version = "0.2.2" +version = "0.2.3" dependencies = [ "chrono", "rocket", @@ -1,6 +1,6 @@ [package] name = "rocket_test" -version = "0.2.2" +version = "0.2.3" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/database.rs b/src/database.rs index 7905552..89cc213 100644 --- a/src/database.rs +++ b/src/database.rs @@ -112,11 +112,16 @@ impl Database { }, None => return Err("User not found"), } - + // Truncate whitespace on the ends of the message let trunc_message = msg.message().trim().to_string(); msg.set_message(trunc_message); + // Check if message is empty + if msg.message().len() < 1 { + return Err("Message cannot be empty") + } + let query = "INSERT INTO messages (date, sender, message, id, reply_to, deleted) VALUES (:date, :sender, :message, :id, :reply_to, :deleted)"; let statement = self.db.prepare(query).unwrap().into_iter() .bind::<&[(_, sqlite::Value)]>(&[ @@ -209,6 +214,14 @@ impl Database { } pub fn create_user(&mut self, name: String) -> Result<UID, &'static str> { + // Truncate whitespace on the ends of the username + let name = name.trim().to_string(); + + // No empty usernames + if name.len() < 1 { + return Err("Username cannot be empty") + } + let id: UID = self.get_user_count().into(); let query = "INSERT INTO users (id, username, deleted) VALUES (:id, :name, false)"; |