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.OnIqPacketReceived;
 21import eu.siacs.conversations.xmpp.jid.Jid;
 22import eu.siacs.conversations.xmpp.mam.MamReference;
 23import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 24
 25public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
 26
 27	private final XmppConnectionService mXmppConnectionService;
 28
 29	private final HashSet<Query> queries = new HashSet<>();
 30	private final ArrayList<Query> pendingQueries = new ArrayList<>();
 31
 32	public enum PagingOrder {
 33		NORMAL,
 34		REVERSE
 35	}
 36
 37	public MessageArchiveService(final XmppConnectionService service) {
 38		this.mXmppConnectionService = service;
 39	}
 40
 41	private void catchup(final Account account) {
 42		synchronized (this.queries) {
 43			for(Iterator<Query> iterator = this.queries.iterator(); iterator.hasNext();) {
 44				Query query = iterator.next();
 45				if (query.getAccount() == account) {
 46					iterator.remove();
 47				}
 48			}
 49		}
 50		MamReference mamReference = MamReference.max(
 51				mXmppConnectionService.databaseBackend.getLastMessageReceived(account),
 52				mXmppConnectionService.databaseBackend.getLastClearDate(account)
 53		);
 54		mamReference = MamReference.max(mamReference,mXmppConnectionService.getAutomaticMessageDeletionDate());
 55		long endCatchup = account.getXmppConnection().getLastSessionEstablished();
 56		final Query query;
 57		if (mamReference.getTimestamp() == 0) {
 58			return;
 59		} else if (endCatchup - mamReference.getTimestamp() >= Config.MAM_MAX_CATCHUP) {
 60			long startCatchup = endCatchup - Config.MAM_MAX_CATCHUP;
 61			List<Conversation> conversations = mXmppConnectionService.getConversations();
 62			for (Conversation conversation : conversations) {
 63				if (conversation.getMode() == Conversation.MODE_SINGLE && conversation.getAccount() == account && startCatchup > conversation.getLastMessageTransmitted().getTimestamp()) {
 64					this.query(conversation,startCatchup,true);
 65				}
 66			}
 67			query = new Query(account, new MamReference(startCatchup), endCatchup);
 68		} else {
 69			query = new Query(account, mamReference, endCatchup);
 70		}
 71		synchronized (this.queries) {
 72			this.queries.add(query);
 73		}
 74		this.execute(query);
 75	}
 76
 77	public void catchupMUC(final Conversation conversation) {
 78		if (conversation.getLastMessageTransmitted().getTimestamp() < 0 && conversation.countMessages() == 0) {
 79			query(conversation,
 80					new MamReference(0),
 81					System.currentTimeMillis(),
 82					true);
 83		} else {
 84			query(conversation,
 85					conversation.getLastMessageTransmitted(),
 86					System.currentTimeMillis(),
 87					true);
 88		}
 89	}
 90
 91	public Query query(final Conversation conversation) {
 92		if (conversation.getLastMessageTransmitted().getTimestamp() < 0 && conversation.countMessages() == 0) {
 93			return query(conversation,
 94					new MamReference(0),
 95					System.currentTimeMillis(),
 96					false);
 97		} else {
 98			return query(conversation,
 99					conversation.getLastMessageTransmitted(),
100					conversation.getAccount().getXmppConnection().getLastSessionEstablished(),
101					false);
102		}
103	}
104
105	public boolean isCatchingUp(Conversation conversation) {
106		final Account account = conversation.getAccount();
107		if (account.getXmppConnection().isWaitingForSmCatchup()) {
108			return true;
109		} else {
110			synchronized (this.queries) {
111				for(Query query : this.queries) {
112					if (query.getAccount() == account && query.isCatchup() && ((conversation.getMode() == Conversation.MODE_SINGLE && query.getWith() == null) || query.getConversation() == conversation)) {
113						return true;
114					}
115				}
116			}
117			return false;
118		}
119	}
120
121	public Query query(final Conversation conversation, long end, boolean allowCatchup) {
122		return this.query(conversation,conversation.getLastMessageTransmitted(),end, allowCatchup);
123	}
124
125	public Query query(Conversation conversation, MamReference start, long end, boolean allowCatchup) {
126		synchronized (this.queries) {
127			final Query query;
128			final MamReference startActual = MamReference.max(start,mXmppConnectionService.getAutomaticMessageDeletionDate());
129			if (start.getTimestamp() == 0) {
130				query = new Query(conversation, startActual, end, false);
131				query.reference = conversation.getFirstMamReference();
132			} else {
133				if (allowCatchup) {
134					MamReference maxCatchup = MamReference.max(startActual, System.currentTimeMillis() - Config.MAM_MAX_CATCHUP);
135					if (maxCatchup.greaterThan(startActual)) {
136						Query reverseCatchup = new Query(conversation, startActual, maxCatchup.getTimestamp(), false);
137						this.queries.add(reverseCatchup);
138						this.execute(reverseCatchup);
139					}
140					query = new Query(conversation, maxCatchup, end, allowCatchup);
141				} else {
142					query = new Query(conversation, startActual, end, false);
143				}
144			}
145			if (start.greaterThan(end)) {
146				return null;
147			}
148			this.queries.add(query);
149			this.execute(query);
150			return query;
151		}
152	}
153
154	public void executePendingQueries(final Account account) {
155		List<Query> pending = new ArrayList<>();
156		synchronized(this.pendingQueries) {
157			for(Iterator<Query> iterator = this.pendingQueries.iterator(); iterator.hasNext();) {
158				Query query = iterator.next();
159				if (query.getAccount() == account) {
160					pending.add(query);
161					iterator.remove();
162				}
163			}
164		}
165		for(Query query : pending) {
166			this.execute(query);
167		}
168	}
169
170	private void execute(final Query query) {
171		final Account account=  query.getAccount();
172		if (account.getStatus() == Account.State.ONLINE) {
173			Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": running mam query " + query.toString());
174			IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query);
175			this.mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
176				@Override
177				public void onIqPacketReceived(Account account, IqPacket packet) {
178					Element fin = packet.findChild("fin", Namespace.MAM);
179					if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
180						synchronized (MessageArchiveService.this.queries) {
181							MessageArchiveService.this.queries.remove(query);
182							if (query.hasCallback()) {
183								query.callback(false);
184							}
185						}
186					} else if (packet.getType() == IqPacket.TYPE.RESULT && fin != null ) {
187						processFin(query, fin);
188					} else if (packet.getType() == IqPacket.TYPE.RESULT && query.isLegacy()) {
189						//do nothing
190					} else {
191						Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": error executing mam: " + packet.toString());
192						finalizeQuery(query, true);
193					}
194				}
195			});
196		} else {
197			synchronized (this.pendingQueries) {
198				this.pendingQueries.add(query);
199			}
200		}
201	}
202
203	private void finalizeQuery(Query query, boolean done) {
204		synchronized (this.queries) {
205			this.queries.remove(query);
206		}
207		final Conversation conversation = query.getConversation();
208		if (conversation != null) {
209			conversation.sort();
210			conversation.setHasMessagesLeftOnServer(!done);
211		} else {
212			for(Conversation tmp : this.mXmppConnectionService.getConversations()) {
213				if (tmp.getAccount() == query.getAccount()) {
214					tmp.sort();
215				}
216			}
217		}
218		if (query.hasCallback()) {
219			query.callback(done);
220		} else {
221			this.mXmppConnectionService.updateConversationUi();
222		}
223	}
224
225	public boolean inCatchup(Account account) {
226		synchronized (this.queries) {
227			for(Query query : queries) {
228				if (query.account == account && query.isCatchup() && query.getWith() == null) {
229					return true;
230				}
231			}
232		}
233		return false;
234	}
235
236	public boolean queryInProgress(Conversation conversation, XmppConnectionService.OnMoreMessagesLoaded callback) {
237		synchronized (this.queries) {
238			for(Query query : queries) {
239				if (query.conversation == conversation) {
240					if (!query.hasCallback() && callback != null) {
241						query.setCallback(callback);
242					}
243					return true;
244				}
245			}
246			return false;
247		}
248	}
249
250	public boolean queryInProgress(Conversation conversation) {
251		return queryInProgress(conversation, null);
252	}
253
254	public void processFinLegacy(Element fin, Jid from) {
255		Query query = findQuery(fin.getAttribute("queryid"));
256		if (query != null && query.validFrom(from)) {
257			processFin(query, fin);
258		}
259	}
260
261	private void processFin(Query query, Element fin) {
262		boolean complete = fin.getAttributeAsBoolean("complete");
263		Element set = fin.findChild("set","http://jabber.org/protocol/rsm");
264		Element last = set == null ? null : set.findChild("last");
265		String count = set == null ? null : set.findChildContent("count");
266		Element first = set == null ? null : set.findChild("first");
267		Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first;
268		boolean abort = (!query.isCatchup() && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES;
269		if (query.getConversation() != null) {
270			query.getConversation().setFirstMamReference(first == null ? null : first.getContent());
271		}
272		if (complete || relevant == null || abort) {
273			boolean done;
274			if (query.isCatchup()) {
275				done = false;
276			} else {
277				if (count != null) {
278					try {
279						done = Integer.parseInt(count) <= query.getTotalCount();
280					} catch (NumberFormatException e) {
281						done = false;
282					}
283				} else {
284					done = query.getTotalCount() == 0;
285				}
286			}
287			done = done || (query.getActualMessageCount() == 0 && !query.isCatchup());
288			this.finalizeQuery(query, done);
289
290			Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": finished mam after "+query.getTotalCount()+"("+query.getActualMessageCount()+") messages. messages left="+Boolean.toString(!done)+" count="+count);
291			if (query.isCatchup() && query.getActualMessageCount() > 0) {
292				mXmppConnectionService.getNotificationService().finishBacklog(true,query.getAccount());
293			}
294			processPostponed(query);
295		} else {
296			final Query nextQuery;
297			if (query.getPagingOrder() == PagingOrder.NORMAL) {
298				nextQuery = query.next(last == null ? null : last.getContent());
299			} else {
300				nextQuery = query.prev(first == null ? null : first.getContent());
301			}
302			this.execute(nextQuery);
303			this.finalizeQuery(query, false);
304			synchronized (this.queries) {
305				this.queries.add(nextQuery);
306			}
307		}
308	}
309
310	public void kill(Conversation conversation) {
311		synchronized (this.queries) {
312			for (Query q : queries) {
313				if (q.conversation == conversation) {
314					kill(q);
315				}
316			}
317		}
318	}
319
320	private void kill(Query query) {
321		Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": killing mam query prematurely");
322		query.callback = null;
323		this.finalizeQuery(query,false);
324		if (query.isCatchup() && query.getActualMessageCount() > 0) {
325			mXmppConnectionService.getNotificationService().finishBacklog(true,query.getAccount());
326		}
327		this.processPostponed(query);
328	}
329
330	private void processPostponed(Query query) {
331		query.account.getAxolotlService().processPostponed();
332		Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": found "+query.pendingReceiptRequests.size()+" pending receipt requests");
333		Iterator<ReceiptRequest> iterator = query.pendingReceiptRequests.iterator();
334		while (iterator.hasNext()) {
335			ReceiptRequest rr = iterator.next();
336			mXmppConnectionService.sendMessagePacket(query.account,mXmppConnectionService.getMessageGenerator().received(query.account,        rr.getJid(),rr.getId()));
337			iterator.remove();
338		}
339	}
340
341	public Query findQuery(String id) {
342		if (id == null) {
343			return null;
344		}
345		synchronized (this.queries) {
346			for(Query query : this.queries) {
347				if (query.getQueryId().equals(id)) {
348					return query;
349				}
350			}
351			return null;
352		}
353	}
354
355	@Override
356	public void onAdvancedStreamFeaturesAvailable(Account account) {
357		if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) {
358			this.catchup(account);
359		}
360	}
361
362	public class Query {
363		private int totalCount = 0;
364		private int actualCount = 0;
365		private int actualInThisQuery = 0;
366		private long start;
367		private long end;
368		private String queryId;
369		private String reference = null;
370		private Account account;
371		private Conversation conversation;
372		private PagingOrder pagingOrder = PagingOrder.NORMAL;
373		private XmppConnectionService.OnMoreMessagesLoaded callback = null;
374		private boolean catchup = true;
375		public HashSet<ReceiptRequest> pendingReceiptRequests = new HashSet<>();
376
377
378		public 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		public 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		public 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().toBareJid();
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		public int getTotalCount() {
494			return this.totalCount;
495		}
496
497		public 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().toBareJid().equals(from.toBareJid());
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="+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="+Boolean.toString(catchup));
543			return builder.toString();
544		}
545
546		public boolean hasCallback() {
547			return this.callback != null;
548		}
549	}
550}