1
0
Fork 0
mirror of https://gitlab.com/famedly/conduit.git synced 2025-04-22 14:10:16 +03:00

Preview titles/images required

This commit is contained in:
Steven Vergenz 2024-10-30 21:16:24 -07:00
parent 839498ada7
commit c8d5b05855
3 changed files with 11 additions and 25 deletions
src
api/client_server
database/key_value
service/media

View file

@ -77,7 +77,7 @@ async fn download_image(
};
Ok(UrlPreviewData {
image: Some(mxc),
image: mxc,
image_size: Some(image.len()),
image_width: width,
image_height: height,
@ -118,7 +118,7 @@ async fn download_html(
let props = html.opengraph.properties;
/* use OpenGraph title/description, but fall back to HTML if not available */
data.title = props.get("title").cloned().or(html.title);
data.title = props.get("title").cloned().or(html.title).unwrap_or(String::from(url));
data.description = props.get("description").cloned().or(html.description);
Ok(data)
}
@ -192,7 +192,7 @@ async fn request_url_preview(url: &Url) -> Result<UrlPreviewData> {
let content_type = match response
.headers()
.get(reqwest::header::CONTENT_TYPE)
.get(CONTENT_TYPE)
.and_then(|x| x.to_str().ok())
{
Some(ct) => ct,

View file

@ -78,10 +78,7 @@ impl service::media::Data for KeyValueDatabase {
value.extend_from_slice(&timestamp.as_secs().to_be_bytes());
value.push(0xff);
value.extend_from_slice(
data.title
.as_ref()
.map(|t| t.as_bytes())
.unwrap_or_default(),
data.title.as_bytes(),
);
value.push(0xff);
value.extend_from_slice(
@ -92,10 +89,7 @@ impl service::media::Data for KeyValueDatabase {
);
value.push(0xff);
value.extend_from_slice(
data.image
.as_ref()
.map(|i| i.as_bytes())
.unwrap_or_default(),
data.image.as_bytes(),
);
value.push(0xff);
value.extend_from_slice(&data.image_size.unwrap_or(0).to_be_bytes());
@ -119,13 +113,10 @@ impl service::media::Data for KeyValueDatabase {
Some(0) => None,
x => x,
};
let title = match values
let title = values
.next()
.and_then(|b| String::from_utf8(b.to_vec()).ok())
{
Some(s) if s.is_empty() => None,
x => x,
};
.unwrap_or_default();
let description = match values
.next()
.and_then(|b| String::from_utf8(b.to_vec()).ok())
@ -133,13 +124,10 @@ impl service::media::Data for KeyValueDatabase {
Some(s) if s.is_empty() => None,
x => x,
};
let image = match values
let image = values
.next()
.and_then(|b| String::from_utf8(b.to_vec()).ok())
{
Some(s) if s.is_empty() => None,
x => x,
};
.unwrap_or_default();
let image_size = match values
.next()
.map(|b| usize::from_be_bytes(b.try_into().expect("valid BE array")))

View file

@ -28,20 +28,18 @@ pub struct FileMeta {
#[derive(Serialize, Default)]
pub struct UrlPreviewData {
#[serde(
skip_serializing_if = "Option::is_none",
rename(serialize = "og:title")
)]
pub title: Option<String>,
pub title: String,
#[serde(
skip_serializing_if = "Option::is_none",
rename(serialize = "og:description")
)]
pub description: Option<String>,
#[serde(
skip_serializing_if = "Option::is_none",
rename(serialize = "og:image")
)]
pub image: Option<String>,
pub image: String,
#[serde(
skip_serializing_if = "Option::is_none",
rename(serialize = "matrix:image:size")