conversations-classic/src/main/java/eu/siacs/conversations/services/MessageArchiveService.java

292 lines
8.2 KiB
Java
Raw Normal View History

2014-12-05 00:54:16 +00:00
package eu.siacs.conversations.services;
import android.util.Log;
import java.math.BigInteger;
import java.util.HashSet;
import java.util.List;
2014-12-05 00:54:16 +00:00
import eu.siacs.conversations.Config;
import eu.siacs.conversations.entities.Account;
import eu.siacs.conversations.entities.Conversation;
2014-12-13 11:25:52 +00:00
import eu.siacs.conversations.generator.AbstractGenerator;
import eu.siacs.conversations.parser.AbstractParser;
2014-12-05 00:54:16 +00:00
import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
2014-12-05 00:54:16 +00:00
import eu.siacs.conversations.xmpp.OnIqPacketReceived;
import eu.siacs.conversations.xmpp.jid.Jid;
import eu.siacs.conversations.xmpp.stanzas.IqPacket;
public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
2014-12-05 00:54:16 +00:00
private final XmppConnectionService mXmppConnectionService;
private final HashSet<Query> queries = new HashSet<Query>();
public enum PagingOrder {
NORMAL,
REVERSE
};
2014-12-05 00:54:16 +00:00
public MessageArchiveService(final XmppConnectionService service) {
this.mXmppConnectionService = service;
}
2014-12-13 11:25:52 +00:00
public void catchup(final Account account) {
long startCatchup = getLastMessageTransmitted(account);
long endCatchup = account.getXmppConnection().getLastSessionEstablished();
if (startCatchup == 0) {
return;
} else if (endCatchup - startCatchup >= Config.MAX_CATCHUP) {
startCatchup = endCatchup - Config.MAX_CATCHUP;
List<Conversation> conversations = mXmppConnectionService.getConversations();
for (Conversation conversation : conversations) {
if (conversation.getMode() == Conversation.MODE_SINGLE && conversation.getAccount() == account && startCatchup > conversation.getLastMessageTransmitted()) {
this.query(conversation,startCatchup);
}
}
}
final Query query = new Query(account, startCatchup, endCatchup);
this.queries.add(query);
this.execute(query);
}
private long getLastMessageTransmitted(final Account account) {
long timestamp = 0;
for(final Conversation conversation : mXmppConnectionService.getConversations()) {
if (conversation.getAccount() == account) {
long tmp = conversation.getLastMessageTransmitted();
if (tmp > timestamp) {
timestamp = tmp;
}
}
}
return timestamp;
}
2014-12-05 00:54:16 +00:00
public void query(final Conversation conversation) {
2014-12-13 11:25:52 +00:00
query(conversation,conversation.getAccount().getXmppConnection().getLastSessionEstablished());
}
public void query(final Conversation conversation, long end) {
2014-12-05 00:54:16 +00:00
synchronized (this.queries) {
final Account account = conversation.getAccount();
long start = conversation.getLastMessageTransmitted();
2014-12-13 11:25:52 +00:00
if (start > end) {
return;
} else if (end - start >= Config.MAX_HISTORY_AGE) {
start = end - Config.MAX_HISTORY_AGE;
}
final Query query = new Query(conversation, start, end,PagingOrder.REVERSE);
2014-12-05 00:54:16 +00:00
this.queries.add(query);
2014-12-13 11:25:52 +00:00
this.execute(query);
}
}
private void execute(final Query query) {
Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid().toString()+": running mam query "+query.toString());
IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query);
this.mXmppConnectionService.sendIqPacket(query.getAccount(), packet, new OnIqPacketReceived() {
2014-12-05 00:54:16 +00:00
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
if (packet.getType() == IqPacket.TYPE_ERROR) {
2014-12-13 11:25:52 +00:00
Log.d(Config.LOGTAG,account.getJid().toBareJid().toString()+": error executing mam: "+packet.toString());
finalizeQuery(query);
}
2014-12-05 00:54:16 +00:00
}
});
}
private void finalizeQuery(Query query) {
synchronized (this.queries) {
this.queries.remove(query);
}
final Conversation conversation = query.getConversation();
2014-12-13 11:25:52 +00:00
if (conversation != null) {
conversation.sort();
if (conversation.setLastMessageTransmitted(query.getEnd())) {
this.mXmppConnectionService.databaseBackend.updateConversation(conversation);
}
this.mXmppConnectionService.updateConversationUi();
} else {
for(Conversation tmp : this.mXmppConnectionService.getConversations()) {
if (tmp.getAccount() == query.getAccount()) {
tmp.sort();
if (tmp.setLastMessageTransmitted(query.getEnd())) {
this.mXmppConnectionService.databaseBackend.updateConversation(tmp);
}
}
}
}
}
public boolean queryInProgress(Conversation conversation) {
synchronized (this.queries) {
for(Query query : queries) {
if (query.conversation == conversation) {
return true;
}
}
return false;
}
}
2014-12-05 00:54:16 +00:00
public void processFin(Element fin) {
if (fin == null) {
return;
}
Query query = findQuery(fin.getAttribute("queryid"));
if (query == null) {
return;
}
boolean complete = fin.getAttributeAsBoolean("complete");
Element set = fin.findChild("set","http://jabber.org/protocol/rsm");
Element last = set == null ? null : set.findChild("last");
Element first = set == null ? null : set.findChild("first");
Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first;
if (complete || relevant == null) {
this.finalizeQuery(query);
2014-12-05 00:54:16 +00:00
} else {
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());
}
2014-12-13 11:25:52 +00:00
this.execute(nextQuery);
this.finalizeQuery(query);
2014-12-05 00:54:16 +00:00
synchronized (this.queries) {
this.queries.remove(query);
this.queries.add(nextQuery);
}
}
}
2014-12-13 11:25:52 +00:00
public Query findQuery(String id) {
2014-12-05 00:54:16 +00:00
if (id == null) {
return null;
}
synchronized (this.queries) {
for(Query query : this.queries) {
if (query.getQueryId().equals(id)) {
return query;
}
}
return null;
}
}
@Override
public void onAdvancedStreamFeaturesAvailable(Account account) {
if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) {
2014-12-13 11:25:52 +00:00
this.catchup(account);
}
}
2014-12-05 00:54:16 +00:00
public class Query {
private long start;
private long end;
2014-12-13 11:25:52 +00:00
private Jid with = null;
2014-12-05 00:54:16 +00:00
private String queryId;
private String reference = null;
2014-12-13 11:25:52 +00:00
private Account account;
2014-12-05 00:54:16 +00:00
private Conversation conversation;
private PagingOrder pagingOrder = PagingOrder.NORMAL;
2014-12-05 00:54:16 +00:00
public Query(Conversation conversation, long start, long end) {
2014-12-13 11:25:52 +00:00
this(conversation.getAccount(), start, end);
2014-12-05 00:54:16 +00:00
this.conversation = conversation;
this.with = conversation.getContactJid().toBareJid();
2014-12-13 11:25:52 +00:00
}
public Query(Conversation conversation, long start, long end, PagingOrder order) {
this(conversation,start,end);
this.pagingOrder = order;
}
2014-12-13 11:25:52 +00:00
public Query(Account account, long start, long end) {
this.account = account;
2014-12-05 00:54:16 +00:00
this.start = start;
this.end = end;
this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
}
private Query page(String reference) {
2014-12-13 11:25:52 +00:00
Query query = new Query(this.account,this.start,this.end);
query.reference = reference;
2014-12-13 11:25:52 +00:00
query.conversation = conversation;
query.with = with;
2014-12-05 00:54:16 +00:00
return query;
}
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;
2014-12-05 00:54:16 +00:00
}
public String getQueryId() {
return queryId;
}
public Jid getWith() {
return with;
}
public long getStart() {
return start;
}
public long getEnd() {
return end;
}
public Conversation getConversation() {
return conversation;
}
2014-12-13 11:25:52 +00:00
public Account getAccount() {
return this.account;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("with=");
if (this.with==null) {
builder.append("*");
} else {
builder.append(with.toString());
}
builder.append(", start=");
builder.append(AbstractGenerator.getTimestamp(this.start));
builder.append(", end=");
builder.append(AbstractGenerator.getTimestamp(this.end));
if (this.reference!=null) {
if (this.pagingOrder == PagingOrder.NORMAL) {
builder.append(", after=");
} else {
builder.append(", before=");
}
builder.append(this.reference);
2014-12-13 11:25:52 +00:00
}
return builder.toString();
}
2014-12-05 00:54:16 +00:00
}
}