Formatting
This commit is contained in:
parent
768bb9f175
commit
f5e1d3e020
|
@ -51,7 +51,6 @@ async fn main() -> Result<()> {
|
||||||
avatar = Some(bluesky.upload_blob(bytes).await?);
|
avatar = Some(bluesky.upload_blob(bytes).await?);
|
||||||
println!("Uploaded avatar");
|
println!("Uploaded avatar");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bluesky
|
bluesky
|
||||||
.publish_feed(
|
.publish_feed(
|
||||||
|
|
|
@ -2,7 +2,7 @@ extern crate nederlandskie;
|
||||||
|
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use anyhow::{Context, Result, anyhow};
|
use anyhow::{anyhow, Context, Result};
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
|
|
||||||
use nederlandskie::services::Bluesky;
|
use nederlandskie::services::Bluesky;
|
||||||
|
@ -18,7 +18,9 @@ async fn main() -> Result<()> {
|
||||||
.context("PUBLISHER_BLUESKY_PASSWORD environment variable must be set")?;
|
.context("PUBLISHER_BLUESKY_PASSWORD environment variable must be set")?;
|
||||||
|
|
||||||
let bluesky = Bluesky::login("https://bsky.social", &handle, &password).await?;
|
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);
|
println!("{}", session.did);
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use log::{error, info, debug};
|
use log::{debug, error, info};
|
||||||
|
|
||||||
use crate::algos::Algos;
|
use crate::algos::Algos;
|
||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
|
|
|
@ -25,14 +25,14 @@ pub struct ProfileDetails {
|
||||||
|
|
||||||
pub struct Bluesky {
|
pub struct Bluesky {
|
||||||
client: AtpServiceClient<AtpServiceWrapper<AuthenticateableXrpcClient>>,
|
client: AtpServiceClient<AtpServiceWrapper<AuthenticateableXrpcClient>>,
|
||||||
session: Option<Arc<Mutex<Session>>>
|
session: Option<Arc<Mutex<Session>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Bluesky {
|
impl Bluesky {
|
||||||
pub fn unauthenticated(host: &str) -> Self {
|
pub fn unauthenticated(host: &str) -> Self {
|
||||||
Self {
|
Self {
|
||||||
client: AtpServiceClient::new(AuthenticateableXrpcClient::new(host.to_owned())),
|
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(
|
let authenticated_client = AtpServiceClient::new(AuthenticateableXrpcClient::with_session(
|
||||||
host.to_owned(),
|
host.to_owned(),
|
||||||
session.clone()
|
session.clone(),
|
||||||
));
|
));
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
client: authenticated_client,
|
client: authenticated_client,
|
||||||
session: Some(session)
|
session: Some(session),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn session(&self) -> Option<Session> {
|
pub fn session(&self) -> Option<Session> {
|
||||||
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<u8>) -> Result<BlobRef> {
|
pub async fn upload_blob(&self, blob: Vec<u8>) -> Result<BlobRef> {
|
||||||
|
@ -204,15 +207,28 @@ impl Bluesky {
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn ensure_token_valid(&self) -> Result<()> {
|
async fn ensure_token_valid(&self) -> Result<()> {
|
||||||
let access_jwt_exp =
|
let access_jwt_exp = self
|
||||||
self.session.as_ref().ok_or_else(|| anyhow!("Not authenticated"))?.lock().map_err(|e| anyhow!("session mutex is poisoned: {e}"))?.access_jwt_exp;
|
.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;
|
let jwt_expired = Utc::now() > access_jwt_exp;
|
||||||
|
|
||||||
if jwt_expired {
|
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()
|
.as_ref()
|
||||||
.ok_or_else(|| anyhow!("Not authenticated"))?
|
.ok_or_else(|| anyhow!("Not authenticated"))?
|
||||||
.lock()
|
.lock()
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use atrium_xrpc::{client::reqwest::ReqwestClient, HttpClient, XrpcClient};
|
use atrium_xrpc::{client::reqwest::ReqwestClient, HttpClient, XrpcClient};
|
||||||
use http::{Request, Response, Method};
|
use http::{Method, Request, Response};
|
||||||
use std::sync::{Arc, Mutex};
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
use super::session::Session;
|
use super::session::Session;
|
||||||
|
@ -35,9 +35,16 @@ impl HttpClient for AuthenticateableXrpcClient {
|
||||||
let (mut parts, body) = req.into_parts();
|
let (mut parts, body) = req.into_parts();
|
||||||
|
|
||||||
/* NOTE: This is a huge hack because auth is currently totally broken in atrium-api */
|
/* 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) {
|
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);
|
let req = Request::from_parts(parts, body);
|
||||||
|
|
Loading…
Reference in New Issue