🚑️: avoid opening session when not necessary

This commit is contained in:
VC
2024-10-03 09:38:50 +02:00
parent f46f90ad34
commit 6a7eef757a
4 changed files with 22 additions and 12 deletions

View File

@@ -1,3 +1,4 @@
use bsky_sdk::BskyAgent;
use log::debug;
mod error;
@@ -21,7 +22,7 @@ mod twitter;
use twitter::{delete_tweet, generate_media_ids, post_tweet, transform_poll};
mod bsky;
use bsky::{build_post_record, generate_media_records, get_session, BskyReply};
use bsky::{build_post_record, generate_media_records, BskyReply};
use rusqlite::Connection;
@@ -32,9 +33,10 @@ pub async fn run(config: &Config) {
let mastodon = get_mastodon_instance(&config.mastodon);
let bluesky = get_session(&config.bluesky.handle, &config.bluesky.password)
let bluesky = BskyAgent::builder()
.build()
.await
.unwrap_or_else(|e| panic!("Cannot connect to Bsky: {}", e));
.unwrap_or_else(|e| panic!("Cant build Bsky Agent: {}", e));
let last_entry =
read_state(&conn, None).unwrap_or_else(|e| panic!("Cannot get last toot id: {}", e));
@@ -62,6 +64,13 @@ pub async fn run(config: &Config) {
panic!("Cannot delete Tweet ID ({}): {}", t.tweet_id, e)
});
}
debug!("Create Bsky session prior to deletion");
bluesky
.login(&config.bluesky.handle, &config.bluesky.password)
.await
.unwrap_or_else(|e| panic!("Cannot login to Bsky: {}", e));
for local_record_uri in local_record_uris.into_iter() {
bluesky
.delete_record(&local_record_uri)
@@ -99,6 +108,14 @@ pub async fn run(config: &Config) {
continue; // skip in case we cant strip something
};
debug!("Now we need a valid Bsky session, creating it");
if bluesky.api.com.atproto.server.get_session().await.is_err() {
bluesky
.login(&config.bluesky.handle, &config.bluesky.password)
.await
.unwrap_or_else(|e| panic!("Cannot login to Bsky: {}", e));
}
// threads if necessary
let (mut tweet_reply_to, mut record_reply_to) = toot
.in_reply_to_id