From 7a0eae68c1f3496a1d421b61f66115a7889d7e92 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 18 Jul 2023 23:12:34 -0400 Subject: reconnect to websockets after every successful sync rather than scattering it around various parts of the code --- src/bin/rbw-agent/actions.rs | 14 ++++++-------- src/bin/rbw-agent/agent.rs | 25 +++++-------------------- 2 files changed, 11 insertions(+), 28 deletions(-) (limited to 'src/bin') diff --git a/src/bin/rbw-agent/actions.rs b/src/bin/rbw-agent/actions.rs index b7295f8..4c7f6fb 100644 --- a/src/bin/rbw-agent/actions.rs +++ b/src/bin/rbw-agent/actions.rs @@ -205,10 +205,6 @@ pub async fn login( } } } - - if let Err(e) = subscribe_to_notifications(state.clone()).await { - eprintln!("failed to subscribe to notifications: {e}"); - } } respond_ack(sock).await?; @@ -332,7 +328,7 @@ async fn login_success( db.protected_key = Some(protected_key.to_string()); save_db(&db).await?; - sync(None).await?; + sync(None, state.clone()).await?; let db = load_db().await?; let Some(protected_private_key) = db.protected_private_key @@ -501,6 +497,7 @@ pub async fn check_lock( pub async fn sync( sock: Option<&mut crate::sock::Sock>, + state: std::sync::Arc>, ) -> anyhow::Result<()> { let mut db = load_db().await?; @@ -529,6 +526,10 @@ pub async fn sync( db.entries = entries; save_db(&db).await?; + if let Err(e) = subscribe_to_notifications(state.clone()).await { + eprintln!("failed to subscribe to notifications: {e}"); + } + if let Some(sock) = sock { respond_ack(sock).await?; } @@ -688,9 +689,6 @@ pub async fn subscribe_to_notifications( return Ok(()); } - // access token might be out of date, so we do a sync to refresh it - sync(None).await?; - let config = rbw::config::Config::load_async() .await .context("Config is missing")?; diff --git a/src/bin/rbw-agent/agent.rs b/src/bin/rbw-agent/agent.rs index 131bab2..29e400b 100644 --- a/src/bin/rbw-agent/agent.rs +++ b/src/bin/rbw-agent/agent.rs @@ -89,13 +89,6 @@ impl Agent { Sync(()), } - let err = - crate::actions::subscribe_to_notifications(self.state.clone()) - .await; - if let Err(e) = err { - eprintln!("failed to subscribe to notifications: {e:#}"); - } - let c: tokio::sync::mpsc::UnboundedReceiver< notifications::NotificationMessage, > = { @@ -160,18 +153,10 @@ impl Agent { tokio::spawn(async move { // this could fail if we aren't logged in, but we // don't care about that - match crate::actions::sync(None).await { - Ok(()) => { - if let Err(e) = crate::actions::subscribe_to_notifications( - state, - ) - .await { - eprintln!("failed to subscribe to notifications: {e:#}"); - } - } - Err(e) => { - eprintln!("failed to sync: {e:#}"); - } + if let Err(e) = + crate::actions::sync(None, state.clone()).await + { + eprintln!("failed to sync: {e:#}"); } }); self.state.lock().await.set_sync_timeout(); @@ -223,7 +208,7 @@ async fn handle_request( false } rbw::protocol::Action::Sync => { - crate::actions::sync(Some(sock)).await?; + crate::actions::sync(Some(sock), state.clone()).await?; false } rbw::protocol::Action::Decrypt { -- cgit v1.2.3-54-g00ecf