From 953d5a9d03b2a3ca439a79775a0c212965d91c20 Mon Sep 17 00:00:00 2001 From: Alexey Rusakov Date: Fri, 29 Jul 2022 17:50:17 +0200 Subject: Moving eventCast() In a situation where you have an EventPtr that you want to place somewhere as an `event_ptr_tt` you have to carefully check that the stored event is actually of SomeMoreSpecificType and if it is, release() that event pointer, downcast, and re-wrap it into that new event_ptr_tt - or, as can be seen from the diff here, re-loadEvent() from JSON, which is simpler but inefficient. To help clients, and the library, eventCast() can now accept an rvalue smart pointer and do all the necessary things with it. --- lib/connection.cpp | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'lib/connection.cpp') diff --git a/lib/connection.cpp b/lib/connection.cpp index 81151135..6e04883e 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -977,22 +977,22 @@ void Connection::Private::consumeToDeviceEvents(Events&& toDeviceEvents) if (!toDeviceEvents.empty()) { qCDebug(E2EE) << "Consuming" << toDeviceEvents.size() << "to-device events"; - visitEach(toDeviceEvents, [this](const EncryptedEvent& event) { - if (event.algorithm() != OlmV1Curve25519AesSha2AlgoKey) { - qCDebug(E2EE) << "Unsupported algorithm" << event.id() - << "for event" << event.algorithm(); - return; - } - if (isKnownCurveKey(event.senderId(), event.senderKey())) { - handleEncryptedToDeviceEvent(event); - return; + for (auto&& tdEvt : toDeviceEvents) + if (auto&& event = eventCast(std::move(tdEvt))) { + if (event->algorithm() != OlmV1Curve25519AesSha2AlgoKey) { + qCDebug(E2EE) << "Unsupported algorithm" << event->id() + << "for event" << event->algorithm(); + return; + } + if (isKnownCurveKey(event->senderId(), event->senderKey())) { + handleEncryptedToDeviceEvent(*event); + return; + } + trackedUsers += event->senderId(); + outdatedUsers += event->senderId(); + encryptionUpdateRequired = true; + pendingEncryptedEvents.push_back(std::move(event)); } - trackedUsers += event.senderId(); - outdatedUsers += event.senderId(); - encryptionUpdateRequired = true; - pendingEncryptedEvents.push_back( - makeEvent(event.fullJson())); - }); } #endif } -- cgit v1.2.3