Skip to content

Commit

Permalink
Things
Browse files Browse the repository at this point in the history
  • Loading branch information
Tobias Fella committed Dec 29, 2024
1 parent 8ef0f20 commit fea61b9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
15 changes: 8 additions & 7 deletions Quotient/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ QFuture<void> Connection::Private::setupPicklingKey()
default:
qWarning(E2EE) << "Error loading pickling key - please fix your keychain:"
<< readJob->errorString();
//TODO: We probably want to fail entirely here.
}
return {};
})
Expand Down Expand Up @@ -583,6 +582,7 @@ void Connection::sync(int timeout)
auto result = (*d->cryptoMachine)->has_pending_backup_key();
if (!result->has_error()) {
if (result->value()) {
//TODO only if backup is not already initialized
d->initializeExistingBackup();
}

Expand All @@ -592,8 +592,7 @@ void Connection::sync(int timeout)
auto request = requestResult->value();
if (request->has_request()) {
//TODO make sure that this is only run once simultaneously
auto transaction_id = request->transaction_id();
callApi<PutRoomKeysJob>(stringFromRust(request->version()), fromJson<QHash<RoomId, RoomKeyBackup>>(jsonFromRust(request->rooms()))).onResult([this, transaction_id](const auto& job){
callApi<PutRoomKeysJob>(stringFromRust(request->version()), fromJson<QHash<RoomId, RoomKeyBackup>>(jsonFromRust(request->rooms()))).onResult([this, transaction_id = request->transaction_id()](const auto& job){
(*d->cryptoMachine)->mark_keys_backup_as_sent(bytesToRust(job->rawData()), transaction_id);
});
}
Expand Down Expand Up @@ -714,11 +713,13 @@ void Connection::Private::processOutgoingRequests()
(*cryptoMachine)->mark_keys_upload_as_sent(bytesToRust(job->rawData()), stringToRust(id));
}).QFuture<UploadKeysJob*>::then([](auto){}));
} else if (type == 1) { // Keys query
futures.append(q->callApi<QueryKeysJob>(fromJson<QHash<UserId, QStringList>>(jsonFromRust(request.keys_query_device_keys())), request.keys_query_timeout() /*TODO check correctness*/).then([id, this](const auto &job){
auto timeout = request.keys_query_timeout();
futures.append(q->callApi<QueryKeysJob>(fromJson<QHash<UserId, QStringList>>(jsonFromRust(request.keys_query_device_keys())), timeout > 0 ? timeout : std::optional<int>()).then([id, this](const auto &job){
(*cryptoMachine)->mark_keys_query_as_sent(bytesToRust(job->rawData()), stringToRust(id));
}).QFuture<QueryKeysJob*>::then([](auto){}));
} else if (type == 2) { // keys claim
futures.append(q->callApi<ClaimKeysJob>(fromJson<QHash<UserId, QHash<QString, QString>>>(jsonFromRust(request.keys_claim_one_time_keys())), std::nullopt /*TODO: timeout */).then([id, this](const auto& job){
auto timeout = request.keys_claim_timeout();
futures.append(q->callApi<ClaimKeysJob>(fromJson<QHash<UserId, QHash<QString, QString>>>(jsonFromRust(request.keys_claim_one_time_keys())), timeout > 0 ? timeout : std::optional<int>()).then([id, this](const auto& job){
(*cryptoMachine)->mark_keys_claim_as_sent(bytesToRust(job->rawData()), stringToRust(id));
}).QFuture<ClaimKeysJob*>::then([](auto){}));
} else if (type == 3) { // to device
Expand All @@ -735,7 +736,7 @@ void Connection::Private::processOutgoingRequests()
auto transactionId = stringFromRust(request.room_msg_txn_id());
//TODO: this is wrong (content <-> full), but i also think we don't need it json[u"unsigned"_s] = QJsonObject { {u"transaction_id"_s, transactionId} };
//TODO: make Room::postJson return a future, then also append to the outer future
auto actualTransactionId = room->postJson(stringFromRust(request.room_msg_matrix_type()), json);
auto actualTransactionId = room->postJson(stringFromRust(request.room_msg_matrix_type()), json)->transactionId();
connectUntil(room, &Room::messageSent, q, [this, actualTransactionId, id](const auto& txnId, const auto &event) {
if (txnId != actualTransactionId) {
return false;
Expand Down Expand Up @@ -2211,7 +2212,7 @@ void Connection::Private::requestUserVerification(KeyVerificationSession* sessio
qWarning() << stringFromRust(request->error_string());
return;
}
auto transactionId = session->room()->postJson(u"m.room.message"_s, jsonFromRust(request->value()));
auto transactionId = session->room()->postJson(u"m.room.message"_s, jsonFromRust(request->value()))->transactionId();
connectUntil(session->room(), &Room::pendingEventAboutToMerge, q, [this, transactionId, session](const auto &event) {
if (event->transactionId() != transactionId) {
return false;
Expand Down
1 change: 1 addition & 0 deletions Quotient/crypto-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ mod ffi {
fn keys_upload_fallback_keys(self: &OutgoingRequest) -> String;
fn keys_query_device_keys(self: &OutgoingRequest) -> String;
fn keys_query_timeout(self: &OutgoingRequest) -> usize;
fn keys_claim_timeout(self: &OutgoingRequest) -> usize;
fn keys_claim_one_time_keys(self: &OutgoingRequest) -> String;
fn to_device_event_type(self: &OutgoingRequest) -> String;
fn to_device_messages(self: &OutgoingRequest) -> String;
Expand Down
11 changes: 11 additions & 0 deletions Quotient/crypto-sdk/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,17 @@ impl OutgoingRequest {
}
}

pub(crate) fn keys_claim_timeout(&self) -> usize {
if let OutgoingRequests::KeysClaim(request) = self.0.request() {
request
.timeout
.unwrap_or(Duration::from_millis(0))
.as_millis() as usize
} else {
panic!()
}
}

pub(crate) fn keys_claim_one_time_keys(&self) -> String {
if let OutgoingRequests::KeysClaim(request) = self.0.request() {
serde_json::to_string(&request.one_time_keys).unwrap()
Expand Down
4 changes: 2 additions & 2 deletions Quotient/room.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2110,9 +2110,9 @@ const PendingEventItem& Room::post(RoomEventPtr event)
return d->sendEvent(std::move(event));
}

QString Room::postJson(const QString& matrixType, const QJsonObject& eventContent)
const PendingEventItem& Room::postJson(const QString& matrixType, const QJsonObject& eventContent)
{
return d->sendEvent(loadEvent<RoomEvent>(matrixType, eventContent))->transactionId();
return d->sendEvent(loadEvent<RoomEvent>(matrixType, eventContent));
}

SetRoomStateWithKeyJob* Room::setState(const StateEvent& evt)
Expand Down
2 changes: 1 addition & 1 deletion Quotient/room.h
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,7 @@ public Q_SLOTS:
*/
[[deprecated("Use post() instead")]]
QString postEvent(RoomEvent* event);
QString postJson(const QString& matrixType, const QJsonObject& eventContent);
const PendingEventItem& postJson(const QString& matrixType, const QJsonObject& eventContent);
QString retryMessage(const QString& txnId);
void discardMessage(const QString& txnId);

Expand Down

0 comments on commit fea61b9

Please sign in to comment.