For now? I'm experimenting on an old concept.
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Migrate to SQLX: user.rs done.

+44 -51
-2
server/src/client_communication.rs
··· 184 184 match e { 185 185 LuminaError::DbError(crate::errors::LuminaDbError::Postgres(e)) => 186 186 error_elog!(ev_log,"While creating session token: {:?}", e), 187 - LuminaError::Bb8RunErrorPg(e) => 188 - warn_elog!(ev_log,"There was an error creating session token: {}", e), 189 187 _ => {} 190 188 } 191 189 // I would return a more specific error message
+44 -49
server/src/user.rs
··· 22 22 use crate::{LuminaError, database::DbConn, helpers::events::EventLogger, info_elog}; 23 23 use cynthia_con::CynthiaColors; 24 24 use uuid::Uuid; 25 + use crate::database::DatabaseConnections; 25 26 26 27 #[derive(Debug, Clone)] 27 28 pub struct User { ··· 62 63 async fn get_hashed_password(self, database: &DbConn) -> Result<String, LuminaError> { 63 64 match database { 64 65 DbConn::PgsqlConnection(pg_pool, _) => { 65 - let client = pg_pool.get().await?; 66 - let row = client 67 - .query_one("SELECT password FROM users WHERE id = $1", &[&self.id]) 66 + 67 + let row = sqlx::query!("SELECT password FROM users WHERE id = $1", &self.id) 68 + .fetch_one(pg_pool) 68 69 .await?; 69 - let password: String = row.get(0); 70 + let password: String = row.password; 70 71 Ok(password) 71 72 } 72 73 } ··· 83 84 bcrypt::hash(password, bcrypt::DEFAULT_COST).map_err(|_| LuminaError::BcryptError)?; 84 85 match db { 85 86 DbConn::PgsqlConnection(pg_pool, _) => { 86 - let client = pg_pool.get().await?; 87 87 // Some username and email validation should be done here 88 88 // Check if the email is already in use 89 - let email_exists = client 90 - .query("SELECT * FROM users WHERE email = $1", &[&email]) 89 + let email_exists = sqlx::query!("SELECT * FROM users WHERE email = $1", &email) 90 + .fetch_optional(pg_pool) 91 91 .await?; 92 - if !email_exists.is_empty() { 92 + if !email_exists.is_none() { 93 93 return Err(LuminaError::RegisterEmailInUse); 94 94 } 95 95 // Check if the username is already in use 96 - let username_exists = client 97 - .query("SELECT * FROM users WHERE username = $1", &[&username]) 96 + let username_exists = 97 + sqlx::query!("SELECT * FROM users WHERE username = $1", &username) 98 + .fetch_optional(pg_pool) 98 99 .await?; 99 - if !username_exists.is_empty() { 100 + if !username_exists.is_none() { 100 101 return Err(LuminaError::RegisterUsernameInUse); 101 102 } 102 103 103 - let id = client 104 - .query_one("INSERT INTO users (email, username, password) VALUES ($1, $2, $3) RETURNING id", &[&email, &username, &password]) 104 + let id = sqlx::query!("INSERT INTO users (email, username, password) VALUES ($1, $2, $3) RETURNING id", &email, &username, &password) 105 + .fetch_one(pg_pool) 105 106 .await 106 107 ?; 107 108 Ok(User { 108 - id: id.get(0), 109 + id: id.id, 109 110 email, 110 111 username, 111 112 foreign_instance_id: "".to_string(), // Default value for new users ··· 124 125 }; 125 126 match db { 126 127 DbConn::PgsqlConnection(pg_pool, _) => { 127 - let client = pg_pool.get().await?; 128 - let user = client 129 - .query_one( 130 - &format!("SELECT id, email, username, COALESCE(foreign_instance_id, '') FROM users WHERE {} = $1", identifyer_type), 131 - &[&identifier], 132 - ) 128 + let user = sqlx::query!("SELECT id, email, username, coalesce(foreign_instance_id, '') as foreign_instance_id FROM users WHERE $1 = $2", identifyer_type, &identifier) 129 + 130 + .fetch_one(pg_pool) 133 131 .await 134 132 ?; 135 133 Ok(User { 136 - id: user.get(0), 137 - email: user.get(1), 138 - username: user.get(2), 139 - foreign_instance_id: user.get(3), 134 + id: user.id, 135 + email: user.email, 136 + username: user.username, 137 + foreign_instance_id: user.foreign_instance_id.unwrap_or("".to_string()), 140 138 }) 141 139 } 142 140 } ··· 151 149 let user_id = user.id; 152 150 match db { 153 151 DbConn::PgsqlConnection(pg_pool, _) => { 154 - let client = pg_pool.get().await?; 155 152 let session_key = Uuid::new_v4().to_string(); 156 - let id = client 157 - .query_one( 153 + let id = sqlx::query!( 158 154 "INSERT INTO sessions (user_id, session_key) VALUES ($1, $2) RETURNING id", 159 - &[&user_id, &session_key], 160 - ) 155 + &user_id, &session_key, 156 + ).fetch_one(pg_pool) 161 157 .await?; 162 158 info_elog!( 163 159 ev_log, 164 160 "New session created by {}", 165 161 user.clone().username.color_bright_cyan() 166 162 ); 167 - let session_id = id.get(0); 163 + let session_id = id.id; 168 164 Ok(( 169 165 SessionReference { 170 166 session_id, ··· 181 177 ) -> Result<User, LuminaError> { 182 178 match db { 183 179 DbConn::PgsqlConnection(pg_pool, _) => { 184 - let client = pg_pool.get().await?; 185 - let user = client 186 - .query_one("SELECT users.id, users.email, users.username FROM users JOIN sessions ON users.id = sessions.user_id WHERE sessions.session_key = $1", &[&token]) 180 + let user = sqlx::query!("SELECT users.id, users.email, users.username FROM users JOIN sessions ON users.id = sessions.user_id WHERE sessions.session_key = $1", &token) 181 + .fetch_one(pg_pool) 187 182 .await 188 183 ?; 189 184 Ok(User { 190 - id: user.get(0), 191 - email: user.get(1), 192 - username: user.get(2), 185 + id: user.id, 186 + email: user.email, 187 + username: user.username, 193 188 foreign_instance_id: "".to_string(), // Default value for revived sessions 194 189 }) 195 190 } ··· 207 202 // Check if the email or username is already in use using fastbloom algorithm with Redis, and fallback to DB check if not found. If not in either, we can go on. 208 203 match db { 209 204 DbConn::PgsqlConnection(pg_pool, redis_pool) => { 210 - let client = pg_pool.get().await?; 211 205 let mut redis_conn = redis_pool.get().await?; 212 206 // fastbloom_rs expects bytes, so we use the string as bytes 213 207 let email_key = String::from("bloom:email"); ··· 220 214 .unwrap_or(false); 221 215 if email_exists { 222 216 // Fallback to DB check if in bloom filter 223 - let email_db = client 224 - .query("SELECT * FROM users WHERE email = $1", &[&email]) 217 + let email_db = sqlx::query!("SELECT * FROM users WHERE email = $1", &email) 218 + .fetch_optional(pg_pool) 225 219 .await?; 226 - if !email_db.is_empty() { 220 + if !email_db.is_none() { 227 221 return Err(LuminaError::RegisterEmailInUse); 228 222 } 229 223 } ··· 235 229 .unwrap_or(false); 236 230 if username_exists { 237 231 // Fallback to DB check if in bloom filter 238 - let username_db = client 239 - .query("SELECT * FROM users WHERE username = $1", &[&username]) 232 + let username_db = sqlx::query!("SELECT * FROM users WHERE username = $1", &username) 233 + .fetch_optional(pg_pool) 240 234 .await?; 241 - if !username_db.is_empty() { 235 + if !username_db.is_none() { 242 236 return Err(LuminaError::RegisterUsernameInUse); 243 237 } 244 238 } 245 239 // Fallback to DB check if not in bloom filter 246 - let email_db = client 247 - .query("SELECT * FROM users WHERE email = $1", &[&email]) 240 + let email_db = 241 + sqlx::query!("SELECT * FROM users WHERE email = $1", &email) 242 + .fetch_optional(pg_pool) 248 243 .await?; 249 - if !email_db.is_empty() { 244 + if !email_db.is_none() { 250 245 // Update bloom filter after DB check 251 246 let _: () = redis::cmd("BF.ADD") 252 247 .arg(&email_key) ··· 256 251 .unwrap_or(()); 257 252 return Err(LuminaError::RegisterEmailInUse); 258 253 } 259 - let username_db = client 260 - .query("SELECT * FROM users WHERE username = $1", &[&username]) 254 + let username_db = sqlx::query!("SELECT * FROM users WHERE username = $1", &username) 255 + .fetch_optional(pg_pool) 261 256 .await?; 262 - if !username_db.is_empty() { 257 + if !username_db.is_none() { 263 258 let _: () = redis::cmd("BF.ADD") 264 259 .arg(&username_key) 265 260 .arg(&username)