# HG changeset patch # User Ralph Meijer # Date 1088195474 0 # Node ID 9701df89c5344d5fbdddda23d7bf55cbf897865c # Parent 4cc41776b7d748cde18b4e38c91f4972e377ae52 First take at notifications diff -r 4cc41776b7d7 -r 9701df89c534 idavoll/backend.py --- a/idavoll/backend.py Wed Jun 23 14:31:49 2004 +0000 +++ b/idavoll/backend.py Fri Jun 25 20:31:14 2004 +0000 @@ -5,6 +5,9 @@ class IBackendService(components.Interface): """ Interface to a backend service of a pubsub service """ + def do_publish(self, node, publisher, item): + """ Returns a deferred that returns """ + class BackendException(Exception): def __init__(self, msg = ''): self.msg = msg @@ -26,8 +29,8 @@ def __init__(self): self.nodes = {"ralphm/test": 'test'} - self.subscribers = {"ralphm/test": ["ralphm@ik.nu", "intosi@ik.nu"] } - self.affiliations = {"ralphm/test": { "ralphm@ik.nu": "owner", "ralphm@se-135.se.wtb.tue.nl": 'publisher' } } + self.subscribers = {"ralphm/test": ["ralphm@ik.nu", "ralphm@doe.ik.nu"] } + self.affiliations = {"ralphm/test": { "ralphm@ik.nu": "owner", "ralphm@se-135.se.wtb.tue.nl": 'publisher', 'ralphm@doe.ik.nu': 'publisher' } } def do_publish(self, node, publisher, item): try: @@ -42,12 +45,21 @@ raise NotAuthorized except KeyError: raise NotAuthorized() + print "publish by %s to %s" % (publisher, node) + + recipients = self.get_subscribers(node) + recipients.addCallback(self.magic_filter, node, item) + recipients.addCallback(self.pubsub_service.do_notification, node, item) + return defer.succeed(result) except: f = failure.Failure() return defer.fail(f) + def magic_filter(self, subscribers, node, item): + return subscribers + def get_subscribers(self, node): d = defer.Deferred() try: diff -r 4cc41776b7d7 -r 9701df89c534 idavoll/pubsub.py --- a/idavoll/pubsub.py Wed Jun 23 14:31:49 2004 +0000 +++ b/idavoll/pubsub.py Fri Jun 25 20:31:14 2004 +0000 @@ -1,14 +1,18 @@ from twisted.protocols.jabber import component,jid -from twisted.xish import utility +from twisted.xish import utility, domish from twisted.python import components import backend import xmpp_error +NS_COMPONENT = 'jabber:component:accept' +NS_PUBSUB = 'http://jabber.org/protocol/pubsub' +NS_PUBSUB_EVENT = NS_PUBSUB + '#event' + IQ_GET = '/iq[@type="get"]' IQ_SET = '/iq[@type="set"]' -PUBSUB_GET = IQ_GET + '/pubsub[@xmlns="http://jabber.org/protocol/pubsub"]' -PUBSUB_SET = IQ_SET + '/pubsub[@xmlns="http://jabber.org/protocol/pubsub"]' - +PUBSUB_ELEMENT = '/pubsub[@xmlns="' + NS_PUBSUB + '"]' +PUBSUB_GET = IQ_GET + PUBSUB_ELEMENT +PUBSUB_SET = IQ_SET + PUBSUB_ELEMENT PUBSUB_CREATE = PUBSUB_SET + '/create' PUBSUB_PUBLISH = PUBSUB_SET + '/publish' @@ -17,6 +21,7 @@ def __init__(self, backend): utility.EventDispatcher.__init__(self) self.backend = backend + self.backend.pubsub_service = self self.addObserver(PUBSUB_PUBLISH, self.onPublish) def componentConnected(self, xmlstream): @@ -52,7 +57,21 @@ d.addErrback(self.error, iq) d.addCallback(self.send) + def do_notification(self, recipients, node, item): + for recipient in recipients: + self.notify(node, item, recipient) + + def notify(self, node, item, recipient): + message = domish.Element((NS_COMPONENT, "message")) + message["from"] = self.parent.jabberId + message["to"] = recipient + x = message.addElement((NS_PUBSUB_EVENT, "x"), NS_PUBSUB_EVENT) + items = x.addElement("items") + items["node"] = node + items.children.append(item) + self.send(message) + """ def onCreateSet(self, iq): node = iq.pubsub.create["node"]