diff --git a/src/api/client/media.rs b/src/api/client/media.rs index e9723b3b1..72f39c8fd 100644 --- a/src/api/client/media.rs +++ b/src/api/client/media.rs @@ -224,8 +224,10 @@ pub(crate) async fn get_media_preview_route( .media .get_url_preview(&url) .await - .inspect(|_| info!(user=%sender_user, "Finished fetching a URL preview for {url}")) - .inspect_err(|e| info!(user=%sender_user, "Failed to fetch a URL preview for {url}: {e}")) + .inspect(|_| info!(user=%sender_user, %url, "Finished fetching a URL preview")) + .inspect_err( + |e| info!(user=%sender_user, %url, error=?e, "Failed to fetch a URL preview"), + ) .map_err(|error| { err!(Request(Unknown( debug_error!(%sender_user, %url, "Failed to fetch URL preview: {error}") diff --git a/src/service/media/preview.rs b/src/service/media/preview.rs index 16cce3240..a90b65a33 100644 --- a/src/service/media/preview.rs +++ b/src/service/media/preview.rs @@ -26,8 +26,8 @@ pub struct UrlPreviewData { pub title: Option, /// The type of your object, e.g., "video.movie". - #[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:type"))] - pub site_type: Option, + #[serde(skip_serializing_if = "String::is_empty", rename(serialize = "og:type"))] + pub site_type: String, /// The canonical URL of your object that will be used as its permanent ID /// in the graph. #[serde(skip_serializing_if = "Option::is_none", rename(serialize = "og:url"))] @@ -214,6 +214,7 @@ impl Service { } #[cfg(feature = "url_preview")] + #[tracing::instrument(skip(self))] pub async fn download_video(&self, url: &str) -> Result { let mut preview_data = UrlPreviewData::default(); @@ -227,6 +228,7 @@ impl Service { } #[cfg(feature = "url_preview")] + #[tracing::instrument(skip(self))] pub async fn download_audio(&self, url: &str) -> Result { let mut preview_data = UrlPreviewData::default(); @@ -239,6 +241,7 @@ impl Service { Ok(preview_data) } + #[tracing::instrument(skip(self))] pub async fn download_media(&self, url: &str) -> Result<(OwnedMxcUri, usize)> { use conduwuit::utils::random_string; use http::header::CONTENT_TYPE; @@ -296,6 +299,7 @@ impl Service { } #[cfg(feature = "url_preview")] + #[tracing::instrument(skip(self))] async fn download_html(&self, url: &str) -> Result { use webpage::HTML; @@ -358,9 +362,6 @@ impl Service { preview_data.title = preview_data.title.or(html.title); preview_data.description = preview_data.description.or(html.description); preview_data.url = preview_data.url.or_else(|| Some(url.to_owned())); - preview_data.site_type = preview_data - .site_type - .or_else(|| Some("website".to_owned())); Ok(preview_data) } @@ -498,12 +499,11 @@ impl From<&webpage::Opengraph> for UrlPreviewData { Self { title: og.properties.get("title").cloned(), description: og.properties.get("description").cloned(), - site_type: og.properties.get("type").cloned(), + site_type: og.og_type.clone(), url: og.properties.get("url").cloned(), determiner: og.properties.get("determiner").cloned(), locale: og.properties.get("locale").cloned(), site_name: og.properties.get("site_name").cloned(), - image: og.images.first().map(|img| img.url.clone()), image_type: og .images .first() @@ -520,7 +520,6 @@ impl From<&webpage::Opengraph> for UrlPreviewData { .images .first() .and_then(|img| img.properties.get("alt").cloned()), - video: og.videos.first().map(|vid| vid.url.clone()), video_type: og .videos .first() @@ -561,11 +560,13 @@ mod tests { - + + + @@ -583,8 +584,16 @@ mod tests { data.image, None, "UrlPreviewData should not inherit HTTP/S URL from URL preview" ); + assert_eq!( + data.video, None, + "UrlPreviewData should not inherit HTTP/S URL from URL preview" + ); + assert_eq!( + data.audio, None, + "UrlPreviewData should not inherit HTTP/S URL from URL preview" + ); assert_eq!(data.url, Some("https://example.com/index.html".to_owned())); - assert_eq!(data.site_type, Some("org.continuwuity.other".to_owned())); + assert_eq!(data.site_type, "org.continuwuity.other".to_owned()); assert_eq!(data.site_name, Some("Example Site".to_owned())); assert_eq!(data.image_alt, Some("An example image".to_owned())); assert_eq!(data.image_type, Some("image/png".parse().unwrap()));