/
alexashkin
/
telegraph
Обзор
Документация
Войти
/
alexashkin
/
telegraph
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
develop
src/tdlib/tdlibrequestservice.cpp
93 строки
3 KB
mbarashkov
Рефакторинг в процессе
19 фев 2025, 16:43
19 фев 2025, 16:43
500f989
Код
Авторство
О чём код?
#include "tdlibrequestservice.h" #include <QJsonDocument> #include <td/telegram/td_json_client.h> #include <td/telegram/Client.h> #include <td/telegram/td_api.h> #include "../debuglog.h" namespace { const char _TYPE[] = "@type"; const int BASE_LOADCHATS_DELAY_MS = 1000; } TDLibRequestService::TDLibRequestService(TDLibState* _tdLibState, QObject *parent): QObject(parent), tdLibState(_tdLibState) { _requestTimer.setSingleShot(true); connect(&_requestTimer, &QTimer::timeout, this, &TDLibRequestService::processNextRequest); } void TDLibRequestService::sendRequest(const QString &jsonRequest) { td_send(tdLibState->getClientId(), jsonRequest.toStdString().c_str()); } void TDLibRequestService::sendRequest(const QVariantMap &requestObject) { QJsonDocument requestDocument = QJsonDocument::fromVariant(requestObject); if (tdLibState->loggingOut) { _requestTimer.stop(); _requestQueue.clear(); return; } auto type = requestObject[_TYPE].toString(); if (shouldExecuteRequestImmediately(type)) { td_send(tdLibState->getClientId(), requestDocument.toJson().constData()); return; } if(isQueuedRequestTopPriority(type)) { _requestQueue.push_front(requestDocument.object()); } else { _requestQueue.enqueue(requestDocument.object()); } if (!_requestTimer.isActive()) { processNextRequest(); } } bool TDLibRequestService::isRequestTimerActive() { return _requestTimer.isActive(); } void TDLibRequestService::callProcessNextRequest() { processNextRequest(); } void TDLibRequestService::processNextRequest() { if (_requestQueue.isEmpty() || tdLibState->floodWait) { return; } auto time = BASE_LOADCHATS_DELAY_MS; if (!_requestTimer.isActive()) { QJsonObject request = _requestQueue.dequeue(); QJsonDocument requestDocument(request); LOG_DEBUG(LOG_TDLIB_COMM, "Sending request to TD Lib, object type name: " << request.value(_TYPE).toString()); td_send(tdLibState->getClientId(), requestDocument.toJson().constData()); _requestTimer.start(time); } } bool TDLibRequestService::shouldExecuteRequestImmediately(QString type) { return type != "getChats" && type != "loadChats" && type != "getChatFolder" && type != "getUserPrivacySettingRules" && type != "getStickerSet"; } bool TDLibRequestService::isQueuedRequestTopPriority(QString type) { return type == "getChats" || type == "loadChats" || type == "getChatFolder"; }