comparison 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
comparison
equal deleted inserted replaced
2988:b5f8cb26ef6f 2989:c13333fcde5e
590 q.append(test_jid('source', from_jid)) 590 q.append(test_jid('source', from_jid))
591 if to_jid is not None: 591 if to_jid is not None:
592 q.append(test_jid('dest', to_jid)) 592 q.append(test_jid('dest', to_jid))
593 query_parts.append(u"AND " + u" AND ".join(q)) 593 query_parts.append(u"AND " + u" AND ".join(q))
594 594
595 # set to True if "ORDER BY" is already added
596 order = False
597
598 if filters: 595 if filters:
599 if u'timestamp_start' in filters: 596 if u'timestamp_start' in filters:
600 query_parts.append(u"AND timestamp>= ?") 597 query_parts.append(u"AND timestamp>= ?")
601 values.append(float(filters[u'timestamp_start'])) 598 values.append(float(filters[u'timestamp_start']))
602 if u'body' in filters: 599 if u'body' in filters:
616 values.extend(types) 613 values.extend(types)
617 if u'last_stanza_id' in filters: 614 if u'last_stanza_id' in filters:
618 # this request get the last message with a "stanza_id" that we 615 # this request get the last message with a "stanza_id" that we
619 # have in history. This is mainly used to retrieve messages sent 616 # have in history. This is mainly used to retrieve messages sent
620 # while we were offline, using MAM (XEP-0313). 617 # while we were offline, using MAM (XEP-0313).
621 # It must be set after all other filters, because it contains an ORDER BY
622 if (filters[u'last_stanza_id'] is not True 618 if (filters[u'last_stanza_id'] is not True
623 or limit != 1): 619 or limit != 1):
624 raise ValueError(u"Unexpected values for last_stanza_id filter") 620 raise ValueError(u"Unexpected values for last_stanza_id filter")
625 query_parts.append(u"AND stanza_id IS NOT NULL ORDER BY history.received_timestamp DESC") 621 query_parts.append(u"AND stanza_id IS NOT NULL")
626 order = True 622
627 623
628 624 # timestamp may be identical for 2 close messages (specially when delay is
629 if not order: 625 # used) that's why we order ties by received_timestamp
630 # timestamp may be identical for 2 close messages (specially when delay is 626 # We'll reverse the order in sqliteHistoryToList
631 # used) that's why we order ties by received_timestamp 627 # we use DESC here so LIMIT keep the last messages
632 # We'll reverse the order in sqliteHistoryToList 628 query_parts.append(u"ORDER BY timestamp DESC, history.received_timestamp DESC")
633 query_parts.append(u"ORDER BY timestamp DESC, history.received_timestamp DESC")
634 # we use DESC here so LIMIT keep the last messages
635 if limit is not None: 629 if limit is not None:
636 query_parts.append(u"LIMIT ?") 630 query_parts.append(u"LIMIT ?")
637 values.append(limit) 631 values.append(limit)
638 632
639 d = self.dbpool.runQuery(u" ".join(query_parts), values) 633 d = self.dbpool.runQuery(u" ".join(query_parts), values)