Ignore:
Timestamp:
Feb 20, 2008, 3:44:44 PM (14 years ago)
Author:
Ralph Meijer <ralphm@…>
Branch:
default
Convert:
svn:b33ecbfc-034c-dc11-8662-000475d9059e/trunk@41
Message:

Add support for receiving notifications for item retraction, node deletion and purging.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wokkel/pubsub.py

    r11 r13  
    1313from zope.interface import implements
    1414
    15 from twisted.internet import defer
     15from twisted.internet import defer, reactor
    1616from twisted.words.protocols.jabber import jid, error, xmlstream
    1717from twisted.words.xish import domish
     
    240240
    241241    def connectionInitialized(self):
    242         self.xmlstream.addObserver('/message/event[@xmlns="%s"]/items' %
    243                                    NS_PUBSUB_EVENT, self._onItems)
    244 
    245     def _onItems(self, message):
     242        self.xmlstream.addObserver('/message/event[@xmlns="%s"]' %
     243                                   NS_PUBSUB_EVENT, self._onEvent)
     244
     245    def _onEvent(self, message):
    246246        try:
    247247            service = jid.JID(message["from"])
    248248            recipient = jid.JID(message["to"])
    249             nodeIdentifier = message.event.items["node"]
    250249        except KeyError:
    251250            return
    252251
    253         items = [element for element in message.event.items.elements()
    254                          if element.name == 'item']
     252        for element in message.event.elements():
     253            if element.uri == NS_PUBSUB_EVENT:
     254                actionElement = element
     255
     256        if not actionElement:
     257            return
     258
     259        eventHandler = getattr(self, "_onEvent_%s" % actionElement.name, None)
     260
     261        if eventHandler:
     262            eventHandler(service, recipient, actionElement)
     263            message.handled = True
     264
     265    def _onEvent_items(self, service, recipient, action):
     266        nodeIdentifier = action["node"]
     267
     268        items = [element for element in action.elements()
     269                         if element.name in ('item', 'retract')]
    255270
    256271        self.itemsReceived(recipient, service, nodeIdentifier, items)
    257272
     273    def _onEvent_delete(self, service, recipient, action):
     274        nodeIdentifier = action["node"]
     275        self.deleteReceived(recipient, service, nodeIdentifier)
     276
     277    def _onEvent_purge(self, service, recipient, action):
     278        nodeIdentifier = action["node"]
     279        self.purgeReceived(recipient, service, nodeIdentifier)
     280
    258281    def itemsReceived(self, recipient, service, nodeIdentifier, items):
     282        pass
     283
     284    def deleteReceived(self, recipient, service, nodeIdentifier):
     285        pass
     286
     287    def purgeReceived(self, recipient, service, nodeIdentifier):
    259288        pass
    260289
Note: See TracChangeset for help on using the changeset viewer.