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.Element;
 18import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
 19import eu.siacs.conversations.xmpp.OnIqPacketReceived;
 20import eu.siacs.conversations.xmpp.jid.Jid;
 21import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 22
 23public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
 24
 25	private final XmppConnectionService mXmppConnectionService;
 26
 27	private final HashSet<Query> queries = new HashSet<>();
 28	private final ArrayList<Query> pendingQueries = new ArrayList<>();
 29
 30	public enum PagingOrder {
 31		NORMAL,
 32		REVERSE
 33	}
 34
 35	public MessageArchiveService(final XmppConnectionService service) {
 36		this.mXmppConnectionService = service;
 37	}
 38
 39	private void catchup(final Account account) {
 40		synchronized (this.queries) {
 41			for(Iterator<Query> iterator = this.queries.iterator(); iterator.hasNext();) {
 42				Query query = iterator.next();
 43				if (query.getAccount() == account) {
 44					iterator.remove();
 45				}
 46			}
 47		}
 48		final Pair<Long,String> lastMessageReceived = mXmppConnectionService.databaseBackend.getLastMessageReceived(account);
 49		final Pair<Long,String> lastClearDate = mXmppConnectionService.databaseBackend.getLastClearDate(account);
 50		long startCatchup;
 51		final String reference;
 52		if (lastMessageReceived != null && lastMessageReceived.first >= lastClearDate.first) {
 53			startCatchup = lastMessageReceived.first;
 54			reference = lastMessageReceived.second;
 55		} else {
 56			startCatchup = lastClearDate.first;
 57			reference = null;
 58		}
 59		startCatchup = Math.max(startCatchup,mXmppConnectionService.getAutomaticMessageDeletionDate());
 60		long endCatchup = account.getXmppConnection().getLastSessionEstablished();
 61		final Query query;
 62		if (startCatchup == 0) {
 63			return;
 64		} else if (endCatchup - startCatchup >= Config.MAM_MAX_CATCHUP) {
 65			startCatchup = endCatchup - Config.MAM_MAX_CATCHUP;
 66			List<Conversation> conversations = mXmppConnectionService.getConversations();
 67			for (Conversation conversation : conversations) {
 68				if (conversation.getMode() == Conversation.MODE_SINGLE && conversation.getAccount() == account && startCatchup > conversation.getLastMessageTransmitted()) {
 69					this.query(conversation,startCatchup);
 70				}
 71			}
 72			query = new Query(account, startCatchup, endCatchup);
 73		} else {
 74			query = new Query(account, startCatchup, endCatchup);
 75			query.reference = reference;
 76		}
 77		this.queries.add(query);
 78		this.execute(query);
 79	}
 80
 81	public void catchupMUC(final Conversation conversation) {
 82		if (conversation.getLastMessageTransmitted() < 0 && conversation.countMessages() == 0) {
 83			query(conversation,
 84					0,
 85					System.currentTimeMillis());
 86		} else {
 87			query(conversation,
 88					conversation.getLastMessageTransmitted(),
 89					System.currentTimeMillis());
 90		}
 91	}
 92
 93	public Query query(final Conversation conversation) {
 94		if (conversation.getLastMessageTransmitted() < 0 && conversation.countMessages() == 0) {
 95			return query(conversation,
 96					0,
 97					System.currentTimeMillis());
 98		} else {
 99			return query(conversation,
100					conversation.getLastMessageTransmitted(),
101					conversation.getAccount().getXmppConnection().getLastSessionEstablished());
102		}
103	}
104
105	public Query query(final Conversation conversation, long end) {
106		return this.query(conversation,conversation.getLastMessageTransmitted(),end);
107	}
108
109	public Query query(Conversation conversation, long start, long end) {
110		synchronized (this.queries) {
111			final Query query;
112			final long startActual = Math.max(start,mXmppConnectionService.getAutomaticMessageDeletionDate());
113			if (start==0) {
114				query = new Query(conversation, startActual, end, false);
115				query.reference = conversation.getFirstMamReference();
116			} else {
117				long maxCatchup = Math.max(startActual,System.currentTimeMillis() - Config.MAM_MAX_CATCHUP);
118				if (maxCatchup > startActual) {
119					Query reverseCatchup = new Query(conversation,startActual,maxCatchup,false);
120					this.queries.add(reverseCatchup);
121					this.execute(reverseCatchup);
122				}
123				query = new Query(conversation, maxCatchup, end);
124			}
125			if (start > end) {
126				return null;
127			}
128			this.queries.add(query);
129			this.execute(query);
130			return query;
131		}
132	}
133
134	public void executePendingQueries(final Account account) {
135		List<Query> pending = new ArrayList<>();
136		synchronized(this.pendingQueries) {
137			for(Iterator<Query> iterator = this.pendingQueries.iterator(); iterator.hasNext();) {
138				Query query = iterator.next();
139				if (query.getAccount() == account) {
140					pending.add(query);
141					iterator.remove();
142				}
143			}
144		}
145		for(Query query : pending) {
146			this.execute(query);
147		}
148	}
149
150	private void execute(final Query query) {
151		final Account account=  query.getAccount();
152		if (account.getStatus() == Account.State.ONLINE) {
153			Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": running mam query " + query.toString());
154			IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query);
155			this.mXmppConnectionService.sendIqPacket(account, packet, new OnIqPacketReceived() {
156				@Override
157				public void onIqPacketReceived(Account account, IqPacket packet) {
158					if (packet.getType() == IqPacket.TYPE.TIMEOUT) {
159						synchronized (MessageArchiveService.this.queries) {
160							MessageArchiveService.this.queries.remove(query);
161							if (query.hasCallback()) {
162								query.callback(false);
163							}
164						}
165					} else if (packet.getType() != IqPacket.TYPE.RESULT) {
166						Log.d(Config.LOGTAG, account.getJid().toBareJid().toString() + ": error executing mam: " + packet.toString());
167						finalizeQuery(query, true);
168					}
169				}
170			});
171		} else {
172			synchronized (this.pendingQueries) {
173				this.pendingQueries.add(query);
174			}
175		}
176	}
177
178	private void finalizeQuery(Query query, boolean done) {
179		synchronized (this.queries) {
180			this.queries.remove(query);
181		}
182		final Conversation conversation = query.getConversation();
183		if (conversation != null) {
184			conversation.sort();
185			conversation.setHasMessagesLeftOnServer(!done);
186		} else {
187			for(Conversation tmp : this.mXmppConnectionService.getConversations()) {
188				if (tmp.getAccount() == query.getAccount()) {
189					tmp.sort();
190				}
191			}
192		}
193		if (query.hasCallback()) {
194			query.callback(done);
195		} else {
196			this.mXmppConnectionService.updateConversationUi();
197		}
198	}
199
200	public boolean queryInProgress(Conversation conversation, XmppConnectionService.OnMoreMessagesLoaded callback) {
201		synchronized (this.queries) {
202			for(Query query : queries) {
203				if (query.conversation == conversation) {
204					if (!query.hasCallback() && callback != null) {
205						query.setCallback(callback);
206					}
207					return true;
208				}
209			}
210			return false;
211		}
212	}
213
214	public boolean queryInProgress(Conversation conversation) {
215		return queryInProgress(conversation, null);
216	}
217
218	public void processFin(Element fin, Jid from) {
219		if (fin == null) {
220			return;
221		}
222		Query query = findQuery(fin.getAttribute("queryid"));
223		if (query == null || !query.validFrom(from)) {
224			return;
225		}
226		boolean complete = fin.getAttributeAsBoolean("complete");
227		Element set = fin.findChild("set","http://jabber.org/protocol/rsm");
228		Element last = set == null ? null : set.findChild("last");
229		Element first = set == null ? null : set.findChild("first");
230		Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first;
231		boolean abort = (!query.isCatchup() && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES;
232		if (query.getConversation() != null) {
233			query.getConversation().setFirstMamReference(first == null ? null : first.getContent());
234		}
235		if (complete || relevant == null || abort) {
236			final boolean done = (complete || query.getActualMessageCount() == 0) && !query.isCatchup();
237			this.finalizeQuery(query, done);
238			Log.d(Config.LOGTAG,query.getAccount().getJid().toBareJid()+": finished mam after "+query.getTotalCount()+"("+query.getActualMessageCount()+") messages. messages left="+Boolean.toString(!done));
239			if (query.getWith() == null && query.getActualMessageCount() > 0) {
240				mXmppConnectionService.getNotificationService().finishBacklog(true,query.getAccount());
241			}
242		} else {
243			final Query nextQuery;
244			if (query.getPagingOrder() == PagingOrder.NORMAL) {
245				nextQuery = query.next(last == null ? null : last.getContent());
246			} else {
247				nextQuery = query.prev(first == null ? null : first.getContent());
248			}
249			this.execute(nextQuery);
250			this.finalizeQuery(query, false);
251			synchronized (this.queries) {
252				this.queries.add(nextQuery);
253			}
254		}
255	}
256
257	public Query findQuery(String id) {
258		if (id == null) {
259			return null;
260		}
261		synchronized (this.queries) {
262			for(Query query : this.queries) {
263				if (query.getQueryId().equals(id)) {
264					return query;
265				}
266			}
267			return null;
268		}
269	}
270
271	@Override
272	public void onAdvancedStreamFeaturesAvailable(Account account) {
273		if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) {
274			this.catchup(account);
275		}
276	}
277
278	public class Query {
279		private int totalCount = 0;
280		private int actualCount = 0;
281		private long start;
282		private long end;
283		private String queryId;
284		private String reference = null;
285		private Account account;
286		private Conversation conversation;
287		private PagingOrder pagingOrder = PagingOrder.NORMAL;
288		private XmppConnectionService.OnMoreMessagesLoaded callback = null;
289		private boolean catchup = true;
290
291
292		public Query(Conversation conversation, long start, long end) {
293			this(conversation.getAccount(), start, end);
294			this.conversation = conversation;
295		}
296
297		public Query(Conversation conversation, long start, long end, boolean catchup) {
298			this(conversation,start,end);
299			this.pagingOrder = catchup ? PagingOrder.NORMAL : PagingOrder.REVERSE;
300			this.catchup = catchup;
301		}
302
303		public Query(Account account, long start, long end) {
304			this.account = account;
305			this.start = start;
306			this.end = end;
307			this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
308		}
309		
310		private Query page(String reference) {
311			Query query = new Query(this.account,this.start,this.end);
312			query.reference = reference;
313			query.conversation = conversation;
314			query.totalCount = totalCount;
315			query.actualCount = actualCount;
316			query.callback = callback;
317			query.catchup = catchup;
318			return query;
319		}
320
321		public Query next(String reference) {
322			Query query = page(reference);
323			query.pagingOrder = PagingOrder.NORMAL;
324			return query;
325		}
326
327		public Query prev(String reference) {
328			Query query = page(reference);
329			query.pagingOrder = PagingOrder.REVERSE;
330			return query;
331		}
332
333		public String getReference() {
334			return reference;
335		}
336
337		public PagingOrder getPagingOrder() {
338			return this.pagingOrder;
339		}
340
341		public String getQueryId() {
342			return queryId;
343		}
344
345		public Jid getWith() {
346			return conversation == null ? null : conversation.getJid().toBareJid();
347		}
348
349		public boolean muc() {
350			return conversation != null && conversation.getMode() == Conversation.MODE_MULTI;
351		}
352
353		public long getStart() {
354			return start;
355		}
356
357		public boolean isCatchup() {
358			return catchup;
359		}
360
361		public void setCallback(XmppConnectionService.OnMoreMessagesLoaded callback) {
362			this.callback = callback;
363		}
364
365		public void callback(boolean done) {
366			if (this.callback != null) {
367				this.callback.onMoreMessagesLoaded(actualCount,conversation);
368				if (done) {
369					this.callback.informUser(R.string.no_more_history_on_server);
370				}
371			}
372		}
373
374		public long getEnd() {
375			return end;
376		}
377
378		public Conversation getConversation() {
379			return conversation;
380		}
381
382		public Account getAccount() {
383			return this.account;
384		}
385
386		public void incrementMessageCount() {
387			this.totalCount++;
388		}
389
390		public void incrementActualMessageCount() {
391			this.actualCount++;
392		}
393
394		public int getTotalCount() {
395			return this.totalCount;
396		}
397
398		public int getActualMessageCount() {
399			return this.actualCount;
400		}
401
402		public boolean validFrom(Jid from) {
403			if (muc()) {
404				return getWith().equals(from);
405			} else {
406				return (from == null) || account.getJid().toBareJid().equals(from.toBareJid());
407			}
408		}
409
410		@Override
411		public String toString() {
412			StringBuilder builder = new StringBuilder();
413			if (this.muc()) {
414				builder.append("to=");
415				builder.append(this.getWith().toString());
416			} else {
417				builder.append("with=");
418				if (this.getWith() == null) {
419					builder.append("*");
420				} else {
421					builder.append(getWith().toString());
422				}
423			}
424			builder.append(", start=");
425			builder.append(AbstractGenerator.getTimestamp(this.start));
426			builder.append(", end=");
427			builder.append(AbstractGenerator.getTimestamp(this.end));
428			builder.append(", order="+pagingOrder.toString());
429			if (this.reference!=null) {
430				if (this.pagingOrder == PagingOrder.NORMAL) {
431					builder.append(", after=");
432				} else {
433					builder.append(", before=");
434				}
435				builder.append(this.reference);
436			}
437			builder.append(", catchup="+Boolean.toString(catchup));
438			return builder.toString();
439		}
440
441		public boolean hasCallback() {
442			return this.callback != null;
443		}
444	}
445}