Skip to content

Commit

Permalink
Merge pull request #20 from QuasarApp/fix_multisending
Browse files Browse the repository at this point in the history
Fix multisending
  • Loading branch information
EndrII authored Dec 17, 2024
2 parents 24c8b4e + 03782ba commit d8fc923
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 16 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/qTbot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 2 additions & 0 deletions src/qTbot/src/private/requests/telegrameditmessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ TelegramEditMessage::TelegramEditMessage(const QVariant &idEditedMessage,

setRequest("editMessageText");
addArg("message_id", idEditedMessage);
setPriority(args.requestPriority);

}

}
2 changes: 2 additions & 0 deletions src/qTbot/src/private/requests/telegramsendcontact.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ TelegramSendContact::TelegramSendContact(const TelegramArgs &args,
addArg("first_name", firstName);
addArg("last_name", lastName);
addArg("phone_number", phone);
setPriority(args.requestPriority);


}

Expand Down
2 changes: 2 additions & 0 deletions src/qTbot/src/private/requests/telegramsendfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand Down
2 changes: 2 additions & 0 deletions src/qTbot/src/private/requests/telegramsendlocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
31 changes: 24 additions & 7 deletions src/qTbot/src/public/qTbot/ibot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ QNetworkReply* IBot::sendRquestImpl(const QSharedPointer<iRequest> &rquest) {
#endif

QNetworkReply* networkReplay = nullptr;
QSharedPointer<QHttpMultiPart> httpData;

switch (rquest->method()) {
case iRequest::Get: {
Expand All @@ -84,9 +83,10 @@ QNetworkReply* IBot::sendRquestImpl(const QSharedPointer<iRequest> &rquest) {
case iRequest::Upload:
QNetworkRequest netRequest(url);

httpData = rquest->argsToMultipartFormData();
auto httpData = rquest->argsToMultipartFormData();
if (httpData) {
networkReplay = _manager->post(netRequest, httpData.data());
connect(networkReplay, &QNetworkReply::destroyed, [httpData](){});

} else {
return {};
Expand Down Expand Up @@ -116,7 +116,6 @@ void IBot::setParallelActiveNetworkThreads(int newParallelActiveNetworkThreads)

void IBot::setCurrentParallelActiveNetworkThreads(int newParallelActiveNetworkThreads) {
_currentParallelActiveNetworkThreads = newParallelActiveNetworkThreads;
qDebug () << "current network active requests count : " << _currentParallelActiveNetworkThreads;
}

int IBot::reqestLimitPerSecond() const {
Expand All @@ -136,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();
}
Expand All @@ -146,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();

Expand Down Expand Up @@ -218,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);
Expand Down Expand Up @@ -256,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();
Expand Down
22 changes: 13 additions & 9 deletions src/qTbot/src/public/qTbot/itelegrambot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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){
Expand Down

0 comments on commit d8fc923

Please sign in to comment.