# HG changeset patch # User Goffi # Date 1718226370 -7200 # Node ID 32388d74334869096cd318589aa9cd9e76a1bf74 # Parent 9fc3d28bc3f6723cc8fdae43854b4f7b01c9caa5 tools (common/data_objects): set attachments `url` from `sources` if missing. diff -r 9fc3d28bc3f6 -r 32388d743348 libervia/backend/tools/common/data_objects.py --- 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):