From 9834fc33e8e62160480d263fa296d659e5fd1e48 Mon Sep 17 00:00:00 2001 From: blue Date: Mon, 1 Jul 2019 09:31:38 +0300 Subject: [PATCH] workaround for ejabberd history fetching --- core/account.cpp | 26 ++++++++++---------------- core/account.h | 2 +- core/contact.cpp | 14 ++++++++++++-- core/contact.h | 2 +- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/core/account.cpp b/core/account.cpp index 7d98834..5277b40 100644 --- a/core/account.cpp +++ b/core/account.cpp @@ -263,7 +263,7 @@ void Core::Account::handleNewContact(Core::Contact* contact) QObject::connect(contact, SIGNAL(nameChanged(const QString&)), this, SLOT(onContactNameChanged(const QString&))); QObject::connect(contact, SIGNAL(subscriptionStateChanged(Shared::SubscriptionState)), this, SLOT(onContactSubscriptionStateChanged(Shared::SubscriptionState))); - QObject::connect(contact, SIGNAL(needHistory(const QString&, const QString&)), this, SLOT(onContactNeedHistory(const QString&, const QString&))); + QObject::connect(contact, SIGNAL(needHistory(const QString&, const QString&, const QDateTime&)), this, SLOT(onContactNeedHistory(const QString&, const QString&, const QDateTime&))); QObject::connect(contact, SIGNAL(historyResponse(const std::list&)), this, SLOT(onContactHistoryResponse(const std::list&))); } @@ -519,8 +519,6 @@ void Core::Account::onMamMessageReceived(const QString& queryId, const QXmppMess QString jid = itr->second; std::map::const_iterator citr = contacts.find(jid); - logMessage(msg, "MAM MESSAGE:"); - if (citr != contacts.end()) { Contact* cnt = citr->second; if (msg.id().size() > 0 && msg.body().size() > 0) { @@ -564,7 +562,7 @@ void Core::Account::requestArchive(const QString& jid, int count, const QString& } } -void Core::Account::onContactNeedHistory(const QString& before, const QString& after) +void Core::Account::onContactNeedHistory(const QString& before, const QString& after, const QDateTime& at) { Contact* contact = static_cast(sender()); QXmppResultSetQuery query; @@ -572,13 +570,18 @@ void Core::Account::onContactNeedHistory(const QString& before, const QString& a if (before.size() > 0) { query.setBefore(before); } - if (after.size() > 0) { - query.setAfter(after); + QDateTime start; + if (after.size() > 0) { //there is some strange behavior of ejabberd server returning empty result set + if (at.isValid()) { //there can be some useful information about it here https://github.com/processone/ejabberd/issues/2924 + start = at; + } else { + query.setAfter(after); + } } qDebug() << "Remote query from" << after << ", to" << before; - QString q = am->retrieveArchivedMessages("", "", contact->jid, QDateTime(), QDateTime(), query); + QString q = am->retrieveArchivedMessages("", "", contact->jid, start, QDateTime(), query); achiveQueries.insert(std::make_pair(q, contact->jid)); } @@ -588,15 +591,6 @@ void Core::Account::onMamResultsReceived(const QString& queryId, const QXmppResu std::map::const_iterator itr = achiveQueries.find(queryId); QString jid = itr->second; - - qDebug() << "MAM RESULTS:"; - qDebug() << "complete:" << complete; - qDebug() << "count:" << resultSetReply.count(); - qDebug() << "first:" << resultSetReply.first(); - qDebug() << "last:" << resultSetReply.last(); - qDebug() << "index:" << resultSetReply.index(); - qDebug() << "isNull:" << resultSetReply.isNull(); - achiveQueries.erase(itr); std::map::const_iterator citr = contacts.find(jid); if (citr != contacts.end()) { diff --git a/core/account.h b/core/account.h index 145970d..180c234 100644 --- a/core/account.h +++ b/core/account.h @@ -106,7 +106,7 @@ private slots: void onContactNameChanged(const QString& name); void onContactSubscriptionStateChanged(Shared::SubscriptionState state); void onContactHistoryResponse(const std::list& list); - void onContactNeedHistory(const QString& before, const QString& after); + void onContactNeedHistory(const QString& before, const QString& after, const QDateTime& at); void onMamLog(QXmppLogger::MessageType type, const QString &msg); diff --git a/core/contact.cpp b/core/contact.cpp index c6104ef..6df230d 100644 --- a/core/contact.cpp +++ b/core/contact.cpp @@ -156,12 +156,14 @@ void Core::Contact::performRequest(int count, const QString& before) emit needHistory(before, ""); break; case chunk: - case beginning: + case beginning: { if (count != -1) { requestCache.emplace_back(requestedCount, before); requestedCount = -1; } - emit needHistory("", archive->newestId()); + Shared::Message msg = archive->newest(); + emit needHistory("", msg.getId(), msg.getTime()); + } break; case end: if (count != -1) { @@ -180,6 +182,10 @@ void Core::Contact::performRequest(int count, const QString& before) requestCache.emplace_back(requestedCount, before); requestedCount = -1; emit needHistory(archive->oldestId(), ""); + } catch (Archive::Empty e) { + requestCache.emplace_back(requestedCount, before); + requestedCount = -1; + emit needHistory(archive->oldestId(), ""); } if (found) { @@ -204,6 +210,8 @@ void Core::Contact::performRequest(int count, const QString& before) responseCache.insert(responseCache.begin(), arc.begin(), arc.end()); } catch (Archive::NotFound e) { qDebug("requesting id hasn't been found in archive, skipping"); + } catch (Archive::Empty e) { + qDebug("requesting id hasn't been found in archive, skipping"); } nextRequest(); break; @@ -301,6 +309,8 @@ void Core::Contact::flushMessagesToArchive(bool finished, const QString& firstId found = true; } catch (Archive::NotFound e) { + } catch (Archive::Empty e) { + } if (!found || requestedCount > responseCache.size()) { if (archiveState == complete) { diff --git a/core/contact.h b/core/contact.h index 650b06d..00d91b9 100644 --- a/core/contact.h +++ b/core/contact.h @@ -62,7 +62,7 @@ signals: void nameChanged(const QString& name); void subscriptionStateChanged(Shared::SubscriptionState state); void historyResponse(const std::list& messages); - void needHistory(const QString& before, const QString& after); + void needHistory(const QString& before, const QString& after, const QDateTime& afterTime = QDateTime()); public: const QString jid;