From f9e48fe76d3475c3e259f516ac79fc6895fe2bad Mon Sep 17 00:00:00 2001 From: ryantaylor <2320507+ryantaylor@users.noreply.github.com> Date: Sat, 17 Feb 2024 18:56:44 -0500 Subject: [PATCH] Keep querying for user till we get a profile ID. --- src-tauri/Cargo.lock | 1 + src-tauri/Cargo.toml | 1 + src-tauri/src/plugins/cohdb/auth/mod.rs | 40 ++++++++++++++++++------- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 89b9e08..333dfdf 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -729,6 +729,7 @@ dependencies = [ "tauri-plugin-store", "tauri-plugin-window-state", "thiserror", + "tokio", "vault", "window-shadows", ] diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 6ce1ae1..59cc94c 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -33,6 +33,7 @@ tauri-plugin-single-instance = { git = "https://github.com/tauri-apps/plugins-wo tauri-plugin-store = { git = "https://github.com/tauri-apps/plugins-workspace", branch = "dev" } tauri-plugin-window-state = "0.1" thiserror = "1.0" +tokio = { version = "1", features = ["time"] } vault = "8" window-shadows = "0.2" diff --git a/src-tauri/src/plugins/cohdb/auth/mod.rs b/src-tauri/src/plugins/cohdb/auth/mod.rs index 3aaf363..18db3f2 100644 --- a/src-tauri/src/plugins/cohdb/auth/mod.rs +++ b/src-tauri/src/plugins/cohdb/auth/mod.rs @@ -22,6 +22,7 @@ use tauri::{ plugin::{Builder, TauriPlugin}, AppHandle, Manager, Runtime, }; +use tokio::time::{interval, Duration}; #[derive(Debug)] struct ActiveRequestState { @@ -132,6 +133,11 @@ pub async fn retrieve_token(request: &str, handle: &AppHandle) -> if let MeResponse::Ok(user) = me { info!("retrieved user: {user:?}"); + if user.profile_id.is_none() { + info!("no profile ID found for user, querying till we get one"); + init_user(handle.clone()); + } + *state.http_client.lock().await = Some(client); *state.user.lock().await = Some(user.clone()); @@ -236,19 +242,30 @@ pub fn init(client_id: String, redirect_uri: String) -> TauriPlugin< fn init_user(handle: AppHandle) -> JoinHandle<()> { tauri::async_runtime::spawn(async move { - let state = handle.state::(); - let client_option = state.http_client.lock().await; - if let Some(client) = client_option.as_ref() { - match query_user(client).await { - Ok(MeResponse::Ok(user)) => { - info!("got user on init: {:?}", user); - *state.user.lock().await = Some(user); + let mut ticker = interval(Duration::from_secs(5)); + + loop { + ticker.tick().await; + + let state = handle.state::(); + let client_option = state.http_client.lock().await; + if let Some(client) = client_option.as_ref() { + match query_user(client).await { + Ok(MeResponse::Ok(user)) => { + info!("got user: {:?}", user); + *state.user.lock().await = Some(user.clone()); + + if user.profile_id.is_some() { + break; + } + } + Ok(res) => warn!("there was a problem loading the user: {res:?}"), + Err(err) => error!("failed to query user: {err}"), } - Ok(res) => warn!("there was a problem loading the user: {res:?}"), - Err(err) => error!("failed to query user: {err}"), + } else { + info!("not connected, skipping user query"); + break; } - } else { - info!("not connected, skipping user query"); } }) } @@ -274,6 +291,7 @@ async fn query_user(client: &Client) -> Result { MeResponse::from_response(res).await } + fn set_focus(handle: &AppHandle) { for (_, val) in handle.windows().iter() { val.set_focus().ok();