Don't display inlined images twice

This commit is contained in:
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 from typing import Any
import pydantic import pydantic
from bs4 import BeautifulSoup # type: ignore
from markdown import markdown from markdown import markdown
from app import activitypub as ap from app import activitypub as ap
@ -73,6 +74,23 @@ class Object:
def tags(self) -> list[ap.RawObject]: def tags(self) -> list[ap.RawObject]:
return ap.as_list(self.ap_object.get("tag", [])) 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 @cached_property
def attachments(self) -> list["Attachment"]: def attachments(self) -> list["Attachment"]:
attachments = [] attachments = []
@ -97,7 +115,7 @@ class Object:
{ {
"proxiedUrl": proxied_url, "proxiedUrl": proxied_url,
"resizedUrl": proxied_url + "/740" "resizedUrl": proxied_url + "/740"
if obj["mediaType"].startswith("image") if obj.get("mediaType", "").startswith("image")
else None, else None,
**obj, **obj,
} }

View file

@ -339,7 +339,9 @@
{% endif %} {% endif %}
{% for attachment in object.attachments %} {% for attachment in object.attachments %}
{% if attachment.type == "Image" or (attachment | has_media_type("image")) %} {% if attachment.type == "Image" or (attachment | has_media_type("image")) %}
{% 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"> <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")) %} {% 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> <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")) %} {% elif attachment.type == "Audio" or (attachment | has_media_type("audio")) %}