changeset 3312:77177b13ff54

plugin XEP-0060: serialise psItemsGet result with data_format
author Goffi <goffi@goffi.org>
date Fri, 17 Jul 2020 12:57:23 +0200
parents 29f8122f00f3
children 624c60293deb
files sat/plugins/plugin_xep_0060.py sat_frontends/jp/cmd_pubsub.py
diffstat 2 files changed, 65 insertions(+), 50 deletions(-) [+]
line wrap: on
line diff
--- a/sat/plugins/plugin_xep_0060.py	Thu Jul 16 09:07:26 2020 +0200
+++ b/sat/plugins/plugin_xep_0060.py	Fri Jul 17 12:57:23 2020 +0200
@@ -190,7 +190,7 @@
             "psItemsGet",
             ".plugin",
             in_sign="ssiassa{ss}s",
-            out_sign="(asa{ss})",
+            out_sign="s",
             method=self._getItems,
             async_=True,
         )
@@ -599,6 +599,11 @@
             raise exceptions.DataError("Can't find Item in MAM message element")
         return item_elt
 
+    def serialiseItems(self, items_data):
+        items, metadata = items_data
+        metadata['items'] = items
+        return data_format.serialise(metadata)
+
     def _getItems(self, service="", node="", max_items=10, item_ids=None, sub_id=None,
                   extra_dict=None, profile_key=C.PROF_KEY_NONE):
         """Get items from pubsub node
@@ -620,6 +625,7 @@
             extra.extra,
         )
         d.addCallback(self.transItemsData)
+        d.addCallback(self.serialiseItems)
         return d
 
     def getItems(self, client, service, node, max_items=None, item_ids=None, sub_id=None,
--- a/sat_frontends/jp/cmd_pubsub.py	Thu Jul 16 09:07:26 2020 +0200
+++ b/sat_frontends/jp/cmd_pubsub.py	Fri Jul 17 12:57:23 2020 +0200
@@ -800,20 +800,22 @@
 
     async def start(self):
         try:
-            ps_result = await self.host.bridge.psItemsGet(
-                self.args.service,
-                self.args.node,
-                self.args.max,
-                self.args.items,
-                self.args.sub_id,
-                self.getPubsubExtra(),
-                self.profile,
+            ps_result = data_format.deserialise(
+                await self.host.bridge.psItemsGet(
+                    self.args.service,
+                    self.args.node,
+                    self.args.max,
+                    self.args.items,
+                    self.args.sub_id,
+                    self.getPubsubExtra(),
+                    self.profile,
+                )
             )
         except Exception as e:
             self.disp(f"can't get pubsub items: {e}", error=True)
             self.host.quit(C.EXIT_BRIDGE_ERRBACK)
         else:
-            await self.output(ps_result[0])
+            await self.output(ps_result['items'])
             self.host.quit(C.EXIT_OK)
 
 
@@ -901,9 +903,12 @@
             )
             self.host.quit(1)
         items = [item] if item else []
-        item_raw = (await self.host.bridge.psItemsGet(
-            service, node, 1, items, "", {}, self.profile
-        ))[0][0]
+        ps_result = data_format.deserialise(
+            await self.host.bridge.psItemsGet(
+                service, node, 1, items, "", {}, self.profile
+            )
+        )
+        item_raw = ps_result['items'][0]
         parser = etree.XMLParser(remove_blank_text=True, recover=True)
         item_elt = etree.fromstring(item_raw, parser)
         item_id = item_elt.get("id")
@@ -1233,14 +1238,16 @@
     async def getItems(self, depth, service, node, items):
         self.to_get += 1
         try:
-            items_data = await self.host.bridge.psItemsGet(
-                service,
-                node,
-                self.args.node_max,
-                items,
-                "",
-                self.getPubsubExtra(),
-                self.profile,
+            ps_result = data_format.deserialise(
+                await self.host.bridge.psItemsGet(
+                    service,
+                    node,
+                    self.args.node_max,
+                    items,
+                    "",
+                    self.getPubsubExtra(),
+                    self.profile,
+                )
             )
         except Exception as e:
             self.disp(
@@ -1249,7 +1256,7 @@
             )
             self.to_get -= 1
         else:
-            await self.search(items_data, depth)
+            await self.search(ps_result, depth)
 
     def _checkPubsubURL(self, match, found_nodes):
         """check that the matched URL is an xmpp: one
@@ -1449,7 +1456,7 @@
                     )
                 )
 
-    async def search(self, items_data, depth):
+    async def search(self, ps_result, depth):
         """callback of getItems
 
         this method filters items, get sub nodes if needed,
@@ -1458,14 +1465,13 @@
         @param depth(int): current depth level
             0 for first node, 1 for first children, and so on
         """
-        items, metadata = items_data
-        for item in items:
+        for item in ps_result['items']:
             if depth < self.args.max_depth:
                 await self.getSubNodes(item, depth)
             keep, item = self.filter(item)
             if not keep:
                 continue
-            await self.doItemAction(item, metadata)
+            await self.doItemAction(item, ps_result)
 
         #  we check if we got all getItems results
         self.to_get -= 1
@@ -1560,9 +1566,9 @@
         @param metadata(dict): metadata as returned by psItemsGet
         """
         try:
-            last = metadata['rsm_last']
-            index = int(metadata['rsm_index'])
-            count = int(metadata['rsm_count'])
+            last = metadata['rsm']['last']
+            index = int(metadata['rsm']['index'])
+            count = int(metadata['rsm']['count'])
         except KeyError:
             self.disp(_("Can't retrieve all items, RSM metadata not available"),
                       error=True)
@@ -1585,14 +1591,16 @@
         extra = self.getPubsubExtra()
         extra['rsm_after'] = last
         try:
-            ps_result = await self.host.bridge.psItemsGet(
-                self.args.service,
-                self.args.node,
-                self.args.rsm_max,
-                self.args.items,
-                "",
-                extra,
-                self.profile,
+            ps_result = await data_format.deserialise(
+                self.host.bridge.psItemsGet(
+                    self.args.service,
+                    self.args.node,
+                    self.args.rsm_max,
+                    self.args.items,
+                    "",
+                    extra,
+                    self.profile,
+                )
             )
         except Exception as e:
             self.disp(
@@ -1603,11 +1611,10 @@
             await self.psItemsGetCb(ps_result)
 
     async def psItemsGetCb(self, ps_result):
-        items, metadata = ps_result
         encoding = 'utf-8'
         new_items = []
 
-        for item in items:
+        for item in ps_result['items']:
             if self.check_duplicates:
                 # this is used when we are not ordering by creation
                 # to avoid infinite loop
@@ -1687,7 +1694,7 @@
         if not self.args.apply:
             # on dry run we have nothing to wait for, we can quit
             if self.args.all:
-                return await self.handleNextPage(metadata)
+                return await self.handleNextPage(ps_result)
             self.host.quit()
         else:
             if self.args.admin:
@@ -1696,7 +1703,7 @@
                 bridge_method = self.host.bridge.psItemsSend
 
             try:
-                ps_result = await bridge_method(
+                ps_items_send_result = await bridge_method(
                     self.args.service,
                     self.args.node,
                     new_items,
@@ -1707,7 +1714,7 @@
                 self.disp(f"can't send item: {e}", error=True)
                 self.host.quit(C.EXIT_BRIDGE_ERRBACK)
             else:
-                await self.psItemsSendCb(ps_result, metadata=metadata)
+                await self.psItemsSendCb(ps_items_send_result, metadata=ps_result)
 
     async def start(self):
         if self.args.all and self.args.order_by != C.ORDER_BY_CREATION:
@@ -1724,14 +1731,16 @@
             self.check_duplicates = False
 
         try:
-            ps_result = await self.host.bridge.psItemsGet(
-                self.args.service,
-                self.args.node,
-                self.args.max,
-                self.args.items,
-                "",
-                self.getPubsubExtra(),
-                self.profile,
+            ps_result = data_format.deserialise(
+                await self.host.bridge.psItemsGet(
+                    self.args.service,
+                    self.args.node,
+                    self.args.max,
+                    self.args.items,
+                    "",
+                    self.getPubsubExtra(),
+                    self.profile,
+                )
             )
         except Exception as e:
             self.disp(f"can't retrieve items: {e}", error=True)