fix: skip double lemmy image, not any attachment

some lemmy posts which had a single link attachment would get that
attachment stripped for this fix. now it should keep those while still
stripping the doubled image
This commit is contained in:
əlemi 2024-12-29 02:44:40 +01:00
parent 68b217e648
commit 5800f39c67
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

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