comparison idavoll/gateway.py @ 187:69cdd8c6a431

Make sure second subscribers through HTTP also get a notification of the last item.
author Ralph Meijer <ralphm@ik.nu>
date Thu, 17 Apr 2008 16:02:22 +0000
parents 365fd3e4daf8
children 48245777acea
comparison
equal deleted inserted replaced
186:365fd3e4daf8 187:69cdd8c6a431
368 def newCallbackList(result): 368 def newCallbackList(result):
369 callbackList = set() 369 callbackList = set()
370 self.callbacks[jid, nodeIdentifier] = callbackList 370 self.callbacks[jid, nodeIdentifier] = callbackList
371 return callbackList 371 return callbackList
372 372
373 def callbackForLastItem(items, callback):
374 atomEntries = extractAtomEntries(items)
375
376 if not atomEntries:
377 return
378
379 self._postTo([callback], jid, nodeIdentifier, atomEntries[0],
380 'application/atom+xml;type=entry')
381
373 try: 382 try:
374 callbackList = self.callbacks[jid, nodeIdentifier] 383 callbackList = self.callbacks[jid, nodeIdentifier]
375 except KeyError: 384 except KeyError:
376 d = self.subscribe(jid, nodeIdentifier, self.jid) 385 d = self.subscribe(jid, nodeIdentifier, self.jid)
377 d.addCallback(newCallbackList) 386 d.addCallback(newCallbackList)
378 else: 387 else:
379 d = defer.succeed(callbackList) 388 d = self.items(jid, nodeIdentifier, 1)
389 d.addCallback(callbackForLastItem, callback)
390 d.addCallback(lambda _: callbackList)
380 391
381 d.addCallback(lambda callbackList: callbackList.add(callback)) 392 d.addCallback(lambda callbackList: callbackList.add(callback))
382 d.addErrback(self.trapNotFound) 393 d.addErrback(self.trapNotFound)
383 return d 394 return d
384 395
413 else: 424 else:
414 contentType = 'application/atom+xml;type=feed' 425 contentType = 'application/atom+xml;type=feed'
415 payload = constructFeed(service, nodeIdentifier, atomEntries, 426 payload = constructFeed(service, nodeIdentifier, atomEntries,
416 title='Received item collection') 427 title='Received item collection')
417 428
418 self.callCallbacks(recipient, service, nodeIdentifier, payload, 429 self.callCallbacks(service, nodeIdentifier, payload, contentType)
419 contentType)
420 430
421 431
422 def deleteReceived(self, recipient, service, nodeIdentifier): 432 def deleteReceived(self, recipient, service, nodeIdentifier):
423 """ 433 """
424 Fire up HTTP client to do callback 434 Fire up HTTP client to do callback
425 """ 435 """
426 436
427 self.callCallbacks(recipient, service, nodeIdentifier, 437 self.callCallbacks(service, nodeIdentifier, eventType='DELETED')
428 eventType='DELETED') 438
429 439
430 440 def _postTo(self, callbacks, service, nodeIdentifier,
431 def callCallbacks(self, recipient, service, nodeIdentifier, 441 payload=None, contentType=None, eventType=None):
432 payload=None, contentType=None, eventType=None):
433 try:
434 callbacks = self.callbacks[service, nodeIdentifier]
435 except KeyError:
436 return
437
438 postdata = None 442 postdata = None
439 nodeURI = 'xmpp:%s?;node=%s' % (service.full(), nodeIdentifier) 443 nodeURI = 'xmpp:%s?;node=%s' % (service.full(), nodeIdentifier)
440 headers = {'Referer': nodeURI.encode('utf-8'), 444 headers = {'Referer': nodeURI.encode('utf-8'),
441 'PubSub-Service': service.full().encode('utf-8')} 445 'PubSub-Service': service.full().encode('utf-8')}
442 446
455 headers=headers) 459 headers=headers)
456 d.addErrback(log.err) 460 d.addErrback(log.err)
457 461
458 for callbackURI in callbacks: 462 for callbackURI in callbacks:
459 reactor.callLater(0, postNotification, callbackURI) 463 reactor.callLater(0, postNotification, callbackURI)
464
465 def callCallbacks(self, service, nodeIdentifier,
466 payload=None, contentType=None, eventType=None):
467 try:
468 callbacks = self.callbacks[service, nodeIdentifier]
469 except KeyError:
470 return
471
472 self._postTo(callbacks, service, nodeIdentifier, payload, contentType,
473 eventType)
474
460 475
461 476
462 477
463 class RemoteSubscribeBaseResource(resource.Resource): 478 class RemoteSubscribeBaseResource(resource.Resource):
464 """ 479 """