changeset 73:ab556b1c2ca4 default tip @

wokkel (pubsub): publish-options parsing/rendering
author Goffi <goffi@goffi.org>
date Sat, 28 Dec 2019 19:53:00 +0100
parents 29b383e14ebf
children
files sat_tmp/wokkel/pubsub.py
diffstat 1 files changed, 41 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/sat_tmp/wokkel/pubsub.py	Fri Dec 27 14:10:22 2019 +0100
+++ b/sat_tmp/wokkel/pubsub.py	Sat Dec 28 19:53:00 2019 +0100
@@ -80,6 +80,7 @@
 NS_PUBSUB_NODE_CONFIG = NS_PUBSUB + "#node_config"
 NS_PUBSUB_META_DATA = NS_PUBSUB + "#meta-data"
 NS_PUBSUB_SUBSCRIBE_OPTIONS = NS_PUBSUB + "#subscribe_options"
+NS_PUBSUB_PUBLISH_OPTIONS = NS_PUBSUB + "#publish-options"
 
 NS_ORDER_BY = "urn:xmpp:order-by:0"
 
@@ -330,7 +331,7 @@
 
     # Map request verb to parameter handler names
     _parameters = {
-        'publish': ['node', 'items'],
+        'publish': ['node', 'items', 'publishOptionsOrNone'],
         'subscribe': ['nodeOrEmpty', 'jid', 'optionsWithSubscribe'],
         'unsubscribe': ['nodeOrEmpty', 'jid', 'subidOrNone'],
         'optionsGet': ['nodeOrEmpty', 'jid', 'subidOrNone'],
@@ -425,6 +426,36 @@
                 verbElement.addChild(item)
 
 
+    def _parse_publishOptionsOrNone(self, verbElement):
+        """
+        Parse optional publish-options form in publish request.
+        """
+        for element in verbElement.parent.elements():
+            form = data_form.findForm(element, NS_PUBSUB_PUBLISH_OPTIONS)
+            if form is not None:
+                if form.formType != 'submit':
+                    raise BadRequest(text="Unexpected form type '%s'" %
+                                          form.formType)
+            else:
+                form = data_form.Form('submit',
+                                      formNamespace=NS_PUBSUB_PUBLISH_OPTIONS)
+
+            self.options = form
+
+
+    def _render_publishOptionsOrNone(self, verbElement):
+        if self.options is not None:
+            if self.options.formType != 'submit':
+                log.err(
+                    "Invalid type for publish-options form ({formType}): {xml}".format(
+                        formType = self.options.formType,
+                        xml = verbElement.toXml())
+                )
+                return
+            publishOptions = verbElement.parent.addElement('publish-options')
+            publishOptions.addChild(self.options.toElement())
+
+
     def _parse_jid(self, verbElement):
         """
         Parse subscriber out of the verbElement for un-/subscribe requests.
@@ -1020,7 +1051,7 @@
         return request.send(self.xmlstream)
 
 
-    def publish(self, service, nodeIdentifier, items=None, sender=None):
+    def publish(self, service, nodeIdentifier, items=None, sender=None, options=None):
         """
         Publish to a publish subscribe node.
 
@@ -1030,11 +1061,18 @@
         @type nodeIdentifier: C{unicode}
         @param items: Optional list of L{Item}s to publish.
         @type items: C{list}
+        @param options: Optional publish-options form (see XEP-0060 ยง7.1.5)
+        @type options: C{dict}
         """
         request = self._request_class('publish')
         request.recipient = service
         request.nodeIdentifier = nodeIdentifier
         request.items = items
+        if options:
+            form = data_form.Form(formType='submit',
+                                  formNamespace=NS_PUBSUB_PUBLISH_OPTIONS)
+            form.makeFields(options)
+            request.options = form
         request.sender = sender
         return request.send(self.xmlstream)
 
@@ -1591,7 +1629,7 @@
         return []
 
 
-    def publish(self, requestor, service, nodeIdentifier, items):
+    def publish(self, requestor, service, nodeIdentifier, items, options):
         raise Unsupported('publish')