forked from lfglabs-dev/api.starknet.quest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_twitter_fw.rs
79 lines (73 loc) · 2.52 KB
/
create_twitter_fw.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::models::{QuestDocument, QuestTaskDocument};
use crate::utils::verify_quest_auth;
use crate::{models::AppState, utils::get_error};
use crate::middleware::auth::auth_middleware;
use axum::{
extract::{Extension, State},
http::StatusCode,
response::{IntoResponse, Json},
};
use axum_auto_routes::route;
use mongodb::bson::doc;
use mongodb::options::FindOneOptions;
use serde::Deserialize;
use serde_json::json;
use std::sync::Arc;
pub_struct!(Deserialize; CreateTwitterFw {
name: String,
desc: String,
username: String,
quest_id: i64,
});
#[route(post, "/admin/tasks/twitter_fw/create", auth_middleware)]
pub async fn handler(
State(state): State<Arc<AppState>>,
Extension(sub): Extension<String>,
body: Json<CreateTwitterFw>,
) -> impl IntoResponse {
let collection = state.db.collection::<QuestTaskDocument>("tasks");
// Get the last id in increasing order
let last_id_filter = doc! {};
let options = FindOneOptions::builder().sort(doc! {"id": -1}).build();
let last_doc = &collection.find_one(last_id_filter, options).await.unwrap();
let quests_collection = state.db.collection::<QuestDocument>("quests");
let res = verify_quest_auth(sub, &quests_collection, &body.quest_id).await;
if !res {
return get_error("Error creating task".to_string());
};
let mut next_id = 1;
if let Some(doc) = last_doc {
let last_id = doc.id;
next_id = last_id + 1;
}
let new_document = QuestTaskDocument {
name: body.name.clone(),
desc: body.desc.clone(),
total_amount: None,
verify_redirect: Some(format!(
"https://twitter.com/intent/user?screen_name={}",
body.username.clone()
)),
href: format!("https://twitter.com/{}", body.username.clone()),
quest_id: body.quest_id.clone(),
id: next_id,
verify_endpoint: "quests/verify_twitter_fw".to_string(),
verify_endpoint_type: "default".to_string(),
task_type: Some("twitter_fw".to_string()),
cta: "Follow".to_string(),
discord_guild_id: None,
quiz_name: None,
contracts: None,
api_url: None,
regex: None,
};
// insert document to boost collection
return match collection.insert_one(new_document, None).await {
Ok(_) => (
StatusCode::OK,
Json(json!({"message": "Task created successfully"})).into_response(),
)
.into_response(),
Err(_e) => get_error("Error creating task".to_string()),
};
}