summary refs log tree commit diff
path: root/src/ChatPage.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/ChatPage.cpp')
-rw-r--r--src/ChatPage.cpp80
1 files changed, 58 insertions, 22 deletions
diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp

index 80d06ae2..5d2117cc 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp
@@ -434,6 +434,7 @@ ChatPage::loadStateFromCache() getProfileInfo(); getBackupVersion(); + verifyOneTimeKeyCountAfterStartup(); emit contentLoaded(); @@ -937,35 +938,70 @@ ChatPage::currentPresence() const } void +ChatPage::verifyOneTimeKeyCountAfterStartup() +{ + http::client()->upload_keys( + olm::client()->create_upload_keys_request(), + [this](const mtx::responses::UploadKeys &res, mtx::http::RequestErr err) { + if (err) { + nhlog::crypto()->warn("failed to update one-time keys: {} {} {}", + err->matrix_error.error, + static_cast<int>(err->status_code), + static_cast<int>(err->error_code)); + + if (err->status_code < 400 || err->status_code >= 500) + return; + } + + std::map<std::string, uint16_t> key_counts; + auto count = 0; + if (auto c = res.one_time_key_counts.find(mtx::crypto::SIGNED_CURVE25519); + c == res.one_time_key_counts.end()) { + key_counts[mtx::crypto::SIGNED_CURVE25519] = 0; + } else { + key_counts[mtx::crypto::SIGNED_CURVE25519] = c->second; + count = c->second; + } + + nhlog::crypto()->info( + "Fetched server key count {} {}", count, mtx::crypto::SIGNED_CURVE25519); + + ensureOneTimeKeyCount(key_counts); + }); +} + +void ChatPage::ensureOneTimeKeyCount(const std::map<std::string, uint16_t> &counts) { - uint16_t count = 0; - if (auto c = counts.find(mtx::crypto::SIGNED_CURVE25519); c != counts.end()) - count = c->second; + if (auto count = counts.find(mtx::crypto::SIGNED_CURVE25519); count != counts.end()) { + nhlog::crypto()->debug( + "Updated server key count {} {}", count->second, mtx::crypto::SIGNED_CURVE25519); - if (count < MAX_ONETIME_KEYS) { - const int nkeys = MAX_ONETIME_KEYS - count; + if (count->second < MAX_ONETIME_KEYS) { + const int nkeys = MAX_ONETIME_KEYS - count->second; - nhlog::crypto()->info( - "uploading {} {} keys", nkeys, mtx::crypto::SIGNED_CURVE25519); - olm::client()->generate_one_time_keys(nkeys); + nhlog::crypto()->info( + "uploading {} {} keys", nkeys, mtx::crypto::SIGNED_CURVE25519); + olm::client()->generate_one_time_keys(nkeys); - http::client()->upload_keys( - olm::client()->create_upload_keys_request(), - [](const mtx::responses::UploadKeys &, mtx::http::RequestErr err) { - if (err) { - nhlog::crypto()->warn("failed to update one-time keys: {} {} {}", - err->matrix_error.error, - static_cast<int>(err->status_code), - static_cast<int>(err->error_code)); + http::client()->upload_keys( + olm::client()->create_upload_keys_request(), + [](const mtx::responses::UploadKeys &, mtx::http::RequestErr err) { + if (err) { + nhlog::crypto()->warn( + "failed to update one-time keys: {} {} {}", + err->matrix_error.error, + static_cast<int>(err->status_code), + static_cast<int>(err->error_code)); - if (err->status_code < 400 || err->status_code >= 500) - return; - } + if (err->status_code < 400 || err->status_code >= 500) + return; + } - // mark as published anyway, otherwise we may end up in a loop. - olm::mark_keys_as_published(); - }); + // mark as published anyway, otherwise we may end up in a loop. + olm::mark_keys_as_published(); + }); + } } }