From 342f708ce9e8269326c79e1cf164ffc03579a61f Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Wed, 3 Aug 2022 19:54:11 +0200 Subject: [PATCH] Don't display inlined images twice --- app/ap_object.py | 20 +++++++++++++++++++- app/templates/utils.html | 4 +++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/ap_object.py b/app/ap_object.py index 0a2ab14..16b91cd 100644 --- a/app/ap_object.py +++ b/app/ap_object.py @@ -4,6 +4,7 @@ from functools import cached_property from typing import Any import pydantic +from bs4 import BeautifulSoup # type: ignore from markdown import markdown from app import activitypub as ap @@ -73,6 +74,23 @@ class Object: def tags(self) -> list[ap.RawObject]: return ap.as_list(self.ap_object.get("tag", [])) + @cached_property + def inlined_images(self) -> set[str]: + image_urls: set[str] = set() + if not self.content: + return image_urls + + soup = BeautifulSoup(self.content, "html5lib") + imgs = soup.find_all("img") + + for img in imgs: + if not img.attrs.get("src"): + continue + + image_urls.add(img.attrs["src"]) + + return image_urls + @cached_property def attachments(self) -> list["Attachment"]: attachments = [] @@ -97,7 +115,7 @@ class Object: { "proxiedUrl": proxied_url, "resizedUrl": proxied_url + "/740" - if obj["mediaType"].startswith("image") + if obj.get("mediaType", "").startswith("image") else None, **obj, } diff --git a/app/templates/utils.html b/app/templates/utils.html index c543d2c..bfc6331 100644 --- a/app/templates/utils.html +++ b/app/templates/utils.html @@ -339,7 +339,9 @@ {% endif %} {% for attachment in object.attachments %} {% if attachment.type == "Image" or (attachment | has_media_type("image")) %} - {{ attachment.name }} + {% if attachment.url not in object.inlined_images %} + {{ attachment.name }} + {% endif %} {% elif attachment.type == "Video" or (attachment | has_media_type("video")) %} {% elif attachment.type == "Audio" or (attachment | has_media_type("audio")) %}