aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/services
diff options
context:
space:
mode:
authoriNPUTmice <daniel@gultsch.de>2014-12-13 15:32:11 +0100
committeriNPUTmice <daniel@gultsch.de>2014-12-13 15:32:11 +0100
commit899802646cbf27622c1eef8c941a84fd60af2424 (patch)
treef17b6f19bfa8cee247f87a752408a9ae34fbac6b /src/main/java/eu/siacs/conversations/services
parent02a89f4ce2228c2e0f528f89fa1fe850c6f50fe8 (diff)
reverse through mam history when loading larger chuncks
Diffstat (limited to 'src/main/java/eu/siacs/conversations/services')
-rw-r--r--src/main/java/eu/siacs/conversations/services/MessageArchiveService.java73
-rw-r--r--src/main/java/eu/siacs/conversations/services/XmppConnectionService.java3
2 files changed, 65 insertions, 11 deletions
diff --git a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java
index 3b84d411..9dfe179c 100644
--- a/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java
+++ b/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java
@@ -23,6 +23,11 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
private final HashSet<Query> queries = new HashSet<Query>();
+ public enum PagingOrder {
+ NORMAL,
+ REVERSE
+ };
+
public MessageArchiveService(final XmppConnectionService service) {
this.mXmppConnectionService = service;
}
@@ -72,7 +77,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
} else if (end - start >= Config.MAX_HISTORY_AGE) {
start = end - Config.MAX_HISTORY_AGE;
}
- final Query query = new Query(conversation, start, end);
+ final Query query = new Query(conversation, start, end,PagingOrder.REVERSE);
this.queries.add(query);
this.execute(query);
}
@@ -115,6 +120,17 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
}
}
+ public boolean queryInProgress(Conversation conversation) {
+ synchronized (this.queries) {
+ for(Query query : queries) {
+ if (query.conversation == conversation) {
+ return true;
+ }
+ }
+ return false;
+ }
+ }
+
public void processFin(Element fin) {
if (fin == null) {
return;
@@ -126,11 +142,19 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
boolean complete = fin.getAttributeAsBoolean("complete");
Element set = fin.findChild("set","http://jabber.org/protocol/rsm");
Element last = set == null ? null : set.findChild("last");
- if (complete || last == null) {
+ Element first = set == null ? null : set.findChild("first");
+ Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first;
+ if (complete || relevant == null) {
this.finalizeQuery(query);
} else {
- final Query nextQuery = query.next(last == null ? null : last.getContent());
+ final Query nextQuery;
+ if (query.getPagingOrder() == PagingOrder.NORMAL) {
+ nextQuery = query.next(last == null ? null : last.getContent());
+ } else {
+ nextQuery = query.prev(first == null ? null : first.getContent());
+ }
this.execute(nextQuery);
+ this.finalizeQuery(query);
synchronized (this.queries) {
this.queries.remove(query);
this.queries.add(nextQuery);
@@ -164,9 +188,11 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
private long end;
private Jid with = null;
private String queryId;
- private String after = null;
+ private String reference = null;
private Account account;
private Conversation conversation;
+ private PagingOrder pagingOrder = PagingOrder.NORMAL;
+
public Query(Conversation conversation, long start, long end) {
this(conversation.getAccount(), start, end);
@@ -174,6 +200,11 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
this.with = conversation.getContactJid().toBareJid();
}
+ public Query(Conversation conversation, long start, long end, PagingOrder order) {
+ this(conversation,start,end);
+ this.pagingOrder = order;
+ }
+
public Query(Account account, long start, long end) {
this.account = account;
this.start = start;
@@ -181,16 +212,32 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
}
- public Query next(String after) {
+ private Query page(String reference) {
Query query = new Query(this.account,this.start,this.end);
- query.after = after;
+ query.reference = reference;
query.conversation = conversation;
query.with = with;
return query;
}
- public String getAfter() {
- return after;
+ public Query next(String reference) {
+ Query query = page(reference);
+ query.pagingOrder = PagingOrder.NORMAL;
+ return query;
+ }
+
+ public Query prev(String reference) {
+ Query query = page(reference);
+ query.pagingOrder = PagingOrder.REVERSE;
+ return query;
+ }
+
+ public String getReference() {
+ return reference;
+ }
+
+ public PagingOrder getPagingOrder() {
+ return this.pagingOrder;
}
public String getQueryId() {
@@ -230,9 +277,13 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
builder.append(AbstractGenerator.getTimestamp(this.start));
builder.append(", end=");
builder.append(AbstractGenerator.getTimestamp(this.end));
- if (this.after!=null) {
- builder.append(", after=");
- builder.append(this.after);
+ if (this.reference!=null) {
+ if (this.pagingOrder == PagingOrder.NORMAL) {
+ builder.append(", after=");
+ } else {
+ builder.append(", before=");
+ }
+ builder.append(this.reference);
}
return builder.toString();
}
diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
index 16a92d4f..43e4a377 100644
--- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
+++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java
@@ -956,6 +956,9 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa
}
public int loadMoreMessages(Conversation conversation, long timestamp) {
+ if (this.getMessageArchiveService().queryInProgress(conversation)) {
+ return 0;
+ }
List<Message> messages = databaseBackend.getMessages(conversation, 50,
timestamp);
for (Message message : messages) {