Use the result of Sync to delete invalid numbers

This commit is contained in:
moyamo 2016-01-06 17:26:59 +02:00
parent e32365a065
commit 2895f78ec7
2 changed files with 31 additions and 14 deletions

View file

@ -60,7 +60,6 @@ class BuddyList(dict):
self.session = session
self.user = user
self.logger = logging.getLogger(self.__class__.__name__)
self.synced = False
def _load(self, buddies):
for buddy in buddies:
@ -77,20 +76,32 @@ class BuddyList(dict):
contacts = self.keys()
contacts.remove('bot')
if self.synced == False:
self.session.sendSync(contacts, delta = False, interactive = True)
self.synced = True
self.session.sendSync(contacts, delta=False, interactive=True,
success=self.onSync)
self.logger.debug("Roster add: %s", str(list(contacts)))
for number in contacts:
buddy = self[number]
self.backend.handleBuddyChanged(self.user, number, buddy.nick,
buddy.groups, protocol_pb2.STATUS_NONE,
iconHash = buddy.image_hash if buddy.image_hash is not None else "")
self.updateSpectrum(buddy)
def onSync(self, existing, nonexisting, invalid):
"""We should only presence subscribe to existing numbers"""
for number in existing:
self.session.subscribePresence(number)
self.logger.debug("%s is requesting statuses of: %s", self.user, contacts)
self.session.requestStatuses(contacts, success = self.onStatus)
self.logger.debug("%s is requesting statuses of: %s", self.user, existing)
self.session.requestStatuses(existing, success = self.onStatus)
self.logger.debug("Removing nonexisting buddies %s", nonexisting)
for number in nonexisting:
self.remove(number)
del self[number]
self.logger.debug("Removing invalid buddies %s", invalid)
for number in invalid:
self.remove(number)
del self[number]
def onStatus(self, contacts):
self.logger.debug("%s received statuses of: %s", self.user, contacts)

View file

@ -271,12 +271,12 @@ class YowsupApp(object):
ChatstateProtocolEntity.STATE_PAUSED, jid
)
self.sendEntity(state)
def sendSync(self, contacts, delta = False, interactive = True):
def sendSync(self, contacts, delta = False, interactive = True, success = None, failure = None):
"""
You need to sync new contacts before you interact with
them, failure to do so could result in a temporary ban.
Args:
- contacts: ([str]) a list of phone numbers of the
contacts you wish to sync
@ -285,13 +285,19 @@ class YowsupApp(object):
contact list.
- interactive: (bool; default: True) Set to false if you are
sure this is the first time registering
- success: (func) - Callback; Takes three arguments: existing numbers,
non-existing numbers, invalid numbers.
"""
# TODO: Implement callbacks
mode = GetSyncIqProtocolEntity.MODE_DELTA if delta else GetSyncIqProtocolEntity.MODE_FULL
context = GetSyncIqProtocolEntity.CONTEXT_INTERACTIVE if interactive else GetSyncIqProtocolEntity.CONTEXT_REGISTRATION
iq = GetSyncIqProtocolEntity(contacts, mode, context)
self.sendIq(iq)
def onSuccess(response, request):
success(response.inNumbers.keys(), response.outNumbers.keys(), response.invalidNumbers)
self.sendIq(iq, onSuccess = onSuccess, onError = failure)
def requestStatuses(self, contacts, success = None, failure = None):
"""
Request the statuses of a number of users.