fix: add compat option to fix lemmy images

no clue why sometimes they come as bare links?? but it's images, they
show as images on lemmy's frontend... this is not really a good check as
we overrule remote decisions, but it's togglable so deployers can decide
if they care more about UX or spec consistency
This commit is contained in:
əlemi 2024-08-11 12:48:46 +02:00
parent 93f61ea0de
commit 77bf9d17a1
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 32 additions and 8 deletions

View file

@ -12,6 +12,9 @@ pub struct Config {
#[serde(default)] #[serde(default)]
pub security: SecurityConfig, pub security: SecurityConfig,
#[serde(default)]
pub compat: CompatibilityConfig,
// TODO should i move app keys here? // TODO should i move app keys here?
} }
@ -96,6 +99,13 @@ pub struct SecurityConfig {
pub reinsertion_attempt_limit: u32, pub reinsertion_attempt_limit: u32,
} }
#[serde_inline_default::serde_inline_default]
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize, serde_default::DefaultFromSerde)]
pub struct CompatibilityConfig {
#[serde(default)]
pub fix_attachment_images_media_type: bool,
}
impl Config { impl Config {
pub fn load(path: Option<&std::path::PathBuf>) -> Self { pub fn load(path: Option<&std::path::PathBuf>) -> Self {

View file

@ -80,14 +80,6 @@ impl Normalizer for crate::Context {
tracing::warn!("ignoring array-in-array while processing attachments"); tracing::warn!("ignoring array-in-array while processing attachments");
continue continue
}, },
Node::Link(l) => crate::model::attachment::ActiveModel {
internal: sea_orm::ActiveValue::NotSet,
url: Set(self.cloaked(l.href().unwrap_or_default())),
object: Set(object_model.internal),
document_type: Set(apb::DocumentType::Page),
name: Set(l.name().str()),
media_type: Set(l.media_type().unwrap_or("link").to_string()),
},
Node::Object(o) => { Node::Object(o) => {
let mut model = AP::attachment_q(o.as_document()?, object_model.internal, None)?; let mut model = AP::attachment_q(o.as_document()?, object_model.internal, None)?;
if let Set(u) | Unchanged(u) = model.url { if let Set(u) | Unchanged(u) = model.url {
@ -95,6 +87,28 @@ impl Normalizer for crate::Context {
} }
model model
}, },
Node::Link(l) => {
let url = l.href().unwrap_or_default();
let mut media_type = l.media_type().unwrap_or("link").to_string();
let mut document_type = apb::DocumentType::Page;
if self.cfg().compat.fix_attachment_images_media_type
&& [".jpg", ".jpeg", ".png", ".webp", ".bmp"] // TODO more image types???
.iter()
.any(|x| url.ends_with(x))
{
document_type = apb::DocumentType::Image;
media_type = format!("image/{}", url.split('.').last().unwrap_or_default());
}
crate::model::attachment::ActiveModel {
internal: sea_orm::ActiveValue::NotSet,
url: Set(self.cloaked(url)),
object: Set(object_model.internal),
document_type: Set(document_type),
name: Set(l.name().str()),
media_type: Set(media_type),
}
},
}; };
crate::model::attachment::Entity::insert(attachment_model) crate::model::attachment::Entity::insert(attachment_model)
.exec(tx) .exec(tx)