···184184 match e {
185185 LuminaError::DbError(crate::errors::LuminaDbError::Postgres(e)) =>
186186 error_elog!(ev_log,"While creating session token: {:?}", e),
187187- LuminaError::Bb8RunErrorPg(e) =>
188188- warn_elog!(ev_log,"There was an error creating session token: {}", e),
189187 _ => {}
190188 }
191189 // I would return a more specific error message
+44-49
server/src/user.rs
···2222use crate::{LuminaError, database::DbConn, helpers::events::EventLogger, info_elog};
2323use cynthia_con::CynthiaColors;
2424use uuid::Uuid;
2525+use crate::database::DatabaseConnections;
25262627#[derive(Debug, Clone)]
2728pub struct User {
···6263 async fn get_hashed_password(self, database: &DbConn) -> Result<String, LuminaError> {
6364 match database {
6465 DbConn::PgsqlConnection(pg_pool, _) => {
6565- let client = pg_pool.get().await?;
6666- let row = client
6767- .query_one("SELECT password FROM users WHERE id = $1", &[&self.id])
6666+6767+ let row = sqlx::query!("SELECT password FROM users WHERE id = $1", &self.id)
6868+ .fetch_one(pg_pool)
6869 .await?;
6969- let password: String = row.get(0);
7070+ let password: String = row.password;
7071 Ok(password)
7172 }
7273 }
···8384 bcrypt::hash(password, bcrypt::DEFAULT_COST).map_err(|_| LuminaError::BcryptError)?;
8485 match db {
8586 DbConn::PgsqlConnection(pg_pool, _) => {
8686- let client = pg_pool.get().await?;
8787 // Some username and email validation should be done here
8888 // Check if the email is already in use
8989- let email_exists = client
9090- .query("SELECT * FROM users WHERE email = $1", &[&email])
8989+ let email_exists = sqlx::query!("SELECT * FROM users WHERE email = $1", &email)
9090+ .fetch_optional(pg_pool)
9191 .await?;
9292- if !email_exists.is_empty() {
9292+ if !email_exists.is_none() {
9393 return Err(LuminaError::RegisterEmailInUse);
9494 }
9595 // Check if the username is already in use
9696- let username_exists = client
9797- .query("SELECT * FROM users WHERE username = $1", &[&username])
9696+ let username_exists =
9797+ sqlx::query!("SELECT * FROM users WHERE username = $1", &username)
9898+ .fetch_optional(pg_pool)
9899 .await?;
9999- if !username_exists.is_empty() {
100100+ if !username_exists.is_none() {
100101 return Err(LuminaError::RegisterUsernameInUse);
101102 }
102103103103- let id = client
104104- .query_one("INSERT INTO users (email, username, password) VALUES ($1, $2, $3) RETURNING id", &[&email, &username, &password])
104104+ let id = sqlx::query!("INSERT INTO users (email, username, password) VALUES ($1, $2, $3) RETURNING id", &email, &username, &password)
105105+ .fetch_one(pg_pool)
105106 .await
106107 ?;
107108 Ok(User {
108108- id: id.get(0),
109109+ id: id.id,
109110 email,
110111 username,
111112 foreign_instance_id: "".to_string(), // Default value for new users
···124125 };
125126 match db {
126127 DbConn::PgsqlConnection(pg_pool, _) => {
127127- let client = pg_pool.get().await?;
128128- let user = client
129129- .query_one(
130130- &format!("SELECT id, email, username, COALESCE(foreign_instance_id, '') FROM users WHERE {} = $1", identifyer_type),
131131- &[&identifier],
132132- )
128128+ let user = sqlx::query!("SELECT id, email, username, coalesce(foreign_instance_id, '') as foreign_instance_id FROM users WHERE $1 = $2", identifyer_type, &identifier)
129129+130130+ .fetch_one(pg_pool)
133131 .await
134132 ?;
135133 Ok(User {
136136- id: user.get(0),
137137- email: user.get(1),
138138- username: user.get(2),
139139- foreign_instance_id: user.get(3),
134134+ id: user.id,
135135+ email: user.email,
136136+ username: user.username,
137137+ foreign_instance_id: user.foreign_instance_id.unwrap_or("".to_string()),
140138 })
141139 }
142140 }
···151149 let user_id = user.id;
152150 match db {
153151 DbConn::PgsqlConnection(pg_pool, _) => {
154154- let client = pg_pool.get().await?;
155152 let session_key = Uuid::new_v4().to_string();
156156- let id = client
157157- .query_one(
153153+ let id = sqlx::query!(
158154 "INSERT INTO sessions (user_id, session_key) VALUES ($1, $2) RETURNING id",
159159- &[&user_id, &session_key],
160160- )
155155+ &user_id, &session_key,
156156+ ).fetch_one(pg_pool)
161157 .await?;
162158 info_elog!(
163159 ev_log,
164160 "New session created by {}",
165161 user.clone().username.color_bright_cyan()
166162 );
167167- let session_id = id.get(0);
163163+ let session_id = id.id;
168164 Ok((
169165 SessionReference {
170166 session_id,
···181177 ) -> Result<User, LuminaError> {
182178 match db {
183179 DbConn::PgsqlConnection(pg_pool, _) => {
184184- let client = pg_pool.get().await?;
185185- let user = client
186186- .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])
180180+ 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)
181181+ .fetch_one(pg_pool)
187182 .await
188183 ?;
189184 Ok(User {
190190- id: user.get(0),
191191- email: user.get(1),
192192- username: user.get(2),
185185+ id: user.id,
186186+ email: user.email,
187187+ username: user.username,
193188 foreign_instance_id: "".to_string(), // Default value for revived sessions
194189 })
195190 }
···207202 // 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.
208203 match db {
209204 DbConn::PgsqlConnection(pg_pool, redis_pool) => {
210210- let client = pg_pool.get().await?;
211205 let mut redis_conn = redis_pool.get().await?;
212206 // fastbloom_rs expects bytes, so we use the string as bytes
213207 let email_key = String::from("bloom:email");
···220214 .unwrap_or(false);
221215 if email_exists {
222216 // Fallback to DB check if in bloom filter
223223- let email_db = client
224224- .query("SELECT * FROM users WHERE email = $1", &[&email])
217217+ let email_db = sqlx::query!("SELECT * FROM users WHERE email = $1", &email)
218218+ .fetch_optional(pg_pool)
225219 .await?;
226226- if !email_db.is_empty() {
220220+ if !email_db.is_none() {
227221 return Err(LuminaError::RegisterEmailInUse);
228222 }
229223 }
···235229 .unwrap_or(false);
236230 if username_exists {
237231 // Fallback to DB check if in bloom filter
238238- let username_db = client
239239- .query("SELECT * FROM users WHERE username = $1", &[&username])
232232+ let username_db = sqlx::query!("SELECT * FROM users WHERE username = $1", &username)
233233+ .fetch_optional(pg_pool)
240234 .await?;
241241- if !username_db.is_empty() {
235235+ if !username_db.is_none() {
242236 return Err(LuminaError::RegisterUsernameInUse);
243237 }
244238 }
245239 // Fallback to DB check if not in bloom filter
246246- let email_db = client
247247- .query("SELECT * FROM users WHERE email = $1", &[&email])
240240+ let email_db =
241241+ sqlx::query!("SELECT * FROM users WHERE email = $1", &email)
242242+ .fetch_optional(pg_pool)
248243 .await?;
249249- if !email_db.is_empty() {
244244+ if !email_db.is_none() {
250245 // Update bloom filter after DB check
251246 let _: () = redis::cmd("BF.ADD")
252247 .arg(&email_key)
···256251 .unwrap_or(());
257252 return Err(LuminaError::RegisterEmailInUse);
258253 }
259259- let username_db = client
260260- .query("SELECT * FROM users WHERE username = $1", &[&username])
254254+ let username_db = sqlx::query!("SELECT * FROM users WHERE username = $1", &username)
255255+ .fetch_optional(pg_pool)
261256 .await?;
262262- if !username_db.is_empty() {
257257+ if !username_db.is_none() {
263258 let _: () = redis::cmd("BF.ADD")
264259 .arg(&username_key)
265260 .arg(&username)