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 = !query.isCatchup();
274			if (count != null && !query.isCatchup()) {
275				try {
276					done = Integer.parseInt(count) <= query.getTotalCount();
277				} catch (NumberFormatException e) {
278					done = false;
279				}
280			}
281			done = done || (query.getActualMessageCount() == 0 && !query.isCatchup());
282			this.finalizeQuery(query, done);
283
284			Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": finished mam after "+query.getTotalCount()+"("+query.getActualMessageCount()+") messages. messages left="+Boolean.toString(!done)+" count="+count);
285			if (query.isCatchup() && query.getActualMessageCount() > 0) {
286				mXmppConnectionService.getNotificationService().finishBacklog(true,query.getAccount());
287			}
288			processPostponed(query);
289		} else {
290			final Query nextQuery;
291			if (query.getPagingOrder() == PagingOrder.NORMAL) {
292				nextQuery = query.next(last == null ? null : last.getContent());
293			} else {
294				nextQuery = query.prev(first == null ? null : first.getContent());
295			}
296			this.execute(nextQuery);
297			this.finalizeQuery(query, false);
298			synchronized (this.queries) {
299				this.queries.add(nextQuery);
300			}
301		}
302	}
303
304	public void kill(Conversation conversation) {
305		synchronized (this.queries) {
306			for (Query q : queries) {
307				if (q.conversation == conversation) {
308					kill(q);
309				}
310			}
311		}
312	}
313
314	private void kill(Query query) {
315		Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": killing mam query prematurely");
316		query.callback = null;
317		this.finalizeQuery(query,false);
318		if (query.isCatchup() && query.getActualMessageCount() > 0) {
319			mXmppConnectionService.getNotificationService().finishBacklog(true,query.getAccount());
320		}
321		this.processPostponed(query);
322	}
323
324	private void processPostponed(Query query) {
325		query.account.getAxolotlService().processPostponed();
326		Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": found "+query.pendingReceiptRequests.size()+" pending receipt requests");
327		Iterator<ReceiptRequest> iterator = query.pendingReceiptRequests.iterator();
328		while (iterator.hasNext()) {
329			ReceiptRequest rr = iterator.next();
330			mXmppConnectionService.sendMessagePacket(query.account,mXmppConnectionService.getMessageGenerator().received(query.account,        rr.getJid(),rr.getId()));
331			iterator.remove();
332		}
333	}
334
335	public Query findQuery(String id) {
336		if (id == null) {
337			return null;
338		}
339		synchronized (this.queries) {
340			for(Query query : this.queries) {
341				if (query.getQueryId().equals(id)) {
342					return query;
343				}
344			}
345			return null;
346		}
347	}
348
349	@Override
350	public void onAdvancedStreamFeaturesAvailable(Account account) {
351		if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) {
352			this.catchup(account);
353		}
354	}
355
356	public class Query {
357		private int totalCount = 0;
358		private int actualCount = 0;
359		private int actualInThisQuery = 0;
360		private long start;
361		private long end;
362		private String queryId;
363		private String reference = null;
364		private Account account;
365		private Conversation conversation;
366		private PagingOrder pagingOrder = PagingOrder.NORMAL;
367		private XmppConnectionService.OnMoreMessagesLoaded callback = null;
368		private boolean catchup = true;
369		public HashSet<ReceiptRequest> pendingReceiptRequests = new HashSet<>();
370
371
372		public Query(Conversation conversation, MamReference start, long end, boolean catchup) {
373			this(conversation.getAccount(),catchup ? start : start.timeOnly(),end);
374			this.conversation = conversation;
375			this.pagingOrder = catchup ? PagingOrder.NORMAL : PagingOrder.REVERSE;
376			this.catchup = catchup;
377		}
378
379		public Query(Account account, MamReference start, long end) {
380			this.account = account;
381			if (start.getReference() != null) {
382				this.reference = start.getReference();
383			} else {
384				this.start = start.getTimestamp();
385			}
386			this.end = end;
387			this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
388		}
389		
390		private Query page(String reference) {
391			Query query = new Query(this.account,new MamReference(this.start,reference),this.end);
392			query.conversation = conversation;
393			query.totalCount = totalCount;
394			query.actualCount = actualCount;
395			query.pendingReceiptRequests = pendingReceiptRequests;
396			query.callback = callback;
397			query.catchup = catchup;
398			return query;
399		}
400
401		public boolean isLegacy() {
402			if (conversation == null || conversation.getMode() == Conversation.MODE_SINGLE) {
403				return account.getXmppConnection().getFeatures().mamLegacy();
404			} else {
405				return conversation.getMucOptions().mamLegacy();
406			}
407		}
408
409		public boolean safeToExtractTrueCounterpart() {
410			return muc() && !isLegacy();
411		}
412
413		public Query next(String reference) {
414			Query query = page(reference);
415			query.pagingOrder = PagingOrder.NORMAL;
416			return query;
417		}
418
419		public Query prev(String reference) {
420			Query query = page(reference);
421			query.pagingOrder = PagingOrder.REVERSE;
422			return query;
423		}
424
425		public String getReference() {
426			return reference;
427		}
428
429		public PagingOrder getPagingOrder() {
430			return this.pagingOrder;
431		}
432
433		public String getQueryId() {
434			return queryId;
435		}
436
437		public Jid getWith() {
438			return conversation == null ? null : conversation.getJid().toBareJid();
439		}
440
441		public boolean muc() {
442			return conversation != null && conversation.getMode() == Conversation.MODE_MULTI;
443		}
444
445		public long getStart() {
446			return start;
447		}
448
449		public boolean isCatchup() {
450			return catchup;
451		}
452
453		public void setCallback(XmppConnectionService.OnMoreMessagesLoaded callback) {
454			this.callback = callback;
455		}
456
457		public void callback(boolean done) {
458			if (this.callback != null) {
459				this.callback.onMoreMessagesLoaded(actualCount,conversation);
460				if (done) {
461					this.callback.informUser(R.string.no_more_history_on_server);
462				}
463			}
464		}
465
466		public long getEnd() {
467			return end;
468		}
469
470		public Conversation getConversation() {
471			return conversation;
472		}
473
474		public Account getAccount() {
475			return this.account;
476		}
477
478		public void incrementMessageCount() {
479			this.totalCount++;
480		}
481
482		public void incrementActualMessageCount() {
483			this.actualInThisQuery++;
484			this.actualCount++;
485		}
486
487		public int getTotalCount() {
488			return this.totalCount;
489		}
490
491		public int getActualMessageCount() {
492			return this.actualCount;
493		}
494
495		public int getActualInThisQuery() {
496			return this.actualInThisQuery;
497		}
498
499		public boolean validFrom(Jid from) {
500			if (muc()) {
501				return getWith().equals(from);
502			} else {
503				return (from == null) || account.getJid().toBareJid().equals(from.toBareJid());
504			}
505		}
506
507		@Override
508		public String toString() {
509			StringBuilder builder = new StringBuilder();
510			if (this.muc()) {
511				builder.append("to=");
512				builder.append(this.getWith().toString());
513			} else {
514				builder.append("with=");
515				if (this.getWith() == null) {
516					builder.append("*");
517				} else {
518					builder.append(getWith().toString());
519				}
520			}
521			if (this.start != 0) {
522				builder.append(", start=");
523				builder.append(AbstractGenerator.getTimestamp(this.start));
524			}
525			builder.append(", end=");
526			builder.append(AbstractGenerator.getTimestamp(this.end));
527			builder.append(", order="+pagingOrder.toString());
528			if (this.reference!=null) {
529				if (this.pagingOrder == PagingOrder.NORMAL) {
530					builder.append(", after=");
531				} else {
532					builder.append(", before=");
533				}
534				builder.append(this.reference);
535			}
536			builder.append(", catchup="+Boolean.toString(catchup));
537			return builder.toString();
538		}
539
540		public boolean hasCallback() {
541			return this.callback != null;
542		}
543	}
544}