Fix a few issues with receiving to_device messages in initial sync

This commit is contained in:
Nicolas Werner 2022-04-08 03:24:10 +02:00
parent 66b7ff639c
commit f316dbed43
5 changed files with 33 additions and 23 deletions

View File

@ -401,7 +401,7 @@ if(USE_BUNDLED_MTXCLIENT)
FetchContent_Declare( FetchContent_Declare(
MatrixClient MatrixClient
GIT_REPOSITORY https://github.com/Nheko-Reborn/mtxclient.git GIT_REPOSITORY https://github.com/Nheko-Reborn/mtxclient.git
GIT_TAG 888601932b876c9a8bb8e1f3b198ce1a5f1252fa GIT_TAG c5e8def06f0fc64aa150f30d5c9c366e876120e1
) )
set(BUILD_LIB_EXAMPLES OFF CACHE INTERNAL "") set(BUILD_LIB_EXAMPLES OFF CACHE INTERNAL "")
set(BUILD_LIB_TESTS OFF CACHE INTERNAL "") set(BUILD_LIB_TESTS OFF CACHE INTERNAL "")

View File

@ -176,7 +176,7 @@ modules:
buildsystem: cmake-ninja buildsystem: cmake-ninja
name: mtxclient name: mtxclient
sources: sources:
- commit: 888601932b876c9a8bb8e1f3b198ce1a5f1252fa - commit: c5e8def06f0fc64aa150f30d5c9c366e876120e1
#tag: v0.7.0 #tag: v0.7.0
type: git type: git
url: https://github.com/Nheko-Reborn/mtxclient.git url: https://github.com/Nheko-Reborn/mtxclient.git

View File

@ -1574,7 +1574,7 @@ Cache::saveState(const mtx::responses::Sync &res)
using namespace mtx::events; using namespace mtx::events;
auto local_user_id = this->localUserId_.toStdString(); auto local_user_id = this->localUserId_.toStdString();
auto currentBatchToken = nextBatchToken(); auto currentBatchToken = res.next_batch;
auto txn = lmdb::txn::begin(env_); auto txn = lmdb::txn::begin(env_);
@ -3942,8 +3942,15 @@ Cache::roomVerificationStatus(const std::string &room_id)
trust = crypto::TOFU; trust = crypto::TOFU;
} }
if (!keysToRequest.empty()) if (!keysToRequest.empty()) {
markUserKeysOutOfDate(txn, keysDb, keysToRequest, ""); std::string_view token;
bool result = syncStateDb_.get(txn, NEXT_BATCH_KEY, token);
if (!result)
token = "";
markUserKeysOutOfDate(txn, keysDb, keysToRequest, std::string(token));
}
} catch (std::exception &e) { } catch (std::exception &e) {
nhlog::db()->error("Failed to calculate verification status for {}: {}", room_id, e.what()); nhlog::db()->error("Failed to calculate verification status for {}: {}", room_id, e.what());
@ -4123,11 +4130,11 @@ Cache::userKeys_(const std::string &user_id, lmdb::txn &txn)
if (res) { if (res) {
return json::parse(keys).get<UserKeyCache>(); return json::parse(keys).get<UserKeyCache>();
} else { } else {
return {}; return std::nullopt;
} }
} catch (std::exception &e) { } catch (std::exception &e) {
nhlog::db()->error("Failed to retrieve user keys for {}: {}", user_id, e.what()); nhlog::db()->error("Failed to retrieve user keys for {}: {}", user_id, e.what());
return {}; return std::nullopt;
} }
} }
@ -4365,7 +4372,7 @@ Cache::query_keys(const std::string &user_id,
QObject *context{new QObject(this)}; QObject *context{new QObject(this)};
QObject::connect( QObject::connect(
this, this,
&Cache::verificationStatusChanged, &Cache::userKeysUpdateFinalize,
context, context,
[cb, user_id, context_ = context, this](std::string updated_user) mutable { [cb, user_id, context_ = context, this](std::string updated_user) mutable {
if (user_id == updated_user) { if (user_id == updated_user) {
@ -4390,6 +4397,7 @@ Cache::query_keys(const std::string &user_id,
} }
emit userKeysUpdate(last_changed, res); emit userKeysUpdate(last_changed, res);
emit userKeysUpdateFinalize(user_id);
}); });
} }

View File

@ -317,6 +317,7 @@ signals:
void roomReadStatus(const std::map<QString, bool> &status); void roomReadStatus(const std::map<QString, bool> &status);
void removeNotification(const QString &room_id, const QString &event_id); void removeNotification(const QString &room_id, const QString &event_id);
void userKeysUpdate(const std::string &sync_token, const mtx::responses::QueryKeys &keyQuery); void userKeysUpdate(const std::string &sync_token, const mtx::responses::QueryKeys &keyQuery);
void userKeysUpdateFinalize(const std::string &user_id);
void verificationStatusChanged(const std::string &userid); void verificationStatusChanged(const std::string &userid);
void selfVerificationStatusChanged(); void selfVerificationStatusChanged();
void secretChanged(const std::string name); void secretChanged(const std::string name);

View File

@ -560,25 +560,26 @@ ChatPage::startInitialSync()
} }
} }
nhlog::net()->info("initial sync completed"); QTimer::singleShot(0, this, [this, res] {
nhlog::net()->info("initial sync completed");
try {
cache::client()->saveState(res);
try { olm::handle_to_device_messages(res.to_device.events);
cache::client()->saveState(res);
olm::handle_to_device_messages(res.to_device.events); emit initializeViews(std::move(res));
emit initializeMentions(cache::getTimelineMentions());
emit initializeViews(std::move(res)); cache::calculateRoomReadStatus();
emit initializeMentions(cache::getTimelineMentions()); } catch (const lmdb::error &e) {
nhlog::db()->error("failed to save state after initial sync: {}", e.what());
startInitialSync();
return;
}
cache::calculateRoomReadStatus(); emit trySyncCb();
} catch (const lmdb::error &e) { emit contentLoaded();
nhlog::db()->error("failed to save state after initial sync: {}", e.what()); });
startInitialSync();
return;
}
emit trySyncCb();
emit contentLoaded();
}); });
} }