refactor: refactor run() fn to be more efficient/more clear

This commit is contained in:
VC
2022-04-24 11:14:32 +02:00
parent 22402f0f46
commit a90facae86

View File

@@ -31,9 +31,16 @@ use rusqlite::Connection;
#[tokio::main] #[tokio::main]
pub async fn run(config: Config) { pub async fn run(config: Config) {
// open the SQLite connection // 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 // 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 // get OAuth2 token
let token = get_oauth2_token(&config.twitter); let token = get_oauth2_token(&config.twitter);
@@ -68,11 +75,10 @@ pub async fn run(config: Config) {
info!("Tweet is a direct response, skipping"); info!("Tweet is a direct response, skipping");
continue; continue;
} }
info!("Tweet is a thread");
let searched_toot = read_state(&conn, tweet.in_reply_to_status_id).unwrap_or(None); toot_reply_id = read_state(&conn, tweet.in_reply_to_status_id)
if let Some(i) = searched_toot { .unwrap_or(None)
toot_reply_id = Some(i.toot_id); .map(|s| s.toot_id);
};
}; };
// build basic status by just yielding text and dereferencing contained urls // 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<String> = vec![]; let mut status_medias: Vec<String> = vec![];
// reupload the attachments if any // reupload the attachments if any
if let Some(m) = &tweet.extended_entities { if let Some(m) = &tweet.extended_entities {
for media in &m.media { 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))) { let mastodon_media_ids = match mastodon.media(Cow::from(String::from(&local_tweet_media_path))) {
Ok(m) => { Ok(m) => {
remove_file(&local_tweet_media_path).await.unwrap_or_else(|e| remove_file(&local_tweet_media_path)
warn!("Attachment for {} has been uploaded, but Im unable to remove the existing file: {}", &local_tweet_media_path, e) .await
); .unwrap_or_else(|e|
warn!("Attachment for {} has been uploaded, but Im unable to remove the existing file: {}", &local_tweet_media_path, e)
);
m.id m.id
}, },
Err(e) => { Err(e) => {
@@ -143,7 +150,7 @@ pub async fn run(config: Config) {
toot_id: published_status.id, 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| write_state(&conn, ttt_towrite).unwrap_or_else(|e|
panic!("Cant write the last tweet retrieved: {}", e) panic!("Cant write the last tweet retrieved: {}", e)
); );