From 024679a0a9639b7666820a5225aafbba9fff9eeb Mon Sep 17 00:00:00 2001 From: alemi Date: Wed, 3 Jul 2024 05:08:41 +0200 Subject: [PATCH] feat: add dedicated image field to objects so we don't need anymore to convert lemmy post images to attachments --- upub/core/src/model/object.rs | 8 ++++- upub/core/src/traits/normalize.rs | 28 +-------------- upub/migrations/src/lib.rs | 2 ++ ...001_create_actor_activity_object_tables.rs | 1 + .../m20240703_000002_add_image_to_objects.rs | 35 +++++++++++++++++++ 5 files changed, 46 insertions(+), 28 deletions(-) create mode 100644 upub/migrations/src/m20240703_000002_add_image_to_objects.rs diff --git a/upub/core/src/model/object.rs b/upub/core/src/model/object.rs index f3348e8..6085c28 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 53fd1c2..37c1e44 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 2a36a72..57dffe4 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 87b0685..3ae3484 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 0000000..903c250 --- /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(()) + } +}