From b0c19d6bac396b5f30432a8ea5e1ef5e89a89048 Mon Sep 17 00:00:00 2001
From: Daniel Gultsch <daniel@gultsch.de>
Date: Sun, 30 Aug 2015 11:24:37 +0200
Subject: [PATCH] fixed bug that prevented newly opened conversations to load
 the entire history via mam. fixes #1328

---
 .../eu/siacs/conversations/entities/Conversation.java  |  4 ++++
 .../conversations/services/MessageArchiveService.java  | 10 +++++++++-
 .../conversations/services/XmppConnectionService.java  |  1 +
 3 files changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/main/java/eu/siacs/conversations/entities/Conversation.java b/src/main/java/eu/siacs/conversations/entities/Conversation.java
index e6c044a1c..1af87c429 100644
--- a/src/main/java/eu/siacs/conversations/entities/Conversation.java
+++ b/src/main/java/eu/siacs/conversations/entities/Conversation.java
@@ -673,6 +673,10 @@ public class Conversation extends AbstractEntity implements Blockable {
 		}
 	}
 
+	public void resetLastMessageTransmitted() {
+		this.setAttribute(ATTRIBUTE_LAST_MESSAGE_TRANSMITTED,String.valueOf(-1));
+	}
+
 	public boolean setLastMessageTransmitted(long value) {
 		long before = getLastMessageTransmitted();
 		if (value - before > 1000) {
diff --git a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java
index a31848a7c..351ded0b8 100644
--- a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java
+++ b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java
@@ -68,7 +68,15 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
 	}
 
 	public Query query(final Conversation conversation) {
-		return query(conversation,conversation.getAccount().getXmppConnection().getLastSessionEstablished());
+		if (conversation.getLastMessageTransmitted() < 0 && conversation.countMessages() == 0) {
+			return query(conversation,
+					0,
+					System.currentTimeMillis());
+		} else {
+			return query(conversation,
+					conversation.getLastMessageTransmitted(),
+					conversation.getAccount().getXmppConnection().getLastSessionEstablished());
+		}
 	}
 
 	public Query query(final Conversation conversation, long end) {
diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
index 4edaa324c..8de160580 100644
--- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
+++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
@@ -2545,6 +2545,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
 	public void clearConversationHistory(final Conversation conversation) {
 		conversation.clearMessages();
 		conversation.setHasMessagesLeftOnServer(false); //avoid messages getting loaded through mam
+		conversation.resetLastMessageTransmitted();
 		new Thread(new Runnable() {
 			@Override
 			public void run() {