From f5e1d3e02095427195ccf8845768524abae15c6f Mon Sep 17 00:00:00 2001 From: Aleksei Voronov Date: Sat, 7 Oct 2023 18:54:43 +0200 Subject: [PATCH] Formatting --- src/bin/publish_feed.rs | 1 - src/bin/who_am_i.rs | 6 +++-- src/processes/post_indexer.rs | 2 +- src/services/bluesky/client.rs | 34 +++++++++++++++++++++-------- src/services/bluesky/xrpc_client.rs | 13 ++++++++--- 5 files changed, 40 insertions(+), 16 deletions(-) diff --git a/src/bin/publish_feed.rs b/src/bin/publish_feed.rs index d339e41..baabc06 100644 --- a/src/bin/publish_feed.rs +++ b/src/bin/publish_feed.rs @@ -51,7 +51,6 @@ async fn main() -> Result<()> { avatar = Some(bluesky.upload_blob(bytes).await?); println!("Uploaded avatar"); } - bluesky .publish_feed( diff --git a/src/bin/who_am_i.rs b/src/bin/who_am_i.rs index 14f856c..df37704 100644 --- a/src/bin/who_am_i.rs +++ b/src/bin/who_am_i.rs @@ -2,7 +2,7 @@ extern crate nederlandskie; use std::env; -use anyhow::{Context, Result, anyhow}; +use anyhow::{anyhow, Context, Result}; use dotenv::dotenv; use nederlandskie::services::Bluesky; @@ -18,7 +18,9 @@ async fn main() -> Result<()> { .context("PUBLISHER_BLUESKY_PASSWORD environment variable must be set")?; let bluesky = Bluesky::login("https://bsky.social", &handle, &password).await?; - let session = bluesky.session().ok_or_else(|| anyhow!("Could not log in"))?; + let session = bluesky + .session() + .ok_or_else(|| anyhow!("Could not log in"))?; println!("{}", session.did); diff --git a/src/processes/post_indexer.rs b/src/processes/post_indexer.rs index 68572bb..ba88f11 100644 --- a/src/processes/post_indexer.rs +++ b/src/processes/post_indexer.rs @@ -3,7 +3,7 @@ use std::time::Duration; use anyhow::Result; use async_trait::async_trait; -use log::{error, info, debug}; +use log::{debug, error, info}; use crate::algos::Algos; use crate::config::Config; diff --git a/src/services/bluesky/client.rs b/src/services/bluesky/client.rs index 6ed3ad6..250aa2f 100644 --- a/src/services/bluesky/client.rs +++ b/src/services/bluesky/client.rs @@ -25,14 +25,14 @@ pub struct ProfileDetails { pub struct Bluesky { client: AtpServiceClient>, - session: Option>> + session: Option>>, } impl Bluesky { pub fn unauthenticated(host: &str) -> Self { Self { client: AtpServiceClient::new(AuthenticateableXrpcClient::new(host.to_owned())), - session: None + session: None, } } @@ -56,17 +56,20 @@ impl Bluesky { let authenticated_client = AtpServiceClient::new(AuthenticateableXrpcClient::with_session( host.to_owned(), - session.clone() + session.clone(), )); Ok(Self { client: authenticated_client, - session: Some(session) + session: Some(session), }) } pub fn session(&self) -> Option { - self.session.as_ref().and_then(|s| s.lock().ok()).map(|s| s.clone()) + self.session + .as_ref() + .and_then(|s| s.lock().ok()) + .map(|s| s.clone()) } pub async fn upload_blob(&self, blob: Vec) -> Result { @@ -204,15 +207,28 @@ impl Bluesky { } async fn ensure_token_valid(&self) -> Result<()> { - let access_jwt_exp = - self.session.as_ref().ok_or_else(|| anyhow!("Not authenticated"))?.lock().map_err(|e| anyhow!("session mutex is poisoned: {e}"))?.access_jwt_exp; + let access_jwt_exp = self + .session + .as_ref() + .ok_or_else(|| anyhow!("Not authenticated"))? + .lock() + .map_err(|e| anyhow!("session mutex is poisoned: {e}"))? + .access_jwt_exp; let jwt_expired = Utc::now() > access_jwt_exp; if jwt_expired { - let refreshed = self.client.service.com.atproto.server.refresh_session().await?; + let refreshed = self + .client + .service + .com + .atproto + .server + .refresh_session() + .await?; - let mut session = self.session + let mut session = self + .session .as_ref() .ok_or_else(|| anyhow!("Not authenticated"))? .lock() diff --git a/src/services/bluesky/xrpc_client.rs b/src/services/bluesky/xrpc_client.rs index cdcf42b..888000f 100644 --- a/src/services/bluesky/xrpc_client.rs +++ b/src/services/bluesky/xrpc_client.rs @@ -1,6 +1,6 @@ use async_trait::async_trait; use atrium_xrpc::{client::reqwest::ReqwestClient, HttpClient, XrpcClient}; -use http::{Request, Response, Method}; +use http::{Method, Request, Response}; use std::sync::{Arc, Mutex}; use super::session::Session; @@ -35,9 +35,16 @@ impl HttpClient for AuthenticateableXrpcClient { let (mut parts, body) = req.into_parts(); /* NOTE: This is a huge hack because auth is currently totally broken in atrium-api */ - let is_request_to_refresh_session = parts.method == Method::POST && parts.uri.to_string().ends_with("com.atproto.server.refreshSession"); + let is_request_to_refresh_session = parts.method == Method::POST + && parts + .uri + .to_string() + .ends_with("com.atproto.server.refreshSession"); if let Some(token) = self.auth(is_request_to_refresh_session) { - parts.headers.insert(http::header::AUTHORIZATION, format!("Bearer {}", token).parse()?); + parts.headers.insert( + http::header::AUTHORIZATION, + format!("Bearer {}", token).parse()?, + ); } let req = Request::from_parts(parts, body);