mirror of
https://github.com/correl/SleekXMPP.git
synced 2024-11-27 19:19:54 +00:00
Fix detecting end of result set paging.
This commit is contained in:
parent
bb3080e829
commit
b25668b5b7
2 changed files with 51 additions and 25 deletions
|
@ -6,7 +6,7 @@
|
||||||
See the file LICENSE for copying permission.
|
See the file LICENSE for copying permission.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from sleekxmpp.xmlstream import ElementBase, ET
|
from sleekxmpp.xmlstream import ElementBase, ET, register_stanza_plugin
|
||||||
|
|
||||||
|
|
||||||
class DiscoItems(ElementBase):
|
class DiscoItems(ElementBase):
|
||||||
|
@ -78,13 +78,11 @@ class DiscoItems(ElementBase):
|
||||||
"""
|
"""
|
||||||
if (jid, node) not in self._items:
|
if (jid, node) not in self._items:
|
||||||
self._items.add((jid, node))
|
self._items.add((jid, node))
|
||||||
item_xml = ET.Element('{%s}item' % self.namespace)
|
item = DiscoItem(parent=self)
|
||||||
item_xml.attrib['jid'] = jid
|
item['jid'] = jid
|
||||||
if name:
|
item['node'] = node
|
||||||
item_xml.attrib['name'] = name
|
item['name'] = name
|
||||||
if node:
|
self.iterables.append(item)
|
||||||
item_xml.attrib['node'] = node
|
|
||||||
self.xml.append(item_xml)
|
|
||||||
return True
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -108,11 +106,9 @@ class DiscoItems(ElementBase):
|
||||||
def get_items(self):
|
def get_items(self):
|
||||||
"""Return all items."""
|
"""Return all items."""
|
||||||
items = set()
|
items = set()
|
||||||
for item_xml in self.findall('{%s}item' % self.namespace):
|
for item in self['substanzas']:
|
||||||
item = (item_xml.attrib['jid'],
|
if isinstance(item, DiscoItem):
|
||||||
item_xml.attrib.get('node'),
|
items.add((item['jid'], item['node'], item['name']))
|
||||||
item_xml.attrib.get('name'))
|
|
||||||
items.add(item)
|
|
||||||
return items
|
return items
|
||||||
|
|
||||||
def set_items(self, items):
|
def set_items(self, items):
|
||||||
|
@ -132,5 +128,23 @@ class DiscoItems(ElementBase):
|
||||||
def del_items(self):
|
def del_items(self):
|
||||||
"""Remove all items."""
|
"""Remove all items."""
|
||||||
self._items = set()
|
self._items = set()
|
||||||
for item_xml in self.findall('{%s}item' % self.namespace):
|
for item in self['substanzas']:
|
||||||
self.xml.remove(item_xml)
|
if isinstance(item, DiscoItem):
|
||||||
|
self.xml.remove(item.xml)
|
||||||
|
|
||||||
|
|
||||||
|
class DiscoItem(ElementBase):
|
||||||
|
name = 'item'
|
||||||
|
namespace = 'http://jabber.org/protocol/disco#items'
|
||||||
|
plugin_attrib = name
|
||||||
|
interfaces = set(('jid', 'node', 'name'))
|
||||||
|
|
||||||
|
def get_node(self):
|
||||||
|
return self._get_attr('node', None)
|
||||||
|
|
||||||
|
def get_name(self):
|
||||||
|
return self._get_attr('name', None)
|
||||||
|
|
||||||
|
|
||||||
|
register_stanza_plugin(DiscoItems, DiscoItem, iterable=True)
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ from sleekxmpp import Iq
|
||||||
from sleekxmpp.plugins.base import base_plugin
|
from sleekxmpp.plugins.base import base_plugin
|
||||||
from sleekxmpp.xmlstream import register_stanza_plugin
|
from sleekxmpp.xmlstream import register_stanza_plugin
|
||||||
from sleekxmpp.plugins.xep_0059 import Set
|
from sleekxmpp.plugins.xep_0059 import Set
|
||||||
|
from sleekxmpp.exceptions import XMPPError
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
@ -70,19 +71,30 @@ class ResultIterator():
|
||||||
elif self.start:
|
elif self.start:
|
||||||
self.query[self.interface]['rsm']['after'] = self.start
|
self.query[self.interface]['rsm']['after'] = self.start
|
||||||
|
|
||||||
r = self.query.send(block=True)
|
try:
|
||||||
|
r = self.query.send(block=True)
|
||||||
|
|
||||||
if not r or not r[self.interface]['rsm']['first'] and \
|
if not r[self.interface]['rsm']['first'] and \
|
||||||
not r[self.interface]['rsm']['last']:
|
not r[self.interface]['rsm']['last']:
|
||||||
|
raise StopIteration
|
||||||
|
|
||||||
|
if r[self.interface]['rsm']['count'] and \
|
||||||
|
r[self.interface]['rsm']['first_index']:
|
||||||
|
count = int(r[self.interface]['rsm']['count'])
|
||||||
|
first = int(r[self.interface]['rsm']['first_index'])
|
||||||
|
num_items = len(r[self.interface]['substanzas'])
|
||||||
|
if first + num_items == count:
|
||||||
|
raise StopIteration
|
||||||
|
|
||||||
|
if self.reverse:
|
||||||
|
self.start = r[self.interface]['rsm']['first']
|
||||||
|
else:
|
||||||
|
self.start = r[self.interface]['rsm']['last']
|
||||||
|
|
||||||
|
return r
|
||||||
|
except XMPPError:
|
||||||
raise StopIteration
|
raise StopIteration
|
||||||
|
|
||||||
if self.reverse:
|
|
||||||
self.start = r[self.interface]['rsm']['first']
|
|
||||||
else:
|
|
||||||
self.start = r[self.interface]['rsm']['last']
|
|
||||||
|
|
||||||
return r
|
|
||||||
|
|
||||||
|
|
||||||
class xep_0059(base_plugin):
|
class xep_0059(base_plugin):
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue