-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from starknet-id/ayush/focustree-quest
feat: add focustree quests
- Loading branch information
Showing
14 changed files
with
214 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
src/endpoints/quests/focustree/engagement/discord_fw_callback.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
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 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, | ||
} | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<TwitterOAuthCallbackQuery>, | ||
) -> impl IntoResponse { | ||
let quest_id = 21; | ||
let task_id = 87; | ||
let guild_id = "986385497888792598"; | ||
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/focustree/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<Guild> = 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 AVNU's Discord server".to_string(), | ||
) | ||
} | ||
|
||
async fn exchange_authorization_code( | ||
params: [(&str, &String); 5], | ||
) -> Result<String, Box<dyn std::error::Error>> { | ||
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 | ||
), | ||
))) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod discord_fw_callback; | ||
pub mod verify_twitter_rt; |
24 changes: 24 additions & 0 deletions
24
src/endpoints/quests/focustree/engagement/verify_twitter_rt.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
use std::sync::Arc; | ||
|
||
use crate::{ | ||
models::{AppState, VerifyQuery}, | ||
utils::{get_error, CompletedTasksTrait}, | ||
}; | ||
use axum::{ | ||
extract::{Query, State}, | ||
http::StatusCode, | ||
response::IntoResponse, | ||
Json, | ||
}; | ||
use serde_json::json; | ||
|
||
pub async fn handler( | ||
State(state): State<Arc<AppState>>, | ||
Query(query): Query<VerifyQuery>, | ||
) -> impl IntoResponse { | ||
let task_id = 88; | ||
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)), | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod claimable; | ||
pub mod verify_twitter_fw; | ||
pub mod verify_twitter_rt; |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,2 @@ | ||
pub mod claimable; | ||
pub mod verify_twitter_fw; | ||
pub mod verify_twitter_rt; | ||
pub mod introduction; | ||
pub mod engagement; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters