use last received message id when querying archive

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/services/MessageArchiveService.java | 24 
1 file changed, 17 insertions(+), 7 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/services/MessageArchiveService.java 🔗

@@ -45,8 +45,10 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
 				}
 			}
 		}
-		long startCatchup = getLastMessageTransmitted(account);
+		Pair<Long,String> pair = mXmppConnectionService.databaseBackend.getLastMessageReceived(account);
+		long startCatchup = pair == null ? 0 : pair.first;
 		long endCatchup = account.getXmppConnection().getLastSessionEstablished();
+		final Query query;
 		if (startCatchup == 0) {
 			return;
 		} else if (endCatchup - startCatchup >= Config.MAM_MAX_CATCHUP) {
@@ -57,8 +59,14 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
 					this.query(conversation,startCatchup);
 				}
 			}
+			query = new Query(account, startCatchup, endCatchup);
+		} else {
+			if (pair.second == null) {
+				query = new Query(account, startCatchup, endCatchup);
+			} else {
+				query = new Query(account, pair.second, endCatchup);
+			}
 		}
-		final Query query = new Query(account, startCatchup, endCatchup);
 		this.queries.add(query);
 		this.execute(query);
 	}
@@ -75,11 +83,6 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
 		}
 	}
 
-	private long getLastMessageTransmitted(final Account account) {
-		Pair<Long,String> pair = mXmppConnectionService.databaseBackend.getLastMessageReceived(account);
-		return pair == null ? 0 : pair.first;
-	}
-
 	public Query query(final Conversation conversation) {
 		if (conversation.getLastMessageTransmitted() < 0 && conversation.countMessages() == 0) {
 			return query(conversation,
@@ -283,6 +286,13 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
 			this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
 		}
 
+		public Query(Account account, String reference, long end) {
+			this.account = account;
+			this.reference = reference;
+			this.end = end;
+			this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
+		}
+
 		private Query page(String reference) {
 			Query query = new Query(this.account,this.start,this.end);
 			query.reference = reference;