feat: add dedicated image field to objects

so we don't need anymore to convert lemmy post images to attachments
This commit is contained in:
əlemi 2024-07-03 05:08:41 +02:00
parent 6907560aaa
commit 024679a0a9
Signed by: alemi
GPG key ID: A4895B84D311642C
5 changed files with 46 additions and 28 deletions

View file

@ -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<String>,
pub summary: Option<String>,
pub content: Option<String>,
pub image: Option<String>,
pub sensitive: bool,
pub in_reply_to: Option<String>,
pub url: Option<String>,
@ -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()))

View file

@ -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()),

View file

@ -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),
]
}
}

View file

@ -52,6 +52,7 @@ pub enum Objects {
Name,
Summary,
Content,
Image, // added with migration m20240703_000002
Sensitive,
Url,
Likes,

View file

@ -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(())
}
}