feat: split toot into 2 tweets when necessary

This commit is contained in:
VC
2023-11-20 12:03:54 +01:00
parent b6f87e829f
commit 87b0567b59
4 changed files with 111 additions and 14 deletions

View File

@@ -14,7 +14,7 @@ use mastodon::get_mastodon_timeline_since;
pub use mastodon::register;
mod utils;
use utils::strip_everything;
use utils::{generate_multi_tweets, strip_everything};
mod twitter;
#[allow(unused_imports)]
@@ -40,13 +40,29 @@ pub async fn run(config: &Config) {
.unwrap_or_else(|e| panic!("Cannot get instance: {}", e));
for toot in timeline {
let Ok(tweet_content) = strip_everything(&toot.content, &toot.tags) else {
let Ok(mut tweet_content) = strip_everything(&toot.content, &toot.tags) else {
continue; // skip in case we cant strip something
};
let mut medias: Vec<u64> = vec![];
// threads if necessary
let mut reply_to = toot.in_reply_to_id.and_then(|t| {
read_state(&conn, Some(t.parse::<u64>().unwrap()))
.ok()
.flatten()
.map(|s| s.tweet_id)
});
// if we wanted to cut toot in half, now would be the right time to do so
// if the toot is too long, we cut it in half here
if let Some((first_half, second_half)) = generate_multi_tweets(&tweet_content) {
tweet_content = second_half;
let reply_id = post_tweet(&config.twitter, &first_half, &[], &reply_to)
.await
.unwrap_or_else(|e| panic!("Cannot post the first half of {}: {}", &toot.id, e));
reply_to = Some(reply_id);
};
// treats medias
let mut medias: Vec<u64> = vec![];
let media_attachments = toot.media_attachments.clone();
let mut stream = stream::iter(media_attachments)
@@ -84,14 +100,6 @@ pub async fn run(config: &Config) {
}
}
// threads if necessary
let reply_to = toot.in_reply_to_id.and_then(|t| {
read_state(&conn, Some(t.parse::<u64>().unwrap()))
.ok()
.flatten()
.map(|s| s.tweet_id)
});
// posts corresponding tweet
let tweet_id = post_tweet(&config.twitter, &tweet_content, &medias, &reply_to)
.await