Merge pull request #53 from dazzzl/yowsup2-port

Read receipts & fix for group chat receipt asks

Thanks @dazzzl !
This commit is contained in:
Steffen Vogel 2016-04-05 21:24:19 +02:00
commit 229d7b9e2e
2 changed files with 24 additions and 3 deletions

View file

@ -78,6 +78,7 @@ class Session(YowsupApp):
self.msgIDs = { } self.msgIDs = { }
self.groupOfflineQueue = { } self.groupOfflineQueue = { }
self.loggedIn = False self.loggedIn = False
self.recvMsgIDs = []
self.timer = None self.timer = None
self.password = None self.password = None
@ -209,6 +210,12 @@ class Session(YowsupApp):
self.onPresenceAvailable(number) self.onPresenceAvailable(number)
else: else:
self.onPresenceUnavailable(number) self.onPresenceUnavailable(number)
def sendReadReceipts(self, buddy):
for _id, _from, participant in self.recvMsgIDs:
if _from.split('@')[0] == buddy:
self.sendReceipt(_id, _from, 'read', participant)
self.recvMsgIDs.remove((_id, _from, participant))
self.logger.debug("Send read receipt to %s (ID: %s)", _from, _id)
# Called by superclass # Called by superclass
def onAuthSuccess(self, status, kind, creation, def onAuthSuccess(self, status, kind, creation,
@ -285,6 +292,7 @@ class Session(YowsupApp):
buddy = _from.split('@')[0] buddy = _from.split('@')[0]
messageContent = utils.softToUni(body) messageContent = utils.softToUni(body)
self.sendReceipt(_id, _from, None, participant) self.sendReceipt(_id, _from, None, participant)
self.recvMsgIDs.append((_id, _from, participant))
self.logger.info("Message received from %s to %s: %s (at ts=%s)", self.logger.info("Message received from %s to %s: %s (at ts=%s)",
buddy, self.legacyName, messageContent, timestamp) buddy, self.legacyName, messageContent, timestamp)
if participant is not None: # Group message or broadcast if participant is not None: # Group message or broadcast
@ -320,6 +328,8 @@ class Session(YowsupApp):
self.sendMessageToXMPP(buddy, image.url, image.timestamp) self.sendMessageToXMPP(buddy, image.url, image.timestamp)
self.sendMessageToXMPP(buddy, image.caption, image.timestamp) self.sendMessageToXMPP(buddy, image.caption, image.timestamp)
self.sendReceipt(image._id, image._from, None, image.participant) self.sendReceipt(image._id, image._from, None, image.participant)
self.recvMsgIDs.append((image._id, image._from, image.participant))
# Called by superclass # Called by superclass
def onAudio(self, audio): def onAudio(self, audio):
@ -337,6 +347,8 @@ class Session(YowsupApp):
else: else:
self.sendMessageToXMPP(buddy, message, audio.timestamp) self.sendMessageToXMPP(buddy, message, audio.timestamp)
self.sendReceipt(audio._id, audio._from, None, audio.participant) self.sendReceipt(audio._id, audio._from, None, audio.participant)
self.recvMsgIDs.append((audio._id, audio._from, audio.participant))
# Called by superclass # Called by superclass
def onVideo(self, video): def onVideo(self, video):
@ -355,6 +367,8 @@ class Session(YowsupApp):
else: else:
self.sendMessageToXMPP(buddy, message, video.timestamp) self.sendMessageToXMPP(buddy, message, video.timestamp)
self.sendReceipt(video._id, video._from, None, video.participant) self.sendReceipt(video._id, video._from, None, video.participant)
self.recvMsgIDs.append((video._id, video._from, video.participant))
def onLocation(self, location): def onLocation(self, location):
buddy = location._from.split('@')[0] buddy = location._from.split('@')[0]
@ -384,6 +398,8 @@ class Session(YowsupApp):
self.sendMessageToXMPP(buddy, url, location.timestamp) self.sendMessageToXMPP(buddy, url, location.timestamp)
self.sendMessageToXMPP(buddy, latlong, location.timestamp) self.sendMessageToXMPP(buddy, latlong, location.timestamp)
self.sendReceipt(location._id, location._from, None, location.participant) self.sendReceipt(location._id, location._from, None, location.participant)
self.recvMsgIDs.append((loaction._id, location._from, location.participant))
# Called by superclass # Called by superclass
@ -407,6 +423,8 @@ class Session(YowsupApp):
# self.sendMessageToXMPP(buddy, card_data) # self.sendMessageToXMPP(buddy, card_data)
#self.transferFile(buddy, str(name), card_data) #self.transferFile(buddy, str(name), card_data)
self.sendReceipt(_id, _from, None, participant) self.sendReceipt(_id, _from, None, participant)
self.recvMsgIDs.append((_id, _from, participant))
def transferFile(self, buddy, name, data): def transferFile(self, buddy, name, data):
# Not working # Not working
@ -560,6 +578,7 @@ class Session(YowsupApp):
if buddy != "bot": if buddy != "bot":
self.logger.info("Started typing: %s to %s", self.legacyName, buddy) self.logger.info("Started typing: %s to %s", self.legacyName, buddy)
self.sendTyping(buddy, True) self.sendTyping(buddy, True)
self.sendReadReceipts(buddy)
# If he is typing he is present # If he is typing he is present
# I really don't know where else to put this. # I really don't know where else to put this.
# Ideally, this should be sent if the user is looking at his client # Ideally, this should be sent if the user is looking at his client
@ -569,6 +588,7 @@ class Session(YowsupApp):
if buddy != "bot": if buddy != "bot":
self.logger.info("Stopped typing: %s to %s", self.legacyName, buddy) self.logger.info("Stopped typing: %s to %s", self.legacyName, buddy)
self.sendTyping(buddy, False) self.sendTyping(buddy, False)
self.sendReadReceipts(buddy)
def sendImage(self, message, ID, to): def sendImage(self, message, ID, to):
if (".jpg" in message.lower()): if (".jpg" in message.lower()):
@ -610,6 +630,7 @@ class Session(YowsupApp):
self.legacyName, sender, message, xhtml) self.legacyName, sender, message, xhtml)
message = message.encode("utf-8") message = message.encode("utf-8")
self.sendReadReceipts(sender)
if sender == "bot": if sender == "bot":
self.bot.parse(message) self.bot.parse(message)

View file

@ -752,9 +752,9 @@ class YowsupAppLayer(YowInterfaceLayer):
def onReceipt(self, entity): def onReceipt(self, entity):
"""Sends ack automatically""" """Sends ack automatically"""
# entity is IncomingReceiptProtocolEntity # entity is IncomingReceiptProtocolEntity
ack = OutgoingAckProtocolEntity(entity.getId(), #ack = OutgoingAckProtocolEntity(entity.getId(),
'receipt', entity.getType(), entity.getFrom()) # 'receipt', entity.getType(), entity.getFrom())
self.toLower(ack) self.toLower(entity.ack())
_id = entity._id _id = entity._id
_from = entity._from _from = entity._from
timestamp = entity.timestamp timestamp = entity.timestamp