diff --git a/src/lib.rs b/src/lib.rs index 2b2a18e..ba9d965 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -64,8 +64,8 @@ pub async fn run(config: Config) { .map(|r| r.tweet_id); drop(lconn); - // get user timeline feed (Vec) - let mut feed = get_user_timeline( + // get reversed, curated user timeline + let feed = get_user_timeline( &mastodon_config.twitter_screen_name, &token, last_tweet_id, @@ -73,41 +73,23 @@ pub async fn run(config: Config) { ) .await?; - // empty feed -> exiting - if feed.is_empty() { - info!("Nothing to retrieve since last time, exiting…"); - return Ok(()); - } - // get Mastodon instance let mastodon = get_mastodon_token(&mastodon_config); - // order needs to be chronological - feed.reverse(); - for tweet in &feed { info!("Treating Tweet {} inside feed", tweet.id); - // initiate the toot_reply_id var - let mut toot_reply_id: Option = None; - // determine if the tweet is part of a thread (response to self) or a standard response - if let Some(r) = &tweet.in_reply_to_screen_name { - if r.to_lowercase() != mastodon_config.twitter_screen_name.to_lowercase() { - // we are responding not threading - info!("Tweet is a direct response, skipping"); - continue; - } - info!("Tweet is a thread"); - // get the corresponding toot id - let lconn = task_conn.lock().await; - toot_reply_id = read_state( + let lconn = task_conn.lock().await; + // initiate the toot_reply_id var and retrieve the corresponding toot_id + let toot_reply_id: Option = tweet.in_reply_to_user_id.and_then(|_| { + read_state( &lconn, &mastodon_config.twitter_screen_name, tweet.in_reply_to_status_id, ) .unwrap_or(None) - .map(|s| s.toot_id); - drop(lconn); - }; + .map(|s| s.toot_id) + }); + drop(lconn); // build basic status by just yielding text and dereferencing contained urls let mut status_text = build_basic_status(tweet); diff --git a/src/twitter.rs b/src/twitter.rs index 01b2ad0..d14e01a 100644 --- a/src/twitter.rs +++ b/src/twitter.rs @@ -27,7 +27,7 @@ pub fn get_oauth2_token(config: &TwitterConfig) -> Token { } } -/// Gets Twitter user timeline +/// Gets Twitter user timeline, eliminate responses to others and reverse it pub async fn get_user_timeline( screen_name: &str, token: &Token, @@ -40,7 +40,18 @@ pub async fn get_user_timeline( .older(lid) .await?; - Ok(feed.to_vec()) + let mut feed: Vec = feed + .iter() + .cloned() + .filter(|t| match &t.in_reply_to_screen_name { + Some(r) => r.to_lowercase() == screen_name.to_lowercase(), + None => true, + }) + .collect(); + + feed.reverse(); + + Ok(feed) } /// Retrieves a single media from a tweet and store it in a temporary file