mirror of
https://gitlab.com/famedly/conduit.git
synced 2025-06-27 16:35:59 +00:00
Preview titles/images required
This commit is contained in:
parent
839498ada7
commit
c8d5b05855
3 changed files with 11 additions and 25 deletions
|
@ -77,7 +77,7 @@ async fn download_image(
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(UrlPreviewData {
|
Ok(UrlPreviewData {
|
||||||
image: Some(mxc),
|
image: mxc,
|
||||||
image_size: Some(image.len()),
|
image_size: Some(image.len()),
|
||||||
image_width: width,
|
image_width: width,
|
||||||
image_height: height,
|
image_height: height,
|
||||||
|
@ -118,7 +118,7 @@ async fn download_html(
|
||||||
|
|
||||||
let props = html.opengraph.properties;
|
let props = html.opengraph.properties;
|
||||||
/* use OpenGraph title/description, but fall back to HTML if not available */
|
/* 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);
|
data.description = props.get("description").cloned().or(html.description);
|
||||||
Ok(data)
|
Ok(data)
|
||||||
}
|
}
|
||||||
|
@ -192,7 +192,7 @@ async fn request_url_preview(url: &Url) -> Result<UrlPreviewData> {
|
||||||
|
|
||||||
let content_type = match response
|
let content_type = match response
|
||||||
.headers()
|
.headers()
|
||||||
.get(reqwest::header::CONTENT_TYPE)
|
.get(CONTENT_TYPE)
|
||||||
.and_then(|x| x.to_str().ok())
|
.and_then(|x| x.to_str().ok())
|
||||||
{
|
{
|
||||||
Some(ct) => ct,
|
Some(ct) => ct,
|
||||||
|
|
|
@ -78,10 +78,7 @@ impl service::media::Data for KeyValueDatabase {
|
||||||
value.extend_from_slice(×tamp.as_secs().to_be_bytes());
|
value.extend_from_slice(×tamp.as_secs().to_be_bytes());
|
||||||
value.push(0xff);
|
value.push(0xff);
|
||||||
value.extend_from_slice(
|
value.extend_from_slice(
|
||||||
data.title
|
data.title.as_bytes(),
|
||||||
.as_ref()
|
|
||||||
.map(|t| t.as_bytes())
|
|
||||||
.unwrap_or_default(),
|
|
||||||
);
|
);
|
||||||
value.push(0xff);
|
value.push(0xff);
|
||||||
value.extend_from_slice(
|
value.extend_from_slice(
|
||||||
|
@ -92,10 +89,7 @@ impl service::media::Data for KeyValueDatabase {
|
||||||
);
|
);
|
||||||
value.push(0xff);
|
value.push(0xff);
|
||||||
value.extend_from_slice(
|
value.extend_from_slice(
|
||||||
data.image
|
data.image.as_bytes(),
|
||||||
.as_ref()
|
|
||||||
.map(|i| i.as_bytes())
|
|
||||||
.unwrap_or_default(),
|
|
||||||
);
|
);
|
||||||
value.push(0xff);
|
value.push(0xff);
|
||||||
value.extend_from_slice(&data.image_size.unwrap_or(0).to_be_bytes());
|
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,
|
Some(0) => None,
|
||||||
x => x,
|
x => x,
|
||||||
};
|
};
|
||||||
let title = match values
|
let title = values
|
||||||
.next()
|
.next()
|
||||||
.and_then(|b| String::from_utf8(b.to_vec()).ok())
|
.and_then(|b| String::from_utf8(b.to_vec()).ok())
|
||||||
{
|
.unwrap_or_default();
|
||||||
Some(s) if s.is_empty() => None,
|
|
||||||
x => x,
|
|
||||||
};
|
|
||||||
let description = match values
|
let description = match values
|
||||||
.next()
|
.next()
|
||||||
.and_then(|b| String::from_utf8(b.to_vec()).ok())
|
.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,
|
Some(s) if s.is_empty() => None,
|
||||||
x => x,
|
x => x,
|
||||||
};
|
};
|
||||||
let image = match values
|
let image = values
|
||||||
.next()
|
.next()
|
||||||
.and_then(|b| String::from_utf8(b.to_vec()).ok())
|
.and_then(|b| String::from_utf8(b.to_vec()).ok())
|
||||||
{
|
.unwrap_or_default();
|
||||||
Some(s) if s.is_empty() => None,
|
|
||||||
x => x,
|
|
||||||
};
|
|
||||||
let image_size = match values
|
let image_size = match values
|
||||||
.next()
|
.next()
|
||||||
.map(|b| usize::from_be_bytes(b.try_into().expect("valid BE array")))
|
.map(|b| usize::from_be_bytes(b.try_into().expect("valid BE array")))
|
||||||
|
|
|
@ -28,20 +28,18 @@ pub struct FileMeta {
|
||||||
#[derive(Serialize, Default)]
|
#[derive(Serialize, Default)]
|
||||||
pub struct UrlPreviewData {
|
pub struct UrlPreviewData {
|
||||||
#[serde(
|
#[serde(
|
||||||
skip_serializing_if = "Option::is_none",
|
|
||||||
rename(serialize = "og:title")
|
rename(serialize = "og:title")
|
||||||
)]
|
)]
|
||||||
pub title: Option<String>,
|
pub title: String,
|
||||||
#[serde(
|
#[serde(
|
||||||
skip_serializing_if = "Option::is_none",
|
skip_serializing_if = "Option::is_none",
|
||||||
rename(serialize = "og:description")
|
rename(serialize = "og:description")
|
||||||
)]
|
)]
|
||||||
pub description: Option<String>,
|
pub description: Option<String>,
|
||||||
#[serde(
|
#[serde(
|
||||||
skip_serializing_if = "Option::is_none",
|
|
||||||
rename(serialize = "og:image")
|
rename(serialize = "og:image")
|
||||||
)]
|
)]
|
||||||
pub image: Option<String>,
|
pub image: String,
|
||||||
#[serde(
|
#[serde(
|
||||||
skip_serializing_if = "Option::is_none",
|
skip_serializing_if = "Option::is_none",
|
||||||
rename(serialize = "matrix:image:size")
|
rename(serialize = "matrix:image:size")
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue