changeset 4267:32388d743348

tools (common/data_objects): set attachments `url` from `sources` if missing.
author Goffi <goffi@goffi.org>
date Wed, 12 Jun 2024 23:06:10 +0200
parents 9fc3d28bc3f6
children 51d004e50786
files libervia/backend/tools/common/data_objects.py
diffstat 1 files changed, 17 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libervia/backend/tools/common/data_objects.py	Wed Jun 12 22:47:34 2024 +0200
+++ b/libervia/backend/tools/common/data_objects.py	Wed Jun 12 23:06:10 2024 +0200
@@ -19,6 +19,7 @@
 """Objects handling bridge data, with jinja2 safe markup handling"""
 
 from libervia.backend.core.constants import Const as C
+from libervia.backend.core.log import getLogger
 from libervia.backend.tools.common import data_format
 from os.path import basename
 
@@ -30,6 +31,10 @@
 from libervia.backend.tools.common import uri as xmpp_uri
 import urllib.request, urllib.parse, urllib.error
 
+
+log = getLogger(__name__)
+
+
 q = lambda value: urllib.parse.quote(value.encode("utf-8"), safe="@")
 
 
@@ -44,6 +49,18 @@
         self._type = msg_data[6]
         self.extra = data_format.deserialise(msg_data[7])
         self._html = dict(data_format.get_sub_dict("xhtml", self.extra))
+        attachments = self.extra.get("attachments")
+        if 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}"
+                        )
 
     @property
     def id(self):