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                    finalizeQuery(query, true);
260                }
261            });
262        } else {
263            synchronized (this.pendingQueries) {
264                this.pendingQueries.add(query);
265            }
266        }
267    }
268
269    private void finalizeQuery(final Query query, boolean done) {
270        synchronized (this.queries) {
271            if (!this.queries.remove(query)) {
272                throw new IllegalStateException("Unable to remove query from queries");
273            }
274        }
275        final Conversation conversation = query.getConversation();
276        if (conversation != null) {
277            conversation.sort();
278            conversation.setHasMessagesLeftOnServer(!done);
279        } else {
280            for (Conversation tmp : this.mXmppConnectionService.getConversations()) {
281                if (tmp.getAccount() == query.getAccount()) {
282                    tmp.sort();
283                }
284            }
285        }
286        if (query.hasCallback()) {
287            query.callback(done);
288        } else {
289            this.mXmppConnectionService.updateConversationUi();
290        }
291    }
292
293    boolean inCatchup(Account account) {
294        synchronized (this.queries) {
295            for (Query query : queries) {
296                if (query.account == account && query.isCatchup() && query.getWith() == null) {
297                    return true;
298                }
299            }
300        }
301        return false;
302    }
303
304    public boolean isCatchupInProgress(Conversation conversation) {
305        synchronized (this.queries) {
306            for (Query query : queries) {
307                if (query.account == conversation.getAccount() && query.isCatchup()) {
308                    final Jid with = query.getWith() == null ? null : query.getWith().asBareJid();
309                    if ((conversation.getMode() == Conversational.MODE_SINGLE && with == null) || (conversation.getJid().asBareJid().equals(with))) {
310                        return true;
311                    }
312                }
313            }
314        }
315        return false;
316    }
317
318    boolean queryInProgress(Conversation conversation, XmppConnectionService.OnMoreMessagesLoaded callback) {
319        synchronized (this.queries) {
320            for (Query query : queries) {
321                if (query.conversation == conversation) {
322                    if (!query.hasCallback() && callback != null) {
323                        query.setCallback(callback);
324                    }
325                    return true;
326                }
327            }
328            return false;
329        }
330    }
331
332    public boolean queryInProgress(Conversation conversation) {
333        return queryInProgress(conversation, null);
334    }
335
336    public void processFinLegacy(Element fin, Jid from) {
337        Query query = findQuery(fin.getAttribute("queryid"));
338        if (query != null && query.validFrom(from)) {
339            processFin(query, fin);
340        }
341    }
342
343    private void processFin(Query query, Element fin) {
344        boolean complete = fin.getAttributeAsBoolean("complete");
345        Element set = fin.findChild("set", "http://jabber.org/protocol/rsm");
346        Element last = set == null ? null : set.findChild("last");
347        String count = set == null ? null : set.findChildContent("count");
348        Element first = set == null ? null : set.findChild("first");
349        Element relevant = query.getPagingOrder() == PagingOrder.NORMAL ? last : first;
350        boolean abort = (!query.isCatchup() && query.getTotalCount() >= Config.PAGE_SIZE) || query.getTotalCount() >= Config.MAM_MAX_MESSAGES;
351        if (query.getConversation() != null) {
352            query.getConversation().setFirstMamReference(first == null ? null : first.getContent());
353        }
354        if (complete || relevant == null || abort) {
355            //TODO: FIX done logic to look at complete. using count is probably unreliable because it can be ommited and doesn’t work with paging.
356            boolean done;
357            if (query.isCatchup()) {
358                done = false;
359            } else {
360                if (count != null) {
361                    try {
362                        done = Integer.parseInt(count) <= query.getTotalCount();
363                    } catch (NumberFormatException e) {
364                        done = false;
365                    }
366                } else {
367                    done = query.getTotalCount() == 0;
368                }
369            }
370            done = done || (query.getActualMessageCount() == 0 && !query.isCatchup());
371            this.finalizeQuery(query, done);
372
373            Log.d(Config.LOGTAG, query.getAccount().getJid().asBareJid() + ": finished mam after " + query.getTotalCount() + "(" + query.getActualMessageCount() + ") messages. messages left=" + !done + " count=" + count);
374            if (query.isCatchup() && query.getActualMessageCount() > 0) {
375                mXmppConnectionService.getNotificationService().finishBacklog(true, query.getAccount());
376            }
377            processPostponed(query);
378        } else {
379            final Query nextQuery;
380            if (query.getPagingOrder() == PagingOrder.NORMAL) {
381                nextQuery = query.next(last == null ? null : last.getContent());
382            } else {
383                nextQuery = query.prev(first == null ? null : first.getContent());
384            }
385            this.execute(nextQuery);
386            this.finalizeQuery(query, false);
387            synchronized (this.queries) {
388                this.queries.add(nextQuery);
389            }
390        }
391    }
392
393    void kill(final Conversation conversation) {
394        final ArrayList<Query> toBeKilled = new ArrayList<>();
395        synchronized (this.pendingQueries) {
396            for (final Iterator<Query> iterator = this.pendingQueries.iterator(); iterator.hasNext(); ) {
397                final Query query = iterator.next();
398                if (query.getConversation() == conversation) {
399                    iterator.remove();
400                    Log.d(Config.LOGTAG, conversation.getAccount().getJid().asBareJid() + ": killed pending MAM query for archived conversation");
401                }
402            }
403        }
404        synchronized (this.queries) {
405            for (final Query q : queries) {
406                if (q.conversation == conversation) {
407                    toBeKilled.add(q);
408                }
409            }
410        }
411        for (final Query q : toBeKilled) {
412            kill(q);
413        }
414    }
415
416    private void kill(Query query) {
417        Log.d(Config.LOGTAG, query.getAccount().getJid().asBareJid() + ": killing mam query prematurely");
418        query.callback = null;
419        this.finalizeQuery(query, false);
420        if (query.isCatchup() && query.getActualMessageCount() > 0) {
421            mXmppConnectionService.getNotificationService().finishBacklog(true, query.getAccount());
422        }
423        this.processPostponed(query);
424    }
425
426    private void processPostponed(Query query) {
427        query.account.getAxolotlService().processPostponed();
428        query.pendingReceiptRequests.removeAll(query.receiptRequests);
429        Log.d(Config.LOGTAG, query.getAccount().getJid().asBareJid() + ": found " + query.pendingReceiptRequests.size() + " pending receipt requests");
430        Iterator<ReceiptRequest> iterator = query.pendingReceiptRequests.iterator();
431        while (iterator.hasNext()) {
432            ReceiptRequest rr = iterator.next();
433            mXmppConnectionService.sendMessagePacket(query.account, mXmppConnectionService.getMessageGenerator().received(query.account, rr.getJid(), rr.getId()));
434            iterator.remove();
435        }
436    }
437
438    public Query findQuery(String id) {
439        if (id == null) {
440            return null;
441        }
442        synchronized (this.queries) {
443            for (Query query : this.queries) {
444                if (query.getQueryId().equals(id)) {
445                    return query;
446                }
447            }
448            return null;
449        }
450    }
451
452    @Override
453    public void onAdvancedStreamFeaturesAvailable(Account account) {
454        if (account.getXmppConnection() != null && account.getXmppConnection().getFeatures().mam()) {
455            this.catchup(account);
456        }
457    }
458
459    public enum PagingOrder {
460        NORMAL,
461        REVERSE
462    }
463
464    public class Query {
465        private HashSet<ReceiptRequest> pendingReceiptRequests = new HashSet<>();
466        private HashSet<ReceiptRequest> receiptRequests = new HashSet<>();
467        private int totalCount = 0;
468        private int actualCount = 0;
469        private int actualInThisQuery = 0;
470        private long start;
471        private final long end;
472        private final String queryId;
473        private String reference = null;
474        private final Account account;
475        private Conversation conversation;
476        private PagingOrder pagingOrder = PagingOrder.NORMAL;
477        private XmppConnectionService.OnMoreMessagesLoaded callback = null;
478        private boolean catchup = true;
479        public final Version version;
480
481
482        Query(Conversation conversation, MamReference start, long end, boolean catchup) {
483            this(conversation.getAccount(), Version.get(conversation.getAccount(), conversation), catchup ? start : start.timeOnly(), end);
484            this.conversation = conversation;
485            this.pagingOrder = catchup ? PagingOrder.NORMAL : PagingOrder.REVERSE;
486            this.catchup = catchup;
487        }
488
489        Query(Account account, MamReference start, long end) {
490            this(account, Version.get(account), start, end);
491        }
492
493        Query(Account account, Version version, MamReference start, long end) {
494            this.account = account;
495            if (start.getReference() != null) {
496                this.reference = start.getReference();
497            } else {
498                this.start = start.getTimestamp();
499            }
500            this.end = end;
501            this.queryId = new BigInteger(50, mXmppConnectionService.getRNG()).toString(32);
502            this.version = version;
503        }
504
505        private Query page(String reference) {
506            Query query = new Query(this.account, this.version, new MamReference(this.start, reference), this.end);
507            query.conversation = conversation;
508            query.totalCount = totalCount;
509            query.actualCount = actualCount;
510            query.pendingReceiptRequests = pendingReceiptRequests;
511            query.receiptRequests = receiptRequests;
512            query.callback = callback;
513            query.catchup = catchup;
514            return query;
515        }
516
517        public void removePendingReceiptRequest(ReceiptRequest receiptRequest) {
518            if (!this.pendingReceiptRequests.remove(receiptRequest)) {
519                this.receiptRequests.add(receiptRequest);
520            }
521        }
522
523        public void addPendingReceiptRequest(ReceiptRequest receiptRequest) {
524            this.pendingReceiptRequests.add(receiptRequest);
525        }
526
527        public boolean isLegacy() {
528            return version.legacy;
529        }
530
531        public boolean safeToExtractTrueCounterpart() {
532            return muc() && !isLegacy();
533        }
534
535        public Query next(String reference) {
536            Query query = page(reference);
537            query.pagingOrder = PagingOrder.NORMAL;
538            return query;
539        }
540
541        Query prev(String reference) {
542            Query query = page(reference);
543            query.pagingOrder = PagingOrder.REVERSE;
544            return query;
545        }
546
547        public String getReference() {
548            return reference;
549        }
550
551        public PagingOrder getPagingOrder() {
552            return this.pagingOrder;
553        }
554
555        public String getQueryId() {
556            return queryId;
557        }
558
559        public Jid getWith() {
560            return conversation == null ? null : conversation.getJid().asBareJid();
561        }
562
563        public boolean muc() {
564            return conversation != null && conversation.getMode() == Conversation.MODE_MULTI;
565        }
566
567        public long getStart() {
568            return start;
569        }
570
571        public boolean isCatchup() {
572            return catchup;
573        }
574
575        public void setCallback(XmppConnectionService.OnMoreMessagesLoaded callback) {
576            this.callback = callback;
577        }
578
579        public void callback(boolean done) {
580            if (this.callback != null) {
581                this.callback.onMoreMessagesLoaded(actualCount, conversation);
582                if (done) {
583                    this.callback.informUser(R.string.no_more_history_on_server);
584                }
585            }
586        }
587
588        public long getEnd() {
589            return end;
590        }
591
592        public Conversation getConversation() {
593            return conversation;
594        }
595
596        public Account getAccount() {
597            return this.account;
598        }
599
600        public void incrementMessageCount() {
601            this.totalCount++;
602        }
603
604        public void incrementActualMessageCount() {
605            this.actualInThisQuery++;
606            this.actualCount++;
607        }
608
609        int getTotalCount() {
610            return this.totalCount;
611        }
612
613        int getActualMessageCount() {
614            return this.actualCount;
615        }
616
617        public int getActualInThisQuery() {
618            return this.actualInThisQuery;
619        }
620
621        public boolean validFrom(Jid from) {
622            if (muc()) {
623                return getWith().equals(from);
624            } else {
625                return (from == null) || account.getJid().asBareJid().equals(from.asBareJid());
626            }
627        }
628
629        @NotNull
630        @Override
631        public String toString() {
632            StringBuilder builder = new StringBuilder();
633            if (this.muc()) {
634                builder.append("to=");
635                builder.append(this.getWith().toString());
636            } else {
637                builder.append("with=");
638                if (this.getWith() == null) {
639                    builder.append("*");
640                } else {
641                    builder.append(getWith().toString());
642                }
643            }
644            if (this.start != 0) {
645                builder.append(", start=");
646                builder.append(AbstractGenerator.getTimestamp(this.start));
647            }
648            if (this.end != 0) {
649                builder.append(", end=");
650                builder.append(AbstractGenerator.getTimestamp(this.end));
651            }
652            builder.append(", order=").append(pagingOrder.toString());
653            if (this.reference != null) {
654                if (this.pagingOrder == PagingOrder.NORMAL) {
655                    builder.append(", after=");
656                } else {
657                    builder.append(", before=");
658                }
659                builder.append(this.reference);
660            }
661            builder.append(", catchup=").append(catchup);
662            builder.append(", ns=").append(version.namespace);
663            return builder.toString();
664        }
665
666        boolean hasCallback() {
667            return this.callback != null;
668        }
669    }
670}