Merge branch 'reply_improvements' into 'master'

refactor: improve reply/thread management

See merge request veretcle/scootaloo!25
This commit is contained in:
VC
2022-11-09 20:26:20 +00:00
4 changed files with 738 additions and 526 deletions

1203
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,6 +1,6 @@
[package]
name = "scootaloo"
version = "0.8.0"
version = "0.8.1"
authors = ["VC <veretcle+framagit@mateu.be>"]
edition = "2021"

View File

@@ -1,8 +1,12 @@
use std::{
boxed::Box,
convert::From,
error::Error,
fmt::{Display, Formatter, Result},
};
use elefren::Error as elefrenError;
#[derive(Debug)]
pub struct ScootalooError {
details: String,
@@ -23,3 +27,15 @@ impl Display for ScootalooError {
write!(f, "{}", self.details)
}
}
impl From<Box<dyn Error>> for ScootalooError {
fn from(error: Box<dyn Error>) -> Self {
ScootalooError::new(&format!("Error in a subset crate: {}", error))
}
}
impl From<elefrenError> for ScootalooError {
fn from(error: elefrenError) -> Self {
ScootalooError::new(&format!("Error in elefren crate: {}", error))
}
}

View File

@@ -52,31 +52,24 @@ pub async fn run(config: Config) {
// retrieve the last tweet ID for the username
let lconn = task_conn.lock().await;
let last_tweet_id = read_state(&lconn, &mastodon_config.twitter_screen_name, None)
.unwrap_or_else(|e| panic!("Cannot retrieve last_tweet_id: {}", e))
.map(|s| s.tweet_id);
let last_tweet_id =
read_state(&lconn, &mastodon_config.twitter_screen_name, None)?.map(|r| r.tweet_id);
drop(lconn);
// get Mastodon instance
let mastodon = get_mastodon_token(&mastodon_config);
// get user timeline feed (Vec<tweet>)
let mut feed =
get_user_timeline(&mastodon_config.twitter_screen_name, &token, last_tweet_id)
.await
.unwrap_or_else(|e| {
panic!(
"Something went wrong when trying to retrieve {}s timeline: {}",
&mastodon_config.twitter_screen_name, e
)
});
.await?;
// empty feed -> exiting
if feed.is_empty() {
info!("Nothing to retrieve since last time, exiting…");
return;
return Ok(());
}
// get Mastodon instance
let mastodon = get_mastodon_token(&mastodon_config);
// order needs to be chronological
feed.reverse();
@@ -126,15 +119,11 @@ pub async fn run(config: Config) {
// can be activated for test purposes
// status_builder.visibility(elefren::status_builder::Visibility::Private);
let status = status_builder
.build()
.unwrap_or_else(|_| panic!("Cannot build status with text {}", &status_text));
let status = status_builder.build()?;
// publish status
// again unwrap is safe here as we are in the main thread
let published_status = mastodon.new_status(status).unwrap();
// this will panic if it cannot publish the status, which is a good thing, it allows the
// last_tweet gathered not to be written
let published_status = mastodon.new_status(status)?;
// this will return if it cannot publish the status preventing the last_tweet from
// being written into db
let ttt_towrite = TweetToToot {
twitter_screen_name: mastodon_config.twitter_screen_name.clone(),
@@ -144,10 +133,10 @@ pub async fn run(config: Config) {
// write the current state (tweet ID and toot ID) to avoid copying it another time
let lconn = task_conn.lock().await;
write_state(&lconn, ttt_towrite)
.unwrap_or_else(|e| panic!("Cant write the last tweet retrieved: {}", e));
write_state(&lconn, ttt_towrite)?;
drop(lconn);
}
Ok::<(), ScootalooError>(())
});
// push each task into the vec task
@@ -156,6 +145,10 @@ pub async fn run(config: Config) {
// launch and wait for every handle
for handle in mtask {
handle.await.unwrap();
match handle.await {
Ok(Err(e)) => eprintln!("Error within thread: {}", e),
Err(e) => eprintln!("Error with thread: {}", e),
_ => (),
}
}
}