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

488 lines
15 KiB
Java
Raw Normal View History

2014-12-05 00:54:16 +00:00
package eu.siacs.conversations.services;
import android.util.Log;
import android.util.Pair;
2014-12-05 00:54:16 +00:00
import java.math.BigInteger;
2014-12-14 22:23:32 +00:00
import java.util.ArrayList;
2014-12-05 00:54:16 +00:00
import java.util.HashSet;
2014-12-14 22:23:32 +00:00
import java.util.Iterator;
import java.util.List;
2014-12-05 00:54:16 +00:00
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
2014-12-05 00:54:16 +00:00
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;
2017-03-01 12:01:46 +00:00
import eu.siacs.conversations.xml.Namespace;
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;
2017-05-07 19:05:35 +00:00
import eu.siacs.conversations.xmpp.mam.MamReference;
2014-12-05 00:54:16 +00:00
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<>();
private final ArrayList<Query> pendingQueries = new ArrayList<>();
2014-12-05 00:54:16 +00:00
public enum PagingOrder {
NORMAL,
REVERSE
}
2014-12-05 00:54:16 +00:00
public MessageArchiveService(final XmppConnectionService service) {
this.mXmppConnectionService = service;
}
private void catchup(final Account account) {
synchronized (this.queries) {
for(Iterator<Query> iterator = this.queries.iterator(); iterator.hasNext();) {
Query query = iterator.next();
if (query.getAccount() == account) {
iterator.remove();
}
}
}
2017-05-07 19:05:35 +00:00
MamReference mamReference = MamReference.max(
mXmppConnectionService.databaseBackend.getLastMessageReceived(account),
mXmppConnectionService.databaseBackend.getLastClearDate(account)
);
mamReference = MamReference.max(mamReference,mXmppConnectionService.getAutomaticMessageDeletionDate());
2014-12-13 11:25:52 +00:00
long endCatchup = account.getXmppConnection().getLastSessionEstablished();
final Query query;
2017-05-07 19:05:35 +00:00
if (mamReference.getTimestamp() == 0) {
2014-12-13 11:25:52 +00:00
return;
2017-05-07 19:05:35 +00:00
} else if (endCatchup - mamReference.getTimestamp() >= Config.MAM_MAX_CATCHUP) {
long startCatchup = endCatchup - Config.MAM_MAX_CATCHUP;
2014-12-13 11:25:52 +00:00
List<Conversation> conversations = mXmppConnectionService.getConversations();
for (Conversation conversation : conversations) {
2017-05-07 19:05:35 +00:00
if (conversation.getMode() == Conversation.MODE_SINGLE && conversation.getAccount() == account && startCatchup > conversation.getLastMessageTransmitted().getTimestamp()) {
this.query(conversation,startCatchup,true);
2014-12-13 11:25:52 +00:00
}
}
2017-05-07 19:05:35 +00:00
query = new Query(account, new MamReference(startCatchup), endCatchup);
} else {
2017-05-07 19:05:35 +00:00
query = new Query(account, mamReference, endCatchup);
2014-12-13 11:25:52 +00:00
}
synchronized (this.queries) {
this.queries.add(query);
}
2014-12-13 11:25:52 +00:00
this.execute(query);
}
public void catchupMUC(final Conversation conversation) {
2017-05-07 19:05:35 +00:00
if (conversation.getLastMessageTransmitted().getTimestamp() < 0 && conversation.countMessages() == 0) {
query(conversation,
2017-05-07 19:05:35 +00:00
new MamReference(0),
System.currentTimeMillis(),
true);
} else {
query(conversation,
conversation.getLastMessageTransmitted(),
System.currentTimeMillis(),
true);
}
}
public Query query(final Conversation conversation) {
2017-05-07 19:05:35 +00:00
if (conversation.getLastMessageTransmitted().getTimestamp() < 0 && conversation.countMessages() == 0) {
return query(conversation,
2017-05-07 19:05:35 +00:00
new MamReference(0),
System.currentTimeMillis(),
false);
} else {
return query(conversation,
conversation.getLastMessageTransmitted(),
conversation.getAccount().getXmppConnection().getLastSessionEstablished(),
false);
}
2014-12-13 11:25:52 +00:00
}
public boolean isCatchingUp(Conversation conversation) {
final Account account = conversation.getAccount();
if (account.getXmppConnection().isWaitingForSmCatchup()) {
return true;
} else {
synchronized (this.queries) {
for(Query query : this.queries) {
if (query.getAccount() == account && query.isCatchup() && ((conversation.getMode() == Conversation.MODE_SINGLE && query.getWith() == null) || query.getConversation() == conversation)) {
return true;
}
}
}
return false;
}
}
public Query query(final Conversation conversation, long end, boolean allowCatchup) {
return this.query(conversation,conversation.getLastMessageTransmitted(),end, allowCatchup);
}
2017-05-07 19:05:35 +00:00
public Query query(Conversation conversation, MamReference start, long end, boolean allowCatchup) {
2014-12-05 00:54:16 +00:00
synchronized (this.queries) {
2017-02-14 15:50:33 +00:00
final Query query;
2017-05-07 19:05:35 +00:00
final MamReference startActual = MamReference.max(start,mXmppConnectionService.getAutomaticMessageDeletionDate());
if (start.getTimestamp() == 0) {
2017-02-14 15:50:33 +00:00
query = new Query(conversation, startActual, end, false);
query.reference = conversation.getFirstMamReference();
2017-02-14 15:50:33 +00:00
} else {
2017-05-07 19:05:35 +00:00
if (allowCatchup) {
MamReference maxCatchup = MamReference.max(startActual, System.currentTimeMillis() - Config.MAM_MAX_CATCHUP);
if (maxCatchup.greaterThan(startActual)) {
Query reverseCatchup = new Query(conversation, startActual, maxCatchup.getTimestamp(), false);
this.queries.add(reverseCatchup);
this.execute(reverseCatchup);
}
query = new Query(conversation, maxCatchup, end, allowCatchup);
} else {
query = new Query(conversation, startActual, end, false);
2017-02-14 15:50:33 +00:00
}
}
2017-05-07 19:05:35 +00:00
if (start.greaterThan(end)) {
return null;
}
2014-12-05 00:54:16 +00:00
this.queries.add(query);
2014-12-13 11:25:52 +00:00
this.execute(query);
return query;
2014-12-13 11:25:52 +00:00
}
}
2014-12-14 22:23:32 +00:00
public void executePendingQueries(final Account account) {
List<Query> pending = new ArrayList<>();
synchronized(this.pendingQueries) {
for(Iterator<Query> iterator = this.pendingQueries.iterator(); iterator.hasNext();) {
Query query = iterator.next();
if (query.getAccount() == account) {
pending.add(query);
iterator.remove();
}
}
}
for(Query query : pending) {
this.execute(query);
}
}
2014-12-13 11:25:52 +00:00
private void execute(final Query query) {
2014-12-14 22:23:32 +00:00
final Account account= query.getAccount();
if (account.getStatus() == Account.State.ONLINE) {
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": running mam query " + query.toString());
IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query);
this.mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
2014-12-05 00:54:16 +00:00
@Override
public void onIqPacketReceived(Account account, IqPacket packet) {
2017-03-01 12:01:46 +00:00
Element fin = packet.findChild("fin", Namespace.MAM);
if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
synchronized (MessageArchiveService.this.queries) {
MessageArchiveService.this.queries.remove(query);
if (query.hasCallback()) {
query.callback(false);
}
}
2017-02-15 15:42:35 +00:00
} else if (packet.getType() == IqPacket.TYPE.RESULT && fin != null ) {
2017-02-14 16:19:45 +00:00
processFin(fin);
2017-02-15 15:42:35 +00:00
} else if (packet.getType() == IqPacket.TYPE.RESULT && query.isLegacy()) {
//do nothing
2017-02-14 16:19:45 +00:00
} else {
2014-12-14 22:23:32 +00:00
Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": error executing mam: " + packet.toString());
finalizeQuery(query, true);
}
2014-12-05 00:54:16 +00:00
}
});
2014-12-14 22:23:32 +00:00
} else {
synchronized (this.pendingQueries) {
this.pendingQueries.add(query);
}
}
2014-12-05 00:54:16 +00:00
}
private void finalizeQuery(Query query, boolean done) {
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();
conversation.setHasMessagesLeftOnServer(!done);
2014-12-13 11:25:52 +00:00
} else {
for(Conversation tmp : this.mXmppConnectionService.getConversations()) {
if (tmp.getAccount() == query.getAccount()) {
tmp.sort();
}
}
}
if (query.hasCallback()) {
query.callback(done);
} else {
this.mXmppConnectionService.updateConversationUi();
}
}
public boolean queryInProgress(Conversation conversation, XmppConnectionService.OnMoreMessagesLoaded callback) {
synchronized (this.queries) {
for(Query query : queries) {
if (query.conversation == conversation) {
if (!query.hasCallback() && callback != null) {
query.setCallback(callback);
}
return true;
}
}
return false;
}
}
public boolean queryInProgress(Conversation conversation) {
return queryInProgress(conversation, null);
}
2017-02-19 13:47:57 +00:00
public void processFinLegacy(Element fin, Jid from) {
2017-02-15 15:42:35 +00:00
Query query = findQuery(fin.getAttribute("queryid"));
if (query != null && query.validFrom(from)) {
processFin(fin);
}
}
2017-02-14 16:19:45 +00:00
public void processFin(Element fin) {
2014-12-05 00:54:16 +00:00
Query query = findQuery(fin.getAttribute("queryid"));
2017-02-14 16:19:45 +00:00
if (query == null) {
2014-12-05 00:54:16 +00:00
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;
2017-02-14 15:50:33 +00:00
boolean abort = (!query.isCatchup() && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES;
if (query.getConversation() != null) {
query.getConversation().setFirstMamReference(first == null ? null : first.getContent());
}
if (complete || relevant == null || abort) {
2017-02-14 15:50:33 +00:00
final boolean done = (complete || query.getActualMessageCount() == 0) && !query.isCatchup();
this.finalizeQuery(query, done);
2017-02-14 15:50:33 +00:00
Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": finished mam after "+query.getTotalCount()+"("+query.getActualMessageCount()+") messages. messages left="+Boolean.toString(!done));
if (query.isCatchup() && query.getActualMessageCount() > 0) {
mXmppConnectionService.getNotificationService().finishBacklog(true,query.getAccount());
2015-12-10 22:05:11 +00:00
}
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, false);
2014-12-05 00:54:16 +00:00
synchronized (this.queries) {
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 int totalCount = 0;
2017-02-14 15:50:33 +00:00
private int actualCount = 0;
2014-12-05 00:54:16 +00:00
private long start;
private long end;
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;
private XmppConnectionService.OnMoreMessagesLoaded callback = null;
2017-02-14 15:50:33 +00:00
private boolean catchup = true;
2014-12-05 00:54:16 +00:00
2017-05-07 19:05:35 +00:00
public Query(Conversation conversation, MamReference start, long end, boolean catchup) {
this(conversation.getAccount(),catchup ? start : start.timeOnly(),end);
2014-12-05 00:54:16 +00:00
this.conversation = conversation;
2017-02-14 15:50:33 +00:00
this.pagingOrder = catchup ? PagingOrder.NORMAL : PagingOrder.REVERSE;
this.catchup = catchup;
}
2017-05-07 19:05:35 +00:00
public Query(Account account, MamReference start, long end) {
2014-12-13 11:25:52 +00:00
this.account = account;
2017-05-07 19:05:35 +00:00
if (start.getReference() != null) {
this.reference = start.getReference();
} else {
this.start = start.getTimestamp();
}
2014-12-05 00:54:16 +00:00
this.end = end;
this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
}
private Query page(String reference) {
2017-05-07 19:05:35 +00:00
Query query = new Query(this.account,new MamReference(this.start,reference),this.end);
2014-12-13 11:25:52 +00:00
query.conversation = conversation;
query.totalCount = totalCount;
2017-02-14 15:50:33 +00:00
query.actualCount = actualCount;
query.callback = callback;
2017-02-14 15:50:33 +00:00
query.catchup = catchup;
2014-12-05 00:54:16 +00:00
return query;
}
2017-02-15 15:42:35 +00:00
public boolean isLegacy() {
if (conversation == null || conversation.getMode() == Conversation.MODE_SINGLE) {
return account.getXmppConnection().getFeatures().mamLegacy();
} else {
return conversation.getMucOptions().mamLegacy();
}
}
public boolean safeToExtractTrueCounterpart() {
return muc() && !isLegacy();
}
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() {
2015-01-23 23:22:51 +00:00
return conversation == null ? null : conversation.getJid().toBareJid();
}
public boolean muc() {
return conversation != null && conversation.getMode() == Conversation.MODE_MULTI;
2014-12-05 00:54:16 +00:00
}
public long getStart() {
return start;
}
2017-02-14 15:50:33 +00:00
public boolean isCatchup() {
return catchup;
}
public void setCallback(XmppConnectionService.OnMoreMessagesLoaded callback) {
this.callback = callback;
}
public void callback(boolean done) {
if (this.callback != null) {
2017-02-14 15:50:33 +00:00
this.callback.onMoreMessagesLoaded(actualCount,conversation);
if (done) {
this.callback.informUser(R.string.no_more_history_on_server);
}
}
}
2014-12-05 00:54:16 +00:00
public long getEnd() {
return end;
}
public Conversation getConversation() {
return conversation;
}
2014-12-13 11:25:52 +00:00
public Account getAccount() {
return this.account;
}
2014-12-17 05:59:58 +00:00
public void incrementMessageCount() {
2016-02-03 15:04:21 +00:00
this.totalCount++;
2014-12-17 05:59:58 +00:00
}
2017-02-14 15:50:33 +00:00
public void incrementActualMessageCount() {
this.actualCount++;
}
public int getTotalCount() {
return this.totalCount;
}
2017-02-14 15:50:33 +00:00
public int getActualMessageCount() {
return this.actualCount;
2015-01-02 23:47:22 +00:00
}
public boolean validFrom(Jid from) {
if (muc()) {
return getWith().equals(from);
} else {
return (from == null) || account.getJid().toBareJid().equals(from.toBareJid());
}
}
2014-12-13 11:25:52 +00:00
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
2015-01-23 23:22:51 +00:00
if (this.muc()) {
builder.append("to=");
builder.append(this.getWith().toString());
2014-12-13 11:25:52 +00:00
} else {
2015-01-23 23:22:51 +00:00
builder.append("with=");
if (this.getWith() == null) {
builder.append("*");
} else {
builder.append(getWith().toString());
}
2014-12-13 11:25:52 +00:00
}
2017-05-07 19:05:35 +00:00
if (this.start != 0) {
builder.append(", start=");
builder.append(AbstractGenerator.getTimestamp(this.start));
}
2014-12-13 11:25:52 +00:00
builder.append(", end=");
builder.append(AbstractGenerator.getTimestamp(this.end));
2017-02-14 15:50:33 +00:00
builder.append(", order="+pagingOrder.toString());
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
}
2017-02-14 15:50:33 +00:00
builder.append(", catchup="+Boolean.toString(catchup));
2014-12-13 11:25:52 +00:00
return builder.toString();
}
public boolean hasCallback() {
return this.callback != null;
}
2014-12-05 00:54:16 +00:00
}
}