From 9227850c9906fadd3148cf8447e3f94d263e61d8 Mon Sep 17 00:00:00 2001 From: VC Date: Tue, 22 Nov 2022 23:54:26 +0100 Subject: [PATCH] feat: add necessary changes to decode_urls fn --- src/mastodon.rs | 92 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 77 insertions(+), 15 deletions(-) diff --git a/src/mastodon.rs b/src/mastodon.rs index 84d4dea..c7086ec 100644 --- a/src/mastodon.rs +++ b/src/mastodon.rs @@ -6,6 +6,7 @@ use egg_mode::{ }; use elefren::{apps::App, prelude::*, scopes::Read, scopes::Scopes, scopes::Write}; use html_escape::decode_html_entities; +use regex::Regex; use std::{borrow::Cow, collections::HashMap, io::stdin}; /// Decodes the Twitter mention to something that will make sense once Twitter has joined the @@ -22,14 +23,34 @@ fn twitter_mentions(ums: &[MentionEntity]) -> HashMap { } /// Decodes urls from UrlEntities -fn decode_urls(urls: &[UrlEntity]) -> HashMap { +fn decode_urls( + urls: &[UrlEntity], + re: &Option, + alt_urls: &Option>, +) -> HashMap { urls.iter() .filter(|s| s.expanded_url.is_some()) .map(|s| { - ( - s.url.to_owned(), - s.expanded_url.as_deref().unwrap().to_owned(), - ) + (s.url.to_owned(), { + let mut def = s.expanded_url.as_deref().unwrap().to_owned(); + + if let Some(a) = &alt_urls { + for (url_source, url_destination) in a { + def = def.replace( + &format!("https://{}", url_source), + &format!("https://{}", url_destination), + ); + } + } + + if let Some(r) = &re { + if r.is_match(s.expanded_url.as_deref().unwrap()) { + def = s.display_url.clone(); + } + } + + def + }) }) .collect() } @@ -48,10 +69,15 @@ pub fn get_mastodon_token(masto: &MastodonConfig) -> Mastodon { } /// Builds toot text from tweet -pub fn build_basic_status(tweet: &Tweet, mentions: &HashMap) -> String { +pub fn build_basic_status( + tweet: &Tweet, + mentions: &HashMap, + url_regex_filter: &Option, + url_alt_services: &Option>, +) -> String { let mut toot = tweet.text.to_owned(); - for decoded_url in decode_urls(&tweet.entities.urls) { + for decoded_url in decode_urls(&tweet.entities.urls, url_regex_filter, url_alt_services) { toot = toot.replace(&decoded_url.0, &decoded_url.1); } @@ -170,15 +196,51 @@ mod tests { url: "https://t.me/tamerelol".to_string(), }; - let twitter_urls = vec![url_entity1, url_entity2]; + let wrong_url_entity = UrlEntity { + display_url: "invité.es".to_string(), + expanded_url: Some("http://xn--invit-fsa.es".to_string()), + range: (85, 108), + url: "https://t.co/WAUgnpHLmo".to_string(), + }; - let mut expected_urls = HashMap::new(); - expected_urls.insert( - "https://t.me/tamerelol".to_string(), - "https://www.nintendojo.fr/dojobar".to_string(), - ); + let rewritten_url_entity = UrlEntity { + display_url: "youtu.be/w5TrSaoYmZ8".to_string(), + expanded_url: Some("https://youtu.be/w5TrSaoYmZ8".to_string()), + range: (0, 23), + url: "https://t.co/fUVYXuF7tg".to_string(), + }; - let decoded_urls = decode_urls(&twitter_urls); + let re = Regex::new("(.+)\\.es$").ok(); + + let alt: HashMap = HashMap::from([ + ("youtube.com".to_string(), "invidio.us".to_string()), + ("youtu.be".to_string(), "invidio.us".to_string()), + ("www.youtube.com".to_string(), "invidio.us".to_string()), + ]); + + let twitter_urls = vec![ + url_entity1, + url_entity2, + wrong_url_entity, + rewritten_url_entity, + ]; + + let expected_urls = HashMap::from([ + ( + "https://t.me/tamerelol".to_string(), + "https://www.nintendojo.fr/dojobar".to_string(), + ), + ( + "https://t.co/WAUgnpHLmo".to_string(), + "invité.es".to_string(), + ), + ( + "https://t.co/fUVYXuF7tg".to_string(), + "https://invidio.us/w5TrSaoYmZ8".to_string(), + ), + ]); + + let decoded_urls = decode_urls(&twitter_urls, &re, &Some(alt)); assert_eq!(expected_urls, decoded_urls); } @@ -247,7 +309,7 @@ mod tests { "@WillyWonka@chocolatefactory.org".to_string(), )]); - let t_out = build_basic_status(&t, &s); + let t_out = build_basic_status(&t, &s, &None, &None); assert_eq!(&t_out, "Mother 1 & 2 sur le NES/SNES online !\nDispo maintenant. cc @NintendoFrance@twitter.com @WillyWonka@chocolatefactory.org https://www.youtube.com/watch?v=w5TrSaoYmZ8"); }