diff --git a/upload/src/main.rs b/upload/src/main.rs index 91a9068..87fdc00 100644 --- a/upload/src/main.rs +++ b/upload/src/main.rs @@ -7,6 +7,7 @@ use jacquard::client::MemorySessionStore; use jacquard::oauth::loopback::LoopbackConfig; use jacquard::oauth::types::AuthorizeOptions; use jacquard::types::string::{AtStrError, RecordKey, Rkey}; +use jacquard::{AuthorizationToken, CowStr, atproto, oauth}; use jacquard::{ Data, api::com_atproto::{self, repo::list_records::ListRecords}, @@ -16,7 +17,6 @@ use jacquard::{ types::{ident::AtIdentifier, nsid::Nsid, string::AtprotoStr, uri::Uri}, xrpc::XrpcExt, }; -use jacquard::{atproto, oauth}; use miette::{Context, IntoDiagnostic, Result}; use std::{collections::HashMap, fs, path::PathBuf}; @@ -140,7 +140,7 @@ async fn upload_site_blobs( k, SitemapNode { mime_type: v.mime_type, - blob: BlobRef::Remote(res.blob), + blob: BlobRef::Remote(res.blob.into()), }, ); } @@ -151,6 +151,7 @@ async fn upload_site_blobs( async fn update_remote_site( agent: &impl AgentSessionExt, config: Config, + auth: AuthorizationToken<'_>, remote_records: Vec, new_sitemap: Sitemap, ) -> Result> { @@ -185,7 +186,9 @@ async fn update_remote_site( "$type": "dev.atcities.route#blob", "blob": { "$type": "blob", - "ref": blob.r#ref.as_str(), + "ref": { + "$link": blob.r#ref.as_str() + }, "mimeType": blob.mime_type.0.as_str(), "size": blob.size } @@ -204,13 +207,20 @@ async fn update_remote_site( writes.append(&mut delete_records); writes.append(&mut create_records); + let repo = if config.user.contains(":") { + AtIdentifier::Did(config.user.into()) + } else { + AtIdentifier::Handle(config.user.into()) + }; + let req = com_atproto::repo::apply_writes::ApplyWrites::new() - .repo(AtIdentifier::Did(config.user.into())) + .repo(repo) .writes(writes) .build(); let res = agent .xrpc(agent.endpoint().await) + .auth(auth) .send::(&req) .await? .into_output()?; @@ -235,7 +245,7 @@ async fn main() -> Result<(), miette::Error> { let store = MemorySessionStore::default(); let session = CredentialSession::new(store.into(), client.into()); - let _ = session + let auth = session .login(config.user.clone().into(), password, None, None, None) .await?; @@ -243,9 +253,16 @@ async fn main() -> Result<(), miette::Error> { let remote_sitemap = live_records(&agent, config.clone()).await?; let new_sitemap = upload_site_blobs(&agent, config.clone(), local_sitemap).await?; - let writes_output = - update_remote_site(&agent, config.clone(), remote_sitemap, new_sitemap).await?; - println!("{writes_output:#?}"); + let _ = update_remote_site( + &agent, + config.clone(), + AuthorizationToken::Bearer(auth.access_jwt), + remote_sitemap, + new_sitemap, + ) + .await?; + + println!("Site is now updated. Live at {}", utils::site_handle(config.user)); } else { let oauth = oauth::client::OAuthClient::with_memory_store(); let session = oauth @@ -256,13 +273,32 @@ async fn main() -> Result<(), miette::Error> { ) .await?; + // sick and twisted reference mangling BUT it works So + // tldr: the cowstr is a borrowed cowstr iiuc, + // so it needs to be turned into an owned cowstr + // to break reference to session which gets moved + let auth = session.access_token().await; + let auth = match auth { + AuthorizationToken::Bearer(cow_str) => CowStr::copy_from_str(cow_str.as_str()), + AuthorizationToken::Dpop(cow_str) => CowStr::copy_from_str(cow_str.as_str()), + }; + + println!("{}", auth); + let agent = Agent::from(session); let remote_sitemap = live_records(&agent, config.clone()).await?; let new_sitemap = upload_site_blobs(&agent, config.clone(), local_sitemap).await?; - let writes_output = - update_remote_site(&agent, config.clone(), remote_sitemap, new_sitemap).await?; - println!("{writes_output:#?}"); + let _ = update_remote_site( + &agent, + config.clone(), + AuthorizationToken::Dpop(auth), + remote_sitemap, + new_sitemap, + ) + .await?; + + println!("Site is now updated. Live at {}", utils::site_handle(config.user)); }; Ok(()) diff --git a/upload/src/utils.rs b/upload/src/utils.rs index 4469067..5044121 100644 --- a/upload/src/utils.rs +++ b/upload/src/utils.rs @@ -1,5 +1,6 @@ use regex::Regex; +#[allow(dead_code)] pub fn rkey_to_url(rkey: String) -> Option { let regex = Regex::new( // symbols A-Za-z0-9 -._~: are all valid rkey characters @@ -62,3 +63,12 @@ pub fn url_to_rkey(url: String) -> Option { Some(res) } + +pub fn site_handle(user: String) -> String { + if user.contains(":") { + let user = user.split(":").collect::>(); + let method = user[1]; + let did = user[2]; + format!("https://{did}.did-{method}.atcities.dev/") + } else { format!("https://{user}.atcities.dev/") } +} \ No newline at end of file