changeset 435:53bb3886b408

chat: generate preview if there is not one already for non encrypted attachments
author Goffi <goffi@goffi.org>
date Sun, 01 Mar 2020 22:11:25 +0100
parents 977158b56ce6
children 036ff0ed7474
files cagou/plugins/plugin_wid_chat.py
diffstat 1 files changed, 24 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/cagou/plugins/plugin_wid_chat.py	Sun Mar 01 22:11:25 2020 +0100
+++ b/cagou/plugins/plugin_wid_chat.py	Sun Mar 01 22:11:25 2020 +0100
@@ -142,7 +142,7 @@
         attachment['preview'] = preview_path
         wid.source = preview_path
 
-    def _setDecryptedPath(self, attachment, wid, path):
+    def _setPath(self, attachment, wid, path):
         attachment['path'] = path
         if wid is not None:
             # we also need a preview for the widget
@@ -160,17 +160,28 @@
         self.clear_widgets()
         for idx, attachment in enumerate(attachments):
             url = attachment['url']
-            to_decrypt = url.startswith("aesgcm:")
+            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
                      or self.chat.contact_list.isInRoster(self.mess_data.from_jid))):
-                    wid_kwargs = {
-                        "size_hint": (1, 1),
-                    }
-                    if not to_decrypt:
-                        wid_kwargs["source"] = url
-                    wid = AsyncImage(**wid_kwargs)
+                    wid = AsyncImage(size_hint=(1, 1))
+                    if 'preview' in attachment:
+                        wid.source = attachment["preview"]
+                    elif 'path' in attachment:
+                        G.host.bridge.imageGeneratePreview(
+                            attachment['path'],
+                            self.chat.profile,
+                            callback=partial(self._setPreview, attachment, wid),
+                        )
+                    else:
+                        # we'll download the file, the preview will then be generated
+                        to_download = True
                 else:
                     # we don't download automatically the image if the contact is not
                     # in roster, to avoid leaking the ip
@@ -179,13 +190,12 @@
             else:
                 wid = None
 
-            if to_decrypt:
-                # the file needs to be decrypted, it will be done by downloadURL
-                # and the widget source and attachment path will then be completed
-                del attachment['url']
+            if to_download:
+                # the file needs to be downloaded, the widget source,
+                # attachment path, and preview will then be completed
                 G.host.downloadURL(
                     url,
-                    callback=partial(self._setDecryptedPath, attachment, wid),
+                    callback=partial(self._setPath, attachment, wid),
                     dest=C.FILE_DEST_CACHE,
                     profile=self.chat.profile,
                 )
@@ -200,7 +210,7 @@
     def on_press(self):
         sources = []
         for attachment in self.attachments:
-            source = attachment.get('path', attachment.get('url'))
+            source = attachment.get('path') or attachment.get('url')
             if not source:
                 log.warning(f"no source for {attachment}")
             else: