aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcurly <curlybryce@protonmail.com>2023-02-15 12:04:01 -0700
committercurly <curlybryce@protonmail.com>2023-02-15 12:04:01 -0700
commit3caac1927468112ff4bf883aaa271b0205362b40 (patch)
tree6e2d3bb0ce9e2f61cacac68b440ec0255a69b2c2 /src
parent31d1c381a6e834c6c7069806749b0a11d0d15e05 (diff)
downloadrocket_test-3caac1927468112ff4bf883aaa271b0205362b40.tar.gz
rocket_test-3caac1927468112ff4bf883aaa271b0205362b40.tar.bz2
rocket_test-3caac1927468112ff4bf883aaa271b0205362b40.zip
No empty usernames, messages
Diffstat (limited to 'src')
-rw-r--r--src/database.rs15
1 files changed, 14 insertions, 1 deletions
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)";