From aa19211b8e9decc5d4c9dfec9ae1549aa34074a0 Mon Sep 17 00:00:00 2001 From: alemi Date: Sun, 12 May 2024 01:21:12 +0200 Subject: [PATCH] fix: very ugly way to infer media-type since lemmy sends us images but doesnt tell us what they are... --- src/model/attachment.rs | 5 +++-- src/server/fetcher.rs | 15 +++++++++++++-- src/server/inbox.rs | 15 +++++++++++++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/model/attachment.rs b/src/model/attachment.rs index e508d1b..833a76a 100644 --- a/src/model/attachment.rs +++ b/src/model/attachment.rs @@ -22,13 +22,14 @@ pub struct Model { impl ActiveModel { // 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! - pub fn new(document: &serde_json::Value, object: String) -> Result { + pub fn new(document: &serde_json::Value, object: String, media_type: Option) -> Result { + let media_type = media_type.unwrap_or_else(|| document.media_type().unwrap_or("link").to_string()); Ok(ActiveModel { id: sea_orm::ActiveValue::NotSet, object: Set(object), url: Set(document.url().id().unwrap_or_else(|| document.href().to_string())), 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())), created: Set(document.published().unwrap_or(chrono::Utc::now())), }) diff --git a/src/server/fetcher.rs b/src/server/fetcher.rs index fe4602e..271be3b 100644 --- a/src/server/fetcher.rs +++ b/src/server/fetcher.rs @@ -292,14 +292,25 @@ async fn fetch_object_inner(ctx: &Context, id: &str, depth: usize) -> crate::Res } 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) .exec(ctx.db()) .await?; } // lemmy sends us an image field in posts, treat it like an attachment i'd say 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) .exec(ctx.db()) .await?; diff --git a/src/server/inbox.rs b/src/server/inbox.rs index 7db572a..2f6b9b3 100644 --- a/src/server/inbox.rs +++ b/src/server/inbox.rs @@ -54,14 +54,25 @@ impl apb::server::Inbox for Context { 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?; 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) .exec(self.db()) .await?; } // lemmy sends us an image field in posts, treat it like an attachment i'd say 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) .exec(self.db()) .await?;