From ef1a9846bf7a0b8b160b52e58c4e80078f782d9b Mon Sep 17 00:00:00 2001 From: blue Date: Sun, 9 Aug 2020 19:28:03 +0300 Subject: [PATCH] found a way to avoid requesting all the MUCs history alltogether on startup --- CHANGELOG.md | 3 +++ core/archive.cpp | 20 ++++++++++++++++++++ core/archive.h | 1 + core/rosteritem.cpp | 16 ++++++++++------ 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e1ebd4..06c4ce1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ - global availability (in drop down list) gets restored after reconnection - status icon in active chat changes when presence of the pen pal changes +### Improvements +- slightly reduced the traffic on the startup by not requesting history of all MUCs + ## Squawk 0.1.5 (Jul 29, 2020) ### Bug fixes diff --git a/core/archive.cpp b/core/archive.cpp index 628723d..a1f8b76 100644 --- a/core/archive.cpp +++ b/core/archive.cpp @@ -214,6 +214,26 @@ Shared::Message Core::Archive::getElement(const QString& id) const } } +bool Core::Archive::hasElement(const QString& id) const +{ + if (!opened) { + throw Closed("hasElement", jid.toStdString()); + } + + MDB_txn *txn; + mdb_txn_begin(environment, NULL, MDB_RDONLY, &txn); + + bool has; + MDB_val lmdbKey, lmdbData; + lmdbKey.mv_size = id.size(); + lmdbKey.mv_data = (char*)id.toStdString().c_str(); + int rc = mdb_get(txn, main, &lmdbKey, &lmdbData); + has = rc == 0; + mdb_txn_abort(txn); + + return has; +} + Shared::Message Core::Archive::getMessage(const std::string& id, MDB_txn* txn) const { MDB_val lmdbKey, lmdbData; diff --git a/core/archive.h b/core/archive.h index b71a8be..dd7a167 100644 --- a/core/archive.h +++ b/core/archive.h @@ -46,6 +46,7 @@ public: bool addElement(const Shared::Message& message); unsigned int addElements(const std::list& messages); Shared::Message getElement(const QString& id) const; + bool hasElement(const QString& id) const; void changeMessage(const QString& id, const QMap& data); Shared::Message oldest(); QString oldestId(); diff --git a/core/rosteritem.cpp b/core/rosteritem.cpp index 5275993..32b70f4 100644 --- a/core/rosteritem.cpp +++ b/core/rosteritem.cpp @@ -248,18 +248,22 @@ void Core::RosterItem::appendMessageToArchive(const Shared::Message& msg) } break; case beginning: - appendCache.push_back(msg); - if (!syncronizing) { - requestHistory(-1, getId(msg)); + if (!archive->hasElement(msg.getId())) { + appendCache.push_back(msg); + if (!syncronizing) { + requestHistory(-1, getId(msg)); + } } break; case end: archive->addElement(msg); break; case chunk: - appendCache.push_back(msg); - if (!syncronizing) { - requestHistory(-1, getId(msg)); + if (!archive->hasElement(msg.getId())) { + appendCache.push_back(msg); + if (!syncronizing) { + requestHistory(-1, getId(msg)); + } } break; case complete: