2021-05-21 14:26:58 +00:00
|
|
|
use crate::error::{BotError, DiceRollingError};
|
2021-05-21 14:44:03 +00:00
|
|
|
use crate::parser::dice::{Amount, Element};
|
2021-05-26 13:56:14 +00:00
|
|
|
use crate::{context::Context, models::Account};
|
|
|
|
use crate::{
|
|
|
|
db::{sqlite::Database, Users, Variables},
|
|
|
|
models::TransientUser,
|
|
|
|
};
|
2021-05-22 14:52:32 +00:00
|
|
|
use argon2::{self, Config, Error as ArgonError};
|
2021-05-21 14:26:58 +00:00
|
|
|
use futures::stream::{self, StreamExt, TryStreamExt};
|
2021-05-22 14:52:32 +00:00
|
|
|
use rand::Rng;
|
2021-05-21 14:26:58 +00:00
|
|
|
use std::slice;
|
2020-11-30 19:53:26 +00:00
|
|
|
|
2021-05-21 14:26:58 +00:00
|
|
|
/// Calculate the amount of dice to roll by consulting the database
|
|
|
|
/// and replacing variables with corresponding the amount. Errors out
|
|
|
|
/// if it cannot find a variable defined, or if the database errors.
|
|
|
|
pub async fn calculate_single_die_amount(
|
|
|
|
amount: &Amount,
|
|
|
|
ctx: &Context<'_>,
|
|
|
|
) -> Result<i32, BotError> {
|
|
|
|
calculate_dice_amount(slice::from_ref(amount), ctx).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calculate the amount of dice to roll by consulting the database
|
|
|
|
/// and replacing variables with corresponding amounts. Errors out if
|
|
|
|
/// it cannot find a variable defined, or if the database errors.
|
|
|
|
pub async fn calculate_dice_amount(amounts: &[Amount], ctx: &Context<'_>) -> Result<i32, BotError> {
|
|
|
|
let stream = stream::iter(amounts);
|
|
|
|
let variables = &ctx
|
|
|
|
.db
|
2021-05-30 14:17:33 +00:00
|
|
|
.get_user_variables(&ctx.username, ctx.active_room_id().as_str())
|
2021-05-21 14:26:58 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
use DiceRollingError::VariableNotFound;
|
|
|
|
let dice_amount: i32 = stream
|
|
|
|
.then(|amount| async move {
|
|
|
|
match &amount.element {
|
|
|
|
Element::Number(num_dice) => Ok(num_dice * amount.operator.mult()),
|
|
|
|
Element::Variable(variable) => variables
|
|
|
|
.get(variable)
|
|
|
|
.ok_or_else(|| VariableNotFound(variable.clone()))
|
|
|
|
.map(|i| *i),
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.try_fold(0, |total, num_dice| async move { Ok(total + num_dice) })
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(dice_amount)
|
|
|
|
}
|
2021-05-22 14:52:32 +00:00
|
|
|
|
|
|
|
/// Hash a password using the argon2 algorithm with a 16 byte salt.
|
|
|
|
pub(crate) fn hash_password(raw_password: &str) -> Result<String, ArgonError> {
|
|
|
|
let salt = rand::thread_rng().gen::<[u8; 16]>();
|
|
|
|
let config = Config::default();
|
|
|
|
argon2::hash_encoded(raw_password.as_bytes(), &salt, &config)
|
|
|
|
}
|
2021-05-26 13:56:14 +00:00
|
|
|
|
|
|
|
pub(crate) async fn get_account(db: &Database, username: &str) -> Result<Account, BotError> {
|
|
|
|
Ok(db
|
|
|
|
.get_user(username)
|
|
|
|
.await?
|
|
|
|
.map(|user| Account::Registered(user))
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
Account::Transient(TransientUser {
|
|
|
|
username: username.to_owned(),
|
|
|
|
})
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use crate::db::Users;
|
|
|
|
use crate::models::{AccountStatus, User};
|
2021-09-05 07:56:41 +00:00
|
|
|
use std::future::Future;
|
2021-05-26 13:56:14 +00:00
|
|
|
|
2021-09-05 07:56:41 +00:00
|
|
|
async fn with_db<Fut>(f: impl FnOnce(Database) -> Fut)
|
|
|
|
where
|
|
|
|
Fut: Future<Output = ()>,
|
|
|
|
{
|
2021-05-26 13:56:14 +00:00
|
|
|
let db_path = tempfile::NamedTempFile::new_in(".").unwrap();
|
|
|
|
crate::db::sqlite::migrator::migrate(db_path.path().to_str().unwrap())
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2021-09-05 07:56:41 +00:00
|
|
|
let db = Database::new(db_path.path().to_str().unwrap())
|
2021-05-26 13:56:14 +00:00
|
|
|
.await
|
2021-09-05 07:56:41 +00:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
f(db).await;
|
2021-05-26 13:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
|
|
|
async fn get_account_no_user_exists() {
|
2021-09-05 07:56:41 +00:00
|
|
|
with_db(|db| async move {
|
|
|
|
let account = get_account(&db, "@test:example.com")
|
|
|
|
.await
|
|
|
|
.expect("Account retrieval didn't work");
|
2021-05-26 13:56:14 +00:00
|
|
|
|
2021-09-05 07:56:41 +00:00
|
|
|
assert!(matches!(account, Account::Transient(_)));
|
2021-05-26 13:56:14 +00:00
|
|
|
|
2021-09-05 07:56:41 +00:00
|
|
|
let user = account.transient_user().unwrap();
|
|
|
|
assert_eq!(user.username, "@test:example.com");
|
|
|
|
})
|
|
|
|
.await;
|
2021-05-26 13:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
|
|
|
|
async fn get_or_create_user_when_user_exists() {
|
2021-09-05 07:56:41 +00:00
|
|
|
with_db(|db| async move {
|
|
|
|
let user = User {
|
|
|
|
username: "myuser".to_string(),
|
|
|
|
password: Some("abc".to_string()),
|
|
|
|
account_status: AccountStatus::Registered,
|
|
|
|
active_room: Some("myroom".to_string()),
|
|
|
|
};
|
2021-05-26 13:56:14 +00:00
|
|
|
|
2021-09-05 07:56:41 +00:00
|
|
|
let insert_result = db.upsert_user(&user).await;
|
|
|
|
assert!(insert_result.is_ok());
|
2021-05-26 13:56:14 +00:00
|
|
|
|
2021-09-05 07:56:41 +00:00
|
|
|
let account = get_account(&db, "myuser")
|
|
|
|
.await
|
|
|
|
.expect("Account retrieval did not work");
|
2021-05-26 13:56:14 +00:00
|
|
|
|
2021-09-05 07:56:41 +00:00
|
|
|
assert!(matches!(account, Account::Registered(_)));
|
2021-05-26 13:56:14 +00:00
|
|
|
|
2021-09-05 07:56:41 +00:00
|
|
|
let user_again = account.registered_user().unwrap();
|
|
|
|
assert_eq!(user, *user_again);
|
|
|
|
})
|
|
|
|
.await;
|
2021-05-26 13:56:14 +00:00
|
|
|
}
|
|
|
|
}
|