refactor(quark): port add_friend, block_user, change_username, remove_friend, send_friend_request, unblock_user

#283
This commit is contained in:
Paul Makles
2024-04-07 18:30:59 +01:00
parent 4e7049d51e
commit 49bb235938
12 changed files with 89 additions and 72 deletions

View File

@@ -348,19 +348,6 @@ impl User {
}
}
/// Check whether a username is already in use by another user
#[allow(dead_code)]
async fn is_username_taken(db: &Database, username: &str) -> Result<bool> {
match db.fetch_user_by_username(username).await {
Ok(_) => Ok(true),
Err(Error {
error_type: ErrorType::NotFound,
..
}) => Ok(false),
Err(error) => Err(error),
}
}
/// Set a relationship to another user
pub async fn set_relationship(
&mut self,

View File

@@ -14,7 +14,7 @@ pub trait AbstractUsers: Sync + Send {
async fn fetch_user(&self, id: &str) -> Result<User>;
/// Fetch a user from the database by their username
async fn fetch_user_by_username(&self, username: &str) -> Result<User>;
async fn fetch_user_by_username(&self, username: &str, discriminator: &str) -> Result<User>;
/// Fetch a user from the database by their session token
async fn fetch_user_by_token(&self, token: &str) -> Result<User>;

View File

@@ -25,13 +25,14 @@ impl AbstractUsers for MongoDb {
}
/// Fetch a user from the database by their username
async fn fetch_user_by_username(&self, username: &str) -> Result<User> {
async fn fetch_user_by_username(&self, username: &str, discriminator: &str) -> Result<User> {
query!(
self,
find_one_with_options,
COL,
doc! {
"username": username
"username": username,
"discriminator": discriminator
},
FindOneOptions::builder()
.collation(

View File

@@ -28,12 +28,14 @@ impl AbstractUsers for ReferenceDb {
}
/// Fetch a user from the database by their username
async fn fetch_user_by_username(&self, username: &str) -> Result<User> {
async fn fetch_user_by_username(&self, username: &str, discriminator: &str) -> Result<User> {
let users = self.users.lock().await;
let lowercase = username.to_lowercase();
users
.values()
.find(|user| user.username.to_lowercase() == lowercase)
.find(|user| {
user.username.to_lowercase() == lowercase && user.discriminator == discriminator
})
.cloned()
.ok_or_else(|| create_error!(NotFound))
}