Mercurial > libervia-pubsub
changeset 146:b4490bdc77e5
Change semantics of Node.is_subscribed() to match all subscriptions for an
entity (all resources).
author | Ralph Meijer <ralphm@ik.nu> |
---|---|
date | Fri, 29 Jul 2005 15:33:43 +0000 |
parents | f393bccec4bc |
children | fee92e499d6d |
files | idavoll/memory_storage.py idavoll/pgsql_storage.py idavoll/storage.py idavoll/test/test_storage.py |
diffstat | 4 files changed, 34 insertions(+), 28 deletions(-) [+] |
line wrap: on
line diff
--- a/idavoll/memory_storage.py Thu Jul 14 20:51:48 2005 +0000 +++ b/idavoll/memory_storage.py Fri Jul 29 15:33:43 2005 +0000 @@ -128,13 +128,13 @@ return defer.succeed(subscribers) - def is_subscribed(self, subscriber): - try: - subscription = self._subscriptions[subscriber.full()] - except KeyError: - return defer.succeed(False) - - return defer.succeed(subscription.state == 'subscribed') + def is_subscribed(self, entity): + for subscriber, subscription in self._subscriptions.iteritems(): + if jid.JID(subscriber).userhost() == entity.userhost() and \ + subscription.state == 'subscribed': + return defer.succeed(True) + + return defer.succeed(False) def get_affiliations(self): affiliations = [(jid.JID(entity), affiliation) for entity, affiliation @@ -142,13 +142,11 @@ return defer.succeed(affiliations) -class LeafNode(Node): +class LeafNodeMixin: - implements(storage.ILeafNode) type = 'leaf' - def __init__(self, node_id, owner, config): - Node.__init__(self, node_id, owner, config) + def __init__(self): self._items = {} self._itemlist = [] @@ -205,6 +203,14 @@ return defer.succeed(None) +class LeafNode(Node, LeafNodeMixin): + + implements(storage.ILeafNode) + + def __init__(self, node_id, owner, config): + Node.__init__(self, node_id, owner, config) + LeafNodeMixin.__init__(self) + class Subscription: implements(storage.ISubscription)
--- a/idavoll/pgsql_storage.py Thu Jul 14 20:51:48 2005 +0000 +++ b/idavoll/pgsql_storage.py Fri Jul 29 15:33:43 2005 +0000 @@ -258,24 +258,20 @@ def _convert_to_jids(self, list): return [jid.JID("%s/%s" % (l[0], l[1])) for l in list] - def is_subscribed(self, subscriber): - return self._dbpool.runInteraction(self._is_subscribed, subscriber) + def is_subscribed(self, entity): + return self._dbpool.runInteraction(self._is_subscribed, entity) - def _is_subscribed(self, cursor, subscriber): + def _is_subscribed(self, cursor, entity): self._check_node_exists(cursor) - userhost = subscriber.userhost() - resource = subscriber.resource or '' - cursor.execute("""SELECT 1 FROM entities JOIN subscriptions ON (entities.id=subscriptions.entity_id) JOIN nodes ON (nodes.id=subscriptions.node_id) - WHERE entities.jid=%s AND resource=%s + WHERE entities.jid=%s AND node=%s AND subscription='subscribed'""", - (userhost, - resource, + (entity.userhost(), self.id)) return cursor.fetchone() is not None @@ -297,9 +293,7 @@ return [(jid.JID(r[0]), r[1]) for r in result] -class LeafNode(Node): - - implements(storage.ILeafNode) +class LeafNodeMixin: type = 'leaf' @@ -395,3 +389,7 @@ cursor.execute("""DELETE FROM items WHERE node_id=(SELECT id FROM nodes WHERE node=%s)""", (self.id,)) + +class LeafNode(Node, LeafNodeMixin): + + implements(storage.ILeafNode)
--- a/idavoll/storage.py Thu Jul 14 20:51:48 2005 +0000 +++ b/idavoll/storage.py Fri Jul 29 15:33:43 2005 +0000 @@ -169,13 +169,13 @@ @return: a deferred that returns a L{list} of L{jid.JID}s. """ - def is_subscribed(self, subscriber): - """ Returns whether subscriber has a subscription to this node. + def is_subscribed(self, entity): + """ Returns whether entity has any subscription to this node. Only returns C{True} when the subscription state (if present) is - C{'subscribed'}. + C{'subscribed'} for any subscription that matches the bare JID. - @param subscriber: JID of the subscriptions' entity. + @param subscriber: bare JID of the subscriptions' entity. @type subscriber: L{jid.JID} @return: deferred that returns a L{bool}. """
--- a/idavoll/test/test_storage.py Thu Jul 14 20:51:48 2005 +0000 +++ b/idavoll/test/test_storage.py Fri Jul 29 15:33:43 2005 +0000 @@ -211,10 +211,12 @@ def testIsSubscriber(self): def cb(subscribed): assertEquals(subscribed[0][1], True) - assertEquals(subscribed[1][1], False) + assertEquals(subscribed[1][1], True) assertEquals(subscribed[2][1], False) + assertEquals(subscribed[3][1], False) d = defer.DeferredList([self.node.is_subscribed(SUBSCRIBER), + self.node.is_subscribed(SUBSCRIBER.userhostJID()), self.node.is_subscribed(SUBSCRIBER_PENDING), self.node.is_subscribed(OWNER)]) d.addCallback(cb)