1
0
Fork 0
forked from alemi/upub

fix: very ugly way to infer media-type

since lemmy sends us images but doesnt tell us what they are...
This commit is contained in:
əlemi 2024-05-12 01:21:12 +02:00
parent 987c450312
commit aa19211b8e
Signed by: alemi
GPG key ID: A4895B84D311642C
3 changed files with 29 additions and 6 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?;