9 Commits

Author SHA1 Message Date
VC
ffbe98f838 Merge branch 'generate_media_flow' into 'master'
Better media flow

See merge request veretcle/scootaloo!26
2022-11-14 13:41:07 +00:00
VC
822f4044c6 chore: bump version 2022-11-14 14:33:42 +01:00
VC
78924f6eeb refactor: simpler error bubbling inside async block 2022-11-14 14:33:39 +01:00
VC
9c14636735 refactor: avoid Box::new syntax, prefer into() 2022-11-14 14:25:08 +01:00
VC
01bac63fb9 Merge branch 'reply_improvements' into 'master'
refactor: improve reply/thread management

See merge request veretcle/scootaloo!25
2022-11-09 20:26:20 +00:00
VC
4f5663b450 feature: better error implementation for ScootalooError inside async block 2022-11-09 19:36:00 +01:00
VC
9a9c4b4809 chore: cargo update 2022-11-09 18:33:04 +01:00
VC
9970968b47 refactor: avoid panicking into thread, bubble up errors to main thread to be handled 2022-11-09 18:23:06 +01:00
VC
291c86677e refactor: get mastodon token after ensuring feed is not empty 2022-11-09 08:40:04 +01:00
6 changed files with 744 additions and 549 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.2"
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),
_ => (),
}
}
}

View File

@@ -87,7 +87,7 @@ pub fn migrate_db(d: &str, s: &str) -> Result<(), Box<dyn Error>> {
match res {
Err(e) => match e.to_string().as_str() {
"duplicate column name: twitter_screen_name" => Ok(()),
_ => Err(Box::new(e)),
_ => Err(e.into()),
},
_ => Ok(()),
}

View File

@@ -45,33 +45,16 @@ pub async fn generate_media_ids(
let task = tokio::task::spawn(async move {
info!("Start treating {}", media.media_url_https);
// get the tweet embedded media
let local_tweet_media_path = match get_tweet_media(&media, &cache_path).await {
Ok(l) => l,
Err(e) => {
return Err(ScootalooError::new(&format!(
"Cannot get tweet media for {}: {}",
&media.url, e
)))
}
};
let local_tweet_media_path = get_tweet_media(&media, &cache_path).await?;
// upload media to Mastodon
let mastodon_media = mastodon.media(Cow::from(local_tweet_media_path.to_owned()));
let mastodon_media =
mastodon.media(Cow::from(local_tweet_media_path.to_owned()))?;
// at this point, we can safely erase the original file
// it doesnt matter if we cant remove, cache_media fn is idempotent
remove_file(&local_tweet_media_path).await.ok();
let mastodon_media = match mastodon_media {
Ok(m) => m,
Err(e) => {
return Err(ScootalooError::new(&format!(
"Attachment {} cannot be uploaded to Mastodon Instance: {}",
&local_tweet_media_path, e
)))
}
};
Ok((i, mastodon_media.id))
Ok::<(usize, String), ScootalooError>((i, mastodon_media.id))
});
tasks.push(task);
@@ -81,7 +64,7 @@ pub async fn generate_media_ids(
match task.await {
// insert the media at the right place
Ok(Ok((i, v))) => media_ids[i] = v,
Ok(Err(e)) => warn!("{}", e),
Ok(Err(e)) => warn!("Cannot treat media: {}", e),
Err(e) => error!("Something went wrong when joining the main thread: {}", e),
}
}