Search in sources :

Example 6 with SyncInfo

use of com.xabber.android.data.database.messagerealm.SyncInfo in project xabber-android by redsolution.

the class MamManager method requestPreviousHistory.

public void requestPreviousHistory(final AbstractChat chat) {
    if (chat == null || chat.isRemotePreviousHistoryCompletelyLoaded()) {
        return;
    }
    final AccountItem accountItem = AccountManager.getInstance().getAccount(chat.getAccount());
    if (accountItem == null || !accountItem.getFactualStatusMode().isOnline()) {
        return;
    }
    Application.getInstance().runInBackgroundUserRequest(new Runnable() {

        @Override
        public void run() {
            if (!checkSupport(accountItem)) {
                return;
            }
            String firstMamMessageMamId;
            boolean remoteHistoryCompletelyLoaded;
            {
                Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
                SyncInfo syncInfo = getSyncInfo(realm, chat.getAccount(), chat.getUser());
                firstMamMessageMamId = syncInfo.getFirstMamMessageMamId();
                remoteHistoryCompletelyLoaded = syncInfo.isRemoteHistoryCompletelyLoaded();
                realm.close();
            }
            if (remoteHistoryCompletelyLoaded) {
                chat.setRemotePreviousHistoryCompletelyLoaded(true);
            }
            if (firstMamMessageMamId == null || remoteHistoryCompletelyLoaded) {
                return;
            }
            org.jivesoftware.smackx.mam.MamManager mamManager = org.jivesoftware.smackx.mam.MamManager.getInstanceFor(accountItem.getConnection());
            final org.jivesoftware.smackx.mam.MamManager.MamQueryResult mamQueryResult;
            try {
                EventBus.getDefault().post(new PreviousHistoryLoadStartedEvent(chat));
                LogManager.i("MAM", "Loading previous history");
                mamQueryResult = mamManager.pageBefore(chat.getUser().getJid(), firstMamMessageMamId, PAGE_SIZE);
            } catch (SmackException.NotLoggedInException | SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException | SmackException.NotConnectedException e) {
                LogManager.exception(this, e);
                EventBus.getDefault().post(new PreviousHistoryLoadFinishedEvent(chat));
                return;
            }
            EventBus.getDefault().post(new PreviousHistoryLoadFinishedEvent(chat));
            LogManager.i("MAM", "queryArchive finished. fin count expected: " + mamQueryResult.mamFin.getRSMSet().getCount() + " real: " + mamQueryResult.forwardedMessages.size());
            Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
            List<MessageItem> messageItems = getMessageItems(mamQueryResult, chat);
            syncMessages(realm, chat, messageItems);
            updatePreviousHistorySyncInfo(realm, chat, mamQueryResult, messageItems);
            realm.close();
        }
    });
}
Also used : AccountItem(com.xabber.android.data.account.AccountItem) SmackException(org.jivesoftware.smack.SmackException) SyncInfo(com.xabber.android.data.database.messagerealm.SyncInfo) ArrayList(java.util.ArrayList) List(java.util.List) XMPPException(org.jivesoftware.smack.XMPPException) Realm(io.realm.Realm)

Example 7 with SyncInfo

use of com.xabber.android.data.database.messagerealm.SyncInfo in project xabber-android by redsolution.

the class MamManager method updateLastHistorySyncInfo.

private void updateLastHistorySyncInfo(Realm realm, BaseEntity chat, org.jivesoftware.smackx.mam.MamManager.MamQueryResult mamQueryResult) {
    SyncInfo syncInfo = getSyncInfo(realm, chat.getAccount(), chat.getUser());
    realm.beginTransaction();
    if (mamQueryResult.mamFin.getRSMSet() != null) {
        if (syncInfo.getFirstMamMessageMamId() == null) {
            syncInfo.setFirstMamMessageMamId(mamQueryResult.mamFin.getRSMSet().getFirst());
            if (!mamQueryResult.forwardedMessages.isEmpty()) {
                syncInfo.setFirstMamMessageStanzaId(mamQueryResult.forwardedMessages.get(0).getForwardedStanza().getStanzaId());
            }
        }
        if (mamQueryResult.mamFin.getRSMSet().getLast() != null) {
            syncInfo.setLastMessageMamId(mamQueryResult.mamFin.getRSMSet().getLast());
        }
    }
    realm.commitTransaction();
}
Also used : SyncInfo(com.xabber.android.data.database.messagerealm.SyncInfo)

Aggregations

SyncInfo (com.xabber.android.data.database.messagerealm.SyncInfo)7 Realm (io.realm.Realm)3 AccountItem (com.xabber.android.data.account.AccountItem)2 MessageItem (com.xabber.android.data.database.messagerealm.MessageItem)2 NonNull (android.support.annotation.NonNull)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 SmackException (org.jivesoftware.smack.SmackException)1 XMPPException (org.jivesoftware.smack.XMPPException)1