Remove blacklisting to make SiteIcons Send

This commit is contained in:
CenTdemeern1 2025-01-29 22:58:57 +01:00
parent 9e41e65c45
commit 1925d4748e
2 changed files with 15 additions and 45 deletions

View file

@ -13,7 +13,6 @@ use url::Url;
pub async fn parse_site_logo(
url: &Url,
mut body: impl Stream<Item = Result<Vec<u8>, String>> + Unpin,
is_blacklisted: impl Fn(&Url) -> bool,
) -> Result<Icon, Box<dyn Error>> {
let mut parser = driver::parse_document(Html::new_document(), Default::default());
while let Some(data) = body.next().await {
@ -123,12 +122,6 @@ pub async fn parse_site_logo(
elem.attr("src").and_then(|href| url.join(&href).ok())
};
if let Some(href) = &href {
if is_blacklisted(href) {
return None;
}
}
href.map(|href| (href, elem_ref, weight))
})
.collect();

View file

@ -10,9 +10,7 @@ use std::error::Error;
use url::Url;
use vec1::Vec1;
pub struct SiteIcons {
blacklist: Option<Box<dyn Fn(&Url) -> bool>>,
}
pub struct SiteIcons;
#[derive(Debug, Clone)]
enum LoadedKind {
@ -24,21 +22,7 @@ enum LoadedKind {
impl SiteIcons {
pub fn new() -> Self {
SiteIcons { blacklist: None }
}
pub fn new_with_blacklist(blacklist: impl Fn(&Url) -> bool + 'static) -> Self {
SiteIcons {
blacklist: Some(Box::new(blacklist)),
}
}
pub fn is_blacklisted(&self, url: &Url) -> bool {
if let Some(is_blacklisted) = &self.blacklist {
is_blacklisted(url)
} else {
false
}
SiteIcons
}
pub async fn load_website<U: IntoUrl>(
@ -78,24 +62,20 @@ impl SiteIcons {
let url = res.url().clone();
if self.is_blacklisted(&url) {
None
} else {
let body = res.bytes_stream().map(|res| {
res
.map(|bytes| bytes.to_vec())
.map_err(|err| err.to_string())
});
let body = res.bytes_stream().map(|res| {
res
.map(|bytes| bytes.to_vec())
.map_err(|err| err.to_string())
});
let mut publisher = Publisher::new(128);
let subscriber = publisher.subscribe();
let mut publisher = Publisher::new(128);
let subscriber = publisher.subscribe();
Some((
url,
async move { StreamPublisher::new(&mut publisher, body).await }.shared(),
subscriber,
))
}
Some((
url,
async move { StreamPublisher::new(&mut publisher, body).await }.shared(),
subscriber,
))
}
.shared();
@ -117,10 +97,7 @@ impl SiteIcons {
LoadedKind::SiteLogo(match html_response {
Some((url, complete, body)) => {
let (icons, _) = join!(
html_parser::parse_site_logo(&url, body, |url| self.is_blacklisted(url)),
complete
);
let (icons, _) = join!(html_parser::parse_site_logo(&url, body), complete);
icons.ok()
}