From a90facae866ec7825f02509c982e9afc6d8396e7 Mon Sep 17 00:00:00 2001 From: VC Date: Sun, 24 Apr 2022 11:14:32 +0200 Subject: [PATCH] refactor: refactor run() fn to be more efficient/more clear --- src/lib.rs | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index bb50934..49f454c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -31,9 +31,16 @@ use rusqlite::Connection; #[tokio::main] pub async fn run(config: Config) { // open the SQLite connection - let conn = Connection::open(&config.scootaloo.db_path).unwrap(); + let conn = Connection::open(&config.scootaloo.db_path) + .unwrap_or_else(|e| + panic!("Something went wrong when opening the DB {}: {}", &config.scootaloo.db_path, e) + ); // retrieve the last tweet ID for the username - let last_tweet_id = read_state(&conn, None).unwrap().map(|s| s.tweet_id); + let last_tweet_id = read_state(&conn, None) + .unwrap_or_else(|e| + panic!("Cannot retrieve last_tweet_id: {}", e) + ) + .map(|s| s.tweet_id); // get OAuth2 token let token = get_oauth2_token(&config.twitter); @@ -68,11 +75,10 @@ pub async fn run(config: Config) { info!("Tweet is a direct response, skipping"); continue; } - - let searched_toot = read_state(&conn, tweet.in_reply_to_status_id).unwrap_or(None); - if let Some(i) = searched_toot { - toot_reply_id = Some(i.toot_id); - }; + info!("Tweet is a thread"); + toot_reply_id = read_state(&conn, tweet.in_reply_to_status_id) + .unwrap_or(None) + .map(|s| s.toot_id); }; // build basic status by just yielding text and dereferencing contained urls @@ -85,7 +91,6 @@ pub async fn run(config: Config) { }; let mut status_medias: Vec = vec![]; - // reupload the attachments if any if let Some(m) = &tweet.extended_entities { for media in &m.media { @@ -99,9 +104,11 @@ pub async fn run(config: Config) { let mastodon_media_ids = match mastodon.media(Cow::from(String::from(&local_tweet_media_path))) { Ok(m) => { - remove_file(&local_tweet_media_path).await.unwrap_or_else(|e| - warn!("Attachment for {} has been uploaded, but I’m unable to remove the existing file: {}", &local_tweet_media_path, e) - ); + remove_file(&local_tweet_media_path) + .await + .unwrap_or_else(|e| + warn!("Attachment for {} has been uploaded, but I’m unable to remove the existing file: {}", &local_tweet_media_path, e) + ); m.id }, Err(e) => { @@ -143,7 +150,7 @@ pub async fn run(config: Config) { toot_id: published_status.id, }; - // write the current state (tweet ID) to avoid copying it another time + // write the current state (tweet ID and toot ID) to avoid copying it another time write_state(&conn, ttt_towrite).unwrap_or_else(|e| panic!("Can’t write the last tweet retrieved: {}", e) );