feat: truncate poll when too long

This commit is contained in:
VC
2024-01-17 15:13:17 +01:00
parent eeaea52e80
commit 567dfae7ab
3 changed files with 89 additions and 47 deletions

View File

@@ -464,7 +464,11 @@ pub fn transform_poll(p: &Poll) -> TweetPoll {
let diff = poll_end_datetime.signed_duration_since(now);
TweetPoll {
options: p.options.iter().map(|i| i.title.clone()).collect(),
options: p
.options
.iter()
.map(|i| i.title.chars().take(25).collect::<String>())
.collect(),
duration_minutes: diff.num_minutes().try_into().unwrap(), // safe here, number is positive
// and cant be over 21600
}
@@ -512,3 +516,41 @@ pub async fn post_tweet(
Ok(res.data.id.parse::<u64>().unwrap())
}
#[cfg(test)]
mod tests {
use super::*;
use megalodon::entities::PollOption;
#[test]
fn test_transform_poll() {
let poll = Poll {
id: "youpi".to_string(),
expires_at: Some(Utc::now()),
expired: false,
multiple: false,
votes_count: 0,
voters_count: None,
options: vec![
PollOption {
title: "Je suis beaucoup trop long comme option, tronque-moi !".to_string(),
votes_count: None,
},
PollOption {
title: "nope".to_string(),
votes_count: None,
},
],
voted: None,
emojis: vec![],
};
let tweet_poll_res = transform_poll(&poll);
let tweet_pool_expected = TweetPoll {
duration_minutes: 0,
options: vec!["Je suis beaucoup trop lon".to_string(), "nope".to_string()],
};
assert_eq!(tweet_poll_res.options, tweet_pool_expected.options);
}
}