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 --- src/database.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/database.rs') 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