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