changeset 3362:02583a401e51

plugin XEP-0329: fix conflict check + directory creation in `onComponentCreateDir`
author Goffi <goffi@goffi.org>
date Thu, 17 Sep 2020 21:41:19 +0200 (2020-09-17)
parents 43e60c40de65
children e3bdfecaf1b0
files sat/plugins/plugin_xep_0329.py
diffstat 1 files changed, 19 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0329.py	Thu Sep 17 21:40:23 2020 +0200
+++ b/sat/plugins/plugin_xep_0329.py	Thu Sep 17 21:41:19 2020 +0200
@@ -1098,26 +1098,22 @@
             client.sendError(iq_elt, 'forbidden', "You can't create a directory there")
             return
         # when going further into the path, the permissions will be checked by getFiles
-        try:
-            await self.host.memory.getFiles(
+        if len(path.parts) > 2:
+            files_data = await self.host.memory.getFiles(
                 client,
                 peer_jid=peer_jid,
                 path=path.parent,
-                name=path.name,
                 namespace=namespace,
                 owner=owner,
             )
-        except exceptions.NotFound:
-            # this is expected, nothing should be found at this path
-            pass
-        else:
-            log.warning(
-                f"Conflict when trying to create a directory (from: {peer_jid} "
-                f"namespace: {namespace!r} path: {path!r})"
-            )
-            client.sendError(
-                iq_elt, 'conflict', "there is already a file or dir at this path")
-            return
+            if path.name in [d['name'] for d in files_data]:
+                log.warning(
+                    f"Conflict when trying to create a directory (from: {peer_jid} "
+                    f"namespace: {namespace!r} path: {path!r})"
+                )
+                client.sendError(
+                    iq_elt, 'conflict', "there is already a file or dir at this path")
+                return
 
         try:
             configuration_elt = next(
@@ -1125,7 +1121,15 @@
         except StopIteration:
             configuration_elt = None
 
-        await self.host.memory.setFile(client, path.name, path=path.parent, namespace=namespace, owner=owner, peer_jid=peer_jid)
+        await self.host.memory.setFile(
+            client,
+            path.name,
+            path=path.parent,
+            type_=C.FILE_TYPE_DIRECTORY,
+            namespace=namespace,
+            owner=owner,
+            peer_jid=peer_jid
+        )
 
         if configuration_elt is not None:
             file_data = (await self.host.memory.getFiles(