diff sat/memory/memory.py @ 3319:3a15e76a694e

core (memory): `setFileAccessModel` implementation: this method let change read permission using access model similar to what is used with Pubsub.
author Goffi <goffi@goffi.org>
date Sat, 01 Aug 2020 16:07:39 +0200
parents 460606155bec
children 4f1fcee83d36
line wrap: on
line diff
--- a/sat/memory/memory.py	Sat Aug 01 16:06:12 2020 +0200
+++ b/sat/memory/memory.py	Sat Aug 01 16:07:39 2020 +0200
@@ -1409,6 +1409,47 @@
             ),
         )
 
+    def _setFileAccessModelUpdate(
+        self,
+        access: dict,
+        file_data: dict,
+        access_model: str
+    ) -> None:
+        read_data = access.setdefault(C.ACCESS_PERM_READ, {})
+        if access_model == "open":
+            requested_type = C.ACCESS_TYPE_PUBLIC
+        elif access_model == "whitelist":
+            requested_type = C.ACCESS_TYPE_WHITELIST
+        else:
+            raise ValueError(f"unknown access model: {access_model}")
+
+        read_data['type'] = requested_type
+        if requested_type == C.ACCESS_TYPE_WHITELIST and 'jids' not in read_data:
+            read_data['jids'] = []
+
+    async def setFileAccessModel(
+        self,
+        client,
+        file_data: dict,
+        access_model: str,
+    ) -> None:
+        """Apply pubsub like access_model to file_data
+
+        Only 2 access models are supported so far:
+            - "open": set public access to file/dir
+            - "whitelist": set whitelist to file/dir
+        """
+        file_id = file_data['id']
+        await self.fileUpdate(
+            file_id,
+            'access',
+            update_cb=partial(
+                self._setFileAccessModelUpdate,
+                file_data=file_data,
+                access_model=access_model
+            ),
+        )
+
     @defer.inlineCallbacks
     def getFiles(
         self, client, peer_jid, file_id=None, version=None, parent=None, path=None,