Move updateRoster code to BuddyList

This commit is contained in:
moyamo 2015-12-01 21:54:58 +02:00
parent da8a415d32
commit 107354af81
2 changed files with 48 additions and 40 deletions

View file

@ -50,8 +50,13 @@ class Buddy():
class BuddyList(dict):
def __init__(self, owner):
def __init__(self, owner, backend, user, sendSync):
self.owner = owner
self.backend = backend
self.sendSync = sendSync
self.user = user
self.logger = logging.getLogger(self.__class__.__name__)
self.synced = False
def load(self, buddies):
for buddy in buddies:
@ -62,13 +67,49 @@ class BuddyList(dict):
image_hash = buddy.iconHash
self[number] = Buddy(self.owner, number, nick, statusMsg,
groups, image_hash)
self.logger.debug("Update roster")
# old = self.buddies.keys()
# self.buddies.load()
# new = self.buddies.keys()
# contacts = new
contacts = self.keys()
if self.synced == False:
self.sendSync(contacts, delta = False, interactive = True)
self.synced = True
# add = set(new) - set(old)
# remove = set(old) - set(new)
# self.logger.debug("Roster remove: %s", str(list(remove)))
self.logger.debug("Roster add: %s", str(list(contacts)))
# for number in remove:
# self.backend.handleBuddyChanged(self.user, number, "", [],
# protocol_pb2.STATUS_NONE)
# self.backend.handleBuddyRemoved(self.user, number)
# self.unsubscribePresence(number)
#
for buddy in contacts:
# self.subscribePresence(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 "")
def update(self, number, nick, groups, image_hash):
if number in self:
buddy = self[number]
buddy.update(nick, groups, image_hash)
else:
self.sendSync([number], delta = True, interactive = True)
buddy = Buddy(self.owner, number, nick, "", groups, image_hash)
self.logger.debug("Roster add: %s", buddy)
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 "")
return buddy
@ -76,6 +117,11 @@ class BuddyList(dict):
try:
buddy = self[number]
buddy.delete()
self.backend.handleBuddyChanged(self.user, number, "", [],
protocol_pb2.STATUS_NONE)
self.backend.handleBuddyRemoved(self.user, number)
# self.unsubscribePresence(number)
# TODO Sync remove
return buddy
except KeyError:
return None

View file

@ -87,7 +87,7 @@ class Session(YowsupApp):
self.lastMsgId = None
self.synced = False
self.buddies = BuddyList(self.legacyName)
self.buddies = BuddyList(self.legacyName, self.backend, self.user, self.sendSync)
self.bot = Bot(self)
self.imgMsgId = None
@ -132,39 +132,6 @@ class Session(YowsupApp):
'\n'.join(text) + '\nIf you do not join them you will lose messages'
#self.bot.send(message)
def updateRoster(self):
self.logger.debug("Update roster")
old = self.buddies.keys()
self.buddies.load()
new = self.buddies.keys()
contacts = new
if self.synced == False:
self.sendSync(contacts, delta = False, interactive = True)
self.synced = True
add = set(new) - set(old)
remove = set(old) - set(new)
self.logger.debug("Roster remove: %s", str(list(remove)))
self.logger.debug("Roster add: %s", str(list(add)))
for number in remove:
self.backend.handleBuddyChanged(self.user, number, "", [],
protocol_pb2.STATUS_NONE)
self.backend.handleBuddyRemoved(self.user, number)
self.unsubscribePresence(number)
for number in add:
buddy = self.buddies[number]
self.subscribePresence(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.requestLastSeen(number, self._lastSeen)
def _updateGroups(self, response, request):
self.logger.debug('Received groups list %s', response)
groups = response.getGroups()
@ -278,7 +245,6 @@ class Session(YowsupApp):
#self.bot.call("welcome")
self.initialized = True
self.sendPresence(True)
self.updateRoster()
self.logger.debug('Requesting groups list')
self.requestGroupsList(self._updateGroups)
@ -797,15 +763,11 @@ class Session(YowsupApp):
def updateBuddy(self, buddy, nick, groups, image_hash = None):
if buddy != "bot":
self.buddies.update(buddy, nick, groups, image_hash)
if self.initialized == True:
self.updateRoster()
def removeBuddy(self, buddy):
if buddy != "bot":
self.logger.info("Buddy removed: %s", buddy)
self.buddies.remove(buddy)
self.updateRoster()
def requestVCard(self, buddy, ID=None):
def onSuccess(response, request):