mirror of
				https://git.sr.ht/~tsileo/microblog.pub
				synced 2025-06-05 21:59:23 +02:00 
			
		
		
		
	Don't display inlined images twice
This commit is contained in:
		| @@ -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, | ||||
|                     } | ||||
|   | ||||
| @@ -339,7 +339,9 @@ | ||||
|     {% endif %} | ||||
|     {% for attachment in object.attachments %} | ||||
|     {% 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"> | ||||
|         {% 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")) %} | ||||
|   | ||||
		Reference in New Issue
	
	Block a user