comparison sat_pubsub/pgsql_storage.py @ 357:1167e48e5f52

handle single node on subscriptions request
author Goffi <goffi@goffi.org>
date Fri, 08 Sep 2017 08:02:05 +0200
parents 95c83899b5e9
children 8bd8be6815ab
comparison
equal deleted inserted replaced
356:95c83899b5e9 357:1167e48e5f52
344 344
345 cursor.execute(*withPEP(' '.join(query), args, pep, recipient)) 345 cursor.execute(*withPEP(' '.join(query), args, pep, recipient))
346 rows = cursor.fetchall() 346 rows = cursor.fetchall()
347 return [tuple(r) for r in rows] 347 return [tuple(r) for r in rows]
348 348
349 def getSubscriptions(self, entity, pep, recipient=None): 349 def getSubscriptions(self, entity, nodeIdentifier=None, pep=False, recipient=None):
350 """retrieve subscriptions of an entity
351
352 @param entity(jid.JID): entity to check
353 @param nodeIdentifier(unicode, None): node identifier
354 None to retrieve all subscriptions
355 @param pep: True if we are in PEP mode
356 @param recipient: jid of the recipient
357 """
358
350 def toSubscriptions(rows): 359 def toSubscriptions(rows):
351 subscriptions = [] 360 subscriptions = []
352 for row in rows: 361 for row in rows:
353 subscriber = jid.internJID('%s/%s' % (row[1], 362 subscriber = jid.internJID('%s/%s' % (row.jid,
354 row[2])) 363 row.resource))
355 subscription = Subscription(row[0], subscriber, row[3]) 364 subscription = Subscription(row.node, subscriber, row.state)
356 subscriptions.append(subscription) 365 subscriptions.append(subscription)
357 return subscriptions 366 return subscriptions
358 367
359 d = self.dbpool.runQuery("""SELECT node, jid, resource, state 368 query = ["""SELECT node,
360 FROM entities 369 jid,
361 NATURAL JOIN subscriptions 370 resource,
362 NATURAL JOIN nodes 371 state
363 WHERE jid=%s AND nodes.pep=%s""", 372 FROM entities
364 (entity.userhost(), recipient.userhost() if pep else None)) 373 NATURAL JOIN subscriptions
374 NATURAL JOIN nodes
375 WHERE jid=%s"""]
376
377 args = [entity.userhost()]
378
379 if nodeIdentifier is not None:
380 query.append("AND node=%s")
381 args.append(nodeIdentifier)
382
383 d = self.dbpool.runQuery(*withPEP(' '.join(query), args, pep, recipient))
365 d.addCallback(toSubscriptions) 384 d.addCallback(toSubscriptions)
366 return d 385 return d
367 386
368 def getDefaultConfiguration(self, nodeType): 387 def getDefaultConfiguration(self, nodeType):
369 return self.defaultConfig[nodeType] 388 return self.defaultConfig[nodeType]
543 cursor.execute(query, values) 562 cursor.execute(query, values)
544 rows = cursor.fetchall() 563 rows = cursor.fetchall()
545 564
546 subscriptions = [] 565 subscriptions = []
547 for row in rows: 566 for row in rows:
548 subscriber = jid.JID(u'%s/%s' % (row[1], row[2])) 567 subscriber = jid.JID(u'%s/%s' % (row.jid, row.resource))
549 568
550 options = {} 569 options = {}
551 if row[4]: 570 if row.subscription_type:
552 options['pubsub#subscription_type'] = row[4]; 571 options['pubsub#subscription_type'] = row.subscription_type;
553 if row[5]: 572 if row.subscription_depth:
554 options['pubsub#subscription_depth'] = row[5]; 573 options['pubsub#subscription_depth'] = row.subscription_depth;
555 574
556 subscriptions.append(Subscription(row[0], subscriber, 575 subscriptions.append(Subscription(row.node, subscriber,
557 row[3], options)) 576 row.state, options))
558 577
559 return subscriptions 578 return subscriptions
560 579
561 def addSubscription(self, subscriber, state, config): 580 def addSubscription(self, subscriber, state, config):
562 return self.dbpool.runInteraction(self._addSubscription, subscriber, 581 return self.dbpool.runInteraction(self._addSubscription, subscriber,