ignore IQ result when MAM query had been killed

This commit is contained in:
Daniel Gultsch 2020-07-26 16:39:48 +02:00
parent 1c66772202
commit 6941d5edd1

View file

@ -225,19 +225,31 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
private void execute(final Query query) { private void execute(final Query query) {
final Account account = query.getAccount(); final Account account = query.getAccount();
if (account.getStatus() == Account.State.ONLINE) { if (account.getStatus() == Account.State.ONLINE) {
final Conversation conversation = query.getConversation();
if (conversation != null && conversation.getStatus() == Conversation.STATUS_ARCHIVED) {
throw new IllegalStateException("Attempted to run MAM query for archived conversation");
}
Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": running mam query " + query.toString()); Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": running mam query " + query.toString());
IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query); final IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query);
this.mXmppConnectionService.sendIqPacket(account, packet, (a, p) -> { this.mXmppConnectionService.sendIqPacket(account, packet, (a, p) -> {
Element fin = p.findChild("fin", query.version.namespace); final Element fin = p.findChild("fin", query.version.namespace);
if (p.getType() == IqPacket.TYPE.TIMEOUT) { if (p.getType() == IqPacket.TYPE.TIMEOUT) {
synchronized (MessageArchiveService.this.queries) { synchronized (this.queries) {
MessageArchiveService.this.queries.remove(query); this.queries.remove(query);
if (query.hasCallback()) { if (query.hasCallback()) {
query.callback(false); query.callback(false);
} }
} }
} else if (p.getType() == IqPacket.TYPE.RESULT && fin != null) { } else if (p.getType() == IqPacket.TYPE.RESULT && fin != null) {
processFin(query, fin); final boolean running;
synchronized (this.queries) {
running = this.queries.contains(query);
}
if (running) {
processFin(query, fin);
} else {
Log.d(Config.LOGTAG,account.getJid().asBareJid()+": ignoring MAM iq result because query had been killed");
}
} else if (p.getType() == IqPacket.TYPE.RESULT && query.isLegacy()) { } else if (p.getType() == IqPacket.TYPE.RESULT && query.isLegacy()) {
//do nothing //do nothing
} else { } else {
@ -252,9 +264,11 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
} }
} }
private void finalizeQuery(Query query, boolean done) { private void finalizeQuery(final Query query, boolean done) {
synchronized (this.queries) { synchronized (this.queries) {
this.queries.remove(query); if (!this.queries.remove(query)) {
throw new IllegalStateException("Unable to remove query from queries");
}
} }
final Conversation conversation = query.getConversation(); final Conversation conversation = query.getConversation();
if (conversation != null) { if (conversation != null) {
@ -377,7 +391,7 @@ public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
void kill(Conversation conversation) { void kill(Conversation conversation) {
final ArrayList<Query> toBeKilled = new ArrayList<>(); final ArrayList<Query> toBeKilled = new ArrayList<>();
synchronized (this.queries) { synchronized (this.queries) {
for (Query q : queries) { for (final Query q : queries) {
if (q.conversation == conversation) { if (q.conversation == conversation) {
toBeKilled.add(q); toBeKilled.add(q);
} }