Search in sources :

Example 1 with SyncInfo

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

the class MamManager method updatePreviousHistorySyncInfo.

private void updatePreviousHistorySyncInfo(Realm realm, BaseEntity chat, org.jivesoftware.smackx.mam.MamManager.MamQueryResult mamQueryResult, List<MessageItem> messageItems) {
    SyncInfo syncInfo = getSyncInfo(realm, chat.getAccount(), chat.getUser());
    realm.beginTransaction();
    if (mamQueryResult.forwardedMessages.size() < PAGE_SIZE) {
        syncInfo.setRemoteHistoryCompletelyLoaded(true);
    }
    syncInfo.setFirstMamMessageMamId(mamQueryResult.mamFin.getRSMSet().getFirst());
    if (!mamQueryResult.forwardedMessages.isEmpty()) {
        syncInfo.setFirstMamMessageStanzaId(mamQueryResult.forwardedMessages.get(0).getForwardedStanza().getStanzaId());
    }
    realm.commitTransaction();
}
Also used : SyncInfo(com.xabber.android.data.database.messagerealm.SyncInfo)

Example 2 with SyncInfo

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

the class MamManager method requestFullChatHistory.

/**
 * Only for debugging
 * Call only from background thread
 * @param chat
 */
public void requestFullChatHistory(final AbstractChat chat) {
    if (chat == null || chat.isRemotePreviousHistoryCompletelyLoaded()) {
        return;
    }
    final AccountItem accountItem = AccountManager.getInstance().getAccount(chat.getAccount());
    if (accountItem == null || !accountItem.getFactualStatusMode().isOnline()) {
        return;
    }
    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 {
        LogManager.i("MAM", "Loading previous history");
        mamQueryResult = mamManager.queryArchive(chat.getUser().getJid());
    } catch (SmackException.NotLoggedInException | SmackException.NoResponseException | XMPPException.XMPPErrorException | InterruptedException | SmackException.NotConnectedException e) {
        LogManager.exception(this, e);
        return;
    }
    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 : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) AccountItem(com.xabber.android.data.account.AccountItem) SyncInfo(com.xabber.android.data.database.messagerealm.SyncInfo) Realm(io.realm.Realm)

Example 3 with SyncInfo

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

the class MamManager method setRemoteHistoryCompletelyLoaded.

public void setRemoteHistoryCompletelyLoaded(AbstractChat chat) {
    LogManager.i(this, "setRemoteHistoryCompletelyLoaded " + chat.getUser());
    Realm realm = MessageDatabaseManager.getInstance().getNewBackgroundRealm();
    SyncInfo syncInfo = getSyncInfo(realm, chat.getAccount(), chat.getUser());
    realm.beginTransaction();
    syncInfo.setRemoteHistoryCompletelyLoaded(true);
    realm.commitTransaction();
    realm.close();
}
Also used : SyncInfo(com.xabber.android.data.database.messagerealm.SyncInfo) Realm(io.realm.Realm)

Example 4 with SyncInfo

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

the class MamManager method getSyncInfo.

@NonNull
private SyncInfo getSyncInfo(Realm realm, AccountJid account, UserJid user) {
    SyncInfo syncInfo = realm.where(SyncInfo.class).equalTo(SyncInfo.FIELD_ACCOUNT, account.toString()).equalTo(SyncInfo.FIELD_USER, user.toString()).findFirst();
    if (syncInfo == null) {
        realm.beginTransaction();
        syncInfo = realm.createObject(SyncInfo.class);
        syncInfo.setAccount(account);
        syncInfo.setUser(user);
        realm.commitTransaction();
    }
    return syncInfo;
}
Also used : SyncInfo(com.xabber.android.data.database.messagerealm.SyncInfo) NonNull(android.support.annotation.NonNull)

Example 5 with SyncInfo

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

the class NextMamManager method runMigrationToNewArchive.

private void runMigrationToNewArchive(AccountItem accountItem, Realm realm) {
    LogManager.d(LOG_TAG, "run migration for account: " + accountItem.getAccount().toString());
    Collection<RosterContact> contacts = RosterManager.getInstance().getAccountRosterContacts(accountItem.getAccount());
    for (RosterContact contact : contacts) {
        AbstractChat chat = MessageManager.getInstance().getOrCreateChat(contact.getAccount(), contact.getUser());
        MessageItem firstMessage = getFirstMessageForMigration(chat, realm);
        SyncInfo syncInfo = realm.where(SyncInfo.class).equalTo(SyncInfo.FIELD_ACCOUNT, accountItem.getAccount().toString()).equalTo(SyncInfo.FIELD_USER, chat.getUser().toString()).findFirst();
        if (firstMessage != null && syncInfo != null) {
            realm.beginTransaction();
            firstMessage.setArchivedId(syncInfo.getFirstMamMessageMamId());
            firstMessage.setPreviousId(null);
            realm.commitTransaction();
        }
    }
}
Also used : MessageItem(com.xabber.android.data.database.messagerealm.MessageItem) RosterContact(com.xabber.android.data.roster.RosterContact) AbstractChat(com.xabber.android.data.message.AbstractChat) SyncInfo(com.xabber.android.data.database.messagerealm.SyncInfo)

Aggregations

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