From 03782ba2723134d2bf79c4c0faa51e7d8335a37b Mon Sep 17 00:00:00 2001
From: EndrII <EndrIIMail@gmail.com>
Date: Mon, 16 Dec 2024 23:42:32 +0100
Subject: [PATCH] fix sending

---
 CMakeLists.txt                                |  1 +
 src/qTbot/CMakeLists.txt                      |  5 ++++
 .../private/requests/telegrameditmessage.cpp  |  2 ++
 .../private/requests/telegramsendcontact.cpp  |  2 ++
 .../src/private/requests/telegramsendfile.cpp |  2 ++
 .../private/requests/telegramsendlocation.cpp |  2 ++
 src/qTbot/src/public/qTbot/ibot.cpp           | 26 ++++++++++++++++---
 src/qTbot/src/public/qTbot/itelegrambot.cpp   | 22 +++++++++-------
 8 files changed, 49 insertions(+), 13 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 85c7c2d..523e10a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -36,6 +36,7 @@ set(QTBOT_PACKAGE_ID "quasarapp.core.qTbot")
 option(QTBOT_TESTS "This option disables or enables tests of the ${PROJECT_NAME} project"  ON)
 option(QTBOT_EXAMPLE "This option disables or enables example app of the ${PROJECT_NAME} project" ON)
 option(QTBOT_PRINT_RQUESTS "This option disables or enables printing requests" OFF)
+option(QTBOT_PRINT_ERRORS "This option disables or enables printing errors" ON)
 
 if (ANDROID OR IOS OR QA_WASM32)
     set(QTBOT_TESTS OFF CACHE BOOL "This option force disbled for ANDROID IOS QA_WASM32 and Not Qt projects" FORCE)
diff --git a/src/qTbot/CMakeLists.txt b/src/qTbot/CMakeLists.txt
index 661f98c..6db8514 100644
--- a/src/qTbot/CMakeLists.txt
+++ b/src/qTbot/CMakeLists.txt
@@ -14,6 +14,11 @@ if (QTBOT_PRINT_RQUESTS)
     add_definitions(-DQTBOT_PRINT_RQUESTS)
 endif()
 
+if (QTBOT_PRINT_ERRORS)
+    add_definitions(-DQTBOT_PRINT_ERRORS)
+endif()
+
+
 file(GLOB_RECURSE SOURCE_CPP
     "src/*.cpp"
     "src/*.h"
diff --git a/src/qTbot/src/private/requests/telegrameditmessage.cpp b/src/qTbot/src/private/requests/telegrameditmessage.cpp
index b88bb28..17e95ca 100644
--- a/src/qTbot/src/private/requests/telegrameditmessage.cpp
+++ b/src/qTbot/src/private/requests/telegrameditmessage.cpp
@@ -16,6 +16,8 @@ TelegramEditMessage::TelegramEditMessage(const QVariant &idEditedMessage,
 
     setRequest("editMessageText");
     addArg("message_id", idEditedMessage);
+    setPriority(args.requestPriority);
+
 }
 
 }
diff --git a/src/qTbot/src/private/requests/telegramsendcontact.cpp b/src/qTbot/src/private/requests/telegramsendcontact.cpp
index 7a74f19..281c936 100644
--- a/src/qTbot/src/private/requests/telegramsendcontact.cpp
+++ b/src/qTbot/src/private/requests/telegramsendcontact.cpp
@@ -19,6 +19,8 @@ TelegramSendContact::TelegramSendContact(const TelegramArgs &args,
     addArg("first_name", firstName);
     addArg("last_name", lastName);
     addArg("phone_number", phone);
+    setPriority(args.requestPriority);
+
 
 }
 
diff --git a/src/qTbot/src/private/requests/telegramsendfile.cpp b/src/qTbot/src/private/requests/telegramsendfile.cpp
index 462f373..a95003c 100644
--- a/src/qTbot/src/private/requests/telegramsendfile.cpp
+++ b/src/qTbot/src/private/requests/telegramsendfile.cpp
@@ -19,6 +19,7 @@ qTbot::TelegramSendFile::TelegramSendFile(const QString &request,
                                           const ExtraJsonObjects& extraObjects
                                           ):
     TelegramSingleRquest(request, args.toMap(true)) {
+    setPriority(args.requestPriority);
 
 
     for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) {
@@ -33,6 +34,7 @@ qTbot::TelegramSendFile::TelegramSendFile(const QString &request,
                                           const TelegramArgs &args,
                                           const QHash<QString, QSharedPointer<QJsonObject> > &extraObjects):
     TelegramSingleRquest(request, args.toMap(true)) {
+    setPriority(args.requestPriority);
 
     QFile readFile(file.absoluteFilePath());
     if (!readFile.open(QIODevice::ReadOnly)) {
diff --git a/src/qTbot/src/private/requests/telegramsendlocation.cpp b/src/qTbot/src/private/requests/telegramsendlocation.cpp
index 582a01e..153a6aa 100644
--- a/src/qTbot/src/private/requests/telegramsendlocation.cpp
+++ b/src/qTbot/src/private/requests/telegramsendlocation.cpp
@@ -18,6 +18,8 @@ TelegramSendLocation::TelegramSendLocation(const TelegramArgs &args,
 
     addArg("latitude", latitude);
     addArg("longitude", longitude);
+    setPriority(args.requestPriority);
+
 
     for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) {
         addArg(it.key(), QJsonDocument(*it.value()).toJson(QJsonDocument::Compact));
diff --git a/src/qTbot/src/public/qTbot/ibot.cpp b/src/qTbot/src/public/qTbot/ibot.cpp
index b3e2747..fa98cb2 100644
--- a/src/qTbot/src/public/qTbot/ibot.cpp
+++ b/src/qTbot/src/public/qTbot/ibot.cpp
@@ -135,7 +135,11 @@ IBot::sendRequest(const QSharedPointer<iRequest> &rquest) {
     _requestQueue.insert(makeKey(rquest->priority()),
                          RequestData{rquest, "", responce});
 
-    _requestExecutor->start();
+    if (!_requestExecutor->isActive()) {
+        handleEcxecuteRequest();
+        _requestExecutor->start();
+
+    }
 
     return responce->future();
 }
@@ -145,10 +149,15 @@ IBot::sendRequest(const QSharedPointer<iRequest> &rquest,
                   const QString &pathToResult) {
     auto&& responce = QSharedPointer<QPromise<QByteArray>>::create();
     responce->start();
+
     _requestQueue.insert(makeKey(rquest->priority()),
                          RequestData{rquest, pathToResult, responce});
 
-    _requestExecutor->start();
+    if (!_requestExecutor->isActive()) {
+        handleEcxecuteRequest();
+        _requestExecutor->start();
+
+    }
 
     return responce->future();
 
@@ -217,7 +226,12 @@ void IBot::sendRequestPrivate(const QSharedPointer<iRequest> &rquest,
             promise->finish();
 
         } else {
-            promise->setException(HttpException(networkReplay->error(), networkReplay->errorString().toLatin1() + networkReplay->readAll()));
+            QByteArray msg = networkReplay->errorString().toLatin1() + networkReplay->readAll();
+            promise->setException(HttpException(networkReplay->error(), msg));
+#ifdef QTBOT_PRINT_ERRORS
+            qCritical() << msg;
+#endif
+
         }
 
         setCurrentParallelActiveNetworkThreads(_currentParallelActiveNetworkThreads - 1);
@@ -255,7 +269,11 @@ void IBot::sendRequestPrivate(const QSharedPointer<iRequest> &rquest,
     connect(networkReplay, &QNetworkReply::finished, [this, promise, networkReplay, pathToResult](){
 
         if (networkReplay->error() == QNetworkReply::NoError) {
-            promise->setException(HttpException(networkReplay->error(), networkReplay->errorString().toLatin1()));
+            QByteArray msg = networkReplay->errorString().toLatin1();
+            promise->setException(HttpException(networkReplay->error(), msg));
+#ifdef QTBOT_PRINT_ERRORS
+            qCritical() << msg;
+#endif
         } else {
             promise->addResult(pathToResult.toUtf8()); // wil not work with UTF 8 path names
             promise->finish();
diff --git a/src/qTbot/src/public/qTbot/itelegrambot.cpp b/src/qTbot/src/public/qTbot/itelegrambot.cpp
index 0a4a4b8..4b893e0 100644
--- a/src/qTbot/src/public/qTbot/itelegrambot.cpp
+++ b/src/qTbot/src/public/qTbot/itelegrambot.cpp
@@ -299,6 +299,7 @@ QFuture<QByteArray> ITelegramBot::getFile(const QString &fileId, FileType fileTy
 
     if (!localFilePath.isEmpty()) {
         QPromise<QByteArray> fileDataResult;
+        fileDataResult.start();
 
         if (fileType == FileType::Ram) {
             QFile localFile(localFilePath);
@@ -353,18 +354,21 @@ QFuture<QByteArray> ITelegramBot::getFile(const QString &fileId, FileType fileTy
     }
 
     future.then([this, fileId, fileType, longWay](const QByteArray& header){
-        handleFileHeader(header);
+              handleFileHeader(header);
 
-        auto&& future = getFile(fileId, fileType);
+              auto&& future = getFile(fileId, fileType);
 
-        if (!future.isValid()) {
-            longWay->setException(InternalException("Failed to wrote file into internal cache!"));
-            return;
-        };
+              if (!future.isValid()) {
+                  longWay->setException(InternalException("Failed to wrote file into internal cache!"));
+                  return;
+              };
 
-        future.then([longWay](const QByteArray& data){
-            longWay->addResult(data);
-        });
+              future.then([longWay](const QByteArray& data){
+                        longWay->addResult(data);
+                        longWay->finish();
+                    }).onFailed([longWay](const QException& exep){
+                      longWay->setException(exep);
+                  });
 
 
           }).onFailed([longWay](const QException& exep){