Skip to content

Commit

Permalink
Keep querying for user till we get a profile ID.
Browse files Browse the repository at this point in the history
  • Loading branch information
ryantaylor committed Feb 17, 2024
1 parent a5303c1 commit f9e48fe
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
1 change: 1 addition & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
40 changes: 29 additions & 11 deletions src-tauri/src/plugins/cohdb/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use tauri::{
plugin::{Builder, TauriPlugin},
AppHandle, Manager, Runtime,
};
use tokio::time::{interval, Duration};

#[derive(Debug)]
struct ActiveRequestState {
Expand Down Expand Up @@ -132,6 +133,11 @@ pub async fn retrieve_token<R: Runtime>(request: &str, handle: &AppHandle<R>) ->
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());

Expand Down Expand Up @@ -236,19 +242,30 @@ pub fn init<R: Runtime>(client_id: String, redirect_uri: String) -> TauriPlugin<

fn init_user<R: Runtime>(handle: AppHandle<R>) -> JoinHandle<()> {
tauri::async_runtime::spawn(async move {
let state = handle.state::<PluginState>();
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::<PluginState>();
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");
}
})
}
Expand All @@ -274,6 +291,7 @@ async fn query_user(client: &Client) -> Result<MeResponse> {

MeResponse::from_response(res).await
}

fn set_focus<R: Runtime>(handle: &AppHandle<R>) {
for (_, val) in handle.windows().iter() {
val.set_focus().ok();
Expand Down

0 comments on commit f9e48fe

Please sign in to comment.