diff --git a/src/endpoints/quests/haiko/claimable.rs b/src/endpoints/quests/haiko/claimable.rs index 9a3d7a2b..fa2043fc 100644 --- a/src/endpoints/quests/haiko/claimable.rs +++ b/src/endpoints/quests/haiko/claimable.rs @@ -17,8 +17,8 @@ use starknet::{ use std::sync::Arc; const QUEST_ID: u32 = 29; -const TASK_IDS: &[u32] = &[140, 141, 142, 143, 144]; -const LAST_TASK: u32 = TASK_IDS[4]; +const TASK_IDS: &[u32] = &[140, 141, 142, 143]; +const LAST_TASK: u32 = TASK_IDS[3]; const NFT_LEVEL: u32 = 41; #[derive(Deserialize)] diff --git a/src/endpoints/quests/haiko/discord_fw_callback.rs b/src/endpoints/quests/haiko/discord_fw_callback.rs deleted file mode 100644 index 44689cc5..00000000 --- a/src/endpoints/quests/haiko/discord_fw_callback.rs +++ /dev/null @@ -1,151 +0,0 @@ -use std::sync::Arc; - -use crate::utils::CompletedTasksTrait; -use crate::{ - models::AppState, - utils::{get_error_redirect, success_redirect}, -}; -use axum::{ - extract::{Query, State}, - response::IntoResponse, -}; -use axum_auto_routes::route; -use mongodb::bson::doc; -use reqwest::header::AUTHORIZATION; -use serde::Deserialize; -use starknet::core::types::FieldElement; - -#[derive(Deserialize)] -pub struct TwitterOAuthCallbackQuery { - code: String, - state: FieldElement, -} - -#[derive(Deserialize)] -pub struct Guild { - id: String, - #[allow(dead_code)] - name: String, -} - -#[route( -get, -"/quests/haiko/discord_fw_callback", -crate::endpoints::quests::haiko::discord_fw_callback -)] -pub async fn handler( - State(state): State>, - Query(query): Query, -) -> impl IntoResponse { - let quest_id = 29; - let task_id = 142; - let guild_id = "1181839548775419934"; - let authorization_code = &query.code; - let error_redirect_uri = format!( - "{}/quest/{}?task_id={}&res=false", - state.conf.variables.app_link, quest_id, task_id - ); - - // Exchange the authorization code for an access token - let params = [ - ("client_id", &state.conf.discord.oauth2_clientid), - ("client_secret", &state.conf.discord.oauth2_secret), - ("code", &authorization_code.to_string()), - ( - "redirect_uri", - &format!( - "{}/quests/haiko/discord_fw_callback", - state.conf.variables.api_link - ), - ), - ("grant_type", &"authorization_code".to_string()), - ]; - let access_token = match exchange_authorization_code(params).await { - Ok(token) => token, - Err(e) => { - return get_error_redirect( - error_redirect_uri, - format!("Failed to exchange authorization code: {}", e), - ); - } - }; - - // Get user guild information - let client = reqwest::Client::new(); - let response_result = client - .get("https://discord.com/api/users/@me/guilds") - .header(AUTHORIZATION, format!("Bearer {}", access_token)) - .send() - .await; - let response: Vec = match response_result { - Ok(response) => { - let json_result = response.json().await; - match json_result { - Ok(json) => json, - Err(e) => { - return get_error_redirect( - error_redirect_uri, - format!( - "Failed to get JSON response while fetching user info: {}", - e - ), - ); - } - } - } - Err(e) => { - return get_error_redirect( - error_redirect_uri, - format!("Failed to send request to get user info: {}", e), - ); - } - }; - - for guild in response { - if guild.id == guild_id { - match state.upsert_completed_task(query.state, task_id).await { - Ok(_) => { - let redirect_uri = format!( - "{}/quest/{}?task_id={}&res=true", - state.conf.variables.app_link, quest_id, task_id - ); - return success_redirect(redirect_uri); - } - Err(e) => return get_error_redirect(error_redirect_uri, format!("{}", e)), - } - } - } - - get_error_redirect( - error_redirect_uri, - "You're not part of Haiko's Discord server".to_string(), - ) -} - -async fn exchange_authorization_code( - params: [(&str, &String); 5], -) -> Result> { - let client = reqwest::Client::new(); - let res = client - .post("https://discord.com/api/oauth2/token") - .form(¶ms) - .send() - .await?; - let json: serde_json::Value = res.json().await?; - match json["access_token"].as_str() { - Some(s) => Ok(s.to_string()), - None => { - println!( - "Failed to get 'access_token' from JSON response : {:?}", - json - ); - Err(Box::new(std::io::Error::new( - std::io::ErrorKind::Other, - format!( - "Failed to get 'access_token' from JSON response : {:?}", - json - ), - ))) - } - } -} diff --git a/src/endpoints/quests/haiko/mod.rs b/src/endpoints/quests/haiko/mod.rs index 2c5c4121..26278749 100644 --- a/src/endpoints/quests/haiko/mod.rs +++ b/src/endpoints/quests/haiko/mod.rs @@ -1,4 +1,3 @@ -pub mod discord_fw_callback; pub mod verify_deposit; pub mod verify_twitter_fw; pub mod verify_twitter_rw; diff --git a/src/endpoints/quests/haiko/verify_deposit.rs b/src/endpoints/quests/haiko/verify_deposit.rs index 06301aed..18dfcc84 100644 --- a/src/endpoints/quests/haiko/verify_deposit.rs +++ b/src/endpoints/quests/haiko/verify_deposit.rs @@ -23,7 +23,7 @@ pub async fn handler( State(state): State>, Query(query): Query, ) -> impl IntoResponse { - let task_id = 143; + let task_id = 142; let addr = &query.addr; let url = format!( diff --git a/src/endpoints/quests/haiko/verify_twitter_rw.rs b/src/endpoints/quests/haiko/verify_twitter_rw.rs index 7b4cbad4..18fe4c68 100644 --- a/src/endpoints/quests/haiko/verify_twitter_rw.rs +++ b/src/endpoints/quests/haiko/verify_twitter_rw.rs @@ -22,7 +22,7 @@ pub async fn handler( State(state): State>, Query(query): Query, ) -> impl IntoResponse { - let task_id = 144; + let task_id = 143; match state.upsert_completed_task(query.addr, task_id).await { Ok(_) => (StatusCode::OK, Json(json!({"res": true}))).into_response(), Err(e) => get_error(format!("{}", e)),