Compare commits

..

2 commits

Author SHA1 Message Date
aa19211b8e
fix: very ugly way to infer media-type
since lemmy sends us images but doesnt tell us what they are...
2024-05-12 01:21:12 +02:00
987c450312
fix(web): proper sizing of attachments 2024-05-12 01:20:55 +02:00
5 changed files with 31 additions and 7 deletions

View file

@ -22,13 +22,14 @@ pub struct Model {
impl ActiveModel { impl ActiveModel {
// TODO receive an impl, not a specific type! // TODO receive an impl, not a specific type!
// issue is that it's either an apb::Link or apb::Document, but Document doesnt inherit from link! // issue is that it's either an apb::Link or apb::Document, but Document doesnt inherit from link!
pub fn new(document: &serde_json::Value, object: String) -> Result<ActiveModel, super::FieldError> { pub fn new(document: &serde_json::Value, object: String, media_type: Option<String>) -> Result<ActiveModel, super::FieldError> {
let media_type = media_type.unwrap_or_else(|| document.media_type().unwrap_or("link").to_string());
Ok(ActiveModel { Ok(ActiveModel {
id: sea_orm::ActiveValue::NotSet, id: sea_orm::ActiveValue::NotSet,
object: Set(object), object: Set(object),
url: Set(document.url().id().unwrap_or_else(|| document.href().to_string())), url: Set(document.url().id().unwrap_or_else(|| document.href().to_string())),
document_type: Set(document.document_type().unwrap_or(apb::DocumentType::Page)), document_type: Set(document.document_type().unwrap_or(apb::DocumentType::Page)),
media_type: Set(document.media_type().unwrap_or("link").to_string()), media_type: Set(media_type),
name: Set(document.name().map(|x| x.to_string())), name: Set(document.name().map(|x| x.to_string())),
created: Set(document.published().unwrap_or(chrono::Utc::now())), created: Set(document.published().unwrap_or(chrono::Utc::now())),
}) })

View file

@ -292,14 +292,25 @@ async fn fetch_object_inner(ctx: &Context, id: &str, depth: usize) -> crate::Res
} }
for attachment in object.attachment() { for attachment in object.attachment() {
let attachment_model = model::attachment::ActiveModel::new(&attachment, object_model.id.clone())?; let attachment_model = model::attachment::ActiveModel::new(&attachment, object_model.id.clone(), None)?;
model::attachment::Entity::insert(attachment_model) model::attachment::Entity::insert(attachment_model)
.exec(ctx.db()) .exec(ctx.db())
.await?; .await?;
} }
// lemmy sends us an image field in posts, treat it like an attachment i'd say // lemmy sends us an image field in posts, treat it like an attachment i'd say
if let Some(img) = object.image().get() { if let Some(img) = object.image().get() {
let attachment_model = model::attachment::ActiveModel::new(img, object_model.id.clone())?; // TODO lemmy doesnt tell us the media type but we use it to display the thing...
let img_url = img.url().id().unwrap_or_default();
let media_type = if img_url.ends_with("png") {
Some("image/png".to_string())
} else if img_url.ends_with("webp") {
Some("image/webp".to_string())
} else if img_url.ends_with("jpeg") || img_url.ends_with("jpg") {
Some("image/jpeg".to_string())
} else {
None
};
let attachment_model = model::attachment::ActiveModel::new(img, object_model.id.clone(), media_type)?;
model::attachment::Entity::insert(attachment_model) model::attachment::Entity::insert(attachment_model)
.exec(ctx.db()) .exec(ctx.db())
.await?; .await?;

View file

@ -54,14 +54,25 @@ impl apb::server::Inbox for Context {
model::object::Entity::insert(object_model.into_active_model()).exec(self.db()).await?; model::object::Entity::insert(object_model.into_active_model()).exec(self.db()).await?;
model::activity::Entity::insert(activity_model.into_active_model()).exec(self.db()).await?; model::activity::Entity::insert(activity_model.into_active_model()).exec(self.db()).await?;
for attachment in object_node.attachment() { for attachment in object_node.attachment() {
let attachment_model = model::attachment::ActiveModel::new(&attachment, oid.clone())?; let attachment_model = model::attachment::ActiveModel::new(&attachment, oid.clone(), None)?;
model::attachment::Entity::insert(attachment_model) model::attachment::Entity::insert(attachment_model)
.exec(self.db()) .exec(self.db())
.await?; .await?;
} }
// lemmy sends us an image field in posts, treat it like an attachment i'd say // lemmy sends us an image field in posts, treat it like an attachment i'd say
if let Some(img) = object_node.image().get() { if let Some(img) = object_node.image().get() {
let attachment_model = model::attachment::ActiveModel::new(img, oid.clone())?; // TODO lemmy doesnt tell us the media type but we use it to display the thing...
let img_url = img.url().id().unwrap_or_default();
let media_type = if img_url.ends_with("png") {
Some("image/png".to_string())
} else if img_url.ends_with("webp") {
Some("image/webp".to_string())
} else if img_url.ends_with("jpeg") || img_url.ends_with("jpg") {
Some("image/jpeg".to_string())
} else {
None
};
let attachment_model = model::attachment::ActiveModel::new(img, oid.clone(), media_type)?;
model::attachment::Entity::insert(attachment_model) model::attachment::Entity::insert(attachment_model)
.exec(self.db()) .exec(self.db())
.await?; .await?;

View file

@ -186,6 +186,7 @@
border: 3px solid #bf616a; border: 3px solid #bf616a;
padding: 5px; padding: 5px;
object-fit: cover; object-fit: cover;
box-sizing: border-box;
} }
img.expand, img.expand,
video.expand { video.expand {

View file

@ -28,7 +28,7 @@ pub fn Attachment(
view! { view! {
<p class="center"> <p class="center">
<img <img
class="attachment ml-1" class="attachment"
class:expand=expand class:expand=expand
src={move || if sensitive && !expand.get() { src={move || if sensitive && !expand.get() {
URL_SENSITIVE.to_string() URL_SENSITIVE.to_string()