From 71d8f109069128afabde633b2eb64fb48f6d06f5 Mon Sep 17 00:00:00 2001 From: Guillermo Roche Date: Sun, 8 Mar 2026 19:22:11 +0000 Subject: [PATCH] fix regressions and add tests --- src/rewrite_links/check_contain_links.rs | 11 +------ src/rewrite_links/links_to_rewrite.rs | 42 ++++++++++++++++++++---- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/rewrite_links/check_contain_links.rs b/src/rewrite_links/check_contain_links.rs index 76a29de..e5edf2d 100644 --- a/src/rewrite_links/check_contain_links.rs +++ b/src/rewrite_links/check_contain_links.rs @@ -4,20 +4,11 @@ use crate::rewrite_links::links_to_rewrite; pub fn contain_links(msg: Message) -> bool { match msg.text() { - Some(text) => links_to_rewrite::check_domains(get_domain(text)), + Some(text) => links_to_rewrite::check_domains(String::from(text)), None => false, } } -pub fn get_domain(text: &str) -> String { - let s_text = String::from(text); - let split_array = s_text.split("/").collect::>(); - if split_array[0] == String::from("https:") && split_array[1].len() == 0 { - return split_array[2].to_string(); - } - return String::new(); -} - pub async fn fix_links(msg: Message, bot: Bot) -> anyhow::Result<()> { let text = msg.text().unwrap(); let url_and_domain = links_to_rewrite::get_domain_from_text(String::from(text)); diff --git a/src/rewrite_links/links_to_rewrite.rs b/src/rewrite_links/links_to_rewrite.rs index 3dafaf1..e40555e 100644 --- a/src/rewrite_links/links_to_rewrite.rs +++ b/src/rewrite_links/links_to_rewrite.rs @@ -8,8 +8,8 @@ static URLS: phf::Map<&'static str, (&'static str, bool)> = phf_map! { "vt.tiktok.com" => ("kktiktok.com", true), "lite.tiktok.com" => ("kktiktok.com", true), "www.instagram.com" => ("kkinstagram.com", false), - "https://x.com" => ("https://fxtwitter.com", false), - "https://twitter.com" => ("https://fxtwitter.com",false), + "https://x.com" => ("fxtwitter.com", false), + "https://twitter.com" => ("fxtwitter.com",false), }; pub fn filter_string(url: String, domain: String) -> Option { @@ -36,7 +36,7 @@ pub fn check_domains(text: String) -> bool { pub fn get_domain_from_text(text: String) -> (String, String) { for word in text.split(' ') { for domain in URLS.keys() { - if word.contains(domain) { + if get_domain(word.to_string()).contains(domain) { if URLS[domain].1 { let url = match get_tiktok_redirection(String::from(word)) { Ok(furl) => furl, @@ -51,6 +51,14 @@ pub fn get_domain_from_text(text: String) -> (String, String) { (String::from("Error"), String::from("Error")) } +pub fn get_domain(text: String) -> String { + let split_array = text.split("/").collect::>(); + if split_array[0] == String::from("https:") && split_array[1].len() == 0 { + return split_array[2].to_string(); + } + return String::new(); +} + fn get_tiktok_redirection(url: String) -> Result { let mut easy = Easy::new(); easy.url(url.as_str())?; @@ -91,17 +99,39 @@ fn test_rewrite_refered_tiktok() { let domain = filter_string(url_and_domain.0, url_and_domain.1); assert_eq!( domain, - Some("https://vxtiktok.com/@/video/7417808362957589778".to_string()) + Some("https://kktiktok.com/@/video/7417808362957589778".to_string()) ); } #[test] -fn test_rewrite_refered_instagram() { +fn test_rewrite_instagram() { let url_and_domain = get_domain_from_text("https://www.instagram.com/reel/DJAE4JXSvHn/".to_string()); let domain = filter_string(url_and_domain.0, url_and_domain.1); assert_eq!( domain, - Some("https://ddinstagram.com/reel/DJAE4JXSvHn/".to_string()) + Some("https://kkinstagram.com/reel/DJAE4JXSvHn/".to_string()) + ); +} + +#[test] +fn test_rewrite_instagram_message() { + let url_and_domain = + get_domain_from_text("enlace: https://www.instagram.com/reel/DJAE4JXSvHn/ espectacular".to_string()); + let domain = filter_string(url_and_domain.0, url_and_domain.1); + assert_eq!( + domain, + Some("https://kkinstagram.com/reel/DJAE4JXSvHn/".to_string()) + ); +} + +#[test] +fn test_bad_redirect_instagram() { + let url_and_domain = + get_domain_from_text("enlace: https://dominiofake.com/instagram.com espectacular".to_string()); + let domain = filter_string(url_and_domain.0, url_and_domain.1); + assert_ne!( + domain, + Some("https://dominiofake.com/kkinstagram.com".to_string()) ); }