diff sat_tmp/wokkel/pubsub.py @ 58:f4d569dc8e6b

wokkel/mam, wokkel/pubsub, wokkel/rsm: implemented "order-by" protoXEP (for Pubsub and MAM)
author Goffi <goffi@goffi.org>
date Sun, 06 Jan 2019 17:25:30 +0100
parents c8cb4e867897
children c8b468a96c5c
line wrap: on
line diff
--- a/sat_tmp/wokkel/pubsub.py	Sat Dec 01 09:55:51 2018 +0100
+++ b/sat_tmp/wokkel/pubsub.py	Sun Jan 06 17:25:30 2019 +0100
@@ -81,6 +81,8 @@
 NS_PUBSUB_META_DATA = NS_PUBSUB + "#meta-data"
 NS_PUBSUB_SUBSCRIBE_OPTIONS = NS_PUBSUB + "#subscribe_options"
 
+NS_ORDER_BY = u"urn:xmpp:order-by:0"
+
 # XPath to match pubsub requests
 PUBSUB_REQUEST = '/iq[@type="get" or @type="set"]/' + \
                     'pubsub[@xmlns="' + NS_PUBSUB + '" or ' + \
@@ -298,6 +300,7 @@
     subscriptions = None
     affiliations = None
     notify = None
+    orderBy = None
 
     # Map request iq type and subelement name to request verb
     _requestVerbMap = {
@@ -338,7 +341,7 @@
         'default': ['default'],
         'configureGet': ['nodeOrEmpty'],
         'configureSet': ['nodeOrEmpty', 'configureOrNone'],
-        'items': ['node', 'maxItems', 'itemIdentifiers', 'subidOrNone'],
+        'items': ['node', 'maxItems', 'itemIdentifiers', 'subidOrNone', 'orderBy'],
         'retract': ['node', 'notify', 'itemIdentifiers'],
         'purge': ['node'],
         'delete': ['node'],
@@ -672,6 +675,22 @@
             verbElement['notify'] = "true" if self.notify else "false"
 
 
+    def _parse_orderBy(self, verbElement):
+        pubsub_elt = verbElement.parent
+        self.orderBy = []
+        for element in pubsub_elt.elements(NS_ORDER_BY, 'order'):
+            self.orderBy.append(element['by'])
+
+
+    def _render_orderBy(self, verbElement):
+        if self.orderBy is None:
+            return
+        pubsub_elt = verbElement.parent
+        for by_attr in self.orderBy:
+            order_elt = pubsub_elt.addElement((NS_ORDER_BY,'order'))
+            order_elt['by'] = by_attr
+
+
     def parseElement(self, element):
         """
         Parse the publish-subscribe verb and parameters out of a request.
@@ -707,7 +726,6 @@
             getattr(self, '_parse_%s' % parameter)(verbElement)
 
 
-
     def send(self, xs):
         """
         Send this request to its recipient.
@@ -1021,8 +1039,9 @@
         return request.send(self.xmlstream)
 
 
-    def items(self, service, nodeIdentifier, maxItems=None, itemIdentifiers=None,
-              subscriptionIdentifier=None, sender=None):
+    def items(self, service, nodeIdentifier, maxItems=None,
+              subscriptionIdentifier=None, sender=None, itemIdentifiers=None,
+              orderBy=None):
         """
         Retrieve previously published items from a publish subscribe node.
 
@@ -1035,13 +1054,16 @@
         @param maxItems: Optional limit on the number of retrieved items.
         @type maxItems: C{int}
 
-        @param itemIdentifiers: Identifiers of the items to be retrieved.
-        @type itemIdentifiers: C{set}
-
         @param subscriptionIdentifier: Optional subscription identifier. In
             case the node has been subscribed to multiple times, this narrows
             the results to the specific subscription.
         @type subscriptionIdentifier: C{unicode}
+
+        @param itemIdentifiers: Identifiers of the items to be retrieved.
+        @type itemIdentifiers: C{set}
+
+        @param orderBy: Keys to order by
+        @type orderBy: L{list} of L{unicode}
         """
         request = self._request_class('items')
         request.recipient = service
@@ -1051,6 +1073,7 @@
         request.subscriptionIdentifier = subscriptionIdentifier
         request.sender = sender
         request.itemIdentifiers = itemIdentifiers
+        request.orderBy = orderBy
 
         def cb(iq):
             items = []