feat: can now verify the user got the email

This commit is contained in:
silver 2023-09-17 18:00:05 +01:00
parent 98e61d7548
commit a444d6a5e5
4 changed files with 114 additions and 17 deletions

View file

@ -141,6 +141,8 @@ async fn get_skynet(db: &Pool<Sqlite>, config: &Config) {
}
}
}
// TODO: Tidy up
async fn add_users_skynet(db: &Pool<Sqlite>, server: &GuildId, user: &SkynetResult) {
match sqlx::query_as::<_, Wolves>(
"

View file

@ -35,6 +35,12 @@ pub(crate) mod link {
return "Already linked".to_string();
}
db_pending_clear_expired(&db).await;
if get_verify_from_db(&db, &command.user.name).await.is_some() {
return "Linking already in process, please check email.".to_string();
}
let option = command
.data
.options
@ -60,13 +66,6 @@ pub(crate) mod link {
return "Email already verified".to_string();
}
db_pending_clear_expired(&db).await;
// send mail
if get_from_db(&db, &command.user.name).await.is_some() {
return "Please check email".to_string();
}
// generate a auth key
let auth = random_string(20);
match send_mail(&config, &details, &auth, &command.user.name) {
@ -81,7 +80,7 @@ pub(crate) mod link {
}
}
format!("Verification email sent to {}, user is {} {:?}", email, command.user.name, command.guild_id)
format!("Verification email sent to {}", email)
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
@ -96,7 +95,7 @@ pub(crate) mod link {
r#"
SELECT *
FROM wolves
WHERE discord =
WHERE discord = ?
"#,
)
.bind(user)
@ -137,7 +136,7 @@ pub(crate) mod link {
// Substitute in the name of our recipient.
p { "Hi " (user) "," }
p {
"Please use " pre { "/verify " (auth)} " to verify your discord account."
"Please use " pre { "/verify code:" (auth)} " to verify your discord account."
}
p {
"If you have issues please refer to our Discord server:"
@ -156,7 +155,7 @@ pub(crate) mod link {
r#"
Hi {user}
Please use "/verify {auth}" to verify your discord account.
Please use "/verify code:{auth}" to verify your discord account.
If you have issues please refer to our Discord server:
{discord}
@ -189,7 +188,7 @@ pub(crate) mod link {
mailer.send(&email)
}
async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Option<WolvesVerify> {
pub async fn db_pending_clear_expired(pool: &Pool<Sqlite>) -> Option<WolvesVerify> {
sqlx::query_as::<_, WolvesVerify>(
r#"
DELETE
@ -203,12 +202,12 @@ pub(crate) mod link {
.ok()
}
async fn get_from_db(db: &Pool<Sqlite>, user: &str) -> Option<WolvesVerify> {
pub async fn get_verify_from_db(db: &Pool<Sqlite>, user: &str) -> Option<WolvesVerify> {
sqlx::query_as::<_, WolvesVerify>(
r#"
SELECT *
FROM wolves_verify
WHERE discord =
WHERE discord = ?
"#,
)
.bind(user)
@ -232,3 +231,96 @@ pub(crate) mod link {
.await
}
}
pub(crate) mod verify {
use super::*;
use crate::commands::link_email::link::{db_pending_clear_expired, get_verify_from_db};
use sqlx::Error;
pub async fn run(command: &ApplicationCommandInteraction, ctx: &Context) -> String {
let db_lock = {
let data_read = ctx.data.read().await;
data_read.get::<DataBase>().expect("Expected Databse in TypeMap.").clone()
};
let db = db_lock.read().await;
// check if user has used /link
let details = if let Some(x) = get_verify_from_db(&db, &command.user.name).await {
x
} else {
return "Please use /link first".to_string();
};
let option = command
.data
.options
.get(0)
.expect("Expected code option")
.resolved
.as_ref()
.expect("Expected code object");
let code = if let CommandDataOptionValue::String(code) = option {
code
} else {
return "Please provide a verification code".to_string();
};
db_pending_clear_expired(&db).await;
if &details.auth_code != code {
return "Invalid verification code".to_string();
}
match db_pending_clear_successful(&db, &command.user.name).await {
Ok(_) => {
return match set_discord(&db, &command.user.name, &details.email).await {
Ok(_) => "Discord username linked to Wolves".to_string(),
Err(e) => {
println!("{:?}", e);
"Failed to save, please try /link again".to_string()
}
}
}
Err(e) => println!("{:?}", e),
}
"Failed to verify".to_string()
}
pub fn register(command: &mut CreateApplicationCommand) -> &mut CreateApplicationCommand {
command.name("verify").description("Verify Wolves Email").create_option(|option| {
option
.name("code")
.description("Code from verification email")
.kind(CommandOptionType::String)
.required(true)
})
}
async fn db_pending_clear_successful(pool: &Pool<Sqlite>, user: &str) -> Result<Option<WolvesVerify>, Error> {
sqlx::query_as::<_, WolvesVerify>(
r#"
DELETE
FROM wolves_verify
WHERE discord = ?
"#,
)
.bind(user)
.fetch_optional(pool)
.await
}
async fn set_discord(db: &Pool<Sqlite>, discord: &str, email: &str) -> Result<Option<Wolves>, Error> {
sqlx::query_as::<_, Wolves>(
"
UPDATE wolves
SET discord = ?
WHERE email = ?
",
)
.bind(discord)
.bind(email)
.fetch_optional(db)
.await
}
}

View file

@ -212,8 +212,8 @@ pub async fn db_init(config: &Config) -> Result<Pool<Sqlite>, Error> {
sqlx::query(
"CREATE TABLE IF NOT EXISTS wolves_verify (
email text PRIMARY KEY,
discord text not null,
discord text PRIMARY KEY,
email text not null,
auth_code text not null,
date_expiry text not null
)",

View file

@ -56,7 +56,9 @@ impl EventHandler for Handler {
println!("[Main] {} is connected!", ready.user.name);
Command::set_global_application_commands(&ctx.http, |commands| {
commands.create_application_command(|command| commands::link_email::link::register(command))
commands
.create_application_command(|command| commands::link_email::link::register(command))
.create_application_command(|command| commands::link_email::verify::register(command))
})
.await
.ok();
@ -69,6 +71,7 @@ impl EventHandler for Handler {
let content = match command.data.name.as_str() {
"link" => commands::link_email::link::run(&command, &ctx).await,
"verify" => commands::link_email::verify::run(&command, &ctx).await,
_ => "not implemented :(".to_string(),
};