diff sat/memory/sqlite.py @ 2989:c13333fcde5e

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).
author Goffi <goffi@goffi.org>
date Sat, 06 Jul 2019 15:50:55 +0200
parents 17c61d09a85b
children 860c550028d6
line wrap: on
line diff
--- 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)