diff cagou/plugins/plugin_wid_chat.py @ 443:61322ff8090b

chat: fixed handling of attachments without URL
author Goffi <goffi@goffi.org>
date Sat, 07 Mar 2020 00:05:50 +0100
parents c2b63d3bb088
children ffdf2390ea56
line wrap: on
line diff
--- a/cagou/plugins/plugin_wid_chat.py	Sat Mar 07 00:05:50 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.py	Sat Mar 07 00:05:50 2020 +0100
@@ -164,13 +164,18 @@
         attachments = self.attachments
         self.clear_widgets()
         for idx, attachment in enumerate(attachments):
-            url = attachment['url']
-            if url.startswith("aesgcm:"):
-                del attachment['url']
-                # if the file is encrypted, we need to download it for decryption
-                to_download = True
+            try:
+                url = attachment['url']
+            except KeyError:
+                url = None
+                to_download = False
             else:
-                to_download = False
+                if url.startswith("aesgcm:"):
+                    del attachment['url']
+                    # if the file is encrypted, we need to download it for decryption
+                    to_download = True
+                else:
+                    to_download = False
 
             if idx < 3 or len(attachments) <= 4:
                 if ((self.mess_data.own_mess
@@ -184,6 +189,8 @@
                             self.chat.profile,
                             callback=partial(self._setPreview, attachment, wid),
                         )
+                    elif url is None:
+                        log.warning(f"Can't find any source for {attachment}")
                     else:
                         # we'll download the file, the preview will then be generated
                         to_download = True
@@ -346,20 +353,24 @@
             # roster
             if ((self.mess_data.own_mess
                  or self.chat.contact_list.isInRoster(self.mess_data.from_jid))):
-                url = urlparse(attachment['url'])
-                if url.scheme == "aesgcm":
-                    # we remove the URL now, we'll replace it by
-                    # the local decrypted version
-                    del attachment['url']
+                try:
+                    url = urlparse(attachment['url'])
+                except KeyError:
                     item = AttachmentImageItem(data=attachment)
-                    G.host.downloadURL(
-                        url.geturl(),
-                        callback=partial(self._setPath, item.data),
-                        dest=C.FILE_DEST_CACHE,
-                        profile=self.chat.profile,
-                    )
                 else:
-                    item = AttachmentImageItem(data=attachment)
+                    if url.scheme == "aesgcm":
+                        # we remove the URL now, we'll replace it by
+                        # the local decrypted version
+                        del attachment['url']
+                        item = AttachmentImageItem(data=attachment)
+                        G.host.downloadURL(
+                            url.geturl(),
+                            callback=partial(self._setPath, item.data),
+                            dest=C.FILE_DEST_CACHE,
+                            profile=self.chat.profile,
+                        )
+                    else:
+                        item = AttachmentImageItem(data=attachment)
             else:
                 item = AttachmentItem(data=attachment)