mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-20 17:11:19 +02:00
Merge branch 'reply_improvements' into 'master'
refactor: improve reply/thread management See merge request veretcle/scootaloo!25
This commit is contained in:
1203
Cargo.lock
generated
1203
Cargo.lock
generated
File diff suppressed because it is too large
Load Diff
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "scootaloo"
|
name = "scootaloo"
|
||||||
version = "0.8.0"
|
version = "0.8.1"
|
||||||
authors = ["VC <veretcle+framagit@mateu.be>"]
|
authors = ["VC <veretcle+framagit@mateu.be>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
16
src/error.rs
16
src/error.rs
@@ -1,8 +1,12 @@
|
|||||||
use std::{
|
use std::{
|
||||||
|
boxed::Box,
|
||||||
|
convert::From,
|
||||||
error::Error,
|
error::Error,
|
||||||
fmt::{Display, Formatter, Result},
|
fmt::{Display, Formatter, Result},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use elefren::Error as elefrenError;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ScootalooError {
|
pub struct ScootalooError {
|
||||||
details: String,
|
details: String,
|
||||||
@@ -23,3 +27,15 @@ impl Display for ScootalooError {
|
|||||||
write!(f, "{}", self.details)
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
43
src/lib.rs
43
src/lib.rs
@@ -52,31 +52,24 @@ pub async fn run(config: Config) {
|
|||||||
|
|
||||||
// retrieve the last tweet ID for the username
|
// retrieve the last tweet ID for the username
|
||||||
let lconn = task_conn.lock().await;
|
let lconn = task_conn.lock().await;
|
||||||
let last_tweet_id = read_state(&lconn, &mastodon_config.twitter_screen_name, None)
|
let last_tweet_id =
|
||||||
.unwrap_or_else(|e| panic!("Cannot retrieve last_tweet_id: {}", e))
|
read_state(&lconn, &mastodon_config.twitter_screen_name, None)?.map(|r| r.tweet_id);
|
||||||
.map(|s| s.tweet_id);
|
|
||||||
drop(lconn);
|
drop(lconn);
|
||||||
|
|
||||||
// get Mastodon instance
|
|
||||||
let mastodon = get_mastodon_token(&mastodon_config);
|
|
||||||
|
|
||||||
// get user timeline feed (Vec<tweet>)
|
// get user timeline feed (Vec<tweet>)
|
||||||
let mut feed =
|
let mut feed =
|
||||||
get_user_timeline(&mastodon_config.twitter_screen_name, &token, last_tweet_id)
|
get_user_timeline(&mastodon_config.twitter_screen_name, &token, last_tweet_id)
|
||||||
.await
|
.await?;
|
||||||
.unwrap_or_else(|e| {
|
|
||||||
panic!(
|
|
||||||
"Something went wrong when trying to retrieve {}’s timeline: {}",
|
|
||||||
&mastodon_config.twitter_screen_name, e
|
|
||||||
)
|
|
||||||
});
|
|
||||||
|
|
||||||
// empty feed -> exiting
|
// empty feed -> exiting
|
||||||
if feed.is_empty() {
|
if feed.is_empty() {
|
||||||
info!("Nothing to retrieve since last time, exiting…");
|
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
|
// order needs to be chronological
|
||||||
feed.reverse();
|
feed.reverse();
|
||||||
|
|
||||||
@@ -126,15 +119,11 @@ pub async fn run(config: Config) {
|
|||||||
// can be activated for test purposes
|
// can be activated for test purposes
|
||||||
// status_builder.visibility(elefren::status_builder::Visibility::Private);
|
// status_builder.visibility(elefren::status_builder::Visibility::Private);
|
||||||
|
|
||||||
let status = status_builder
|
let status = status_builder.build()?;
|
||||||
.build()
|
|
||||||
.unwrap_or_else(|_| panic!("Cannot build status with text {}", &status_text));
|
|
||||||
|
|
||||||
// publish status
|
let published_status = mastodon.new_status(status)?;
|
||||||
// again unwrap is safe here as we are in the main thread
|
// this will return if it cannot publish the status preventing the last_tweet from
|
||||||
let published_status = mastodon.new_status(status).unwrap();
|
// being written into db
|
||||||
// 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 ttt_towrite = TweetToToot {
|
let ttt_towrite = TweetToToot {
|
||||||
twitter_screen_name: mastodon_config.twitter_screen_name.clone(),
|
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
|
// write the current state (tweet ID and toot ID) to avoid copying it another time
|
||||||
let lconn = task_conn.lock().await;
|
let lconn = task_conn.lock().await;
|
||||||
write_state(&lconn, ttt_towrite)
|
write_state(&lconn, ttt_towrite)?;
|
||||||
.unwrap_or_else(|e| panic!("Can’t write the last tweet retrieved: {}", e));
|
|
||||||
drop(lconn);
|
drop(lconn);
|
||||||
}
|
}
|
||||||
|
Ok::<(), ScootalooError>(())
|
||||||
});
|
});
|
||||||
|
|
||||||
// push each task into the vec task
|
// push each task into the vec task
|
||||||
@@ -156,6 +145,10 @@ pub async fn run(config: Config) {
|
|||||||
|
|
||||||
// launch and wait for every handle
|
// launch and wait for every handle
|
||||||
for handle in mtask {
|
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),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user