diff --git a/upub/core/src/model/object.rs b/upub/core/src/model/object.rs index f3348e8a..6085c28b 100644 --- a/upub/core/src/model/object.rs +++ b/upub/core/src/model/object.rs @@ -1,4 +1,4 @@ -use apb::{BaseMut, CollectionMut, ObjectMut, ObjectType}; +use apb::{BaseMut, CollectionMut, DocumentMut, ObjectMut, ObjectType}; use sea_orm::{entity::prelude::*, QuerySelect, SelectColumns}; use super::Audience; @@ -15,6 +15,7 @@ pub struct Model { pub name: Option, pub summary: Option, pub content: Option, + pub image: Option, pub sensitive: bool, pub in_reply_to: Option, pub url: Option, @@ -159,6 +160,11 @@ impl Model { .set_name(self.name.as_deref()) .set_summary(self.summary.as_deref()) .set_content(self.content.as_deref()) + .set_image(apb::Node::maybe_object(self.image.map(|x| + apb::new() + .set_document_type(Some(apb::DocumentType::Image)) + .set_url(apb::Node::link(x)) + ))) .set_context(apb::Node::maybe_link(self.context.clone())) .set_conversation(apb::Node::maybe_link(self.context.clone())) // duplicate context for mastodon .set_in_reply_to(apb::Node::maybe_link(self.in_reply_to.clone())) diff --git a/upub/core/src/traits/normalize.rs b/upub/core/src/traits/normalize.rs index 53fd1c2b..37c1e440 100644 --- a/upub/core/src/traits/normalize.rs +++ b/upub/core/src/traits/normalize.rs @@ -90,33 +90,6 @@ impl Normalizer for crate::Context { .exec(tx) .await?; } - // lemmy sends us an image field in posts, treat it like an attachment i'd say - if let Some(img) = object.image().get() { - // TODO lemmy doesnt tell us the media type but we use it to display the thing... - let img_url = img.url().id().str().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 mut attachment_model = AP::attachment_q(img, object_model.internal, None)?; - - // ugly fix for lemmy - if let Some(m) = media_type { - if img.media_type().ok().is_none() { - attachment_model.media_type = Set(m); - } - } - - crate::model::attachment::Entity::insert(attachment_model) - .exec(tx) - .await?; - } for tag in object.tag().flat() { match tag { @@ -279,6 +252,7 @@ impl AP { name: object.name().str(), summary: object.summary().str(), content: object.content().str(), + image: object.image().get().and_then(|x| x.url().id().str()), context: object.context().id().str(), in_reply_to: object.in_reply_to().id().str(), published: object.published().unwrap_or_else(|_| chrono::Utc::now()), diff --git a/upub/migrations/src/lib.rs b/upub/migrations/src/lib.rs index 2a36a724..57dffe40 100644 --- a/upub/migrations/src/lib.rs +++ b/upub/migrations/src/lib.rs @@ -15,6 +15,7 @@ mod m20240626_000001_add_notifications_table; mod m20240628_000001_add_followers_following_indexes; mod m20240628_000002_add_credentials_activated; mod m20240703_000001_add_audience_index; +mod m20240703_000002_add_image_to_objects; pub struct Migrator; @@ -37,6 +38,7 @@ impl MigratorTrait for Migrator { Box::new(m20240628_000001_add_followers_following_indexes::Migration), Box::new(m20240628_000002_add_credentials_activated::Migration), Box::new(m20240703_000001_add_audience_index::Migration), + Box::new(m20240703_000002_add_image_to_objects::Migration), ] } } diff --git a/upub/migrations/src/m20240524_000001_create_actor_activity_object_tables.rs b/upub/migrations/src/m20240524_000001_create_actor_activity_object_tables.rs index 87b06850..3ae34845 100644 --- a/upub/migrations/src/m20240524_000001_create_actor_activity_object_tables.rs +++ b/upub/migrations/src/m20240524_000001_create_actor_activity_object_tables.rs @@ -52,6 +52,7 @@ pub enum Objects { Name, Summary, Content, + Image, // added with migration m20240703_000002 Sensitive, Url, Likes, diff --git a/upub/migrations/src/m20240703_000002_add_image_to_objects.rs b/upub/migrations/src/m20240703_000002_add_image_to_objects.rs new file mode 100644 index 00000000..903c2508 --- /dev/null +++ b/upub/migrations/src/m20240703_000002_add_image_to_objects.rs @@ -0,0 +1,35 @@ +use sea_orm_migration::prelude::*; + +use crate::m20240524_000001_create_actor_activity_object_tables::Objects; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(Objects::Table) + .add_column(ColumnDef::new(Objects::Image).string().null()) + .to_owned() + ) + .await?; + + Ok(()) + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .alter_table( + Table::alter() + .table(Objects::Table) + .drop_column(Objects::Image) + .to_owned() + ) + .await?; + + Ok(()) + } +}