Search in sources :

Example 1 with MamReference

use of de.pixart.messenger.xmpp.mam.MamReference in project Pix-Art-Messenger by kriztan.

the class MessageArchiveService method query.

public Query query(Conversation conversation, MamReference start, long end, boolean allowCatchup) {
    synchronized (this.queries) {
        final Query query;
        final MamReference startActual = MamReference.max(start, mXmppConnectionService.getAutomaticMessageDeletionDate());
        if (start.getTimestamp() == 0) {
            query = new Query(conversation, startActual, end, false);
            query.reference = conversation.getFirstMamReference();
        } else {
            if (allowCatchup) {
                MamReference maxCatchup = MamReference.max(startActual, System.currentTimeMillis() - Config.MAM_MAX_CATCHUP);
                if (maxCatchup.greaterThan(startActual)) {
                    Query reverseCatchup = new Query(conversation, startActual, maxCatchup.getTimestamp(), false);
                    this.queries.add(reverseCatchup);
                    this.execute(reverseCatchup);
                }
                query = new Query(conversation, maxCatchup, end, true);
            } else {
                query = new Query(conversation, startActual, end, false);
            }
        }
        if (start.greaterThan(end)) {
            return null;
        }
        this.queries.add(query);
        this.execute(query);
        return query;
    }
}
Also used : MamReference(de.pixart.messenger.xmpp.mam.MamReference)

Example 2 with MamReference

use of de.pixart.messenger.xmpp.mam.MamReference in project Pix-Art-Messenger by kriztan.

the class DatabaseBackend method getLastMessageReceived.

public MamReference getLastMessageReceived(Account account) {
    Cursor cursor = null;
    try {
        SQLiteDatabase db = this.getReadableDatabase();
        String sql = "select messages.timeSent,messages.serverMsgId from accounts join conversations on accounts.uuid=conversations.accountUuid join messages on conversations.uuid=messages.conversationUuid where accounts.uuid=? and (messages.status=0 or messages.carbon=1 or messages.serverMsgId not null) and conversations.mode=0 order by messages.timesent desc limit 1";
        String[] args = { account.getUuid() };
        cursor = db.rawQuery(sql, args);
        if (cursor.getCount() == 0) {
            return null;
        } else {
            cursor.moveToFirst();
            return new MamReference(cursor.getLong(0), cursor.getString(1));
        }
    } catch (Exception e) {
        return null;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}
Also used : SQLiteDatabase(android.database.sqlite.SQLiteDatabase) MamReference(de.pixart.messenger.xmpp.mam.MamReference) Cursor(android.database.Cursor) JSONException(org.json.JSONException) CertificateEncodingException(java.security.cert.CertificateEncodingException) InvalidJidException(de.pixart.messenger.xmpp.jid.InvalidJidException) InvalidKeyException(org.whispersystems.libsignal.InvalidKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException)

Example 3 with MamReference

use of de.pixart.messenger.xmpp.mam.MamReference in project Pix-Art-Messenger by kriztan.

the class XmppConnectionService method loadMoreMessages.

public void loadMoreMessages(final Conversation conversation, final long timestamp, final OnMoreMessagesLoaded callback) {
    if (XmppConnectionService.this.getMessageArchiveService().queryInProgress(conversation, callback)) {
        return;
    } else if (timestamp == 0) {
        return;
    }
    Log.d(Config.LOGTAG, "load more messages for " + conversation.getName() + " prior to " + MessageGenerator.getTimestamp(timestamp));
    final Runnable runnable = () -> {
        final Account account = conversation.getAccount();
        List<Message> messages = databaseBackend.getMessages(conversation, 50, timestamp);
        if (messages.size() > 0) {
            conversation.addAll(0, messages);
            checkDeletedFiles(conversation);
            callback.onMoreMessagesLoaded(messages.size(), conversation);
        } else if (conversation.hasMessagesLeftOnServer() && account.isOnlineAndConnected() && conversation.getLastClearHistory().getTimestamp() == 0) {
            final boolean mamAvailable;
            if (conversation.getMode() == Conversation.MODE_SINGLE) {
                mamAvailable = account.getXmppConnection().getFeatures().mam() && !conversation.getContact().isBlocked();
            } else {
                mamAvailable = conversation.getMucOptions().mamSupport();
            }
            if (mamAvailable) {
                MessageArchiveService.Query query = getMessageArchiveService().query(conversation, new MamReference(0), timestamp, false);
                if (query != null) {
                    query.setCallback(callback);
                    callback.informUser(R.string.fetching_history_from_server);
                } else {
                    callback.informUser(R.string.not_fetching_history_retention_period);
                }
            }
        }
    };
    mDatabaseReaderExecutor.execute(runnable);
}
Also used : Account(de.pixart.messenger.entities.Account) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) List(java.util.List) MamReference(de.pixart.messenger.xmpp.mam.MamReference)

Example 4 with MamReference

use of de.pixart.messenger.xmpp.mam.MamReference in project Pix-Art-Messenger by kriztan.

the class Conversation method getLastMessageTransmitted.

public MamReference getLastMessageTransmitted() {
    final MamReference lastClear = getLastClearHistory();
    MamReference lastReceived = new MamReference(0);
    synchronized (this.messages) {
        for (int i = this.messages.size() - 1; i >= 0; --i) {
            final Message message = this.messages.get(i);
            if (message.getType() == Message.TYPE_PRIVATE) {
                // it's unsafe to use private messages as anchor. They could be coming from user archive
                continue;
            }
            if (message.getStatus() == Message.STATUS_RECEIVED || message.isCarbon() || message.getServerMsgId() != null) {
                lastReceived = new MamReference(message.getTimeSent(), message.getServerMsgId());
                break;
            }
        }
    }
    return MamReference.max(lastClear, lastReceived);
}
Also used : MamReference(de.pixart.messenger.xmpp.mam.MamReference)

Example 5 with MamReference

use of de.pixart.messenger.xmpp.mam.MamReference in project Pix-Art-Messenger by kriztan.

the class MessageArchiveService method catchup.

private void catchup(final Account account) {
    synchronized (this.queries) {
        for (Iterator<Query> iterator = this.queries.iterator(); iterator.hasNext(); ) {
            Query query = iterator.next();
            if (query.getAccount() == account) {
                iterator.remove();
            }
        }
    }
    MamReference mamReference = MamReference.max(mXmppConnectionService.databaseBackend.getLastMessageReceived(account), mXmppConnectionService.databaseBackend.getLastClearDate(account));
    mamReference = MamReference.max(mamReference, mXmppConnectionService.getAutomaticMessageDeletionDate());
    long endCatchup = account.getXmppConnection().getLastSessionEstablished();
    final Query query;
    if (mamReference.getTimestamp() == 0) {
        return;
    } else if (endCatchup - mamReference.getTimestamp() >= Config.MAM_MAX_CATCHUP) {
        long startCatchup = endCatchup - Config.MAM_MAX_CATCHUP;
        List<Conversation> conversations = mXmppConnectionService.getConversations();
        for (Conversation conversation : conversations) {
            if (conversation.getMode() == Conversation.MODE_SINGLE && conversation.getAccount() == account && startCatchup > conversation.getLastMessageTransmitted().getTimestamp()) {
                this.query(conversation, startCatchup, true);
            }
        }
        query = new Query(account, new MamReference(startCatchup), endCatchup);
    } else {
        query = new Query(account, mamReference, endCatchup);
    }
    synchronized (this.queries) {
        this.queries.add(query);
    }
    this.execute(query);
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) Conversation(de.pixart.messenger.entities.Conversation) MamReference(de.pixart.messenger.xmpp.mam.MamReference)

Aggregations

MamReference (de.pixart.messenger.xmpp.mam.MamReference)7 Cursor (android.database.Cursor)2 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)2 InvalidJidException (de.pixart.messenger.xmpp.jid.InvalidJidException)2 IOException (java.io.IOException)2 CertificateEncodingException (java.security.cert.CertificateEncodingException)2 CertificateException (java.security.cert.CertificateException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 JSONException (org.json.JSONException)2 InvalidKeyException (org.whispersystems.libsignal.InvalidKeyException)2 Account (de.pixart.messenger.entities.Account)1 Conversation (de.pixart.messenger.entities.Conversation)1 MessageArchiveService (de.pixart.messenger.services.MessageArchiveService)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 JSONObject (org.json.JSONObject)1