aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin/rbw-agent/actions.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/rbw-agent/actions.rs')
-rw-r--r--src/bin/rbw-agent/actions.rs25
1 files changed, 17 insertions, 8 deletions
diff --git a/src/bin/rbw-agent/actions.rs b/src/bin/rbw-agent/actions.rs
index 006f7ca..cc363dd 100644
--- a/src/bin/rbw-agent/actions.rs
+++ b/src/bin/rbw-agent/actions.rs
@@ -659,14 +659,23 @@ async fn config_pinentry() -> anyhow::Result<String> {
}
pub async fn subscribe_to_notifications(state: std::sync::Arc<tokio::sync::RwLock<crate::agent::State>>) -> anyhow::Result<()> {
- let config = rbw::config::Config::load_async().await.expect("Config is missing");
- let mut websocket_url = config.base_url.clone().expect("Config is missing base url").replace("https://", "wss://") + "/notifications/hub?access_token=";
- let email = config.email.clone().expect("Config is missing email");
- let db = rbw::db::Db::load_async(&config.server_name().as_str(), &email).await.expect("Error loading db");
- let access_token = db.access_token.expect("Error getting access token");
+ // 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")?;
+ let email = config.email.clone().context("Config is missing email")?;
+ let db = rbw::db::Db::load_async(&config.server_name().as_str(), &email).await?;
+ let access_token = db.access_token.context("Error getting access token")?;
+
+ let mut websocket_url = config.base_url.clone().expect("config is missing base url").replace("https://", "wss://") + "/notifications/hub?access_token=";
websocket_url = websocket_url + &access_token;
+
let mut state = state.write().await;
- state.notifications_handler.connect(websocket_url).await.expect("Error connecting to websocket");
-
- Ok(())
+ let err = state.notifications_handler.connect(websocket_url).await.err();
+
+ if let Some(err) = err {
+ return Err(anyhow::anyhow!(err.to_string()));
+ } else {
+ Ok(())
+ }
} \ No newline at end of file