plugin XEP-0059: first draft, pubsub and jabber search do not exploit it yet

This commit is contained in:
souliane 2014-09-22 22:25:44 +02:00
commit 20c94f1fbc

View file

@ -23,6 +23,7 @@ from sat.core.log import getLogger
log = getLogger(__name__)
from sat.memory.memory import Sessions
from wokkel.compat import IQ
from wokkel import disco, pubsub
from wokkel.pubsub import PubSubRequest, NS_PUBSUB
from zope.interface import implements
@ -35,6 +36,7 @@ PLUGIN_INFO = {
"type": "XEP",
"protocols": ["XEP-0060"],
"dependencies": [],
"recommendations": ["XEP-0059"],
"main": "XEP_0060",
"handler": "yes",
"description": _("""Implementation of PubSub Protocol""")
@ -232,7 +234,7 @@ class SatPubSubClient(pubsub.PubSubClient):
# FIXME: we have to temporary override this method here just
# to set the attributes itemIdentifiers which is not used
# in pubsub.PubSubClient.items
# in pubsub.PubSubClient.items + use the XEP-0059
def items(self, service, nodeIdentifier, maxItems=None, itemIdentifiers=None,
subscriptionIdentifier=None, sender=None):
"""
@ -255,7 +257,8 @@ class SatPubSubClient(pubsub.PubSubClient):
the results to the specific subscription.
@type subscriptionIdentifier: C{unicode}
"""
request = PubSubRequest('items')
# TODO: add method attributes for RSM: before, after, index
request = PubSubRequest('items', self.host, {'limit': maxItems} if maxItems else {})
request.recipient = service
request.nodeIdentifier = nodeIdentifier
if maxItems:
@ -269,6 +272,7 @@ class SatPubSubClient(pubsub.PubSubClient):
for element in iq.pubsub.items.elements():
if element.uri == NS_PUBSUB and element.name == 'item':
items.append(element)
# TODO: return (items, self.host.plugins['XEP-0059'].extractMetadata(iq)) ??
return items
d = request.send(self.xmlstream)
@ -331,3 +335,64 @@ class SatPubSubClient(pubsub.PubSubClient):
def getDiscoItems(self, requestor, service, nodeIdentifier=''):
return self.host.getDiscoItems(service, nodeIdentifier, self.parent.profile)
class PubSubRequest(pubsub.PubSubRequest):
def __init__(self, verb=None, host=None, page_attrs=None):
"""
@param verb (str): the type of pubsub request
@param host (SAT): the SAT instance
@param page_attrs (dict): options for RSM paging:
- limit (int): the maximum number of items in the page
- index (int): the starting index of the requested page
- after (str, int): the element immediately preceding the page
- before (str, int): the element immediately following the page
"""
self.verb = verb
self.host = host
self.page_attrs = page_attrs
# FIXME: the redefinition of this wokkel method is the easiest way I found
# to handle RSM. We should find a proper solution, maybe just add in wokkel an
# empty method postProcessMessage, call it before sending and overwrite it here
# instead of overwriting the whole send method.
def send(self, xs):
"""
Send this request to its recipient.
This renders all of the relevant parameters for this specific
requests into an L{IQ}, and invoke its C{send} method.
This returns a deferred that fires upon reception of a response. See
L{IQ} for details.
@param xs: The XML stream to send the request on.
@type xs: L{twisted.words.protocols.jabber.xmlstream.XmlStream}
@rtype: L{defer.Deferred}.
"""
try:
(self.stanzaType,
childURI,
childName) = self._verbRequestMap[self.verb]
except KeyError:
raise NotImplementedError()
iq = IQ(xs, self.stanzaType)
iq.addElement((childURI, 'pubsub'))
verbElement = iq.pubsub.addElement(childName)
if self.sender:
iq['from'] = self.sender.full()
if self.recipient:
iq['to'] = self.recipient.full()
for parameter in self._parameters[self.verb]:
getattr(self, '_render_%s' % parameter)(verbElement)
# This lines have been added for RSM
if self.host and 'XEP-0059' in self.host.plugins and self.page_attrs:
self.page_attrs['stanza'] = iq
self.host.plugins['XEP-0059'].requestPage(**self.page_attrs)
return iq.send()