changeset 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 a1ae63fe666d
children 2f2b2f536008
files sat_tmp/wokkel/mam.py sat_tmp/wokkel/pubsub.py sat_tmp/wokkel/rsm.py
diffstat 3 files changed, 57 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/sat_tmp/wokkel/mam.py	Sat Dec 01 09:55:51 2018 +0100
+++ b/sat_tmp/wokkel/mam.py	Sun Jan 06 17:25:30 2019 +0100
@@ -46,6 +46,7 @@
 
 NS_MAM = 'urn:xmpp:mam:2'
 NS_FORWARD = 'urn:xmpp:forward:0'
+NS_ORDER_BY = u"urn:xmpp:order-by:0"
 
 FIELDS_REQUEST = "/iq[@type='get']/query[@xmlns='%s']" % NS_MAM
 ARCHIVE_REQUEST = "/iq[@type='set']/query[@xmlns='%s']" % NS_MAM
@@ -92,10 +93,14 @@
 
     @ivar query_id: id to use to track the query
     @itype query_id: C{unicode}
+
+    @param orderBy: Keys to order by
+    @type orderBy: L{list} of L{unicode}
     """
     # FIXME: should be based on generic.Stanza
 
-    def __init__(self, form=None, rsm_=None, node=None, query_id=None, sender=None, recipient=None):
+    def __init__(self, form=None, rsm_=None, node=None, query_id=None, sender=None,
+                 recipient=None, orderBy=None):
         if form is not None:
             assert form.formType == 'submit'
             assert form.formNamespace == NS_MAM
@@ -105,6 +110,7 @@
         self.query_id = query_id
         self.sender = sender
         self.recipient = recipient
+        self.orderBy = orderBy
 
     @classmethod
     def fromElement(cls, iq):
@@ -129,7 +135,12 @@
             rsm_request = None
         node = query.getAttribute('node')
         query_id = query.getAttribute('queryid')
-        return MAMRequest(form, rsm_request, node, query_id, sender, recipient)
+
+        orderBy = []
+        for element in query.elements(NS_ORDER_BY, 'order'):
+            orderBy.append(element['by'])
+
+        return MAMRequest(form, rsm_request, node, query_id, sender, recipient, orderBy)
 
     def toElement(self):
         """
@@ -146,6 +157,10 @@
             mam_elt.addChild(self.form.toElement())
         if self.rsm is not None:
             mam_elt.addChild(self.rsm.toElement())
+        if self.orderBy is not None:
+            for by_attr in self.orderBy:
+                order_elt = mam_elt.addElement((NS_ORDER_BY,'order'))
+                order_elt['by'] = by_attr
 
         return mam_elt
 
--- 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 = []
--- a/sat_tmp/wokkel/rsm.py	Sat Dec 01 09:55:51 2018 +0100
+++ b/sat_tmp/wokkel/rsm.py	Sun Jan 06 17:25:30 2019 +0100
@@ -345,8 +345,9 @@
 
     _request_class = PubSubRequest
 
-    def items(self, service, nodeIdentifier, maxItems=None, itemIdentifiers=None,
-              subscriptionIdentifier=None, sender=None, rsm_request=None):
+    def items(self, service, nodeIdentifier, maxItems=None,
+              subscriptionIdentifier=None, sender=None, itemIdentifiers=None,
+              orderBy=None, rsm_request=None):
         """
         Retrieve previously published items from a publish subscribe node.
 
@@ -359,14 +360,17 @@
         @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}
+
         @param ext_data: extension data.
         @type ext_data: L{dict}
 
@@ -383,6 +387,7 @@
         request.subscriptionIdentifier = subscriptionIdentifier
         request.sender = sender
         request.itemIdentifiers = itemIdentifiers
+        request.orderBy = orderBy
         request.rsm = rsm_request
 
         def cb(iq):