Commit 59855a6c authored by Philipp Hörist's avatar Philipp Hörist

Determine delay timestamp correctly

- Check the from attr on the delay node to determine if its a user timestamp or from the server
- Dont use user timestamp for sorting
- Record the user timestamp in additional data so its saved to the database

Fixes #9444
parent fd6dec12
Pipeline #2746 passed with stages
in 4 minutes and 2 seconds
......@@ -131,12 +131,13 @@ class MAM:
raise nbxmpp.NodeProcessed
# Timestamp parsing
timestamp = parse_delay(forwarded)
if timestamp is None:
# Most servers dont set the 'from' attr, so we cant check for it
delay_timestamp = parse_delay(forwarded)
if delay_timestamp is None:
log.warning('No timestamp on MAM message')
raise nbxmpp.NodeProcessed
user_timestamp = parse_delay(message)
# Fix for self messaging
if not groupchat:
to = message.getTo()
......@@ -181,8 +182,7 @@ class MAM:
{'conn': self._con,
'additional_data': AdditionalDataDict(),
'encrypted': False,
'timestamp': timestamp,
'user_timestamp': user_timestamp,
'timestamp': delay_timestamp,
'self_message': self_message,
'groupchat': groupchat,
'muc_pm': muc_pm,
......@@ -250,7 +250,12 @@ class MAM:
# For example Chatstates, Receipts, Chatmarkers
user_timestamp = parse_delay(event.stanza)
if user_timestamp is not None:
# Record it as a user timestamp
'gajim', 'user_timestamp', user_timestamp)
event.correct_id = parse_correction(event.message)
......@@ -226,17 +226,42 @@ class Message:
except nbxmpp.NodeProcessed:
timestamp, delayed = parse_delay(event.stanza), True
if timestamp is None:
subject = event.stanza.getSubject()
groupchat = event.mtype == 'groupchat'
muc_subject = subject and groupchat
# Determine timestamps
if groupchat:
delay_entity_jid = event.jid
delay_entity_jid = self._con.get_own_jid().getDomain()
if muc_subject:
# MUC Subjects can have a delay timestamp
# to indicate when the user has set the subject,
# the 'from' attr on these delays is the MUC server
# but we treat it as user timestamp
timestamp = time.time()
delayed = False
user_timestamp = parse_delay(event.stanza, from_=delay_entity_jid)
timestamp = parse_delay(event.stanza, from_=delay_entity_jid)
if timestamp is None:
timestamp = time.time()
user_timestamp = parse_delay(event.stanza,
if user_timestamp is not None:
'gajim', 'user_timestamp', user_timestamp)
event_attr = {
'popup': False,
'msg_log_id': None,
'subject': event.stanza.getSubject(),
'subject': subject,
'displaymarking': parse_securitylabel(event.stanza),
'attention': parse_attention(event.stanza),
'correct_id': parse_correction(event.stanza),
......@@ -244,7 +269,7 @@ class Message:
'form_node': parse_form(event.stanza),
'xhtml': parse_xhtml(event.stanza),
'timestamp': timestamp,
'delayed': delayed,
'delayed': user_timestamp is not None,
......@@ -262,7 +287,7 @@ class Message:
event.session, event.fjid, timestamp)
if event.mtype == 'groupchat':
if groupchat:
......@@ -65,18 +65,45 @@ def parse_eme(stanza):
# XEP-0203: Delayed Delivery
def parse_delay(stanza, epoch=True, convert='utc'):
timestamp = None
delay = stanza.getTagAttr(
'delay', 'stamp', namespace=nbxmpp.NS_DELAY2)
if delay is not None:
timestamp = parse_datetime(delay, check_utc=True,
def parse_delay(stanza, epoch=True, convert='utc', from_=None, not_from=None):
Returns the first valid delay timestamp that matches
:param epoch: Returns the timestamp as epoch
:param convert: Converts the timestamp to either utc or local
:param from_: Matches only delays that have the according
from attr set
:param not_from: Matches only delays that have the according
from attr not set
delays = stanza.getTags('delay', namespace=nbxmpp.NS_DELAY2)
for delay in delays:
stamp = delay.getAttr('stamp')
if stamp is None:
log.warning('Invalid timestamp received: %s', stamp)
delay_from = delay.getAttr('from')
if from_ is not None:
if delay_from != from_:
if not_from is not None:
if delay_from in not_from:
timestamp = parse_datetime(stamp, check_utc=True,
epoch=epoch, convert=convert)
if timestamp is None:
log.warning('Invalid timestamp received: %s', delay)
log.warning('Invalid timestamp received: %s', stamp)
return timestamp
return timestamp
# XEP-0066: Out of Band Data
import unittest
import nbxmpp
from gajim.common.modules.misc import parse_delay
class TestHelpers(unittest.TestCase):
def test_parse_delay(self):
node = """
<delay xmlns='urn:xmpp:delay' from='' stamp='2002-09-10T23:08:25Z' />
<delay xmlns='urn:xmpp:delay' from='' stamp='2010-09-10T23:08:25Z' />
<delay xmlns='urn:xmpp:delay' stamp='2015-09-10T23:08:25Z' />
message = nbxmpp.Node(node=node)
timestamp = parse_delay(message)
self.assertEqual(timestamp, 1031699305.0)
timestamp = parse_delay(message, from_='')
self.assertEqual(timestamp, 1031699305.0)
timestamp = parse_delay(message, from_='')
self.assertEqual(timestamp, 1284160105.0)
timestamp = parse_delay(message, not_from=[''])
self.assertEqual(timestamp, 1031699305.0)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment