MessageArchiveService.java

  1package eu.siacs.conversations.services;
  2
  3import android.util.Log;
  4import android.util.Pair;
  5
  6import java.math.BigInteger;
  7import java.util.ArrayList;
  8import java.util.HashSet;
  9import java.util.Iterator;
 10import java.util.List;
 11
 12import eu.siacs.conversations.Config;
 13import eu.siacs.conversations.R;
 14import eu.siacs.conversations.entities.Account;
 15import eu.siacs.conversations.entities.Conversation;
 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 queryInProgress(Conversation conversation, XmppConnectionService.OnMoreMessagesLoaded callback) {
226		synchronized (this.queries) {
227			for(Query query : queries) {
228				if (query.conversation == conversation) {
229					if (!query.hasCallback() && callback != null) {
230						query.setCallback(callback);
231					}
232					return true;
233				}
234			}
235			return false;
236		}
237	}
238
239	public boolean queryInProgress(Conversation conversation) {
240		return queryInProgress(conversation, null);
241	}
242
243	public void processFinLegacy(Element fin, Jid from) {
244		Query query = findQuery(fin.getAttribute("queryid"));
245		if (query != null && query.validFrom(from)) {
246			processFin(fin);
247		}
248	}
249
250	public void processFin(Element fin) {
251		Query query = findQuery(fin.getAttribute("queryid"));
252		if (query == null) {
253			return;
254		}
255		boolean complete = fin.getAttributeAsBoolean("complete");
256		Element set = fin.findChild("set","http://jabber.org/protocol/rsm");
257		Element last = set == null ? null : set.findChild("last");
258		Element first = set == null ? null : set.findChild("first");
259		Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first;
260		boolean abort = (!query.isCatchup() && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES;
261		if (query.getConversation() != null) {
262			query.getConversation().setFirstMamReference(first == null ? null : first.getContent());
263		}
264		if (complete || relevant == null || abort) {
265			final boolean done = (complete || query.getActualMessageCount() == 0) && !query.isCatchup();
266			this.finalizeQuery(query, done);
267			Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": finished mam after "+query.getTotalCount()+"("+query.getActualMessageCount()+") messages. messages left="+Boolean.toString(!done));
268			if (query.isCatchup() && query.getActualMessageCount() > 0) {
269				mXmppConnectionService.getNotificationService().finishBacklog(true,query.getAccount());
270			}
271		} else {
272			final Query nextQuery;
273			if (query.getPagingOrder() == PagingOrder.NORMAL) {
274				nextQuery = query.next(last == null ? null : last.getContent());
275			} else {
276				nextQuery = query.prev(first == null ? null : first.getContent());
277			}
278			this.execute(nextQuery);
279			this.finalizeQuery(query, false);
280			synchronized (this.queries) {
281				this.queries.add(nextQuery);
282			}
283		}
284	}
285
286	public Query findQuery(String id) {
287		if (id == null) {
288			return null;
289		}
290		synchronized (this.queries) {
291			for(Query query : this.queries) {
292				if (query.getQueryId().equals(id)) {
293					return query;
294				}
295			}
296			return null;
297		}
298	}
299
300	@Override
301	public void onAdvancedStreamFeaturesAvailable(Account account) {
302		if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) {
303			this.catchup(account);
304		}
305	}
306
307	public class Query {
308		private int totalCount = 0;
309		private int actualCount = 0;
310		private long start;
311		private long end;
312		private String queryId;
313		private String reference = null;
314		private Account account;
315		private Conversation conversation;
316		private PagingOrder pagingOrder = PagingOrder.NORMAL;
317		private XmppConnectionService.OnMoreMessagesLoaded callback = null;
318		private boolean catchup = true;
319
320
321		public Query(Conversation conversation, MamReference start, long end, boolean catchup) {
322			this(conversation.getAccount(),catchup ? start : start.timeOnly(),end);
323			this.conversation = conversation;
324			this.pagingOrder = catchup ? PagingOrder.NORMAL : PagingOrder.REVERSE;
325			this.catchup = catchup;
326		}
327
328		public Query(Account account, MamReference start, long end) {
329			this.account = account;
330			if (start.getReference() != null) {
331				this.reference = start.getReference();
332			} else {
333				this.start = start.getTimestamp();
334			}
335			this.end = end;
336			this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
337		}
338		
339		private Query page(String reference) {
340			Query query = new Query(this.account,new MamReference(this.start,reference),this.end);
341			query.conversation = conversation;
342			query.totalCount = totalCount;
343			query.actualCount = actualCount;
344			query.callback = callback;
345			query.catchup = catchup;
346			return query;
347		}
348
349		public boolean isLegacy() {
350			if (conversation == null || conversation.getMode() == Conversation.MODE_SINGLE) {
351				return account.getXmppConnection().getFeatures().mamLegacy();
352			} else {
353				return conversation.getMucOptions().mamLegacy();
354			}
355		}
356
357		public boolean safeToExtractTrueCounterpart() {
358			return muc() && !isLegacy();
359		}
360
361		public Query next(String reference) {
362			Query query = page(reference);
363			query.pagingOrder = PagingOrder.NORMAL;
364			return query;
365		}
366
367		public Query prev(String reference) {
368			Query query = page(reference);
369			query.pagingOrder = PagingOrder.REVERSE;
370			return query;
371		}
372
373		public String getReference() {
374			return reference;
375		}
376
377		public PagingOrder getPagingOrder() {
378			return this.pagingOrder;
379		}
380
381		public String getQueryId() {
382			return queryId;
383		}
384
385		public Jid getWith() {
386			return conversation == null ? null : conversation.getJid().toBareJid();
387		}
388
389		public boolean muc() {
390			return conversation != null && conversation.getMode() == Conversation.MODE_MULTI;
391		}
392
393		public long getStart() {
394			return start;
395		}
396
397		public boolean isCatchup() {
398			return catchup;
399		}
400
401		public void setCallback(XmppConnectionService.OnMoreMessagesLoaded callback) {
402			this.callback = callback;
403		}
404
405		public void callback(boolean done) {
406			if (this.callback != null) {
407				this.callback.onMoreMessagesLoaded(actualCount,conversation);
408				if (done) {
409					this.callback.informUser(R.string.no_more_history_on_server);
410				}
411			}
412		}
413
414		public long getEnd() {
415			return end;
416		}
417
418		public Conversation getConversation() {
419			return conversation;
420		}
421
422		public Account getAccount() {
423			return this.account;
424		}
425
426		public void incrementMessageCount() {
427			this.totalCount++;
428		}
429
430		public void incrementActualMessageCount() {
431			this.actualCount++;
432		}
433
434		public int getTotalCount() {
435			return this.totalCount;
436		}
437
438		public int getActualMessageCount() {
439			return this.actualCount;
440		}
441
442		public boolean validFrom(Jid from) {
443			if (muc()) {
444				return getWith().equals(from);
445			} else {
446				return (from == null) || account.getJid().toBareJid().equals(from.toBareJid());
447			}
448		}
449
450		@Override
451		public String toString() {
452			StringBuilder builder = new StringBuilder();
453			if (this.muc()) {
454				builder.append("to=");
455				builder.append(this.getWith().toString());
456			} else {
457				builder.append("with=");
458				if (this.getWith() == null) {
459					builder.append("*");
460				} else {
461					builder.append(getWith().toString());
462				}
463			}
464			if (this.start != 0) {
465				builder.append(", start=");
466				builder.append(AbstractGenerator.getTimestamp(this.start));
467			}
468			builder.append(", end=");
469			builder.append(AbstractGenerator.getTimestamp(this.end));
470			builder.append(", order="+pagingOrder.toString());
471			if (this.reference!=null) {
472				if (this.pagingOrder == PagingOrder.NORMAL) {
473					builder.append(", after=");
474				} else {
475					builder.append(", before=");
476				}
477				builder.append(this.reference);
478			}
479			builder.append(", catchup="+Boolean.toString(catchup));
480			return builder.toString();
481		}
482
483		public boolean hasCallback() {
484			return this.callback != null;
485		}
486	}
487}