mirror of
https://framagit.org/veretcle/oolatoocs.git
synced 2025-07-20 20:41:17 +02:00
♻️: refactor bsky session
This commit is contained in:
37
src/bsky.rs
37
src/bsky.rs
@@ -3,11 +3,15 @@ use atrium_api::{
|
||||
app::bsky::feed::post::RecordData, com::atproto::repo::upload_blob::Output,
|
||||
types::string::Datetime, types::string::Language,
|
||||
};
|
||||
use bsky_sdk::{rich_text::RichText, BskyAgent};
|
||||
use bsky_sdk::{
|
||||
agent::config::{Config, FileStore},
|
||||
rich_text::RichText,
|
||||
BskyAgent,
|
||||
};
|
||||
use log::error;
|
||||
use megalodon::entities::attachment::{Attachment, AttachmentType};
|
||||
use regex::Regex;
|
||||
use std::error::Error;
|
||||
use std::{error::Error, fs::exists};
|
||||
|
||||
/// Intermediary struct to deal with replies more easily
|
||||
#[derive(Debug)]
|
||||
@@ -16,6 +20,34 @@ pub struct BskyReply {
|
||||
pub root_record_uri: String,
|
||||
}
|
||||
|
||||
pub async fn get_session(config: &BlueskyConfig) -> Result<BskyAgent, Box<dyn Error>> {
|
||||
if exists(&config.config_path)? {
|
||||
let bluesky = BskyAgent::builder()
|
||||
.config(Config::load(&FileStore::new(&config.config_path)).await?)
|
||||
.build()
|
||||
.await?;
|
||||
|
||||
if bluesky.api.com.atproto.server.get_session().await.is_ok() {
|
||||
bluesky
|
||||
.to_config()
|
||||
.await
|
||||
.save(&FileStore::new(&config.config_path))
|
||||
.await?;
|
||||
return Ok(bluesky);
|
||||
}
|
||||
}
|
||||
|
||||
let bluesky = BskyAgent::builder().build().await?;
|
||||
bluesky.login(&config.handle, &config.password).await?;
|
||||
bluesky
|
||||
.to_config()
|
||||
.await
|
||||
.save(&FileStore::new(&config.config_path))
|
||||
.await?;
|
||||
|
||||
Ok(bluesky)
|
||||
}
|
||||
|
||||
pub async fn build_post_record(
|
||||
config: &BlueskyConfig,
|
||||
text: &str,
|
||||
@@ -196,6 +228,7 @@ mod tests {
|
||||
let bsky_conf = BlueskyConfig {
|
||||
handle: "tamerelol.bsky.social".to_string(),
|
||||
password: "dtc".to_string(),
|
||||
config_path: "nope".to_string(),
|
||||
};
|
||||
|
||||
let created_record_data = build_post_record(&bsky_conf, text, &None, None, &None)
|
||||
|
@@ -35,6 +35,7 @@ pub struct MastodonConfig {
|
||||
pub struct BlueskyConfig {
|
||||
pub handle: String,
|
||||
pub password: String,
|
||||
pub config_path: String,
|
||||
}
|
||||
|
||||
/// parses TOML file into Config struct
|
||||
|
23
src/lib.rs
23
src/lib.rs
@@ -1,4 +1,3 @@
|
||||
use bsky_sdk::BskyAgent;
|
||||
use log::debug;
|
||||
|
||||
mod error;
|
||||
@@ -22,7 +21,7 @@ mod twitter;
|
||||
use twitter::{delete_tweet, generate_media_ids, post_tweet, transform_poll};
|
||||
|
||||
mod bsky;
|
||||
use bsky::{build_post_record, generate_media_records, BskyReply};
|
||||
use bsky::{build_post_record, generate_media_records, get_session, BskyReply};
|
||||
|
||||
use rusqlite::Connection;
|
||||
|
||||
@@ -33,10 +32,9 @@ pub async fn run(config: &Config) {
|
||||
|
||||
let mastodon = get_mastodon_instance(&config.mastodon);
|
||||
|
||||
let bluesky = BskyAgent::builder()
|
||||
.build()
|
||||
let bluesky = get_session(&config.bluesky)
|
||||
.await
|
||||
.unwrap_or_else(|e| panic!("Can’t build Bsky Agent: {}", e));
|
||||
.unwrap_or_else(|e| panic!("Cannot get Bsky session: {}", e));
|
||||
|
||||
let last_entry =
|
||||
read_state(&conn, None).unwrap_or_else(|e| panic!("Cannot get last toot id: {}", e));
|
||||
@@ -64,13 +62,6 @@ 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)
|
||||
@@ -108,14 +99,6 @@ pub async fn run(config: &Config) {
|
||||
continue; // skip in case we can’t 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
|
||||
|
Reference in New Issue
Block a user