From c7d992b3208087dae918b609bd0171bd892a9741 Mon Sep 17 00:00:00 2001 From: Nico <60229704+Marchand-Nicolas@users.noreply.github.com> Date: Sun, 24 Nov 2024 09:18:44 +0100 Subject: [PATCH 1/3] fix: build --- src/endpoints/quests/ekubo/mod.rs | 1 - src/endpoints/quests/focustree/engagement/mod.rs | 1 - src/endpoints/quests/nostra/liquidity_quest/mod.rs | 1 - 3 files changed, 3 deletions(-) diff --git a/src/endpoints/quests/ekubo/mod.rs b/src/endpoints/quests/ekubo/mod.rs index 57eade9b..4e642f70 100644 --- a/src/endpoints/quests/ekubo/mod.rs +++ b/src/endpoints/quests/ekubo/mod.rs @@ -1,3 +1,2 @@ pub mod claimable; -pub mod discord_fw_callback; pub mod verify_added_liquidity; diff --git a/src/endpoints/quests/focustree/engagement/mod.rs b/src/endpoints/quests/focustree/engagement/mod.rs index 8de7b0db..50706f6e 100644 --- a/src/endpoints/quests/focustree/engagement/mod.rs +++ b/src/endpoints/quests/focustree/engagement/mod.rs @@ -1,2 +1 @@ -pub mod discord_fw_callback; pub mod verify_twitter_rt; diff --git a/src/endpoints/quests/nostra/liquidity_quest/mod.rs b/src/endpoints/quests/nostra/liquidity_quest/mod.rs index 57eade9b..4e642f70 100644 --- a/src/endpoints/quests/nostra/liquidity_quest/mod.rs +++ b/src/endpoints/quests/nostra/liquidity_quest/mod.rs @@ -1,3 +1,2 @@ pub mod claimable; -pub mod discord_fw_callback; pub mod verify_added_liquidity; From 2199137b88e29541365496e5612ad253d44d5cb9 Mon Sep 17 00:00:00 2001 From: Anton Rudenko Date: Sun, 24 Nov 2024 17:32:42 +0200 Subject: [PATCH 2/3] Fix: Implement get_quest_users admin route --- .../admin/quest_boost/get_quest_users.rs | 89 +++++++++++++++++++ src/endpoints/admin/quest_boost/mod.rs | 1 + src/models.rs | 6 ++ 3 files changed, 96 insertions(+) create mode 100644 src/endpoints/admin/quest_boost/get_quest_users.rs diff --git a/src/endpoints/admin/quest_boost/get_quest_users.rs b/src/endpoints/admin/quest_boost/get_quest_users.rs new file mode 100644 index 00000000..a04636fa --- /dev/null +++ b/src/endpoints/admin/quest_boost/get_quest_users.rs @@ -0,0 +1,89 @@ +use crate::middleware::auth::auth_middleware; +use crate::utils::verify_quest_auth; +use crate::{ + models::{AppState, CompletedTaskDocument, QuestDocument, QuestTaskDocument}, + utils::get_error, +}; +use axum::{ + extract::{Extension, Query, State}, + http::StatusCode, + response::{IntoResponse, Json}, +}; +use axum_auto_routes::route; +use futures::TryStreamExt; +use mongodb::bson::doc; +use serde::Deserialize; +use serde_json::json; +use std::collections::HashSet; +use std::sync::Arc; + +pub_struct!(Deserialize; GetQuestUsersParams { + quest_id: i64, +}); + +#[route(get, "/admin/quests/get_quest_users", auth_middleware)] +pub async fn get_quest_users_handler( + State(state): State>, + Extension(_sub): Extension, + Query(params): Query, +) -> impl IntoResponse { + let tasks_collection = state.db.collection::("tasks"); + let completed_tasks_collection = state + .db + .collection::("completed_tasks"); + let quests_collection = state.db.collection::("quests"); + + let res = verify_quest_auth(_sub, &quests_collection, &(params.quest_id)).await; + if !res { + return get_error("Error getting quest users".to_string()); + }; + + // Fetch all task IDs for the given quest_id + let task_filter = doc! { "quest_id": params.quest_id }; + let task_cursor = match tasks_collection.find(task_filter, None).await { + Ok(cursor) => cursor, + Err(e) => return get_error(format!("Error fetching tasks: {}", e)), + }; + + let task_ids_result: Result, _> = task_cursor + .map_ok(|doc| doc.id as u32) + .try_collect() + .await; + + let task_ids = match task_ids_result { + Ok(ids) => ids, + Err(e) => return get_error(format!("Error processing tasks: {}", e)), + }; + + if task_ids.is_empty() { + return get_error(format!("No tasks found for quest_id {}", params.quest_id)); + } + + // Fetch all completed tasks for these task IDs + let completed_task_filter = doc! { "task_id": { "$in": &task_ids } }; + let completed_task_cursor = match completed_tasks_collection + .find(completed_task_filter, None) + .await + { + Ok(cursor) => cursor, + Err(e) => return get_error(format!("Error fetching completed tasks: {}", e)), + }; + + let completed_tasks_result: Result, _> = + completed_task_cursor.try_collect().await; + + let users = match completed_tasks_result { + Ok(completed_tasks) => { + let user_set: HashSet = completed_tasks + .into_iter() + .map(|task: CompletedTaskDocument| task.address().to_string()) + .collect(); + + let users_list: Vec = user_set.into_iter().collect(); + users_list + } + Err(e) => return get_error(format!("Error processing completed tasks: {}", e)), + }; + + return (StatusCode::OK, Json(json!({ "users": users }))).into_response(); +} diff --git a/src/endpoints/admin/quest_boost/mod.rs b/src/endpoints/admin/quest_boost/mod.rs index 5b1910ad..da8d6c4f 100644 --- a/src/endpoints/admin/quest_boost/mod.rs +++ b/src/endpoints/admin/quest_boost/mod.rs @@ -1,2 +1,3 @@ pub mod create_boost; +pub mod get_quest_users; pub mod update_boost; diff --git a/src/models.rs b/src/models.rs index ad07e816..dc86f03e 100644 --- a/src/models.rs +++ b/src/models.rs @@ -110,6 +110,12 @@ pub struct CompletedTaskDocument { timestamp: i64, } +impl CompletedTaskDocument { + pub fn address(&self) -> &str { + &self.address + } +} + #[derive(Clone, Debug, Deserialize, Serialize)] pub struct Call { pub contract: String, From d250eae2269d58c55e08e0b78bb67c83472cb6fc Mon Sep 17 00:00:00 2001 From: Nico <60229704+Marchand-Nicolas@users.noreply.github.com> Date: Sun, 24 Nov 2024 17:17:42 +0100 Subject: [PATCH 3/3] fix: build error --- src/endpoints/defi/rewards.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/endpoints/defi/rewards.rs b/src/endpoints/defi/rewards.rs index d147f962..95e80a77 100644 --- a/src/endpoints/defi/rewards.rs +++ b/src/endpoints/defi/rewards.rs @@ -16,7 +16,6 @@ use futures::stream::{FuturesOrdered, StreamExt}; use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, USER_AGENT}; use reqwest_middleware::{ClientBuilder, ClientWithMiddleware, Error}; use reqwest_retry::{policies::ExponentialBackoff, RetryTransientMiddleware}; -use reqwest_tracing::TracingMiddleware; use serde::{Deserialize, Serialize}; use serde_json::json; use starknet::{core::types::FieldElement, macros::selector}; @@ -37,7 +36,6 @@ pub async fn get_defi_rewards( // Retry up to 3 times with increasing intervals between attempts. let retry_policy = ExponentialBackoff::builder().build_with_max_retries(3); let client = ClientBuilder::new(reqwest::Client::new()) - .with(TracingMiddleware::default()) .with(RetryTransientMiddleware::new_with_policy(retry_policy)) .build();