MessageArchiveService.java

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