diff idavoll/backend.py @ 178:07114105885a

Send last published item on subscription if node is so configured.
author Ralph Meijer <ralphm@ik.nu>
date Thu, 10 Apr 2008 14:02:53 +0000
parents 79d451d186b1
children 42e23a62b57f
line wrap: on
line diff
--- a/idavoll/backend.py	Thu Apr 10 11:18:29 2008 +0000
+++ b/idavoll/backend.py	Thu Apr 10 14:02:53 2008 +0000
@@ -159,6 +159,7 @@
             raise error.Forbidden()
 
         d = node.add_subscription(subscriber, 'subscribed')
+        d.addCallback(lambda _: self._send_last_published(node, subscriber))
         d.addCallback(lambda _: 'subscribed')
         d.addErrback(self._get_subscription, node, subscriber)
         d.addCallback(self._return_subscription, node.id)
@@ -171,6 +172,23 @@
     def _return_subscription(self, result, node_id):
         return node_id, result
 
+    def _send_last_published(self, node, subscriber):
+        def notify_item(items):
+            if not items:
+                return
+
+            self.dispatch({'items': items,
+                           'node_id': node.id,
+                           'subscriber': subscriber},
+                          '//event/pubsub/notify')
+
+        config = node.get_configuration()
+        if config["pubsub#send_last_published_item"] != 'on_sub':
+            return
+
+        d = self.get_items(node.id, subscriber.userhostJID(), 1)
+        d.addCallback(notify_item)
+
     def unsubscribe(self, node_id, subscriber, requestor):
         if subscriber.userhostJID() != requestor:
             return defer.fail(error.Forbidden())
@@ -422,7 +440,10 @@
     def _notify(self, data):
         items = data['items']
         nodeIdentifier = data['node_id']
-        d = self.backend.get_notification_list(nodeIdentifier, items)
+        if 'subscriber' not in data:
+            d = self.backend.get_notification_list(nodeIdentifier, items)
+        else:
+            d = defer.succeed([(data['subscriber'], items)])
         d.addCallback(lambda notifications: self.notifyPublish(self.serviceJID,
                                                                nodeIdentifier,
                                                                notifications))