# HG changeset patch # User Goffi # Date 1562421055 -7200 # Node ID c13333fcde5e040a51f68e3d399e5c845653bb2e # Parent b5f8cb26ef6fb957089d21f864ddd6057136ac11 memory (sqlite): fixed order for last_stanza_id: "last_stanza_id" filter was ordered by "received_timestamp" instead of "timestamp", resulting in wrong message being used (and a bad MAM request). diff -r b5f8cb26ef6f -r c13333fcde5e sat/memory/sqlite.py --- a/sat/memory/sqlite.py Sat Jul 06 12:22:25 2019 +0200 +++ b/sat/memory/sqlite.py Sat Jul 06 15:50:55 2019 +0200 @@ -592,9 +592,6 @@ q.append(test_jid('dest', to_jid)) query_parts.append(u"AND " + u" AND ".join(q)) - # set to True if "ORDER BY" is already added - order = False - if filters: if u'timestamp_start' in filters: query_parts.append(u"AND timestamp>= ?") @@ -618,20 +615,17 @@ # this request get the last message with a "stanza_id" that we # have in history. This is mainly used to retrieve messages sent # while we were offline, using MAM (XEP-0313). - # It must be set after all other filters, because it contains an ORDER BY if (filters[u'last_stanza_id'] is not True or limit != 1): raise ValueError(u"Unexpected values for last_stanza_id filter") - query_parts.append(u"AND stanza_id IS NOT NULL ORDER BY history.received_timestamp DESC") - order = True + query_parts.append(u"AND stanza_id IS NOT NULL") - if not order: - # timestamp may be identical for 2 close messages (specially when delay is - # used) that's why we order ties by received_timestamp - # We'll reverse the order in sqliteHistoryToList - query_parts.append(u"ORDER BY timestamp DESC, history.received_timestamp DESC") - # we use DESC here so LIMIT keep the last messages + # timestamp may be identical for 2 close messages (specially when delay is + # used) that's why we order ties by received_timestamp + # We'll reverse the order in sqliteHistoryToList + # we use DESC here so LIMIT keep the last messages + query_parts.append(u"ORDER BY timestamp DESC, history.received_timestamp DESC") if limit is not None: query_parts.append(u"LIMIT ?") values.append(limit)