Update buddies when contact notification is received

This commit is contained in:
moyamo 2016-01-07 12:27:40 +02:00
parent 59f0af9f8d
commit f57b276665
3 changed files with 71 additions and 0 deletions

View file

@ -201,3 +201,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)
def refresh(self, number):
self.session.unsubscribePresence(number)
self.session.subscribePresence(number)
self.requestVCard(number)
self.session.requestStatuses([number], success = self.onStatus)

View file

@ -462,6 +462,33 @@ class Session(YowsupApp):
self.logger.debug("%s changed their profile picture", number)
self.buddies.requestVCard(number)
# Called by superclass
def onContactAdded(self, number, nick):
self.logger.debug("Adding new contact %s (%s)", nick, number)
self.updateBuddy(number, nick, [])
# Called by superclass
def onContactRemoved(self, number):
self.logger.debug("Removing contact %s", number)
self.removeBuddy(number)
def onContactUpdated(self, oldnumber, newnumber):
self.logger.debug("Contact has changed number from %s to %s",
oldnumber, newnumber)
if newnumber in self.buddies:
self.logger.warn("Contact %s exists, just updating", newnumber)
self.buddies.refresh(newnumber)
try:
buddy = self.buddies[oldnumber]
except KeyError:
self.logger.warn("Old contact (%s) not found. Adding new contact (%s)",
oldnumber, newnumber)
nick = ""
else:
self.removeBuddy(buddy.number)
nick = buddy.nick
self.updateBuddy(newnumber, nick, [])
def onPresenceReceived(self, _type, name, jid, lastseen):
self.logger.info("Presence received: %s %s %s %s", _type, name, jid, lastseen)
buddy = jid.split("@")[0]

View file

@ -562,6 +562,32 @@ class YowsupApp(object):
"""
pass
def onContactRemoved(self, number):
"""Called when a contact has been removed
Args:
number: (str) the number of the contact who has been removed
"""
pass
def onContactAdded(self, number, nick):
"""Called when a contact has been added
Args:
number: (str) contacts number
nick: (str) contacts nickname
"""
pass
def onContactUpdated(self, oldNumber, newNumber):
"""Called when a contact has changed their number
Args:
oldNumber: (str) the number the contact previously used
newNumber: (str) the new number of the contact
"""
pass
def sendEntity(self, entity):
"""Sends an entity down the stack (as if YowsupAppLayer called toLower)"""
self.stack.broadcastEvent(YowLayerEvent(YowsupAppLayer.TO_LOWER_EVENT,
@ -676,6 +702,18 @@ class YowsupAppLayer(YowInterfaceLayer):
elif (isinstance(entity, SetPictureNotificationProtocolEntity) or
isinstance(entity, DeletePictureNotificationProtocolEntity)):
self.caller.onContactPictureChanged(entity.setJid.split('@')[0])
elif isinstance(entity, RemoveContactNotificationProtocolEntity):
self.caller.onContactRemoved(entity.contactJid.split('@')[0])
elif isinstance(entity, AddContactNotificationProtocolEntity):
self.caller.onContactAdded(
entity.contactJid.split('@')[0],
entity.notify
)
elif isinstance(entity, UpdateContactNotificationProtocolEntity):
self.caller.onContactUpdated(
entity._from.split('@')[0],
entity.contactJid.split('@')[0],
)
@ProtocolEntityCallback('message')
def onMessageReceived(self, entity):