MessageArchiveService.java

  1package eu.siacs.conversations.services;
  2
  3import android.util.Log;
  4
  5import org.jetbrains.annotations.NotNull;
  6
  7import java.math.BigInteger;
  8import java.util.ArrayList;
  9import java.util.HashSet;
 10import java.util.Iterator;
 11import java.util.List;
 12
 13import eu.siacs.conversations.Config;
 14import eu.siacs.conversations.R;
 15import eu.siacs.conversations.entities.Account;
 16import eu.siacs.conversations.entities.Conversation;
 17import eu.siacs.conversations.entities.Conversational;
 18import eu.siacs.conversations.entities.ReceiptRequest;
 19import eu.siacs.conversations.generator.AbstractGenerator;
 20import eu.siacs.conversations.xml.Element;
 21import eu.siacs.conversations.xmpp.Jid;
 22import eu.siacs.conversations.xmpp.OnAdvancedStreamFeaturesLoaded;
 23import eu.siacs.conversations.xmpp.mam.MamReference;
 24import eu.siacs.conversations.xmpp.stanzas.IqPacket;
 25import eu.siacs.conversations.xmpp.stanzas.MessagePacket;
 26
 27public class MessageArchiveService implements OnAdvancedStreamFeaturesLoaded {
 28
 29    private final XmppConnectionService mXmppConnectionService;
 30
 31    private final HashSet<Query> queries = new HashSet<>();
 32    private final ArrayList<Query> pendingQueries = new ArrayList<>();
 33
 34    public enum Version {
 35        MAM_0("urn:xmpp:mam:0", true),
 36        MAM_1("urn:xmpp:mam:1", false),
 37        MAM_2("urn:xmpp:mam:2", false);
 38
 39        public final boolean legacy;
 40        public final String namespace;
 41
 42        Version(String namespace, boolean legacy) {
 43            this.namespace = namespace;
 44            this.legacy = legacy;
 45        }
 46
 47        public static Version get(Account account) {
 48            return get(account, null);
 49        }
 50
 51        public static Version get(Account account, Conversation conversation) {
 52            if (conversation == null || conversation.getMode() == Conversation.MODE_SINGLE) {
 53                return get(account.getXmppConnection().getFeatures().getAccountFeatures());
 54            } else {
 55                return get(conversation.getMucOptions().getFeatures());
 56            }
 57        }
 58
 59        private static Version get(List<String> features) {
 60            final Version[] values = values();
 61            for (int i = values.length - 1; i >= 0; --i) {
 62                for (String feature : features) {
 63                    if (values[i].namespace.equals(feature)) {
 64                        return values[i];
 65                    }
 66                }
 67            }
 68            return MAM_0;
 69        }
 70
 71        public static boolean has(List<String> features) {
 72            for (String feature : features) {
 73                for (Version version : values()) {
 74                    if (version.namespace.equals(feature)) {
 75                        return true;
 76                    }
 77                }
 78            }
 79            return false;
 80        }
 81
 82        public static Element findResult(MessagePacket packet) {
 83            for (Version version : values()) {
 84                Element result = packet.findChild("result", version.namespace);
 85                if (result != null) {
 86                    return result;
 87                }
 88            }
 89            return null;
 90        }
 91
 92    }
 93
 94    MessageArchiveService(final XmppConnectionService service) {
 95        this.mXmppConnectionService = service;
 96    }
 97
 98    private void catchup(final Account account) {
 99        synchronized (this.queries) {
100            for (Iterator<Query> iterator = this.queries.iterator(); iterator.hasNext(); ) {
101                Query query = iterator.next();
102                if (query.getAccount() == account) {
103                    iterator.remove();
104                }
105            }
106        }
107        MamReference mamReference = MamReference.max(
108                mXmppConnectionService.databaseBackend.getLastMessageReceived(account),
109                mXmppConnectionService.databaseBackend.getLastClearDate(account)
110        );
111        mamReference = MamReference.max(mamReference, mXmppConnectionService.getAutomaticMessageDeletionDate());
112        long endCatchup = account.getXmppConnection().getLastSessionEstablished();
113        final Query query;
114        if (mamReference.getTimestamp() == 0) {
115            return;
116        } else if (endCatchup - mamReference.getTimestamp() >= Config.MAM_MAX_CATCHUP) {
117            long startCatchup = endCatchup - Config.MAM_MAX_CATCHUP;
118            List<Conversation> conversations = mXmppConnectionService.getConversations();
119            for (Conversation conversation : conversations) {
120                if (conversation.getMode() == Conversation.MODE_SINGLE && conversation.getAccount() == account && startCatchup > conversation.getLastMessageTransmitted().getTimestamp()) {
121                    this.query(conversation, startCatchup, true);
122                }
123            }
124            query = new Query(account, new MamReference(startCatchup), 0);
125        } else {
126            query = new Query(account, mamReference, 0);
127        }
128        synchronized (this.queries) {
129            this.queries.add(query);
130        }
131        this.execute(query);
132    }
133
134    void catchupMUC(final Conversation conversation) {
135        if (conversation.getLastMessageTransmitted().getTimestamp() < 0 && conversation.countMessages() == 0) {
136            query(conversation,
137                    new MamReference(0),
138                    0,
139                    true);
140        } else {
141            query(conversation,
142                    conversation.getLastMessageTransmitted(),
143                    0,
144                    true);
145        }
146    }
147
148    public Query query(final Conversation conversation) {
149        if (conversation.getLastMessageTransmitted().getTimestamp() < 0 && conversation.countMessages() == 0) {
150            return query(conversation,
151                    new MamReference(0),
152                    System.currentTimeMillis(),
153                    false);
154        } else {
155            return query(conversation,
156                    conversation.getLastMessageTransmitted(),
157                    conversation.getAccount().getXmppConnection().getLastSessionEstablished(),
158                    false);
159        }
160    }
161
162    public boolean isCatchingUp(Conversation conversation) {
163        final Account account = conversation.getAccount();
164        if (account.getXmppConnection().isWaitingForSmCatchup()) {
165            return true;
166        } else {
167            synchronized (this.queries) {
168                for (Query query : this.queries) {
169                    if (query.getAccount() == account && query.isCatchup() && ((conversation.getMode() == Conversation.MODE_SINGLE && query.getWith() == null) || query.getConversation() == conversation)) {
170                        return true;
171                    }
172                }
173            }
174            return false;
175        }
176    }
177
178    public Query query(final Conversation conversation, long end, boolean allowCatchup) {
179        return this.query(conversation, conversation.getLastMessageTransmitted(), end, allowCatchup);
180    }
181
182    public Query query(Conversation conversation, MamReference start, long end, boolean allowCatchup) {
183        synchronized (this.queries) {
184            final Query query;
185            final MamReference startActual = MamReference.max(start, mXmppConnectionService.getAutomaticMessageDeletionDate());
186            if (start.getTimestamp() == 0) {
187                query = new Query(conversation, startActual, end, false);
188                query.reference = conversation.getFirstMamReference();
189            } else {
190                if (allowCatchup) {
191                    MamReference maxCatchup = MamReference.max(startActual, System.currentTimeMillis() - Config.MAM_MAX_CATCHUP);
192                    if (maxCatchup.greaterThan(startActual)) {
193                        Query reverseCatchup = new Query(conversation, startActual, maxCatchup.getTimestamp(), false);
194                        this.queries.add(reverseCatchup);
195                        this.execute(reverseCatchup);
196                    }
197                    query = new Query(conversation, maxCatchup, end, true);
198                } else {
199                    query = new Query(conversation, startActual, end, false);
200                }
201            }
202            if (end != 0 && start.greaterThan(end)) {
203                return null;
204            }
205            this.queries.add(query);
206            this.execute(query);
207            return query;
208        }
209    }
210
211    void executePendingQueries(final Account account) {
212        final List<Query> pending = new ArrayList<>();
213        synchronized (this.pendingQueries) {
214            for (Iterator<Query> iterator = this.pendingQueries.iterator(); iterator.hasNext(); ) {
215                Query query = iterator.next();
216                if (query.getAccount() == account) {
217                    pending.add(query);
218                    iterator.remove();
219                }
220            }
221        }
222        for (Query query : pending) {
223            this.execute(query);
224        }
225    }
226
227    private void execute(final Query query) {
228        final Account account = query.getAccount();
229        if (account.getStatus() == Account.State.ONLINE) {
230            final Conversation conversation = query.getConversation();
231            if (conversation != null && conversation.getStatus() == Conversation.STATUS_ARCHIVED) {
232                throw new IllegalStateException("Attempted to run MAM query for archived conversation");
233            }
234            Log.d(Config.LOGTAG, account.getJid().asBareJid().toString() + ": running mam query " + query.toString());
235            final IqPacket packet = this.mXmppConnectionService.getIqGenerator().queryMessageArchiveManagement(query);
236            this.mXmppConnectionService.sendIqPacket(account, packet, (a, p) -> {
237                final Element fin = p.findChild("fin", query.version.namespace);
238                if (p.getType() == IqPacket.TYPE.TIMEOUT) {
239                    synchronized (this.queries) {
240                        this.queries.remove(query);
241                        if (query.hasCallback()) {
242                            query.callback(false);
243                        }
244                    }
245                } else if (p.getType() == IqPacket.TYPE.RESULT && fin != null) {
246                    final boolean running;
247                    synchronized (this.queries) {
248                        running = this.queries.contains(query);
249                    }
250                    if (running) {
251                        processFin(query, fin);
252                    } else {
253                        Log.d(Config.LOGTAG, account.getJid().asBareJid() + ": ignoring MAM iq result because query had been killed");
254                    }
255                } else if (p.getType() == IqPacket.TYPE.RESULT && query.isLegacy()) {
256                    //do nothing
257                } else {
258                    Log.d(Config.LOGTAG, a.getJid().asBareJid().toString() + ": error executing mam: " + p.toString());
259                    try {
260                        finalizeQuery(query, true);
261                    } catch (final IllegalStateException e) {
262                        //ignored
263                    }
264                }
265            });
266        } else {
267            synchronized (this.pendingQueries) {
268                this.pendingQueries.add(query);
269            }
270        }
271    }
272
273    private void finalizeQuery(final Query query, boolean done) {
274        synchronized (this.queries) {
275            if (!this.queries.remove(query)) {
276                throw new IllegalStateException("Unable to remove query from queries");
277            }
278        }
279        final Conversation conversation = query.getConversation();
280        if (conversation != null) {
281            conversation.sort();
282            conversation.setHasMessagesLeftOnServer(!done);
283        } else {
284            for (Conversation tmp : this.mXmppConnectionService.getConversations()) {
285                if (tmp.getAccount() == query.getAccount()) {
286                    tmp.sort();
287                }
288            }
289        }
290        if (query.hasCallback()) {
291            query.callback(done);
292        } else {
293            this.mXmppConnectionService.updateConversationUi();
294        }
295    }
296
297    boolean inCatchup(Account account) {
298        synchronized (this.queries) {
299            for (Query query : queries) {
300                if (query.account == account && query.isCatchup() && query.getWith() == null) {
301                    return true;
302                }
303            }
304        }
305        return false;
306    }
307
308    public boolean isCatchupInProgress(Conversation conversation) {
309        synchronized (this.queries) {
310            for (Query query : queries) {
311                if (query.account == conversation.getAccount() && query.isCatchup()) {
312                    final Jid with = query.getWith() == null ? null : query.getWith().asBareJid();
313                    if ((conversation.getMode() == Conversational.MODE_SINGLE && with == null) || (conversation.getJid().asBareJid().equals(with))) {
314                        return true;
315                    }
316                }
317            }
318        }
319        return false;
320    }
321
322    boolean queryInProgress(Conversation conversation, XmppConnectionService.OnMoreMessagesLoaded callback) {
323        synchronized (this.queries) {
324            for (Query query : queries) {
325                if (query.conversation == conversation) {
326                    if (!query.hasCallback() && callback != null) {
327                        query.setCallback(callback);
328                    }
329                    return true;
330                }
331            }
332            return false;
333        }
334    }
335
336    public boolean queryInProgress(Conversation conversation) {
337        return queryInProgress(conversation, null);
338    }
339
340    public void processFinLegacy(Element fin, Jid from) {
341        Query query = findQuery(fin.getAttribute("queryid"));
342        if (query != null && query.validFrom(from)) {
343            processFin(query, fin);
344        }
345    }
346
347    private void processFin(Query query, Element fin) {
348        boolean complete = fin.getAttributeAsBoolean("complete");
349        Element set = fin.findChild("set", "http://jabber.org/protocol/rsm");
350        Element last = set == null ? null : set.findChild("last");
351        String count = set == null ? null : set.findChildContent("count");
352        Element first = set == null ? null : set.findChild("first");
353        Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first;
354        boolean abort = (!query.isCatchup() && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES;
355        if (query.getConversation() != null) {
356            query.getConversation().setFirstMamReference(first == null ? null : first.getContent());
357        }
358        if (complete || relevant == null || abort) {
359            //TODO: FIX done logic to look at complete. using count is probably unreliable because it can be ommited and doesn’t work with paging.
360            boolean done;
361            if (query.isCatchup()) {
362                done = false;
363            } else {
364                if (count != null) {
365                    try {
366                        done = Integer.parseInt(count) <= query.getTotalCount();
367                    } catch (NumberFormatException e) {
368                        done = false;
369                    }
370                } else {
371                    done = query.getTotalCount() == 0;
372                }
373            }
374            done = done || (query.getActualMessageCount() == 0 && !query.isCatchup());
375            this.finalizeQuery(query, done);
376
377            Log.d(Config.LOGTAG, query.getAccount().getJid().asBareJid() + ": finished mam after " + query.getTotalCount() + "(" + query.getActualMessageCount() + ") messages. messages left=" + !done + " count=" + count);
378            if (query.isCatchup() && query.getActualMessageCount() > 0) {
379                mXmppConnectionService.getNotificationService().finishBacklog(true, query.getAccount());
380            }
381            processPostponed(query);
382        } else {
383            final Query nextQuery;
384            if (query.getPagingOrder() == PagingOrder.NORMAL) {
385                nextQuery = query.next(last == null ? null : last.getContent());
386            } else {
387                nextQuery = query.prev(first == null ? null : first.getContent());
388            }
389            this.execute(nextQuery);
390            this.finalizeQuery(query, false);
391            synchronized (this.queries) {
392                this.queries.add(nextQuery);
393            }
394        }
395    }
396
397    void kill(final Conversation conversation) {
398        final ArrayList<Query> toBeKilled = new ArrayList<>();
399        synchronized (this.pendingQueries) {
400            for (final Iterator<Query> iterator = this.pendingQueries.iterator(); iterator.hasNext(); ) {
401                final Query query = iterator.next();
402                if (query.getConversation() == conversation) {
403                    iterator.remove();
404                    Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": killed pending MAM query for archived conversation");
405                }
406            }
407        }
408        synchronized (this.queries) {
409            for (final Query q : queries) {
410                if (q.conversation == conversation) {
411                    toBeKilled.add(q);
412                }
413            }
414        }
415        for (final Query q : toBeKilled) {
416            kill(q);
417        }
418    }
419
420    private void kill(Query query) {
421        Log.d(Config.LOGTAG, query.getAccount().getJid().asBareJid() + ": killing mam query prematurely");
422        query.callback = null;
423        this.finalizeQuery(query, false);
424        if (query.isCatchup() && query.getActualMessageCount() > 0) {
425            mXmppConnectionService.getNotificationService().finishBacklog(true, query.getAccount());
426        }
427        this.processPostponed(query);
428    }
429
430    private void processPostponed(Query query) {
431        query.account.getAxolotlService().processPostponed();
432        query.pendingReceiptRequests.removeAll(query.receiptRequests);
433        Log.d(Config.LOGTAG, query.getAccount().getJid().asBareJid() + ": found " + query.pendingReceiptRequests.size() + " pending receipt requests");
434        Iterator<ReceiptRequest> iterator = query.pendingReceiptRequests.iterator();
435        while (iterator.hasNext()) {
436            ReceiptRequest rr = iterator.next();
437            mXmppConnectionService.sendMessagePacket(query.account, mXmppConnectionService.getMessageGenerator().received(query.account, rr.getJid(), rr.getId()));
438            iterator.remove();
439        }
440    }
441
442    public Query findQuery(String id) {
443        if (id == null) {
444            return null;
445        }
446        synchronized (this.queries) {
447            for (Query query : this.queries) {
448                if (query.getQueryId().equals(id)) {
449                    return query;
450                }
451            }
452            return null;
453        }
454    }
455
456    @Override
457    public void onAdvancedStreamFeaturesAvailable(Account account) {
458        if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) {
459            this.catchup(account);
460        }
461    }
462
463    public enum PagingOrder {
464        NORMAL,
465        REVERSE
466    }
467
468    public class Query {
469        private HashSet<ReceiptRequest> pendingReceiptRequests = new HashSet<>();
470        private HashSet<ReceiptRequest> receiptRequests = new HashSet<>();
471        private int totalCount = 0;
472        private int actualCount = 0;
473        private int actualInThisQuery = 0;
474        private long start;
475        private final long end;
476        private final String queryId;
477        private String reference = null;
478        private final Account account;
479        private Conversation conversation;
480        private PagingOrder pagingOrder = PagingOrder.NORMAL;
481        private XmppConnectionService.OnMoreMessagesLoaded callback = null;
482        private boolean catchup = true;
483        public final Version version;
484
485
486        Query(Conversation conversation, MamReference start, long end, boolean catchup) {
487            this(conversation.getAccount(), Version.get(conversation.getAccount(), conversation), catchup ? start : start.timeOnly(), end);
488            this.conversation = conversation;
489            this.pagingOrder = catchup ? PagingOrder.NORMAL : PagingOrder.REVERSE;
490            this.catchup = catchup;
491        }
492
493        Query(Account account, MamReference start, long end) {
494            this(account, Version.get(account), start, end);
495        }
496
497        Query(Account account, Version version, MamReference start, long end) {
498            this.account = account;
499            if (start.getReference() != null) {
500                this.reference = start.getReference();
501            } else {
502                this.start = start.getTimestamp();
503            }
504            this.end = end;
505            this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
506            this.version = version;
507        }
508
509        private Query page(String reference) {
510            Query query = new Query(this.account, this.version, new MamReference(this.start, reference), this.end);
511            query.conversation = conversation;
512            query.totalCount = totalCount;
513            query.actualCount = actualCount;
514            query.pendingReceiptRequests = pendingReceiptRequests;
515            query.receiptRequests = receiptRequests;
516            query.callback = callback;
517            query.catchup = catchup;
518            return query;
519        }
520
521        public void removePendingReceiptRequest(ReceiptRequest receiptRequest) {
522            if (!this.pendingReceiptRequests.remove(receiptRequest)) {
523                this.receiptRequests.add(receiptRequest);
524            }
525        }
526
527        public void addPendingReceiptRequest(ReceiptRequest receiptRequest) {
528            this.pendingReceiptRequests.add(receiptRequest);
529        }
530
531        public boolean isLegacy() {
532            return version.legacy;
533        }
534
535        public boolean safeToExtractTrueCounterpart() {
536            return muc() && !isLegacy();
537        }
538
539        public Query next(String reference) {
540            Query query = page(reference);
541            query.pagingOrder = PagingOrder.NORMAL;
542            return query;
543        }
544
545        Query prev(String reference) {
546            Query query = page(reference);
547            query.pagingOrder = PagingOrder.REVERSE;
548            return query;
549        }
550
551        public String getReference() {
552            return reference;
553        }
554
555        public PagingOrder getPagingOrder() {
556            return this.pagingOrder;
557        }
558
559        public String getQueryId() {
560            return queryId;
561        }
562
563        public Jid getWith() {
564            return conversation == null ? null : conversation.getJid().asBareJid();
565        }
566
567        public boolean muc() {
568            return conversation != null && conversation.getMode() == Conversation.MODE_MULTI;
569        }
570
571        public long getStart() {
572            return start;
573        }
574
575        public boolean isCatchup() {
576            return catchup;
577        }
578
579        public void setCallback(XmppConnectionService.OnMoreMessagesLoaded callback) {
580            this.callback = callback;
581        }
582
583        public void callback(boolean done) {
584            if (this.callback != null) {
585                this.callback.onMoreMessagesLoaded(actualCount, conversation);
586                if (done) {
587                    this.callback.informUser(R.string.no_more_history_on_server);
588                }
589            }
590        }
591
592        public long getEnd() {
593            return end;
594        }
595
596        public Conversation getConversation() {
597            return conversation;
598        }
599
600        public Account getAccount() {
601            return this.account;
602        }
603
604        public void incrementMessageCount() {
605            this.totalCount++;
606        }
607
608        public void incrementActualMessageCount() {
609            this.actualInThisQuery++;
610            this.actualCount++;
611        }
612
613        int getTotalCount() {
614            return this.totalCount;
615        }
616
617        int getActualMessageCount() {
618            return this.actualCount;
619        }
620
621        public int getActualInThisQuery() {
622            return this.actualInThisQuery;
623        }
624
625        public boolean validFrom(Jid from) {
626            if (muc()) {
627                return getWith().equals(from);
628            } else {
629                return (from == null) || account.getJid().asBareJid().equals(from.asBareJid());
630            }
631        }
632
633        @NotNull
634        @Override
635        public String toString() {
636            StringBuilder builder = new StringBuilder();
637            if (this.muc()) {
638                builder.append("to=");
639                builder.append(this.getWith().toString());
640            } else {
641                builder.append("with=");
642                if (this.getWith() == null) {
643                    builder.append("*");
644                } else {
645                    builder.append(getWith().toString());
646                }
647            }
648            if (this.start != 0) {
649                builder.append(", start=");
650                builder.append(AbstractGenerator.getTimestamp(this.start));
651            }
652            if (this.end != 0) {
653                builder.append(", end=");
654                builder.append(AbstractGenerator.getTimestamp(this.end));
655            }
656            builder.append(", order=").append(pagingOrder.toString());
657            if (this.reference != null) {
658                if (this.pagingOrder == PagingOrder.NORMAL) {
659                    builder.append(", after=");
660                } else {
661                    builder.append(", before=");
662                }
663                builder.append(this.reference);
664            }
665            builder.append(", catchup=").append(catchup);
666            builder.append(", ns=").append(version.namespace);
667            return builder.toString();
668        }
669
670        boolean hasCallback() {
671            return this.callback != null;
672        }
673    }
674}