feat: new compat options for lemmy......

lemmy whyyyyyyyyyy.....
This commit is contained in:
əlemi 2024-08-11 17:38:10 +02:00
parent 0efe1a3301
commit e15952f028
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 59 additions and 43 deletions

View file

@ -110,6 +110,9 @@ pub struct CompatibilityConfig {
#[serde(default)] #[serde(default)]
pub add_explicit_target_to_likes_if_local: bool, pub add_explicit_target_to_likes_if_local: bool,
#[serde(default)]
pub skip_single_attachment_if_image_is_set: bool,
} }
impl Config { impl Config {

View file

@ -73,49 +73,62 @@ impl Normalizer for crate::Context {
.await?; .await?;
} }
let obj_image = object_model.image.clone().unwrap_or_default(); // TODO this check is a bit disgusting but lemmy for some incomprehensible reason sends us
for attachment in object.attachment().flat() { // the same image twice: once in `image` and once as `attachment`. you may say "well just
let attachment_model = match attachment { // check if url is the same" and i absolutely do but lemmy is 10 steps forwards and it sends
Node::Empty => continue, // the same image twice with two distinct links. checkmate fedi developers!!!!!
Node::Array(_) => { // so basically i don't want to clutter my timeline with double images, nor fetch every image
tracing::warn!("ignoring array-in-array while processing attachments"); // that comes from lemmy (we cloak and lazy-load) just to dedupe it...
continue let attachments = object.attachment().flat();
}, if !(
Node::Object(o) => { self.cfg().compat.skip_single_attachment_if_image_is_set
let mut model = AP::attachment_q(o.as_document()?, object_model.internal, None)?; && object_model.image.is_some()
if let Set(u) | Unchanged(u) = model.url { && attachments.len() == 1
if u == obj_image { continue }; ) {
model.url = Set(self.cloaked(&u)); let obj_image = object_model.image.clone().unwrap_or_default();
} for attachment in attachments {
model let attachment_model = match attachment {
}, Node::Empty => continue,
Node::Link(l) => { Node::Array(_) => {
let url = l.href().unwrap_or_default(); tracing::warn!("ignoring array-in-array while processing attachments");
if url == obj_image { continue }; continue
let mut media_type = l.media_type().unwrap_or("link").to_string(); },
let mut document_type = apb::DocumentType::Page; Node::Object(o) => {
if self.cfg().compat.fix_attachment_images_media_type let mut model = AP::attachment_q(o.as_document()?, object_model.internal, None)?;
&& [".jpg", ".jpeg", ".png", ".webp", ".bmp"] // TODO more image types??? if let Set(u) | Unchanged(u) = model.url {
.iter() if u == obj_image { continue };
.any(|x| url.ends_with(x)) model.url = Set(self.cloaked(&u));
{ }
document_type = apb::DocumentType::Image; model
media_type = format!("image/{}", url.split('.').last().unwrap_or_default()); },
Node::Link(l) => {
let url = l.href().unwrap_or_default();
if url == obj_image { continue };
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 { crate::model::attachment::ActiveModel {
internal: sea_orm::ActiveValue::NotSet, internal: sea_orm::ActiveValue::NotSet,
url: Set(self.cloaked(url)), url: Set(self.cloaked(url)),
object: Set(object_model.internal), object: Set(object_model.internal),
document_type: Set(document_type), document_type: Set(document_type),
name: Set(l.name().str()), name: Set(l.name().str()),
media_type: Set(media_type), media_type: Set(media_type),
} }
}, },
}; };
crate::model::attachment::Entity::insert(attachment_model) crate::model::attachment::Entity::insert(attachment_model)
.exec(tx) .exec(tx)
.await?; .await?;
}
} }
for tag in object.tag().flat() { for tag in object.tag().flat() {