Improve timestamp usage for MAM
- Use new parse_datetime() method - Drop message with error if MAM doesnt supply a timestamp. - If the user supplys an own timestamp, save it so we can decide in the future how to display it.
This commit is contained in:
parent
e24d4f8caf
commit
651611b28b
|
@ -1059,7 +1059,7 @@ class MamMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
|
|||
|
||||
if frm.bareMatch(own_jid):
|
||||
self.stanza_id = self.msg_.getOriginID()
|
||||
if not self.stanza_id:
|
||||
if self.stanza_id is None:
|
||||
self.stanza_id = self.msg_.getID()
|
||||
|
||||
self.with_ = to
|
||||
|
@ -1073,20 +1073,30 @@ class MamMessageReceivedEvent(nec.NetworkIncomingEvent, HelperEvent):
|
|||
self.with_ = frm
|
||||
self.kind = KindConstant.CHAT_MSG_RECV
|
||||
|
||||
if not self.stanza_id:
|
||||
if self.stanza_id is None:
|
||||
log.debug('Could not retrieve stanza-id')
|
||||
|
||||
# Use timestamp provided by archive,
|
||||
# Fallback: Use timestamp provided by user and issue a warning
|
||||
delay = self.forwarded.getTag('delay', namespace=nbxmpp.NS_DELAY2)
|
||||
if not delay:
|
||||
log.warning('No timestamp on archive Message, try fallback')
|
||||
delay = self.msg_.getTag('delay', namespace=nbxmpp.NS_DELAY2)
|
||||
if not delay:
|
||||
delay = self.forwarded.getTagAttr(
|
||||
'delay', 'stamp', namespace=nbxmpp.NS_DELAY2)
|
||||
if delay is None:
|
||||
log.error('Received MAM message without timestamp')
|
||||
return
|
||||
|
||||
self.timestamp = helpers.parse_delay(delay)
|
||||
self.timestamp = helpers.parse_datetime(
|
||||
delay, check_utc=True, epoch=True)
|
||||
if self.timestamp is None:
|
||||
log.error('Received MAM message with invalid timestamp: %s', delay)
|
||||
return
|
||||
|
||||
# Save timestamp added by the user
|
||||
user_delay = self.msg_.getTagAttr(
|
||||
'delay', 'stamp', namespace=nbxmpp.NS_DELAY2)
|
||||
if user_delay is not None:
|
||||
self.user_timestamp = helpers.parse_datetime(
|
||||
user_delay, check_utc=True, epoch=True)
|
||||
if self.user_timestamp is None:
|
||||
log.warning('Received MAM message with '
|
||||
'invalid user timestamp: %s', user_delay)
|
||||
|
||||
log.debug('Received mam-message: stanza id: %s', self.stanza_id)
|
||||
return True
|
||||
|
|
|
@ -673,34 +673,6 @@ def datetime_tuple(timestamp):
|
|||
tim = tim.timetuple()
|
||||
return tim
|
||||
|
||||
def parse_delay(timestamp):
|
||||
'''
|
||||
Parse a timestamp
|
||||
https://xmpp.org/extensions/xep-0203.html
|
||||
Note: Not all delay tags should be parsed with this method
|
||||
see https://xmpp.org/extensions/xep-0082.html for more information
|
||||
|
||||
:param timestamp: a XEP-0203 fomated timestring string or a delay Node
|
||||
|
||||
Examples:
|
||||
'2017-11-05T01:41:20Z'
|
||||
'2017-11-05T01:41:20.123Z'
|
||||
|
||||
return epoch UTC timestamp
|
||||
'''
|
||||
if isinstance(timestamp, nbxmpp.protocol.Node):
|
||||
timestamp = timestamp.getAttr('stamp')
|
||||
timestamp += '+0000'
|
||||
try:
|
||||
datetime_ = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%SZ%z')
|
||||
except ValueError:
|
||||
try:
|
||||
datetime_ = datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S.%fZ%z')
|
||||
except ValueError:
|
||||
log.error('Could not parse delay timestamp: %s', timestamp)
|
||||
raise
|
||||
return datetime_.timestamp()
|
||||
|
||||
def parse_datetime(timestring, check_utc=False, convert='utc', epoch=False):
|
||||
'''
|
||||
Parse a XEP-0082 DateTime Profile String
|
||||
|
|
Loading…
Reference in New Issue