From 5a7c3155ace1c3a9385cc0e646c00b3df6ac5845 Mon Sep 17 00:00:00 2001 From: Sticks Date: Tue, 22 Apr 2025 18:56:27 -0400 Subject: [PATCH] feat: add sync progress, break loops once we get 0 users back --- .idea/.gitignore | 8 ++ .idea/FanslySync.iml | 12 ++ .idea/codeStyles/Project.xml | 63 ++++++++++ .idea/codeStyles/codeStyleConfig.xml | 5 + .idea/inspectionProfiles/Project_Default.xml | 6 + .idea/misc.xml | 5 + .idea/modules.xml | 8 ++ .idea/prettier.xml | 6 + .idea/vcs.xml | 6 + src-tauri/src/commands/fansly/mod.rs | 8 +- src-tauri/src/handlers/fansly/mod.rs | 80 ++++++++++++- src-tauri/src/main.rs | 5 +- src/routes/home/+page.svelte | 120 +++++++++++++------ 13 files changed, 292 insertions(+), 40 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/FanslySync.iml create mode 100644 .idea/codeStyles/Project.xml create mode 100644 .idea/codeStyles/codeStyleConfig.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/prettier.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/FanslySync.iml b/.idea/FanslySync.iml new file mode 100644 index 0000000..298d056 --- /dev/null +++ b/.idea/FanslySync.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml new file mode 100644 index 0000000..5055868 --- /dev/null +++ b/.idea/codeStyles/Project.xml @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/codeStyles/codeStyleConfig.xml b/.idea/codeStyles/codeStyleConfig.xml new file mode 100644 index 0000000..79ee123 --- /dev/null +++ b/.idea/codeStyles/codeStyleConfig.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..03d9549 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..6e86672 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..7cf8a06 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/prettier.xml b/.idea/prettier.xml new file mode 100644 index 0000000..b0c1c68 --- /dev/null +++ b/.idea/prettier.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/src-tauri/src/commands/fansly/mod.rs b/src-tauri/src/commands/fansly/mod.rs index 7763aec..a5ecf07 100644 --- a/src-tauri/src/commands/fansly/mod.rs +++ b/src-tauri/src/commands/fansly/mod.rs @@ -5,6 +5,7 @@ use crate::{ use lazy_static::lazy_static; use serde_json::Value; use tokio::sync::Mutex; +use crate::handlers::fansly::{SyncProgress, PROGRESS}; lazy_static! { static ref FANSLY: Mutex = Mutex::new(Fansly::new(None)); @@ -28,7 +29,7 @@ pub async fn fansly_get_me() -> Result #[tauri::command] pub async fn fansly_sync(auto: bool) -> Result { - let fansly = FANSLY.lock().await; + let mut fansly = FANSLY.lock().await; let response = fansly.sync(auto).await; match response { @@ -37,6 +38,11 @@ pub async fn fansly_sync(auto: bool) -> Result { } } +#[tauri::command] +pub async fn fansly_get_sync_status() -> SyncProgress { + PROGRESS.lock().await.clone() +} + #[tauri::command] pub async fn fansly_upload_auto_sync_data( data: SyncDataResponse, diff --git a/src-tauri/src/handlers/fansly/mod.rs b/src-tauri/src/handlers/fansly/mod.rs index 15b7792..1cbedb2 100644 --- a/src-tauri/src/handlers/fansly/mod.rs +++ b/src-tauri/src/handlers/fansly/mod.rs @@ -1,3 +1,5 @@ +use lazy_static::lazy::Lazy; +use lazy_static::lazy_static; // Create a simple module for handling the Fansly API, using reqwest to make requests to the API. // This module will contain a struct Fansly, which will have a method to get the user's profile information. use crate::structs::{ @@ -5,7 +7,24 @@ use crate::structs::{ FanslySubscriptionsResponse, Subscription, SyncDataResponse, }; use reqwest::header::{HeaderMap, HeaderValue, USER_AGENT}; +use serde::{Deserialize, Serialize}; use serde_json::Value; +use tokio::sync::Mutex; + +// Create a PROGRESS mutex to hold the current sync progress, lazy initialized +lazy_static! { + pub static ref PROGRESS: Mutex = Mutex::new(SyncProgress::default()); +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct SyncProgress { + // Should contain the current progress of the sync operation + pub current_step: String, + pub percentage_done: u32, + pub current_count: u32, + pub total_count: u32, + pub complete: bool, +} pub struct Fansly { client: reqwest::Client, @@ -170,6 +189,26 @@ impl Fansly { Ok(subscriptions.response.subscriptions) } + async fn update_progress( + &self, + current_step: impl Into, + curr_count: u32, + total_count: u32, + complete: bool, + ) { + let mut p = PROGRESS.lock().await; + p.current_step = current_step.into(); + p.current_count = curr_count; + p.total_count = total_count; + p.percentage_done = if total_count > 0 { + curr_count * 100 / total_count + } else { + 0 + }; + p.complete = complete; + } + + async fn upload_sync_data(&self, data: SyncDataResponse) -> Result { let url = "https://paste.fanslycreatorbot.com"; @@ -273,7 +312,10 @@ impl Fansly { } } - pub async fn sync(&self, auto: bool) -> Result { + pub async fn sync(&mut self, auto: bool) -> Result { + // Reset progress + self.update_progress("Starting Sync".to_string(), 0, 100, false).await; + // Fetch profile log::info!("[sync::process] Fetching profile..."); let profile = self.get_profile().await.map_err(|e| e.to_string())?; @@ -313,14 +355,28 @@ impl Fansly { "[sync::process] Got {} followers from API.", response.response.len() ); - followers.extend(response.response); + followers.extend(response.response.clone()); offset += 100; total_requests += 1; + // Update progress + self.update_progress( + "Fetching Followers".to_string(), + followers.len() as u32, + total_followers as u32, + false + ).await; + // Every 10 requests, sleep for a bit to avoid rate limiting if total_requests % 10 == 0 { tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; } + + // If we've received no followers, break the loop + if response.response.is_empty() { + log::info!("[sync::process] No more followers found, breaking the loop."); + break; + } } // Fetch subscribers until we have all of them @@ -336,14 +392,28 @@ impl Fansly { .await .map_err(|e| e.to_string())?; - subscribers.extend(response); + subscribers.extend(response.clone()); offset += 100; total_requests += 1; + // Update progress + self.update_progress( + "Fetching Subscribers".to_string(), + subscribers.len() as u32, + total_subscribers as u32, + false + ).await; + // Every 10 requests, sleep for a bit to avoid rate limiting if total_requests % 10 == 0 { tokio::time::sleep(tokio::time::Duration::from_secs(5)).await; } + + // If we've received no subscribers, break the loop + if response.is_empty() { + log::info!("[sync::process] No more subscribers found, breaking the loop."); + break; + } } log::info!( @@ -353,6 +423,10 @@ impl Fansly { ); log::info!("[sync::process] Sync complete."); + + // Reset progress + self.update_progress("Sync Complete".to_string(), 100, 100, true).await; + log::info!("[sync::process] Uploading sync data to paste.hep.gg for processing..."); // Upload sync data to paste.hep.gg diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 55e6cc9..9ace7c5 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -11,7 +11,7 @@ use std::io; use commands::config::{get_config, init_config, save_config}; use commands::fansly::{ fansly_check_sync_token, fansly_get_me, fansly_set_token, fansly_sync, - fansly_upload_auto_sync_data, + fansly_upload_auto_sync_data, fansly_get_sync_status }; use commands::utils::quit; use tauri_plugin_autostart::MacosLauncher; @@ -66,7 +66,8 @@ async fn main() { fansly_get_me, fansly_sync, fansly_upload_auto_sync_data, - fansly_check_sync_token + fansly_check_sync_token, + fansly_get_sync_status ]) .run(tauri::generate_context!()) .expect("error while running tauri application"); diff --git a/src/routes/home/+page.svelte b/src/routes/home/+page.svelte index a4d47df..6bb8b83 100644 --- a/src/routes/home/+page.svelte +++ b/src/routes/home/+page.svelte @@ -1,4 +1,5 @@