From e8eeb480d51b6fc60c9807dd92195e9068582592 Mon Sep 17 00:00:00 2001 From: Chethan2k1 <40890937+Chethan2k1@users.noreply.github.com> Date: Fri, 4 Sep 2020 12:02:24 +0530 Subject: [PATCH] Fix Wrong Emojis Issue in Room Verification --- resources/qml/MatrixText.qml | 2 +- src/Cache.cpp | 38 +++--- src/ChatPage.cpp | 11 +- src/DeviceVerificationFlow.cpp | 28 +++-- src/DeviceVerificationFlow.h | 2 +- src/timeline/EventStore.cpp | 221 ++++++++++++++++++--------------- src/timeline/TimelineModel.cpp | 33 ++--- src/ui/UserProfile.h | 2 +- 8 files changed, 185 insertions(+), 152 deletions(-) diff --git a/resources/qml/MatrixText.qml b/resources/qml/MatrixText.qml index bbbb80cf..29214168 100644 --- a/resources/qml/MatrixText.qml +++ b/resources/qml/MatrixText.qml @@ -19,7 +19,7 @@ TextEdit { TimelineManager.setHistoryView(match[1]) chat.positionViewAtIndex(chat.model.idToIndex(match[2]), ListView.Contain) } - else timelineManager.openLink(link) + else TimelineManager.openLink(link) } MouseArea { diff --git a/src/Cache.cpp b/src/Cache.cpp index 5302218a..07d01819 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -139,24 +139,26 @@ Cache::Cache(const QString &userId, QObject *parent) , localUserId_{userId} { setup(); - connect(this, - &Cache::updateUserCacheFlag, - this, - [this](const std::string &user_id) { - std::optional cache_ = getUserCache(user_id); - if (cache_.has_value()) { - cache_.value().isUpdated = false; - setUserCache(user_id, cache_.value()); - } else { - setUserCache(user_id, UserCache{}); - } - }, - Qt::QueuedConnection); - connect(this, - &Cache::deleteLeftUsers, - this, - [this](const std::string &user_id) { deleteUserCache(user_id); }, - Qt::QueuedConnection); + connect( + this, + &Cache::updateUserCacheFlag, + this, + [this](const std::string &user_id) { + std::optional cache_ = getUserCache(user_id); + if (cache_.has_value()) { + cache_.value().isUpdated = false; + setUserCache(user_id, cache_.value()); + } else { + setUserCache(user_id, UserCache{}); + } + }, + Qt::QueuedConnection); + connect( + this, + &Cache::deleteLeftUsers, + this, + [this](const std::string &user_id) { deleteUserCache(user_id); }, + Qt::QueuedConnection); } void diff --git a/src/ChatPage.cpp b/src/ChatPage.cpp index 31ba38d7..704543b5 100644 --- a/src/ChatPage.cpp +++ b/src/ChatPage.cpp @@ -606,11 +606,12 @@ ChatPage::ChatPage(QSharedPointer userSettings, QWidget *parent) connect( this, &ChatPage::tryInitialSyncCb, this, &ChatPage::tryInitialSync, Qt::QueuedConnection); connect(this, &ChatPage::trySyncCb, this, &ChatPage::trySync, Qt::QueuedConnection); - connect(this, - &ChatPage::tryDelayedSyncCb, - this, - [this]() { QTimer::singleShot(RETRY_TIMEOUT, this, &ChatPage::trySync); }, - Qt::QueuedConnection); + connect( + this, + &ChatPage::tryDelayedSyncCb, + this, + [this]() { QTimer::singleShot(RETRY_TIMEOUT, this, &ChatPage::trySync); }, + Qt::QueuedConnection); connect(this, &ChatPage::newSyncResponse, diff --git a/src/DeviceVerificationFlow.cpp b/src/DeviceVerificationFlow.cpp index 37866716..ae054af7 100644 --- a/src/DeviceVerificationFlow.cpp +++ b/src/DeviceVerificationFlow.cpp @@ -28,10 +28,10 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *, connect(this->model_, &TimelineModel::updateFlowEventId, this, - [this](std::string event_id) { + [this](std::string event_id_) { this->relation.rel_type = mtx::common::RelationType::Reference; - this->relation.event_id = event_id; - this->transaction_id = event_id; + this->relation.event_id = event_id_; + this->transaction_id = event_id_; }); } @@ -60,7 +60,7 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *, msg.hashes.end()) && (std::find(msg.message_authentication_codes.begin(), msg.message_authentication_codes.end(), - "hmac-sha256") != msg.message_authentication_codes.end())) { + "hkdf-hmac-sha256") != msg.message_authentication_codes.end())) { if (std::find(msg.short_authentication_string.begin(), msg.short_authentication_string.end(), mtx::events::msg::SASMethods::Decimal) != @@ -236,11 +236,15 @@ DeviceVerificationFlow::DeviceVerificationFlow(QObject *, &ChatPage::recievedDeviceVerificationReady, this, [this](const mtx::events::msg::KeyVerificationReady &msg) { - if (!sender && msg.from_device != http::client()->device_id()) { - this->deleteLater(); - emit verificationCanceled(); + if (!sender) { + if (msg.from_device != http::client()->device_id()) { + this->deleteLater(); + emit verificationCanceled(); + } + return; } + if (msg.transaction_id.has_value()) { if (msg.transaction_id.value() != this->transaction_id) return; @@ -353,9 +357,9 @@ DeviceVerificationFlow::setMethod(DeviceVerificationFlow::Method method_) } void -DeviceVerificationFlow::setType(Type type) +DeviceVerificationFlow::setType(Type type_) { - this->type = type; + this->type = type_; } void @@ -367,11 +371,11 @@ DeviceVerificationFlow::setSender(bool sender_) } void -DeviceVerificationFlow::setEventId(std::string event_id) +DeviceVerificationFlow::setEventId(std::string event_id_) { this->relation.rel_type = mtx::common::RelationType::Reference; - this->relation.event_id = event_id; - this->transaction_id = event_id; + this->relation.event_id = event_id_; + this->transaction_id = event_id_; } //! accepts a verification diff --git a/src/DeviceVerificationFlow.h b/src/DeviceVerificationFlow.h index 6b2ab81f..c2150980 100644 --- a/src/DeviceVerificationFlow.h +++ b/src/DeviceVerificationFlow.h @@ -10,7 +10,7 @@ class QTimer; using sas_ptr = std::unique_ptr; -struct TimelineModel; +class TimelineModel; class DeviceVerificationFlow : public QObject { diff --git a/src/timeline/EventStore.cpp b/src/timeline/EventStore.cpp index bfc16a02..e5eaefb8 100644 --- a/src/timeline/EventStore.cpp +++ b/src/timeline/EventStore.cpp @@ -32,38 +32,40 @@ EventStore::EventStore(std::string room_id, QObject *) this->last = range->last; } - connect(this, - &EventStore::eventFetched, - this, - [this](std::string id, - std::string relatedTo, - mtx::events::collections::TimelineEvents timeline) { - cache::client()->storeEvent(room_id_, id, {timeline}); + connect( + this, + &EventStore::eventFetched, + this, + [this](std::string id, + std::string relatedTo, + mtx::events::collections::TimelineEvents timeline) { + cache::client()->storeEvent(room_id_, id, {timeline}); - if (!relatedTo.empty()) { - auto idx = idToIndex(relatedTo); - if (idx) - emit dataChanged(*idx, *idx); - } - }, - Qt::QueuedConnection); + if (!relatedTo.empty()) { + auto idx = idToIndex(relatedTo); + if (idx) + emit dataChanged(*idx, *idx); + } + }, + Qt::QueuedConnection); - connect(this, - &EventStore::oldMessagesRetrieved, - this, - [this](const mtx::responses::Messages &res) { - uint64_t newFirst = cache::client()->saveOldMessages(room_id_, res); - if (newFirst == first && !res.chunk.empty()) - fetchMore(); - else { - emit beginInsertRows(toExternalIdx(newFirst), - toExternalIdx(this->first - 1)); - this->first = newFirst; - emit endInsertRows(); - emit fetchedMore(); - } - }, - Qt::QueuedConnection); + connect( + this, + &EventStore::oldMessagesRetrieved, + this, + [this](const mtx::responses::Messages &res) { + uint64_t newFirst = cache::client()->saveOldMessages(room_id_, res); + if (newFirst == first) + fetchMore(); + else { + emit beginInsertRows(toExternalIdx(newFirst), + toExternalIdx(this->first - 1)); + this->first = newFirst; + emit endInsertRows(); + emit fetchedMore(); + } + }, + Qt::QueuedConnection); connect(this, &EventStore::processPending, this, [this]() { if (!current_txn.empty()) { @@ -128,46 +130,48 @@ EventStore::EventStore(std::string room_id, QObject *) event->data); }); - connect(this, - &EventStore::messageFailed, - this, - [this](std::string txn_id) { - if (current_txn == txn_id) { - current_txn_error_count++; - if (current_txn_error_count > 10) { - nhlog::ui()->debug("failing txn id '{}'", txn_id); - cache::client()->removePendingStatus(room_id_, txn_id); - current_txn_error_count = 0; - } - } - QTimer::singleShot(1000, this, [this]() { - nhlog::ui()->debug("timeout"); - this->current_txn = ""; - emit processPending(); - }); - }, - Qt::QueuedConnection); + connect( + this, + &EventStore::messageFailed, + this, + [this](std::string txn_id) { + if (current_txn == txn_id) { + current_txn_error_count++; + if (current_txn_error_count > 10) { + nhlog::ui()->debug("failing txn id '{}'", txn_id); + cache::client()->removePendingStatus(room_id_, txn_id); + current_txn_error_count = 0; + } + } + QTimer::singleShot(1000, this, [this]() { + nhlog::ui()->debug("timeout"); + this->current_txn = ""; + emit processPending(); + }); + }, + Qt::QueuedConnection); - connect(this, - &EventStore::messageSent, - this, - [this](std::string txn_id, std::string event_id) { - nhlog::ui()->debug("sent {}", txn_id); + connect( + this, + &EventStore::messageSent, + this, + [this](std::string txn_id, std::string event_id) { + nhlog::ui()->debug("sent {}", txn_id); - http::client()->read_event( - room_id_, event_id, [this, event_id](mtx::http::RequestErr err) { - if (err) { - nhlog::net()->warn( - "failed to read_event ({}, {})", room_id_, event_id); - } - }); + http::client()->read_event( + room_id_, event_id, [this, event_id](mtx::http::RequestErr err) { + if (err) { + nhlog::net()->warn( + "failed to read_event ({}, {})", room_id_, event_id); + } + }); - cache::client()->removePendingStatus(room_id_, txn_id); - this->current_txn = ""; - this->current_txn_error_count = 0; - emit processPending(); - }, - Qt::QueuedConnection); + cache::client()->removePendingStatus(room_id_, txn_id); + this->current_txn = ""; + this->current_txn_error_count = 0; + emit processPending(); + }, + Qt::QueuedConnection); } void @@ -280,50 +284,77 @@ EventStore::handleSync(const mtx::responses::Timeline &events) if (auto encrypted = std::get_if>( &event)) { - auto event = decryptEvent({room_id_, encrypted->event_id}, *encrypted); + auto d_event = decryptEvent({room_id_, encrypted->event_id}, *encrypted); if (std::visit( [](auto e) { return (e.sender != utils::localUser().toStdString()); }, - *event)) { - if (auto msg = std::get_if>(event)) { + *d_event)) { + if (std::get_if>(d_event)) { + auto msg = std::get_if>(d_event); last_verification_request_event = *msg; - } else if (auto msg = std::get_if>(event)) { + continue; + } else if (std::get_if>(d_event)) { + auto msg = std::get_if>(d_event); last_verification_cancel_event = *msg; ChatPage::instance()->recievedDeviceVerificationCancel( msg->content); - } else if (auto msg = std::get_if>(event)) { + continue; + } else if (std::get_if>(d_event)) { + auto msg = std::get_if>(d_event); ChatPage::instance()->recievedDeviceVerificationAccept( msg->content); - } else if (auto msg = std::get_if>(event)) { + continue; + } else if (std::get_if>(d_event)) { + auto msg = std::get_if>(d_event); ChatPage::instance()->recievedDeviceVerificationKey( msg->content); - } else if (auto msg = std::get_if>(event)) { + continue; + } else if (std::get_if>(d_event)) { + auto msg = std::get_if>(d_event); ChatPage::instance()->recievedDeviceVerificationMac( msg->content); - } else if (auto msg = std::get_if>(event)) { + continue; + } else if (std::get_if>(d_event)) { + auto msg = std::get_if>(d_event); ChatPage::instance()->recievedDeviceVerificationReady( msg->content); - } else if (auto msg = std::get_if>(event)) { + continue; + } else if (std::get_if>(d_event)) { + auto msg = std::get_if>(d_event); ChatPage::instance()->recievedDeviceVerificationDone( msg->content); - } else if (auto msg = std::get_if>(event)) { + continue; + } else if (std::get_if>(d_event)) { + auto msg = std::get_if>(d_event); ChatPage::instance()->recievedDeviceVerificationStart( msg->content, msg->sender); + continue; + } + } else { + // only the key.verification.ready sent by localuser's other device + // is of significance as it is used for detecting accepted request + if (std::get_if>(d_event)) { + auto msg = std::get_if>(d_event); + ChatPage::instance()->recievedDeviceVerificationReady( + msg->content); } - } - // only the key.verification.ready sent by localuser's other device is of - // significance as it is used for detecting accepted request - if (auto msg = std::get_if< - mtx::events::RoomEvent>( - event)) { - ChatPage::instance()->recievedDeviceVerificationReady(msg->content); } } } @@ -614,12 +645,6 @@ EventStore::decryptEvent(const IdIndex &idx, return asCacheEntry(std::move(temp_events[0])); } - dummy.content.body = tr("-- Encrypted Event (Unknown event type) --", - "Placeholder, when the message was decrypted, but we " - "couldn't parse it, because " - "Nheko/mtxclient don't support that event type yet.") - .toStdString(); - return asCacheEntry(std::move(dummy)); auto encInfo = mtx::accessors::file(decryptionResult.event.value()); if (encInfo) emit newEncryptedImage(encInfo.value()); diff --git a/src/timeline/TimelineModel.cpp b/src/timeline/TimelineModel.cpp index 8f0e470e..570186a5 100644 --- a/src/timeline/TimelineModel.cpp +++ b/src/timeline/TimelineModel.cpp @@ -204,11 +204,12 @@ TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObj , room_id_(room_id) , manager_(manager) { - connect(this, - &TimelineModel::redactionFailed, - this, - [](const QString &msg) { emit ChatPage::instance()->showNotification(msg); }, - Qt::QueuedConnection); + connect( + this, + &TimelineModel::redactionFailed, + this, + [](const QString &msg) { emit ChatPage::instance()->showNotification(msg); }, + Qt::QueuedConnection); connect(this, &TimelineModel::newMessageToSend, @@ -217,17 +218,17 @@ TimelineModel::TimelineModel(TimelineViewManager *manager, QString room_id, QObj Qt::QueuedConnection); connect(this, &TimelineModel::addPendingMessageToStore, &events, &EventStore::addPending); - connect(&events, - &EventStore::dataChanged, - this, - [this](int from, int to) { - nhlog::ui()->debug("data changed {} to {}", - events.size() - to - 1, - events.size() - from - 1); - emit dataChanged(index(events.size() - to - 1, 0), - index(events.size() - from - 1, 0)); - }, - Qt::QueuedConnection); + connect( + &events, + &EventStore::dataChanged, + this, + [this](int from, int to) { + nhlog::ui()->debug( + "data changed {} to {}", events.size() - to - 1, events.size() - from - 1); + emit dataChanged(index(events.size() - to - 1, 0), + index(events.size() - from - 1, 0)); + }, + Qt::QueuedConnection); connect(&events, &EventStore::beginInsertRows, this, [this](int from, int to) { int first = events.size() - to; diff --git a/src/ui/UserProfile.h b/src/ui/UserProfile.h index 3d0d2981..de55b6ab 100644 --- a/src/ui/UserProfile.h +++ b/src/ui/UserProfile.h @@ -59,7 +59,7 @@ public: connect(this, &DeviceInfoModel::queueReset, this, &DeviceInfoModel::reset); }; QHash roleNames() const override; - int rowCount(const QModelIndex &parent = QModelIndex()) const + int rowCount(const QModelIndex &parent = QModelIndex()) const override { (void)parent; return (int)deviceList_.size();