Don't display inlined images twice

main
Thomas Sileo 2022-08-03 19:54:11 +02:00
parent 6395a75b6e
commit 342f708ce9
2 changed files with 22 additions and 2 deletions

View File

@ -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,
}

View File

@ -339,7 +339,9 @@
{% endif %}
{% for attachment in object.attachments %}
{% if attachment.type == "Image" or (attachment | has_media_type("image")) %}
<img src="{{ attachment.resized_url or attachment.proxied_url }}"{% if attachment.name %} title="{{ attachment.name }}" alt="{{ attachment.name }}"{% endif %} class="attachment">
{% if attachment.url not in object.inlined_images %}
<img src="{{ attachment.resized_url or attachment.proxied_url }}"{% if attachment.name %} title="{{ attachment.name }}" alt="{{ attachment.name }}"{% endif %} class="attachment">
{% endif %}
{% elif attachment.type == "Video" or (attachment | has_media_type("video")) %}
<video controls preload="metadata" src="{{ attachment.url | media_proxy_url }}"{% if attachment.name %} title="{{ attachment.name }}"{% endif %} class="attachment"></video>
{% elif attachment.type == "Audio" or (attachment | has_media_type("audio")) %}