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		query.pendingReceiptRequests.removeAll(query.receiptRequests);
328		Log.d(Config.LOGTAG, query.getAccount().getJid().asBareJid() + ": found " + query.pendingReceiptRequests.size() + " pending receipt requests");
329		Iterator<ReceiptRequest> iterator = query.pendingReceiptRequests.iterator();
330		while (iterator.hasNext()) {
331			ReceiptRequest rr = iterator.next();
332			mXmppConnectionService.sendMessagePacket(query.account, mXmppConnectionService.getMessageGenerator().received(query.account, rr.getJid(), rr.getId()));
333			iterator.remove();
334		}
335	}
336
337	public Query findQuery(String id) {
338		if (id == null) {
339			return null;
340		}
341		synchronized (this.queries) {
342			for (Query query : this.queries) {
343				if (query.getQueryId().equals(id)) {
344					return query;
345				}
346			}
347			return null;
348		}
349	}
350
351	@Override
352	public void onAdvancedStreamFeaturesAvailable(Account account) {
353		if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) {
354			this.catchup(account);
355		}
356	}
357
358	public enum PagingOrder {
359		NORMAL,
360		REVERSE
361	}
362
363	public class Query {
364		private HashSet<ReceiptRequest> pendingReceiptRequests = new HashSet<>();
365		private HashSet<ReceiptRequest> receiptRequests = new HashSet<>();
366		private int totalCount = 0;
367		private int actualCount = 0;
368		private int actualInThisQuery = 0;
369		private long start;
370		private long end;
371		private String queryId;
372		private String reference = null;
373		private Account account;
374		private Conversation conversation;
375		private PagingOrder pagingOrder = PagingOrder.NORMAL;
376		private XmppConnectionService.OnMoreMessagesLoaded callback = null;
377		private boolean catchup = true;
378
379
380		Query(Conversation conversation, MamReference start, long end, boolean catchup) {
381			this(conversation.getAccount(), catchup ? start : start.timeOnly(), end);
382			this.conversation = conversation;
383			this.pagingOrder = catchup ? PagingOrder.NORMAL : PagingOrder.REVERSE;
384			this.catchup = catchup;
385		}
386
387		Query(Account account, MamReference start, long end) {
388			this.account = account;
389			if (start.getReference() != null) {
390				this.reference = start.getReference();
391			} else {
392				this.start = start.getTimestamp();
393			}
394			this.end = end;
395			this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
396		}
397
398		private Query page(String reference) {
399			Query query = new Query(this.account, new MamReference(this.start, reference), this.end);
400			query.conversation = conversation;
401			query.totalCount = totalCount;
402			query.actualCount = actualCount;
403			query.pendingReceiptRequests = pendingReceiptRequests;
404			query.receiptRequests = receiptRequests;
405			query.callback = callback;
406			query.catchup = catchup;
407			return query;
408		}
409
410		public void removePendingReceiptRequest(ReceiptRequest receiptRequest) {
411			if (!this.pendingReceiptRequests.remove(receiptRequest)) {
412				this.receiptRequests.add(receiptRequest);
413			}
414		}
415
416		public void addPendingReceiptRequest(ReceiptRequest receiptRequest) {
417			this.pendingReceiptRequests.add(receiptRequest);
418		}
419
420		public boolean isLegacy() {
421			if (conversation == null || conversation.getMode() == Conversation.MODE_SINGLE) {
422				return account.getXmppConnection().getFeatures().mamLegacy();
423			} else {
424				return conversation.getMucOptions().mamLegacy();
425			}
426		}
427
428		public boolean safeToExtractTrueCounterpart() {
429			return muc() && !isLegacy();
430		}
431
432		public Query next(String reference) {
433			Query query = page(reference);
434			query.pagingOrder = PagingOrder.NORMAL;
435			return query;
436		}
437
438		Query prev(String reference) {
439			Query query = page(reference);
440			query.pagingOrder = PagingOrder.REVERSE;
441			return query;
442		}
443
444		public String getReference() {
445			return reference;
446		}
447
448		public PagingOrder getPagingOrder() {
449			return this.pagingOrder;
450		}
451
452		public String getQueryId() {
453			return queryId;
454		}
455
456		public Jid getWith() {
457			return conversation == null ? null : conversation.getJid().asBareJid();
458		}
459
460		public boolean muc() {
461			return conversation != null && conversation.getMode() == Conversation.MODE_MULTI;
462		}
463
464		public long getStart() {
465			return start;
466		}
467
468		public boolean isCatchup() {
469			return catchup;
470		}
471
472		public void setCallback(XmppConnectionService.OnMoreMessagesLoaded callback) {
473			this.callback = callback;
474		}
475
476		public void callback(boolean done) {
477			if (this.callback != null) {
478				this.callback.onMoreMessagesLoaded(actualCount, conversation);
479				if (done) {
480					this.callback.informUser(R.string.no_more_history_on_server);
481				}
482			}
483		}
484
485		public long getEnd() {
486			return end;
487		}
488
489		public Conversation getConversation() {
490			return conversation;
491		}
492
493		public Account getAccount() {
494			return this.account;
495		}
496
497		public void incrementMessageCount() {
498			this.totalCount++;
499		}
500
501		public void incrementActualMessageCount() {
502			this.actualInThisQuery++;
503			this.actualCount++;
504		}
505
506		int getTotalCount() {
507			return this.totalCount;
508		}
509
510		int getActualMessageCount() {
511			return this.actualCount;
512		}
513
514		public int getActualInThisQuery() {
515			return this.actualInThisQuery;
516		}
517
518		public boolean validFrom(Jid from) {
519			if (muc()) {
520				return getWith().equals(from);
521			} else {
522				return (from == null) || account.getJid().asBareJid().equals(from.asBareJid());
523			}
524		}
525
526		@Override
527		public String toString() {
528			StringBuilder builder = new StringBuilder();
529			if (this.muc()) {
530				builder.append("to=");
531				builder.append(this.getWith().toString());
532			} else {
533				builder.append("with=");
534				if (this.getWith() == null) {
535					builder.append("*");
536				} else {
537					builder.append(getWith().toString());
538				}
539			}
540			if (this.start != 0) {
541				builder.append(", start=");
542				builder.append(AbstractGenerator.getTimestamp(this.start));
543			}
544			builder.append(", end=");
545			builder.append(AbstractGenerator.getTimestamp(this.end));
546			builder.append(", order=").append(pagingOrder.toString());
547			if (this.reference != null) {
548				if (this.pagingOrder == PagingOrder.NORMAL) {
549					builder.append(", after=");
550				} else {
551					builder.append(", before=");
552				}
553				builder.append(this.reference);
554			}
555			builder.append(", catchup=").append(Boolean.toString(catchup));
556			return builder.toString();
557		}
558
559		boolean hasCallback() {
560			return this.callback != null;
561		}
562	}
563}