Request contacts' statuses when user logs in
This commit is contained in:
parent
7302c4fc2c
commit
9434b1db6f
36
buddy.py
36
buddy.py
|
@ -24,6 +24,7 @@ __email__ = "post@steffenvogel.de"
|
||||||
from Spectrum2 import protocol_pb2
|
from Spectrum2 import protocol_pb2
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import time
|
||||||
|
|
||||||
|
|
||||||
class Buddy():
|
class Buddy():
|
||||||
|
@ -74,6 +75,7 @@ class BuddyList(dict):
|
||||||
# new = self.buddies.keys()
|
# new = self.buddies.keys()
|
||||||
# contacts = new
|
# contacts = new
|
||||||
contacts = self.keys()
|
contacts = self.keys()
|
||||||
|
contacts.remove('bot')
|
||||||
|
|
||||||
if self.synced == False:
|
if self.synced == False:
|
||||||
self.session.sendSync(contacts, delta = False, interactive = True)
|
self.session.sendSync(contacts, delta = False, interactive = True)
|
||||||
|
@ -93,11 +95,19 @@ class BuddyList(dict):
|
||||||
#
|
#
|
||||||
for number in contacts:
|
for number in contacts:
|
||||||
buddy = self[number]
|
buddy = self[number]
|
||||||
if number != 'bot':
|
self.backend.handleBuddyChanged(self.user, number, buddy.nick,
|
||||||
self.backend.handleBuddyChanged(self.user, number, buddy.nick,
|
buddy.groups, protocol_pb2.STATUS_NONE,
|
||||||
buddy.groups, protocol_pb2.STATUS_NONE,
|
iconHash = buddy.image_hash if buddy.image_hash is not None else "")
|
||||||
iconHash = buddy.image_hash if buddy.image_hash is not None else "")
|
self.session.subscribePresence(number)
|
||||||
self.session.subscribePresence(number)
|
self.logger.debug("%s is requesting statuses of: %s", self.user, contacts)
|
||||||
|
self.session.requestStatuses(contacts, success = self.onStatus)
|
||||||
|
|
||||||
|
def onStatus(self, contacts):
|
||||||
|
self.logger.debug("%s received statuses of: %s", self.user, contacts)
|
||||||
|
for number, (status, time) in contacts.iteritems():
|
||||||
|
buddy = self[number]
|
||||||
|
buddy.statusMsg = status
|
||||||
|
self.updateSpectrum(buddy)
|
||||||
|
|
||||||
|
|
||||||
def load(self, buddies):
|
def load(self, buddies):
|
||||||
|
@ -113,21 +123,31 @@ class BuddyList(dict):
|
||||||
else:
|
else:
|
||||||
self.session.sendSync([number], delta = True, interactive = True)
|
self.session.sendSync([number], delta = True, interactive = True)
|
||||||
self.session.subscribePresence(number)
|
self.session.subscribePresence(number)
|
||||||
|
self.session.requestStatuses(contacts, success = self.onStatus)
|
||||||
buddy = Buddy(self.owner, number, nick, "", groups, image_hash)
|
buddy = Buddy(self.owner, number, nick, "", groups, image_hash)
|
||||||
self[number] = buddy
|
self[number] = buddy
|
||||||
self.logger.debug("Roster add: %s", buddy)
|
self.logger.debug("Roster add: %s", buddy)
|
||||||
|
|
||||||
|
self.updateSpectrum(buddy)
|
||||||
|
return buddy
|
||||||
|
|
||||||
|
def updateSpectrum(self, buddy):
|
||||||
if buddy.presence == 0:
|
if buddy.presence == 0:
|
||||||
status = protocol_pb2.STATUS_NONE
|
status = protocol_pb2.STATUS_NONE
|
||||||
elif buddy.presence == 'unavailable':
|
elif buddy.presence == 'unavailable':
|
||||||
status = protocol_pb2.STATUS_AWAY
|
status = protocol_pb2.STATUS_AWAY
|
||||||
else:
|
else:
|
||||||
status = protocol_pb2.STATUS_ONLINE
|
status = protocol_pb2.STATUS_ONLINE
|
||||||
self.backend.handleBuddyChanged(self.user, number, buddy.nick,
|
|
||||||
buddy.groups, status,
|
statusmsg = buddy.statusMsg
|
||||||
|
if buddy.lastseen != 0:
|
||||||
|
timestamp = time.localtime(buddy.lastseen)
|
||||||
|
statusmsg += time.strftime("\n Last seen: %a, %d %b %Y %H:%M:%S", timestamp)
|
||||||
|
|
||||||
|
self.backend.handleBuddyChanged(self.user, buddy.number, buddy.nick,
|
||||||
|
buddy.groups, status, statusMessage = statusmsg,
|
||||||
iconHash = buddy.image_hash if buddy.image_hash is not None else "")
|
iconHash = buddy.image_hash if buddy.image_hash is not None else "")
|
||||||
|
|
||||||
return buddy
|
|
||||||
|
|
||||||
def remove(self, number):
|
def remove(self, number):
|
||||||
try:
|
try:
|
||||||
|
|
18
session.py
18
session.py
|
@ -30,7 +30,6 @@ from PIL import Image
|
||||||
import sys
|
import sys
|
||||||
import os
|
import os
|
||||||
|
|
||||||
from yowsup.common.tools import TimeTools
|
|
||||||
from yowsup.layers.protocol_media.mediauploader import MediaUploader
|
from yowsup.layers.protocol_media.mediauploader import MediaUploader
|
||||||
from yowsup.layers.protocol_media.mediadownloader import MediaDownloader
|
from yowsup.layers.protocol_media.mediadownloader import MediaDownloader
|
||||||
|
|
||||||
|
@ -497,25 +496,20 @@ class Session(YowsupApp):
|
||||||
|
|
||||||
buddy.presence = _type
|
buddy.presence = _type
|
||||||
|
|
||||||
timestamp = time.localtime(buddy.lastseen)
|
|
||||||
statusmsg = buddy.statusMsg + time.strftime("\n Last seen: %a, %d %b %Y %H:%M:%S", timestamp)
|
|
||||||
|
|
||||||
if _type == "unavailable":
|
if _type == "unavailable":
|
||||||
self.onPresenceUnavailable(buddy, statusmsg)
|
self.onPresenceUnavailable(buddy)
|
||||||
else:
|
else:
|
||||||
self.onPresenceAvailable(buddy, statusmsg)
|
self.onPresenceAvailable(buddy)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def onPresenceAvailable(self, buddy, statusmsg):
|
def onPresenceAvailable(self, buddy):
|
||||||
self.logger.info("Is available: %s", buddy)
|
self.logger.info("Is available: %s", buddy)
|
||||||
self.backend.handleBuddyChanged(self.user, buddy.number,
|
self.buddies.updateSpectrum(buddy)
|
||||||
buddy.nick, buddy.groups, protocol_pb2.STATUS_ONLINE, statusmsg, buddy.image_hash)
|
|
||||||
|
|
||||||
def onPresenceUnavailable(self, buddy, statusmsg):
|
def onPresenceUnavailable(self, buddy):
|
||||||
self.logger.info("Is unavailable: %s", buddy)
|
self.logger.info("Is unavailable: %s", buddy)
|
||||||
self.backend.handleBuddyChanged(self.user, buddy.number,
|
self.buddies.updateSpectrum(buddy)
|
||||||
buddy.nick, buddy.groups, protocol_pb2.STATUS_AWAY, statusmsg, buddy.image_hash)
|
|
||||||
|
|
||||||
# spectrum RequestMethods
|
# spectrum RequestMethods
|
||||||
def sendTypingStarted(self, buddy):
|
def sendTypingStarted(self, buddy):
|
||||||
|
|
|
@ -65,6 +65,7 @@ class YowsupApp(object):
|
||||||
YowProfilesProtocolLayer,
|
YowProfilesProtocolLayer,
|
||||||
YowGroupsProtocolLayer,
|
YowGroupsProtocolLayer,
|
||||||
YowPresenceProtocolLayer)),
|
YowPresenceProtocolLayer)),
|
||||||
|
YowLoggerLayer,
|
||||||
YowAxolotlLayer,
|
YowAxolotlLayer,
|
||||||
YowCoderLayer,
|
YowCoderLayer,
|
||||||
YowCryptLayer,
|
YowCryptLayer,
|
||||||
|
@ -292,6 +293,27 @@ class YowsupApp(object):
|
||||||
iq = GetSyncIqProtocolEntity(contacts, mode, context)
|
iq = GetSyncIqProtocolEntity(contacts, mode, context)
|
||||||
self.sendIq(iq)
|
self.sendIq(iq)
|
||||||
|
|
||||||
|
def requestStatuses(self, contacts, success = None, failure = None):
|
||||||
|
"""
|
||||||
|
Request the statuses of a number of users.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
- contacts: ([str]) the phone numbers of users whose statuses you
|
||||||
|
wish to request
|
||||||
|
- success: (func) called when request is successful
|
||||||
|
- failure: (func) called when request has failed
|
||||||
|
"""
|
||||||
|
iq = GetStatusesIqProtocolEntity([c + '@s.whatsapp.net' for c in contacts])
|
||||||
|
def onSuccess(response, request):
|
||||||
|
self.logger.debug("Received Statuses %s", response)
|
||||||
|
s = {}
|
||||||
|
for k, v in response.statuses.iteritems():
|
||||||
|
s[k.split('@')[0]] = v
|
||||||
|
success(s)
|
||||||
|
|
||||||
|
self.sendIq(iq, onSuccess = onSuccess, onError = failure)
|
||||||
|
|
||||||
|
|
||||||
def requestLastSeen(self, phoneNumber, success = None, failure = None):
|
def requestLastSeen(self, phoneNumber, success = None, failure = None):
|
||||||
"""
|
"""
|
||||||
Requests when user was last seen.
|
Requests when user was last seen.
|
||||||
|
|
Loading…
Reference in a new issue