From 2c2c0fde8d9fe419098d58c528705afbd35978a1 Mon Sep 17 00:00:00 2001 From: moyamo Date: Thu, 7 Jan 2016 23:04:20 +0200 Subject: [PATCH] Fix DeletePictureNotification --- buddy.py | 11 ++++++++++- deferred.py | 8 +++++++- yowsupwrapper.py | 5 +++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/buddy.py b/buddy.py index 542f798..d8d3da7 100644 --- a/buddy.py +++ b/buddy.py @@ -178,7 +178,10 @@ class BuddyList(dict): # Get profile picture self.logger.debug('Requesting profile picture of %s', buddy) response = deferred.Deferred() - self.session.requestProfilePicture(buddy, onSuccess = response.run) + # Error probably means image doesn't exist + error = deferred.Deferred() + self.session.requestProfilePicture(buddy, onSuccess=response.run, + onFailure=error.run) response = response.arg(0) pictureData = response.pictureData() @@ -188,6 +191,9 @@ class BuddyList(dict): ID, response.pictureId(), pictureData.then(base64.b64encode)) call(self.backend.handleVCard, self.user, ID, buddy, "", "", pictureData) + # If error + error.when(self.logger.debug, 'Sending VCard (%s) without image', ID) + error.when(self.backend.handleVCard, self.user, ID, buddy, "", "", "") # Send image hash if not buddy == self.session.legacyName: @@ -201,6 +207,9 @@ class BuddyList(dict): image_hash = pictureData.then(utils.sha1hash) call(self.logger.debug, 'Image hash is %s', image_hash) call(self.update, buddy, nick, groups, image_hash) + # No image + error.when(self.logger.debug, 'No image') + error.when(self.update, buddy, nick, groups, '') def refresh(self, number): self.session.unsubscribePresence(number) diff --git a/deferred.py b/deferred.py index 3e86dcf..5ab3d02 100644 --- a/deferred.py +++ b/deferred.py @@ -67,6 +67,12 @@ class Deferred(object): return args[n] return self.then(helper) + def when(self, func, *args, **kwargs): + """ Calls when func(*args, **kwargs) when deferred gets a value """ + def helper(*args2, **kwargs2): + func(*args, **kwargs) + self.then(helper) + def __getattr__(self, method_name): return getattr(Then(self), method_name) @@ -103,7 +109,7 @@ def call(func, *args, **kwargs): colors = Deferred() colors.append('blue') colors.run(['red', 'green']) -call(print, colors) #=> ['red', 'green', 'blue'] + call(print, colors) #=> ['red', 'green', 'blue'] call(print, 'hi', colors) #=> hi ['red', 'green', 'blue'] """ for i, c in enumerate(args): diff --git a/yowsupwrapper.py b/yowsupwrapper.py index 7d195a6..4cb8787 100644 --- a/yowsupwrapper.py +++ b/yowsupwrapper.py @@ -766,9 +766,10 @@ class YowsupAppLayer(YowInterfaceLayer): entity._from.split('@')[0], entity.status ) - elif (isinstance(entity, SetPictureNotificationProtocolEntity) or - isinstance(entity, DeletePictureNotificationProtocolEntity)): + elif isinstance(entity, SetPictureNotificationProtocolEntity): self.caller.onContactPictureChanged(entity.setJid.split('@')[0]) + elif isinstance(entity, DeletePictureNotificationProtocolEntity): + self.caller.onContactPictureChanged(entity.deleteJid.split('@')[0]) elif isinstance(entity, RemoveContactNotificationProtocolEntity): self.caller.onContactRemoved(entity.contactJid.split('@')[0]) elif isinstance(entity, AddContactNotificationProtocolEntity):