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();
}
});
}
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();
}
Aggregations