From 405522f3b09913544bbec43a802d4e13ccd84f14 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 17 Nov 2024 20:47:26 +0100 Subject: [PATCH] feat: add key upload function for module key server sync --- src/m_key_server_sync/KeyServerSyncModule.cpp | 40 +++++++++++++++++++ src/m_key_server_sync/VKSInterface.cpp | 3 +- src/m_key_server_sync/VKSInterface.h | 2 +- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/m_key_server_sync/KeyServerSyncModule.cpp b/src/m_key_server_sync/KeyServerSyncModule.cpp index 2247e3d..8455993 100644 --- a/src/m_key_server_sync/KeyServerSyncModule.cpp +++ b/src/m_key_server_sync/KeyServerSyncModule.cpp @@ -46,6 +46,7 @@ auto GFRegisterModule() -> int { auto GFActiveModule() -> int { LISTEN("REQUEST_GET_PUBLIC_KEY_BY_FINGERPRINT"); LISTEN("REQUEST_GET_PUBLIC_KEY_BY_KEY_ID"); + LISTEN("REQUEST_UPLOAD_PUBLIC_KEY"); return 0; } @@ -124,6 +125,45 @@ EXECUTE_MODULE() { return 0; } + if (event["event_id"] == "REQUEST_UPLOAD_PUBLIC_KEY") { + if (event["key_text"].isEmpty()) CB_ERR(event, -1, "key_text is empty"); + + QByteArray key_text = event["key_text"].toLatin1(); + FLOG_DEBUG("try to get key info of key id: %1", key_text); + + auto* vks = new VKSInterface(); + vks->UploadKey(key_text); + QObject::connect( + vks, &VKSInterface::SignalKeyUploaded, QThread::currentThread(), + [event](const QString& fpr, const QJsonObject& status, + const QString& token) { + CB(event, GFGetModuleID(), + { + {"ret", QString::number(0)}, + {"fingerprint", fpr}, + {"status", QString::fromUtf8(QJsonDocument(status).toJson())}, + {"token", token}, + }); + }); + QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks, + &VKSInterface::deleteLater); + + QObject::connect(vks, &VKSInterface::SignalErrorOccurred, + QThread::currentThread(), + [event](const QString& error, const QString& data) { + CB(event, GFGetModuleID(), + { + {"ret", QString::number(-1)}, + {"error_msg", error}, + {"reply_data", data}, + }); + }); + QObject::connect(vks, &VKSInterface::SignalKeyRetrieved, vks, + &VKSInterface::deleteLater); + + return 0; + } + CB_SUCC(event); } END_EXECUTE_MODULE() diff --git a/src/m_key_server_sync/VKSInterface.cpp b/src/m_key_server_sync/VKSInterface.cpp index 6bf277a..9b4f4fd 100644 --- a/src/m_key_server_sync/VKSInterface.cpp +++ b/src/m_key_server_sync/VKSInterface.cpp @@ -127,7 +127,8 @@ void VKSInterface::on_reply_finished(QNetworkReply* reply) { if (json_response.isObject()) { QJsonObject response_object = json_response.object(); emit SignalKeyUploaded(response_object["key_fpr"].toString(), - response_object["status"].toObject()); + response_object["status"].toObject(), + response_object["token"].toString()); } } else if (url.path().contains("/vks/v1/request-verify")) { if (json_response.isObject()) { diff --git a/src/m_key_server_sync/VKSInterface.h b/src/m_key_server_sync/VKSInterface.h index d5e02c2..e487ea4 100644 --- a/src/m_key_server_sync/VKSInterface.h +++ b/src/m_key_server_sync/VKSInterface.h @@ -48,7 +48,7 @@ class VKSInterface : public QObject { signals: void SignalKeyRetrieved(const QString& key); void SignalKeyUploaded(const QString& key_fingerprint, - const QJsonObject& status); + const QJsonObject& status, const QString& token); void SignalVerificationRequested(const QString& key_fingerprint, const QJsonObject& status); void SignalErrorOccurred(const QString& error_string,