tools (common/data_objects): set attachments url from sources if missing.

This commit is contained in:
Goffi 2024-06-12 23:06:10 +02:00
commit 594112fed1

View file

@ -19,6 +19,7 @@
"""Objects handling bridge data, with jinja2 safe markup handling""" """Objects handling bridge data, with jinja2 safe markup handling"""
from libervia.backend.core.constants import Const as C 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 libervia.backend.tools.common import data_format
from os.path import basename from os.path import basename
@ -30,6 +31,10 @@ except ImportError:
from libervia.backend.tools.common import uri as xmpp_uri from libervia.backend.tools.common import uri as xmpp_uri
import urllib.request, urllib.parse, urllib.error import urllib.request, urllib.parse, urllib.error
log = getLogger(__name__)
q = lambda value: urllib.parse.quote(value.encode("utf-8"), safe="@") q = lambda value: urllib.parse.quote(value.encode("utf-8"), safe="@")
@ -44,6 +49,18 @@ class Message(object):
self._type = msg_data[6] self._type = msg_data[6]
self.extra = data_format.deserialise(msg_data[7]) self.extra = data_format.deserialise(msg_data[7])
self._html = dict(data_format.get_sub_dict("xhtml", self.extra)) 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 @property
def id(self): def id(self):