diff sat/plugins/plugin_xep_0329.py @ 3333:ac9342f359e9

plugin XEP-0329: download thumbnails: - thumbnails are now downloaded directly when BoB is used, `filename` is then filled - `utils.asDeferred` is now used for Jingle methods, so `async` coroutines can be used - (XEP-0231): fixed `dumpData`
author Goffi <goffi@goffi.org>
date Thu, 13 Aug 2020 23:46:18 +0200
parents 8bbd2ed924e8
children 33d9b38b5890
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0329.py	Thu Aug 13 23:46:18 2020 +0200
+++ b/sat/plugins/plugin_xep_0329.py	Thu Aug 13 23:46:18 2020 +0200
@@ -44,7 +44,7 @@
     C.PI_TYPE: "XEP",
     C.PI_MODES: C.PLUG_MODE_BOTH,
     C.PI_PROTOCOLS: ["XEP-0329"],
-    C.PI_DEPENDENCIES: ["XEP-0234", "XEP-0300", "XEP-0106"],
+    C.PI_DEPENDENCIES: ["XEP-0231", "XEP-0234", "XEP-0300", "XEP-0106"],
     C.PI_MAIN: "XEP_0329",
     C.PI_HANDLER: "yes",
     C.PI_DESCRIPTION: _("""Implementation of File Information Sharing"""),
@@ -272,6 +272,7 @@
         log.info(_("File Information Sharing initialization"))
         self.host = host
         ShareNode.host = host
+        self._b = host.plugins["XEP-0231"]
         self._h = host.plugins["XEP-0300"]
         self._jf = host.plugins["XEP-0234"]
         host.bridge.addMethod(
@@ -672,7 +673,7 @@
             client, iq_elt, self._compGetRootNodesCb, self._compGetFilesFromNodeCb
         )
 
-    def _parseResult(self, iq_elt, client):
+    async def _parseResult(self, client, peer_jid, iq_elt):
         query_elt = next(iq_elt.elements(NS_FIS, "query"))
         files = []
 
@@ -680,10 +681,24 @@
             if elt.name == "file":
                 # we have a file
                 try:
-                    file_data = self._jf.parseFileElement(client, elt)
+                    file_data = await self._jf.parseFileElement(client, elt)
                 except exceptions.DataError:
                     continue
                 file_data["type"] = C.FILE_TYPE_FILE
+                try:
+                    thumbs = file_data['extra'][C.KEY_THUMBNAILS]
+                except KeyError:
+                    log.debug(f"No thumbnail found for {file_data}")
+                else:
+                    for thumb in thumbs:
+                        if 'url' not in thumb and "id" in thumb:
+                            try:
+                                file_path = await self._b.getFile(client, peer_jid, thumb['id'])
+                            except Exception as e:
+                                log.warning(f"Can't get thumbnail {thumb['id']!r} for {file_data}: {e}")
+                            else:
+                                thumb['filename'] = file_path.name
+
             elif elt.name == "directory" and elt.uri == NS_FIS:
                 # we have a directory
 
@@ -1018,27 +1033,26 @@
     def _listFiles(self, target_jid, path, extra, profile):
         client = self.host.getClient(profile)
         target_jid = client.jid if not target_jid else jid.JID(target_jid)
-        d = self.listFiles(client, target_jid, path or None)
+        d = defer.ensureDeferred(self.listFiles(client, target_jid, path or None))
         d.addCallback(self._serializeData)
         return d
 
-    def listFiles(self, client, target_jid, path=None, extra=None):
+    async def listFiles(self, client, peer_jid, path=None, extra=None):
         """List file shared by an entity
 
-        @param target_jid(jid.JID): jid of the sharing entity
+        @param peer_jid(jid.JID): jid of the sharing entity
         @param path(unicode, None): path to the directory containing shared files
             None to get root directories
         @param extra(dict, None): extra data
         @return list(dict): shared files
         """
         iq_elt = client.IQ("get")
-        iq_elt["to"] = target_jid.full()
+        iq_elt["to"] = peer_jid.full()
         query_elt = iq_elt.addElement((NS_FIS, "query"))
         if path:
             query_elt["node"] = path
-        d = iq_elt.send()
-        d.addCallback(self._parseResult, client)
-        return d
+        iq_result_elt = await iq_elt.send()
+        return await self._parseResult(client, peer_jid, iq_result_elt)
 
     def _localSharesGet(self, profile):
         client = self.host.getClient(profile)