mirror of
https://framagit.org/veretcle/scootaloo.git
synced 2025-07-21 01:21:19 +02:00
refactor: build better decode functions
This commit is contained in:
@@ -20,27 +20,18 @@ use elefren::{
|
|||||||
/// Decodes the Twitter mention to something that will make sense once Twitter has joined the
|
/// Decodes the Twitter mention to something that will make sense once Twitter has joined the
|
||||||
/// Fediverse
|
/// Fediverse
|
||||||
fn twitter_mentions(ums: &Vec<MentionEntity>) -> HashMap<String, String> {
|
fn twitter_mentions(ums: &Vec<MentionEntity>) -> HashMap<String, String> {
|
||||||
let mut decoded_mentions = HashMap::new();
|
ums.iter().map(|s|
|
||||||
|
(format!("@{}", s.screen_name), format!("@{}@twitter.com", s.screen_name))
|
||||||
for um in ums {
|
).collect()
|
||||||
decoded_mentions.insert(format!("@{}", um.screen_name), format!("@{}@twitter.com", um.screen_name));
|
|
||||||
}
|
|
||||||
|
|
||||||
decoded_mentions
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decodes urls from UrlEntities
|
/// Decodes urls from UrlEntities
|
||||||
fn decode_urls(urls: &Vec<UrlEntity>) -> HashMap<String, String> {
|
fn decode_urls(urls: &Vec<UrlEntity>) -> HashMap<String, String> {
|
||||||
let mut decoded_urls = HashMap::new();
|
urls.iter()
|
||||||
|
.filter(|s| s.expanded_url.is_some())
|
||||||
for url in urls {
|
.map(|s|
|
||||||
if url.expanded_url.is_some() {
|
(String::from(&s.url), String::from(s.expanded_url.as_deref().unwrap()))
|
||||||
// unwrap is safe here as we just verified that there is something inside expanded_url
|
).collect()
|
||||||
decoded_urls.insert(String::from(&url.url), String::from(url.expanded_url.as_deref().unwrap()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
decoded_urls
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets Mastodon Data
|
/// Gets Mastodon Data
|
||||||
@@ -61,13 +52,11 @@ pub fn build_basic_status(tweet: &Tweet) -> Result<String, Box<dyn Error>> {
|
|||||||
let mut toot = String::from(&tweet.text);
|
let mut toot = String::from(&tweet.text);
|
||||||
|
|
||||||
let decoded_urls = decode_urls(&tweet.entities.urls);
|
let decoded_urls = decode_urls(&tweet.entities.urls);
|
||||||
|
|
||||||
for decoded_url in decoded_urls {
|
for decoded_url in decoded_urls {
|
||||||
toot = toot.replace(&decoded_url.0, &decoded_url.1);
|
toot = toot.replace(&decoded_url.0, &decoded_url.1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let decoded_mentions = twitter_mentions(&tweet.entities.user_mentions);
|
let decoded_mentions = twitter_mentions(&tweet.entities.user_mentions);
|
||||||
|
|
||||||
for decoded_mention in decoded_mentions {
|
for decoded_mention in decoded_mentions {
|
||||||
toot = toot.replace(&decoded_mention.0, &decoded_mention.1);
|
toot = toot.replace(&decoded_mention.0, &decoded_mention.1);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user