MessageArchiveService.java

  1package eu.siacs.conversations.services;
  2
  3import android.util.Log;
  4
  5import org.jetbrains.annotations.NotNull;
  6
  7import java.math.BigInteger;
  8import java.util.ArrayList;
  9import java.util.HashSet;
 10import java.util.Iterator;
 11import java.util.List;
 12
 13import eu.siacs.conversations.Config;
 14import eu.siacs.conversations.R;
 15import eu.siacs.conversations.entities.Account;
 16import eu.siacs.conversations.entities.Conversation;
 17import eu.siacs.conversations.entities.Conversational;
 18import eu.siacs.conversations.entities.ReceiptRequest;
 19import eu.siacs.conversations.generator.AbstractGenerator;
 20import eu.siacs.conversations.xml.Element;
 21import eu.siacs.conversations.xmpp.Jid;
 22import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
 23import eu.siacs.conversations.xmpp.mam.MamReference;
 24import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 25import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 26
 27public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
 28
 29	private final XmppConnectionService mXmppConnectionService;
 30
 31	private final HashSet<Query> queries = new HashSet<>();
 32	private final ArrayList<Query> pendingQueries = new ArrayList<>();
 33
 34	public enum Version {
 35		MAM_0("urn:xmpp:mam:0", true),
 36		MAM_1("urn:xmpp:mam:1", false),
 37		MAM_2("urn:xmpp:mam:2", false);
 38
 39		public final boolean legacy;
 40		public final String namespace;
 41
 42		Version(String namespace, boolean legacy) {
 43			this.namespace = namespace;
 44			this.legacy = legacy;
 45		}
 46
 47		public static Version get(Account account) {
 48			return get(account,null);
 49		}
 50
 51		public static Version get(Account account, Conversation conversation) {
 52			if (conversation == null || conversation.getMode() == Conversation.MODE_SINGLE) {
 53				return get(account.getXmppConnection().getFeatures().getAccountFeatures());
 54			} else {
 55				return get(conversation.getMucOptions().getFeatures());
 56			}
 57		}
 58
 59		private static Version get(List<String> features) {
 60			final Version[] values = values();
 61			for(int i = values.length -1; i >= 0; --i) {
 62				for(String feature : features) {
 63					if (values[i].namespace.equals(feature)) {
 64						return values[i];
 65					}
 66				}
 67			}
 68			return MAM_0;
 69		}
 70
 71		public static boolean has(List<String> features) {
 72			for(String feature : features) {
 73				for(Version version : values()) {
 74					if (version.namespace.equals(feature)) {
 75						return true;
 76					}
 77				}
 78			}
 79			return false;
 80		}
 81
 82		public static Element findResult(MessagePacket packet) {
 83			for(Version version : values()) {
 84				Element result = packet.findChild("result", version.namespace);
 85				if (result != null) {
 86					return result;
 87				}
 88			}
 89			return null;
 90		}
 91
 92	}
 93
 94    MessageArchiveService(final XmppConnectionService service) {
 95		this.mXmppConnectionService = service;
 96	}
 97
 98	private void catchup(final Account account) {
 99		synchronized (this.queries) {
100			for (Iterator<Query> iterator = this.queries.iterator(); iterator.hasNext(); ) {
101				Query query = iterator.next();
102				if (query.getAccount() == account) {
103					iterator.remove();
104				}
105			}
106		}
107		MamReference mamReference = MamReference.max(
108				mXmppConnectionService.databaseBackend.getLastMessageReceived(account),
109				mXmppConnectionService.databaseBackend.getLastClearDate(account)
110		);
111		mamReference = MamReference.max(mamReference, mXmppConnectionService.getAutomaticMessageDeletionDate());
112		long endCatchup = account.getXmppConnection().getLastSessionEstablished();
113		final Query query;
114		if (mamReference.getTimestamp() == 0) {
115			return;
116		} else if (endCatchup - mamReference.getTimestamp() >= Config.MAM_MAX_CATCHUP) {
117			long startCatchup = endCatchup - Config.MAM_MAX_CATCHUP;
118			List<Conversation> conversations = mXmppConnectionService.getConversations();
119			for (Conversation conversation : conversations) {
120				if (conversation.getMode() == Conversation.MODE_SINGLE && conversation.getAccount() == account && startCatchup > conversation.getLastMessageTransmitted().getTimestamp()) {
121					this.query(conversation, startCatchup, true);
122				}
123			}
124			query = new Query(account, new MamReference(startCatchup), 0);
125		} else {
126			query = new Query(account, mamReference, 0);
127		}
128		synchronized (this.queries) {
129			this.queries.add(query);
130		}
131		this.execute(query);
132	}
133
134	void catchupMUC(final Conversation conversation) {
135		if (conversation.getLastMessageTransmitted().getTimestamp() < 0 && conversation.countMessages() == 0) {
136			query(conversation,
137					new MamReference(0),
138					0,
139					true);
140		} else {
141			query(conversation,
142					conversation.getLastMessageTransmitted(),
143					0,
144					true);
145		}
146	}
147
148	public Query query(final Conversation conversation) {
149		if (conversation.getLastMessageTransmitted().getTimestamp() < 0 && conversation.countMessages() == 0) {
150			return query(conversation,
151					new MamReference(0),
152					System.currentTimeMillis(),
153					false);
154		} else {
155			return query(conversation,
156					conversation.getLastMessageTransmitted(),
157					conversation.getAccount().getXmppConnection().getLastSessionEstablished(),
158					false);
159		}
160	}
161
162	public boolean isCatchingUp(Conversation conversation) {
163		final Account account = conversation.getAccount();
164		if (account.getXmppConnection().isWaitingForSmCatchup()) {
165			return true;
166		} else {
167			synchronized (this.queries) {
168				for (Query query : this.queries) {
169					if (query.getAccount() == account && query.isCatchup() && ((conversation.getMode() == Conversation.MODE_SINGLE && query.getWith() == null) || query.getConversation() == conversation)) {
170						return true;
171					}
172				}
173			}
174			return false;
175		}
176	}
177
178	public Query query(final Conversation conversation, long end, boolean allowCatchup) {
179		return this.query(conversation, conversation.getLastMessageTransmitted(), end, allowCatchup);
180	}
181
182	public Query query(Conversation conversation, MamReference start, long end, boolean allowCatchup) {
183		synchronized (this.queries) {
184			final Query query;
185			final MamReference startActual = MamReference.max(start, mXmppConnectionService.getAutomaticMessageDeletionDate());
186			if (start.getTimestamp() == 0) {
187				query = new Query(conversation, startActual, end, false);
188				query.reference = conversation.getFirstMamReference();
189			} else {
190				if (allowCatchup) {
191					MamReference maxCatchup = MamReference.max(startActual, System.currentTimeMillis() - Config.MAM_MAX_CATCHUP);
192					if (maxCatchup.greaterThan(startActual)) {
193						Query reverseCatchup = new Query(conversation, startActual, maxCatchup.getTimestamp(), false);
194						this.queries.add(reverseCatchup);
195						this.execute(reverseCatchup);
196					}
197					query = new Query(conversation, maxCatchup, end, true);
198				} else {
199					query = new Query(conversation, startActual, end, false);
200				}
201			}
202			if (end != 0 && start.greaterThan(end)) {
203				return null;
204			}
205			this.queries.add(query);
206			this.execute(query);
207			return query;
208		}
209	}
210
211	void executePendingQueries(final Account account) {
212		List<Query> pending = new ArrayList<>();
213		synchronized (this.pendingQueries) {
214			for (Iterator<Query> iterator = this.pendingQueries.iterator(); iterator.hasNext(); ) {
215				Query query = iterator.next();
216				if (query.getAccount() == account) {
217					pending.add(query);
218					iterator.remove();
219				}
220			}
221		}
222		for (Query query : pending) {
223			this.execute(query);
224		}
225	}
226
227	private void execute(final Query query) {
228		final Account account = query.getAccount();
229		if (account.getStatus() == Account.State.ONLINE) {
230			final Conversation conversation = query.getConversation();
231			if (conversation != null && conversation.getStatus() == Conversation.STATUS_ARCHIVED) {
232				throw new IllegalStateException("Attempted to run MAM query for archived conversation");
233			}
234			Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": running mam query " + query.toString());
235			final IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query);
236			this.mXmppConnectionService.sendIqPacket(account, packet, (a, p) -> {
237				final Element fin = p.findChild("fin", query.version.namespace);
238				if (p.getType() == IqPacket.TYPE.TIMEOUT) {
239					synchronized (this.queries) {
240						this.queries.remove(query);
241						if (query.hasCallback()) {
242							query.callback(false);
243						}
244					}
245				} else if (p.getType() == IqPacket.TYPE.RESULT && fin != null) {
246					final boolean running;
247					synchronized (this.queries) {
248						running = this.queries.contains(query);
249					}
250					if (running) {
251						processFin(query, fin);
252					} else {
253						Log.d(Config.LOGTAG,account.getJid().asBareJid()+": ignoring MAM iq result because query had been killed");
254					}
255				} else if (p.getType() == IqPacket.TYPE.RESULT && query.isLegacy()) {
256					//do nothing
257				} else {
258					Log.d(Config.LOGTAG, a.getJid().asBareJid().toString() + ": error executing mam: " + p.toString());
259					finalizeQuery(query, true);
260				}
261			});
262		} else {
263			synchronized (this.pendingQueries) {
264				this.pendingQueries.add(query);
265			}
266		}
267	}
268
269	private void finalizeQuery(final Query query, boolean done) {
270		synchronized (this.queries) {
271			if (!this.queries.remove(query)) {
272				throw new IllegalStateException("Unable to remove query from queries");
273			}
274		}
275		final Conversation conversation = query.getConversation();
276		if (conversation != null) {
277			conversation.sort();
278			conversation.setHasMessagesLeftOnServer(!done);
279		} else {
280			for (Conversation tmp : this.mXmppConnectionService.getConversations()) {
281				if (tmp.getAccount() == query.getAccount()) {
282					tmp.sort();
283				}
284			}
285		}
286		if (query.hasCallback()) {
287			query.callback(done);
288		} else {
289			this.mXmppConnectionService.updateConversationUi();
290		}
291	}
292
293	boolean inCatchup(Account account) {
294		synchronized (this.queries) {
295			for (Query query : queries) {
296				if (query.account == account && query.isCatchup() && query.getWith() == null) {
297					return true;
298				}
299			}
300		}
301		return false;
302	}
303
304	public boolean isCatchupInProgress(Conversation conversation) {
305		synchronized (this.queries) {
306			for(Query query : queries) {
307				if (query.account == conversation.getAccount() && query.isCatchup()) {
308					final Jid with = query.getWith() == null ? null : query.getWith().asBareJid();
309					if ((conversation.getMode() == Conversational.MODE_SINGLE && with == null) || (conversation.getJid().asBareJid().equals(with))) {
310						return true;
311					}
312				}
313			}
314		}
315		return false;
316	}
317
318	boolean queryInProgress(Conversation conversation, XmppConnectionService.OnMoreMessagesLoaded callback) {
319		synchronized (this.queries) {
320			for (Query query : queries) {
321				if (query.conversation == conversation) {
322					if (!query.hasCallback() && callback != null) {
323						query.setCallback(callback);
324					}
325					return true;
326				}
327			}
328			return false;
329		}
330	}
331
332	public boolean queryInProgress(Conversation conversation) {
333		return queryInProgress(conversation, null);
334	}
335
336	public void processFinLegacy(Element fin, Jid from) {
337		Query query = findQuery(fin.getAttribute("queryid"));
338		if (query != null && query.validFrom(from)) {
339			processFin(query, fin);
340		}
341	}
342
343	private void processFin(Query query, Element fin) {
344		boolean complete = fin.getAttributeAsBoolean("complete");
345		Element set = fin.findChild("set", "http://jabber.org/protocol/rsm");
346		Element last = set == null ? null : set.findChild("last");
347		String count = set == null ? null : set.findChildContent("count");
348		Element first = set == null ? null : set.findChild("first");
349		Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first;
350		boolean abort = (!query.isCatchup() && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES;
351		if (query.getConversation() != null) {
352			query.getConversation().setFirstMamReference(first == null ? null : first.getContent());
353		}
354		if (complete || relevant == null || abort) {
355			//TODO: FIX done logic to look at complete. using count is probably unreliable because it can be ommited and doesn’t work with paging.
356			boolean done;
357			if (query.isCatchup()) {
358				done = false;
359			} else {
360				if (count != null) {
361					try {
362						done = Integer.parseInt(count) <= query.getTotalCount();
363					} catch (NumberFormatException e) {
364						done = false;
365					}
366				} else {
367					done = query.getTotalCount() == 0;
368				}
369			}
370			done = done || (query.getActualMessageCount() == 0 && !query.isCatchup());
371			this.finalizeQuery(query, done);
372
373			Log.d(Config.LOGTAG, query.getAccount().getJid().asBareJid() + ": finished mam after " + query.getTotalCount() + "(" + query.getActualMessageCount() + ") messages. messages left=" + !done + " count=" + count);
374			if (query.isCatchup() && query.getActualMessageCount() > 0) {
375				mXmppConnectionService.getNotificationService().finishBacklog(true, query.getAccount());
376			}
377			processPostponed(query);
378		} else {
379			final Query nextQuery;
380			if (query.getPagingOrder() == PagingOrder.NORMAL) {
381				nextQuery = query.next(last == null ? null : last.getContent());
382			} else {
383				nextQuery = query.prev(first == null ? null : first.getContent());
384			}
385			this.execute(nextQuery);
386			this.finalizeQuery(query, false);
387			synchronized (this.queries) {
388				this.queries.add(nextQuery);
389			}
390		}
391	}
392
393	void kill(Conversation conversation) {
394		final ArrayList<Query> toBeKilled = new ArrayList<>();
395		synchronized (this.queries) {
396			for (final Query q : queries) {
397				if (q.conversation == conversation) {
398					toBeKilled.add(q);
399				}
400			}
401		}
402		for (Query q : toBeKilled) {
403			kill(q);
404		}
405	}
406
407	private void kill(Query query) {
408		Log.d(Config.LOGTAG, query.getAccount().getJid().asBareJid() + ": killing mam query prematurely");
409		query.callback = null;
410		this.finalizeQuery(query, false);
411		if (query.isCatchup() && query.getActualMessageCount() > 0) {
412			mXmppConnectionService.getNotificationService().finishBacklog(true, query.getAccount());
413		}
414		this.processPostponed(query);
415	}
416
417	private void processPostponed(Query query) {
418		query.account.getAxolotlService().processPostponed();
419		query.pendingReceiptRequests.removeAll(query.receiptRequests);
420		Log.d(Config.LOGTAG, query.getAccount().getJid().asBareJid() + ": found " + query.pendingReceiptRequests.size() + " pending receipt requests");
421		Iterator<ReceiptRequest> iterator = query.pendingReceiptRequests.iterator();
422		while (iterator.hasNext()) {
423			ReceiptRequest rr = iterator.next();
424			mXmppConnectionService.sendMessagePacket(query.account, mXmppConnectionService.getMessageGenerator().received(query.account, rr.getJid(), rr.getId()));
425			iterator.remove();
426		}
427	}
428
429	public Query findQuery(String id) {
430		if (id == null) {
431			return null;
432		}
433		synchronized (this.queries) {
434			for (Query query : this.queries) {
435				if (query.getQueryId().equals(id)) {
436					return query;
437				}
438			}
439			return null;
440		}
441	}
442
443	@Override
444	public void onAdvancedStreamFeaturesAvailable(Account account) {
445		if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) {
446			this.catchup(account);
447		}
448	}
449
450	public enum PagingOrder {
451		NORMAL,
452		REVERSE
453	}
454
455	public class Query {
456		private HashSet<ReceiptRequest> pendingReceiptRequests = new HashSet<>();
457		private HashSet<ReceiptRequest> receiptRequests = new HashSet<>();
458		private int totalCount = 0;
459		private int actualCount = 0;
460		private int actualInThisQuery = 0;
461		private long start;
462		private final long end;
463		private final String queryId;
464		private String reference = null;
465		private final Account account;
466		private Conversation conversation;
467		private PagingOrder pagingOrder = PagingOrder.NORMAL;
468		private XmppConnectionService.OnMoreMessagesLoaded callback = null;
469		private boolean catchup = true;
470		public final Version version;
471
472
473		Query(Conversation conversation, MamReference start, long end, boolean catchup) {
474			this(conversation.getAccount(), Version.get(conversation.getAccount(), conversation), catchup ? start : start.timeOnly(), end);
475			this.conversation = conversation;
476			this.pagingOrder = catchup ? PagingOrder.NORMAL : PagingOrder.REVERSE;
477			this.catchup = catchup;
478		}
479
480		Query(Account account, MamReference start, long end) {
481			this(account, Version.get(account), start, end);
482		}
483
484		Query(Account account, Version version, MamReference start, long end) {
485			this.account = account;
486			if (start.getReference() != null) {
487				this.reference = start.getReference();
488			} else {
489				this.start = start.getTimestamp();
490			}
491			this.end = end;
492			this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
493			this.version = version;
494		}
495
496		private Query page(String reference) {
497			Query query = new Query(this.account, this.version, new MamReference(this.start, reference), this.end);
498			query.conversation = conversation;
499			query.totalCount = totalCount;
500			query.actualCount = actualCount;
501			query.pendingReceiptRequests = pendingReceiptRequests;
502			query.receiptRequests = receiptRequests;
503			query.callback = callback;
504			query.catchup = catchup;
505			return query;
506		}
507
508		public void removePendingReceiptRequest(ReceiptRequest receiptRequest) {
509			if (!this.pendingReceiptRequests.remove(receiptRequest)) {
510				this.receiptRequests.add(receiptRequest);
511			}
512		}
513
514		public void addPendingReceiptRequest(ReceiptRequest receiptRequest) {
515			this.pendingReceiptRequests.add(receiptRequest);
516		}
517
518		public boolean isLegacy() {
519			return version.legacy;
520		}
521
522		public boolean safeToExtractTrueCounterpart() {
523			return muc() && !isLegacy();
524		}
525
526		public Query next(String reference) {
527			Query query = page(reference);
528			query.pagingOrder = PagingOrder.NORMAL;
529			return query;
530		}
531
532		Query prev(String reference) {
533			Query query = page(reference);
534			query.pagingOrder = PagingOrder.REVERSE;
535			return query;
536		}
537
538		public String getReference() {
539			return reference;
540		}
541
542		public PagingOrder getPagingOrder() {
543			return this.pagingOrder;
544		}
545
546		public String getQueryId() {
547			return queryId;
548		}
549
550		public Jid getWith() {
551			return conversation == null ? null : conversation.getJid().asBareJid();
552		}
553
554		public boolean muc() {
555			return conversation != null && conversation.getMode() == Conversation.MODE_MULTI;
556		}
557
558		public long getStart() {
559			return start;
560		}
561
562		public boolean isCatchup() {
563			return catchup;
564		}
565
566		public void setCallback(XmppConnectionService.OnMoreMessagesLoaded callback) {
567			this.callback = callback;
568		}
569
570		public void callback(boolean done) {
571			if (this.callback != null) {
572				this.callback.onMoreMessagesLoaded(actualCount, conversation);
573				if (done) {
574					this.callback.informUser(R.string.no_more_history_on_server);
575				}
576			}
577		}
578
579		public long getEnd() {
580			return end;
581		}
582
583		public Conversation getConversation() {
584			return conversation;
585		}
586
587		public Account getAccount() {
588			return this.account;
589		}
590
591		public void incrementMessageCount() {
592			this.totalCount++;
593		}
594
595		public void incrementActualMessageCount() {
596			this.actualInThisQuery++;
597			this.actualCount++;
598		}
599
600		int getTotalCount() {
601			return this.totalCount;
602		}
603
604		int getActualMessageCount() {
605			return this.actualCount;
606		}
607
608		public int getActualInThisQuery() {
609			return this.actualInThisQuery;
610		}
611
612		public boolean validFrom(Jid from) {
613			if (muc()) {
614				return getWith().equals(from);
615			} else {
616				return (from == null) || account.getJid().asBareJid().equals(from.asBareJid());
617			}
618		}
619
620		@NotNull
621		@Override
622		public String toString() {
623			StringBuilder builder = new StringBuilder();
624			if (this.muc()) {
625				builder.append("to=");
626				builder.append(this.getWith().toString());
627			} else {
628				builder.append("with=");
629				if (this.getWith() == null) {
630					builder.append("*");
631				} else {
632					builder.append(getWith().toString());
633				}
634			}
635			if (this.start != 0) {
636				builder.append(", start=");
637				builder.append(AbstractGenerator.getTimestamp(this.start));
638			}
639			if (this.end != 0) {
640				builder.append(", end=");
641				builder.append(AbstractGenerator.getTimestamp(this.end));
642			}
643			builder.append(", order=").append(pagingOrder.toString());
644			if (this.reference != null) {
645				if (this.pagingOrder == PagingOrder.NORMAL) {
646					builder.append(", after=");
647				} else {
648					builder.append(", before=");
649				}
650				builder.append(this.reference);
651			}
652			builder.append(", catchup=").append(catchup);
653			builder.append(", ns=").append(version.namespace);
654			return builder.toString();
655		}
656
657		boolean hasCallback() {
658			return this.callback != null;
659		}
660	}
661}