fix regressions and add tests
This commit is contained in:
@@ -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::<Vec<_>>();
|
||||
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));
|
||||
|
||||
@@ -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<String> {
|
||||
@@ -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::<Vec<_>>();
|
||||
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<String, curl::Error> {
|
||||
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())
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user