From 3caac1927468112ff4bf883aaa271b0205362b40 Mon Sep 17 00:00:00 2001 From: curly Date: Wed, 15 Feb 2023 12:04:01 -0700 Subject: No empty usernames, messages --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/database.rs | 15 ++++++++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 91f7432..b913c38 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1143,7 +1143,7 @@ dependencies = [ [[package]] name = "rocket_test" -version = "0.2.2" +version = "0.2.3" dependencies = [ "chrono", "rocket", diff --git a/Cargo.toml b/Cargo.toml index 09c8487..8f6c23a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 { + // 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)"; -- cgit v1.2.3