changeset 1613:c4407befc52a

browser (chat): fix attachments URL for messages.
author Goffi <goffi@goffi.org>
date Mon, 17 Jun 2024 17:37:15 +0200
parents ebd538cb26cb
children 24ba9ce18375
files libervia/web/pages/chat/_browser/__init__.py
diffstat 1 files changed, 12 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/web/pages/chat/_browser/__init__.py	Wed Jun 12 23:11:08 2024 +0200
+++ b/libervia/web/pages/chat/_browser/__init__.py	Mon Jun 17 17:37:15 2024 +0200
@@ -257,6 +257,17 @@
         else:
             await cache.fill_identities([str(jid.JID(from_jid).bare)])
             from_jid = from_jid.bare
+        attachments = extra.get("attachments", [])
+        for attachment in attachments:
+            if "url" not in attachment:
+                try:
+                    attachment["url"] = next(
+                        s['url'] for s in attachment["sources"] if 'url' in s
+                    )
+                except (StopIteration, KeyError):
+                    log.warning(
+                        f"An attachment has no URL: {attachment}"
+                    )
         msg_data = {
             "id": uid,
             "timestamp": extra.get("updated", timestamp),
@@ -268,7 +279,7 @@
             "reeceived": extra.get("received_timestamp") or timestamp,
             "encrypted": extra.get("encrypted", False),
             "received": extra.get("received", False),
-            "attachments": extra.get("attachments", []),
+            "attachments": attachments,
             "extra": extra
         }
         for key in ("thread", "thread_parent", "delay_sender", "info_type"):