changeset 59:0fa161c00ed9

Use jid.JIDs everywhere in the backend. Fix subscription list retrieval in pgsql_backend, thereby actually making notifications work.
author Ralph Meijer <ralphm@ik.nu>
date Sat, 06 Nov 2004 17:07:47 +0000
parents 3e2e0040e3e0
children f6b7a06b8870
files idavoll/backend.py idavoll/memory_backend.py idavoll/pgsql_backend.py
diffstat 3 files changed, 32 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/idavoll/backend.py	Sat Nov 06 16:02:32 2004 +0000
+++ b/idavoll/backend.py	Sat Nov 06 17:07:47 2004 +0000
@@ -80,7 +80,7 @@
         C{node_id}. The C{subscriber} might be different from the C{requestor},
         and if the C{requestor} is not allowed to subscribe this entity an
         exception should be raised.
-    
+
         @return: a deferred that returns the subscription state
         """
 
@@ -156,7 +156,7 @@
 
     def publish(self, node_id, items, requestor):
         d1 = self.parent.storage.get_node_configuration(node_id)
-        d2 = self.parent.storage.get_affiliation(node_id, requestor.full())
+        d2 = self.parent.storage.get_affiliation(node_id, requestor)
         d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
         d.addErrback(lambda x: x.value[0])
         d.addCallback(self._do_publish, node_id, items, requestor)
@@ -185,7 +185,7 @@
 
         if persist_items:
             d = self.parent.storage.store_items(node_id, items,
-                                                requestor.full())
+                                                requestor)
         else:
             d = defer.succeed(None)
 
@@ -212,7 +212,6 @@
         list = {}
         for subscriber in subscribers:
             list[subscriber] = items
-
         return list
 
     def register_notifier(self, observerfn, *args, **kwargs):
@@ -228,7 +227,7 @@
             raise NotAuthorized
 
         d1 = self.parent.storage.get_node_configuration(node_id)
-        d2 = self.parent.storage.get_affiliation(node_id, subscriber.full())
+        d2 = self.parent.storage.get_affiliation(node_id, subscriber)
         d = defer.DeferredList([d1, d2], fireOnOneErrback=1)
         d.addErrback(lambda x: x.value[0])
         d.addCallback(self._do_subscribe, node_id, subscriber)
@@ -241,14 +240,13 @@
         if affiliation == 'outcast':
             raise NotAuthorized
 
-        d = self.parent.storage.add_subscription(node_id, subscriber.full(),
+        d = self.parent.storage.add_subscription(node_id, subscriber,
                                                  'subscribed')
         d.addCallback(self._return_subscription, affiliation)
         return d
 
     def _return_subscription(self, result, affiliation):
         result['affiliation'] = affiliation
-        result['jid'] = jid.JID(result['jid'])
         return result
 
     def unsubscribe(self, node_id, subscriber, requestor):
@@ -261,7 +259,7 @@
 
     def _do_unsubscribe(self, result, node_id, subscriber):
         return self.parent.storage.remove_subscription(node_id,
-                                                       subscriber.full())
+                                                       subscriber)
 
 class NodeCreationService(service.Service):
 
@@ -274,6 +272,6 @@
         if not node_id:
             raise NoInstantNodes
 
-        d = self.parent.storage.create_node(node_id, requestor.full())
+        d = self.parent.storage.create_node(node_id, requestor)
         d.addCallback(lambda _: node_id)
         return d
--- a/idavoll/memory_backend.py	Sat Nov 06 16:02:32 2004 +0000
+++ b/idavoll/memory_backend.py	Sat Nov 06 17:07:47 2004 +0000
@@ -49,7 +49,7 @@
         except KeyError:
             raise backend.NodeNotFound
         else:
-            return defer.succeed(node.affiliations.get(entity, None))
+            return defer.succeed(node.affiliations.get(entity.full(), None))
 
     def get_subscribers(self, node_id):
         try:
@@ -58,7 +58,7 @@
             raise backend.NodeNotFound
         else:
             subscriptions = self.nodes[node_id].subscriptions
-            subscribers = [s for s in subscriptions
+            subscribers = [jid.JID(s) for s in subscriptions
                              if subscriptions[s].state == 'subscribed']
             return defer.succeed(subscribers)
 
@@ -74,10 +74,10 @@
             raise backend.NodeNotFound
 
         try:
-            subscription = node.subscriptions[subscriber]
+            subscription = node.subscriptions[subscriber.full()]
         except:
             subscription = Subscription(state)
-            node.subscriptions[subscriber] = subscription
+            node.subscriptions[subscriber.full()] = subscription
 
         return defer.succeed({'node': node_id,
                               'jid': subscriber,
@@ -90,7 +90,7 @@
             raise backend.NodeNotFound
 
         try:
-            del node.subscriptions[subscriber]
+            del node.subscriptions[subscriber.full()]
         except KeyError:
             raise backend.NotSubscribed
 
@@ -101,7 +101,7 @@
             raise backend.NodeExists
     
         node = Node(node_id)
-        node.affiliations[owner] = 'owner'
+        node.affiliations[owner.full()] = 'owner'
         self.nodes[node_id] = node
 
         return defer.succeed(None)
--- a/idavoll/pgsql_backend.py	Sat Nov 06 16:02:32 2004 +0000
+++ b/idavoll/pgsql_backend.py	Sat Nov 06 17:07:47 2004 +0000
@@ -38,7 +38,8 @@
                           JOIN nodes ON (node_id=nodes.id)
                           JOIN entities ON (entity_id=entities.id)
                           WHERE node=%s AND jid=%s""",
-                       (node_id.encode('utf8'), entity.encode('utf8')))
+                       (node_id.encode('utf8'),
+                        entity.full().encode('utf8')))
 
         try:
             return cursor.fetchone()[0]
@@ -50,16 +51,20 @@
                                           entity)
 
     def get_subscribers(self, node_id):
-        self._check_node_exists(cursor, node_id)
-        d = self.dbpool.runQuery("""SELECT jid, resource FROM subscriptions
-                                       JOIN nodes ON (node_id=nodes.id)
-                                       JOIN entities ON (entity_id=entities.id)
-                                       WHERE node=%s AND
-                                             subscription='subscribed'""",
-                                    (node_id.encode('utf8'),))
+        d = self.dbpool.runInteraction(self._get_subscribers, node_id)
         d.addCallback(self._convert_to_jids)
         return d
 
+    def _get_subscribers(self, cursor,node_id):
+        self._check_node_exists(cursor, node_id)
+        cursor.execute("""SELECT jid, resource FROM subscriptions
+                          JOIN nodes ON (node_id=nodes.id)
+                          JOIN entities ON (entity_id=entities.id)
+                          WHERE node=%s AND
+                          subscription='subscribed'""",
+                       (node_id.encode('utf8'),))
+        return cursor.fetchall()
+
     def _convert_to_jids(self, list):
         return [jid.JID("%s/%s" % (l[0], l[1])).full() for l in list]
 
@@ -78,7 +83,7 @@
                           FROM nodes
                           WHERE nodes.id = items.node_id AND
                                 nodes.node = %s and items.item=%s""",
-                       (publisher.encode('utf8'),
+                       (publisher.full().encode('utf8'),
                         data.encode('utf8'),
                         node_id.encode('utf8'),
                         item["id"].encode('utf8')))
@@ -88,7 +93,7 @@
         cursor.execute("""INSERT INTO items (node_id, item, publisher, data)
                           SELECT id, %s, %s, %s FROM nodes WHERE node=%s""",
                        (item["id"].encode('utf8'),
-                        publisher.encode('utf8'),
+                        publisher.full().encode('utf8'),
                         data.encode('utf8'),
                         node_id.encode('utf8')))
 
@@ -98,7 +103,6 @@
 
     def _add_subscription(self, cursor, node_id, subscriber, state):
         self._check_node_exists(cursor, node_id)
-        subscriber = jid.JID(subscriber)
         userhost = subscriber.userhost()
         resource = subscriber.resource or ''
 
@@ -131,7 +135,7 @@
             state = cursor.fetchone()[0]
 
         return {'node': node_id,
-                'jid': subscriber.full(),
+                'jid': subscriber,
                 'subscription': state}
 
     def remove_subscription(self, node_id, subscriber):
@@ -140,7 +144,6 @@
 
     def _remove_subscription(self, cursor, node_id, subscriber):
         self._check_node_exists(cursor, node_id)
-        subscriber = jid.JID(subscriber)
         userhost = subscriber.userhost()
         resource = subscriber.resource or ''
 
@@ -168,11 +171,11 @@
             raise backend.NodeExists
        
         cursor.execute("""SELECT 1 from entities where jid=%s""",
-                       (owner.encode('utf8')))
+                       (owner.full().encode('utf8')))
 
         if not cursor.fetchone():
             cursor.execute("""INSERT INTO entities (jid) VALUES (%s)""",
-                           (owner.encode('utf8')))
+                           (owner.full().encode('utf8')))
 
         try:
             cursor.execute("""INSERT INTO affiliations
@@ -182,7 +185,7 @@
                               CROSS JOIN
                               (SELECT id FROM entities WHERE jid=%s) AS e""",
                            (node_id.encode('utf8'),
-                            owner.encode('utf8')))
+                            owner.full().encode('utf8')))
         except Exception, e:
             print e