From 12fff7408ea7539d778a641bbf1746693d30ee2a Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 27 Oct 2020 17:45:28 +0100 Subject: Optimize build --- src/ChatPage.cpp | 185 ++++++++++++++++++++++--------------------------------- 1 file changed, 72 insertions(+), 113 deletions(-) (limited to 'src/ChatPage.cpp') diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 235c5ea7..6e96234c 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -22,9 +22,12 @@ #include #include +#include + #include "AvatarProvider.h" #include "Cache.h" #include "Cache_p.h" +#include "CallManager.h" #include "ChatPage.h" #include "DeviceVerificationFlow.h" #include "EventAccessors.h" @@ -69,7 +72,7 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) , isConnected_(true) , userSettings_{userSettings} , notificationsManager(this) - , callManager_(userSettings) + , callManager_(new CallManager(userSettings, this)) { setObjectName("chatPage"); @@ -126,7 +129,7 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) contentLayout_->setSpacing(0); contentLayout_->setMargin(0); - view_manager_ = new TimelineViewManager(&callManager_, this); + view_manager_ = new TimelineViewManager(callManager_, this); contentLayout_->addWidget(view_manager_->getWidget()); @@ -434,8 +437,8 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) }); connect(text_input_, &TextInputWidget::callButtonPress, this, [this]() { - if (callManager_.onActiveCall()) { - callManager_.hangUp(); + if (callManager_->onActiveCall()) { + callManager_->hangUp(); } else { if (auto roomInfo = cache::singleRoomInfo(current_room_.toStdString()); roomInfo.member_count != 2) { @@ -454,7 +457,7 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) userSettings_, MainWindow::instance()); connect(dialog, &dialogs::PlaceCall::voice, this, [this]() { - callManager_.sendInvite(current_room_); + callManager_->sendInvite(current_room_); }); utils::centerWidget(dialog, MainWindow::instance()); dialog->show(); @@ -692,7 +695,7 @@ ChatPage::bootstrap(QString userid, QString homeserver, QString token) const bool isInitialized = cache::isInitialized(); const auto cacheVersion = cache::formatVersion(); - callManager_.refreshTurnServer(); + callManager_->refreshTurnServer(); if (!isInitialized) { cache::setCurrentFormat(); @@ -762,7 +765,7 @@ ChatPage::loadStateFromCache() cache::restoreSessions(); olm::client()->load(cache::restoreOlmAccount(), STORAGE_SECRET_KEY); - emit initializeEmptyViews(cache::roomMessages()); + emit initializeEmptyViews(cache::client()->roomMessages()); emit initializeRoomList(cache::roomInfo()); emit initializeMentions(cache::getTimelineMentions()); emit syncTags(cache::roomInfo().toStdMap()); @@ -969,13 +972,64 @@ ChatPage::startInitialSync() opts.set_presence = currentPresence(); http::client()->sync( - opts, - std::bind( - &ChatPage::initialSyncHandler, this, std::placeholders::_1, std::placeholders::_2)); + opts, [this](const mtx::responses::Sync &res, mtx::http::RequestErr err) { + // TODO: Initial Sync should include mentions as well... + + if (err) { + const auto error = QString::fromStdString(err->matrix_error.error); + const auto msg = tr("Please try to login again: %1").arg(error); + const auto err_code = mtx::errors::to_string(err->matrix_error.errcode); + const int status_code = static_cast(err->status_code); + + nhlog::net()->error("initial sync error: {} {}", status_code, err_code); + + // non http related errors + if (status_code <= 0 || status_code >= 600) { + startInitialSync(); + return; + } + + switch (status_code) { + case 502: + case 504: + case 524: { + startInitialSync(); + return; + } + default: { + emit dropToLoginPageCb(msg); + return; + } + } + } + + nhlog::net()->info("initial sync completed"); + + try { + cache::client()->saveState(res); + + olm::handle_to_device_messages(res.to_device.events); + + emit initializeViews(std::move(res.rooms)); + emit initializeRoomList(cache::roomInfo()); + emit initializeMentions(cache::getTimelineMentions()); + + cache::calculateRoomReadStatus(); + emit syncTags(cache::roomInfo().toStdMap()); + } catch (const lmdb::error &e) { + nhlog::db()->error("failed to save state after initial sync: {}", + e.what()); + startInitialSync(); + return; + } + + emit trySyncCb(); + emit contentLoaded(); + }); } void -ChatPage::handleSyncResponse(mtx::responses::Sync res) +ChatPage::handleSyncResponse(const mtx::responses::Sync &res) { nhlog::net()->debug("sync completed: {}", res.next_batch); @@ -984,16 +1038,16 @@ ChatPage::handleSyncResponse(mtx::responses::Sync res) // TODO: fine grained error handling try { - cache::saveState(res); + cache::client()->saveState(res); olm::handle_to_device_messages(res.to_device.events); - auto updates = cache::roomUpdates(res); + auto updates = cache::getRoomInfo(cache::client()->roomsWithStateUpdates(res)); emit syncRoomlist(updates); emit syncUI(res.rooms); - emit syncTags(cache::roomTagUpdates(res)); + emit syncTags(cache::getRoomInfo(cache::client()->roomsWithTagUpdates(res))); // if we process a lot of syncs (1 every 200ms), this means we clean the // db every 100s @@ -1068,7 +1122,7 @@ ChatPage::joinRoom(const QString &room) const auto room_id = room.toStdString(); http::client()->join_room( - room_id, [this, room_id](const nlohmann::json &, mtx::http::RequestErr err) { + room_id, [this, room_id](const mtx::responses::RoomId &, mtx::http::RequestErr err) { if (err) { emit showNotification( tr("Failed to join room: %1") @@ -1114,7 +1168,8 @@ void ChatPage::leaveRoom(const QString &room_id) { http::client()->leave_room( - room_id.toStdString(), [this, room_id](const json &, mtx::http::RequestErr err) { + room_id.toStdString(), + [this, room_id](const mtx::responses::RoomId &, mtx::http::RequestErr err) { if (err) { emit showNotification( tr("Failed to leave room: %1") @@ -1289,62 +1344,6 @@ ChatPage::currentPresence() const } } -void -ChatPage::initialSyncHandler(const mtx::responses::Sync &res, mtx::http::RequestErr err) -{ - // TODO: Initial Sync should include mentions as well... - - if (err) { - const auto error = QString::fromStdString(err->matrix_error.error); - const auto msg = tr("Please try to login again: %1").arg(error); - const auto err_code = mtx::errors::to_string(err->matrix_error.errcode); - const int status_code = static_cast(err->status_code); - - nhlog::net()->error("initial sync error: {} {}", status_code, err_code); - - // non http related errors - if (status_code <= 0 || status_code >= 600) { - startInitialSync(); - return; - } - - switch (status_code) { - case 502: - case 504: - case 524: { - startInitialSync(); - return; - } - default: { - emit dropToLoginPageCb(msg); - return; - } - } - } - - nhlog::net()->info("initial sync completed"); - - try { - cache::saveState(res); - - olm::handle_to_device_messages(res.to_device.events); - - emit initializeViews(std::move(res.rooms)); - emit initializeRoomList(cache::roomInfo()); - emit initializeMentions(cache::getTimelineMentions()); - - cache::calculateRoomReadStatus(); - emit syncTags(cache::roomInfo().toStdMap()); - } catch (const lmdb::error &e) { - nhlog::db()->error("failed to save state after initial sync: {}", e.what()); - startInitialSync(); - return; - } - - emit trySyncCb(); - emit contentLoaded(); -} - void ChatPage::ensureOneTimeKeyCount(const std::map &counts) { @@ -1453,51 +1452,11 @@ ChatPage::initiateLogout() emit showOverlayProgressBar(); } -void -ChatPage::query_keys(const std::string &user_id, - std::function cb) -{ - auto cache_ = cache::userKeys(user_id); - - if (cache_.has_value()) { - if (!cache_->updated_at.empty() && cache_->updated_at == cache_->last_changed) { - cb(cache_.value(), {}); - return; - } - } - - mtx::requests::QueryKeys req; - req.device_keys[user_id] = {}; - - std::string last_changed; - if (cache_) - last_changed = cache_->last_changed; - req.token = last_changed; - - http::client()->query_keys(req, - [cb, user_id, last_changed](const mtx::responses::QueryKeys &res, - mtx::http::RequestErr err) { - if (err) { - nhlog::net()->warn( - "failed to query device keys: {},{}", - err->matrix_error.errcode, - static_cast(err->status_code)); - cb({}, err); - return; - } - - cache::updateUserKeys(last_changed, res); - - auto keys = cache::userKeys(user_id); - cb(keys.value_or(UserKeyCache{}), err); - }); -} - template void ChatPage::connectCallMessage() { - connect(&callManager_, + connect(callManager_, qOverload(&CallManager::newMessage), view_manager_, qOverload(&TimelineViewManager::queueCallMessage)); -- cgit 1.5.1 From e939a6b396aafecf9cd17b79917637dcf8b6e5e9 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 27 Oct 2020 22:03:33 +0100 Subject: No roomid on leave --- src/ChatPage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/ChatPage.cpp') diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 6e96234c..fb34f91a 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -1169,7 +1169,7 @@ ChatPage::leaveRoom(const QString &room_id) { http::client()->leave_room( room_id.toStdString(), - [this, room_id](const mtx::responses::RoomId &, mtx::http::RequestErr err) { + [this, room_id](const mtx::responses::Empty &, mtx::http::RequestErr err) { if (err) { emit showNotification( tr("Failed to leave room: %1") -- cgit 1.5.1 From 70f35de449fdcbca2a4ecd1100e1fa614ad069b4 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 28 Oct 2020 13:06:28 +0100 Subject: Don't pass around empty timeline --- src/Cache.cpp | 16 ++++++++-------- src/Cache_p.h | 3 +-- src/ChatPage.cpp | 2 +- src/ChatPage.h | 2 +- src/timeline/TimelineViewManager.cpp | 9 +++------ src/timeline/TimelineViewManager.h | 2 +- 6 files changed, 15 insertions(+), 19 deletions(-) (limited to 'src/ChatPage.cpp') diff --git a/src/Cache.cpp b/src/Cache.cpp index f187af62..b37f69b3 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -1469,22 +1469,22 @@ Cache::getRoomInfo(const std::vector &rooms) return room_info; } -std::map -Cache::roomMessages() +std::vector +Cache::roomIds() { auto txn = lmdb::txn::begin(env_, nullptr, MDB_RDONLY); - std::map msgs; + std::vector rooms; std::string room_id, unused; auto roomsCursor = lmdb::cursor::open(txn, roomsDb_); while (roomsCursor.get(room_id, unused, MDB_NEXT)) - msgs.emplace(QString::fromStdString(room_id), mtx::responses::Timeline()); + rooms.push_back(QString::fromStdString(room_id)); roomsCursor.close(); txn.commit(); - return msgs; + return rooms; } QMap @@ -3967,10 +3967,10 @@ setCurrentFormat() instance_->setCurrentFormat(); } -std::map -roomMessages() +std::vector +roomIds() { - return instance_->roomMessages(); + return instance_->roomIds(); } QMap diff --git a/src/Cache_p.h b/src/Cache_p.h index 96000ae3..05e13128 100644 --- a/src/Cache_p.h +++ b/src/Cache_p.h @@ -118,8 +118,7 @@ public: void setCurrentFormat(); bool runMigrations(); - std::map roomMessages(); - + std::vector roomIds(); QMap getTimelineMentions(); //! Retrieve all the user ids from a room. diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index fb34f91a..b587d521 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -765,7 +765,7 @@ ChatPage::loadStateFromCache() cache::restoreSessions(); olm::client()->load(cache::restoreOlmAccount(), STORAGE_SECRET_KEY); - emit initializeEmptyViews(cache::client()->roomMessages()); + emit initializeEmptyViews(cache::client()->roomIds()); emit initializeRoomList(cache::roomInfo()); emit initializeMentions(cache::getTimelineMentions()); emit syncTags(cache::roomInfo().toStdMap()); diff --git a/src/ChatPage.h b/src/ChatPage.h index da367c29..0c12d89f 100644 --- a/src/ChatPage.h +++ b/src/ChatPage.h @@ -158,7 +158,7 @@ signals: void initializeRoomList(QMap); void initializeViews(const mtx::responses::Rooms &rooms); - void initializeEmptyViews(const std::map &msgs); + void initializeEmptyViews(const std::vector &roomIds); void initializeMentions(const QMap ¬ifs); void syncUI(const mtx::responses::Rooms &rooms); void syncRoomlist(const std::map &updates); diff --git a/src/timeline/TimelineViewManager.cpp b/src/timeline/TimelineViewManager.cpp index e79b86d7..858d1090 100644 --- a/src/timeline/TimelineViewManager.cpp +++ b/src/timeline/TimelineViewManager.cpp @@ -451,13 +451,10 @@ TimelineViewManager::receivedSessionKey(const std::string &room_id, const std::s } void -TimelineViewManager::initWithMessages(const std::map &msgs) +TimelineViewManager::initWithMessages(const std::vector &roomIds) { - for (const auto &e : msgs) { - addRoom(e.first); - - models.value(e.first)->addEvents(e.second); - } + for (const auto &roomId : roomIds) + addRoom(roomId); } void diff --git a/src/timeline/TimelineViewManager.h b/src/timeline/TimelineViewManager.h index 30bbc744..e42dd2f1 100644 --- a/src/timeline/TimelineViewManager.h +++ b/src/timeline/TimelineViewManager.h @@ -94,7 +94,7 @@ signals: public slots: void updateReadReceipts(const QString &room_id, const std::vector &event_ids); void receivedSessionKey(const std::string &room_id, const std::string &session_id); - void initWithMessages(const std::map &msgs); + void initWithMessages(const std::vector &roomIds); void setHistoryView(const QString &room_id); void updateColorPalette(); -- cgit 1.5.1