From 576822b1ff3331f9f1380e274ed84b0cc6638ec7 Mon Sep 17 00:00:00 2001 From: AlexeyZavar Date: Thu, 16 Jan 2025 13:06:26 +0300 Subject: [PATCH] feat: fetch devs and supporters --- Telegram/CMakeLists.txt | 2 + Telegram/SourceFiles/ayu/ayu_infra.cpp | 8 +- Telegram/SourceFiles/ayu/utils/rc_manager.cpp | 156 ++++++++++++++++++ Telegram/SourceFiles/ayu/utils/rc_manager.h | 73 ++++++++ .../ayu/utils/telegram_helpers.cpp | 60 +------ .../SourceFiles/ayu/utils/telegram_helpers.h | 4 +- .../info/profile/info_profile_cover.cpp | 4 +- 7 files changed, 248 insertions(+), 59 deletions(-) create mode 100644 Telegram/SourceFiles/ayu/utils/rc_manager.cpp create mode 100644 Telegram/SourceFiles/ayu/utils/rc_manager.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index d0b1f59d9..10e2d8efd 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -118,6 +118,8 @@ set(ayugram_files ayu/utils/telegram_helpers.h ayu/utils/windows_utils.cpp ayu/utils/windows_utils.h + ayu/utils/rc_manager.cpp + ayu/utils/rc_manager.h ayu/utils/taptic_engine/taptic_engine.cpp ayu/utils/taptic_engine/taptic_engine.h ayu/utils/taptic_engine/platform/taptic_engine_dummy.cpp diff --git a/Telegram/SourceFiles/ayu/ayu_infra.cpp b/Telegram/SourceFiles/ayu/ayu_infra.cpp index 69624c715..e2c5e3294 100644 --- a/Telegram/SourceFiles/ayu/ayu_infra.cpp +++ b/Telegram/SourceFiles/ayu/ayu_infra.cpp @@ -12,6 +12,7 @@ #include "ayu/ayu_worker.h" #include "ayu/data/ayu_database.h" #include "lang/lang_instance.h" +#include "utils/rc_manager.h" namespace AyuInfra { @@ -19,7 +20,7 @@ void initLang() { QString id = Lang::GetInstance().id(); QString baseId = Lang::GetInstance().baseId(); if (id.isEmpty()) { - LOG(("Language ID not found!")); + LOG(("Language is not loaded")); return; } AyuLanguage::init(); @@ -41,11 +42,16 @@ void initWorker() { AyuWorker::initialize(); } +void initRCManager() { + RCManager::getInstance().start(); +} + void init() { initLang(); initDatabase(); initUiSettings(); initWorker(); + initRCManager(); } } diff --git a/Telegram/SourceFiles/ayu/utils/rc_manager.cpp b/Telegram/SourceFiles/ayu/utils/rc_manager.cpp new file mode 100644 index 000000000..5e839e36d --- /dev/null +++ b/Telegram/SourceFiles/ayu/utils/rc_manager.cpp @@ -0,0 +1,156 @@ +// This is the source code of AyuGram for Desktop. +// +// We do not and cannot prevent the use of our code, +// but be respectful and credit the original author. +// +// Copyright @Radolyn, 2024 +#include "rc_manager.h" + +#include +#include +#include + +#include "base/unixtime.h" + +std::unordered_set default_developers = { + 963080346, 1282540315, 1374434073, 168769611, + 1773117711, 5330087923, 666154369, 139303278, + 668557709, 1348136086, 6288255532, 7453676178, + // ------------------------------------------- + 778327202, 238292700, 1795176335, 6247153446, + 1752394339, 7745305003, 1183312839, 497855299, + 623054735 +}; + +std::unordered_set default_channels = { + 1233768168, 1524581881, 1571726392, 1632728092, + 1172503281, 1877362358, 1905581924, 1794457129, + 1434550607, 1947958814, 1815864846, 2130395384, + 1976430343, 1754537498, 1725670701, +}; + +void RCManager::start() { + DEBUG_LOG(("RCManager: starting")); + _manager = std::make_unique(); + + makeRequest(); + + _timer = new QTimer(this); + connect(_timer, &QTimer::timeout, this, &RCManager::makeRequest); + _timer->start(60 * 60 * 1000); // 1 hour +} + +void RCManager::makeRequest() { + if (!_manager) { + return; + } + + LOG(("RCManager: requesting map")); + + clearSentRequest(); + + const auto request = QNetworkRequest(QUrl("https://update.ayugram.one/rc/current/desktop")); + _reply = _manager->get(request); + connect(_reply, + &QNetworkReply::finished, + [=] + { + gotResponse(); + }); + connect(_reply, + &QNetworkReply::errorOccurred, + [=](auto e) + { + gotFailure(e); + }); +} + +void RCManager::gotResponse() { + if (!_reply) { + return; + } + + cSetLastUpdateCheck(base::unixtime::now()); + const auto response = _reply->readAll(); + clearSentRequest(); + + if (!handleResponse(response)) { + LOG(("RCManager: Error bad map size: %1").arg(response.size())); + gotFailure(QNetworkReply::UnknownContentError); + } +} + +bool RCManager::handleResponse(const QByteArray &response) { + return applyResponse(response); +} + +bool RCManager::applyResponse(const QByteArray &response) { + auto error = QJsonParseError{0, QJsonParseError::NoError}; + const auto document = QJsonDocument::fromJson(response, &error); + if (error.error != QJsonParseError::NoError) { + LOG(("RCManager: Failed to parse JSON, error: %1" + ).arg(error.errorString())); + return false; + } + if (!document.isObject()) { + LOG(("RCManager: not an object received in JSON")); + return false; + } + const auto root = document.object(); + + const auto developers = root.value("developers").toArray(); + const auto channels = root.value("channels").toArray(); + const auto supporters = root.value("supporters").toArray(); + + _developers.clear(); + _channels.clear(); + _supporters.clear(); + + for (const auto &developer : developers) { + if (const auto id = developer.toVariant().toLongLong()) { + _developers.insert(id); + } + } + + for (const auto &channel : channels) { + if (const auto id = channel.toVariant().toLongLong()) { + _channels.insert(id); + } + } + + for (const auto &supporter : supporters) { + if (const auto id = supporter.toVariant().toLongLong()) { + _supporters.insert(id); + } + } + + initialized = true; + + LOG(("RCManager: Loaded %1 developers, %2 channels" + ).arg(_developers.size()).arg(_channels.size())); + + return true; +} + +void RCManager::gotFailure(QNetworkReply::NetworkError e) { + LOG(("RCManager: Error %1").arg(e)); + if (const auto reply = base::take(_reply)) { + reply->deleteLater(); + } +} + +void RCManager::clearSentRequest() { + const auto reply = base::take(_reply); + if (!reply) { + return; + } + disconnect(reply, &QNetworkReply::finished, nullptr, nullptr); + disconnect(reply, &QNetworkReply::errorOccurred, nullptr, nullptr); + reply->abort(); + reply->deleteLater(); +} + +RCManager::~RCManager() { + clearSentRequest(); + _manager = nullptr; +} diff --git a/Telegram/SourceFiles/ayu/utils/rc_manager.h b/Telegram/SourceFiles/ayu/utils/rc_manager.h new file mode 100644 index 000000000..765580efe --- /dev/null +++ b/Telegram/SourceFiles/ayu/utils/rc_manager.h @@ -0,0 +1,73 @@ +// This is the source code of AyuGram for Desktop. +// +// We do not and cannot prevent the use of our code, +// but be respectful and credit the original author. +// +// Copyright @Radolyn, 2024 +#pragma once + +#include + +#include "ayu/data/entities.h" + +extern std::unordered_set default_developers; +extern std::unordered_set default_channels; + +class RCManager final : public QObject +{ + Q_OBJECT +public: + static RCManager &getInstance() { + static RCManager instance; + return instance; + } + + RCManager(const RCManager &) = delete; + RCManager &operator=(const RCManager &) = delete; + RCManager(RCManager &&) = delete; + RCManager &operator=(RCManager &&) = delete; + + void start(); + + [[nodiscard]] const std::unordered_set &developers() const { + if (!initialized) { + return default_developers; + } + return _developers; + } + + [[nodiscard]] const std::unordered_set &channels() const { + if (!initialized) { + return default_channels; + } + return _channels; + } + + [[nodiscard]] const std::unordered_set &supporters() const { + return _supporters; + } + +private: + RCManager() = default; + ~RCManager(); + + void makeRequest(); + + void gotResponse(); + void gotFailure(QNetworkReply::NetworkError e); + void clearSentRequest(); + bool handleResponse(const QByteArray &response); + bool applyResponse(const QByteArray &response); + + bool initialized = false; + + std::unordered_set _developers = {}; + std::unordered_set _channels = {}; + std::unordered_set _supporters = {}; + + QTimer* _timer = nullptr; + + std::unique_ptr _manager = nullptr; + QNetworkReply *_reply = nullptr; + +}; diff --git a/Telegram/SourceFiles/ayu/utils/telegram_helpers.cpp b/Telegram/SourceFiles/ayu/utils/telegram_helpers.cpp index 07ddf4719..a2b0b91a1 100644 --- a/Telegram/SourceFiles/ayu/utils/telegram_helpers.cpp +++ b/Telegram/SourceFiles/ayu/utils/telegram_helpers.cpp @@ -10,9 +10,9 @@ #include #include "apiwrap.h" -#include "api/api_text_entities.h" #include "lang_auto.h" +#include "rc_manager.h" #include "ayu/ayu_worker.h" #include "ayu/data/entities.h" #include "core/mime_type.h" @@ -23,7 +23,6 @@ #include "data/data_peer_id.h" #include "data/data_photo.h" #include "data/data_user.h" -#include "inline_bots/inline_bot_result.h" #include "data/data_document.h" #include "data/data_session.h" @@ -38,54 +37,6 @@ #include "ayu/ayu_settings.h" #include "ayu/ayu_state.h" -// https://github.com/AyuGram/AyuGram4AX/blob/rewrite/TMessagesProj/src/main/java/com/radolyn/ayugram/AyuConstants.java -std::unordered_set ayugram_channels = { - 1905581924, // @ayugramchat - 1794457129, // @ayugram1338 - 1434550607, // @radolyn - 1947958814, // @ayugramfun - 1815864846, // @ayugramfcm - 2130395384, // @ayugram_easter -}; - -std::unordered_set ayugram_devs = { - 139303278, // @alexeyzavar - 778327202, // @sharapagorg - 238292700, // @MaxPlays - 1795176335, // @radolyn_services - 1752394339, // mouse -}; - -// https://github.com/AyuGram/AyuGram4AX/blob/rewrite/TMessagesProj/src/main/java/com/exteragram/messenger/ExteraConfig.java -std::unordered_set extera_channels = { - 1233768168, - 1524581881, - 1571726392, - 1632728092, - 1172503281, - 1877362358, - // custom - 1812843581, // @moeGramX - 1634905346, // @moex_log - 1516526055, // @moexci - 1622008530, // @moe_chat -}; - -std::unordered_set extera_devs = { - 963080346, - 1282540315, - 1374434073, - 388099852, - 1972014627, - 168769611, - 480000401, - 639891381, - 1773117711, - 5330087923, - 666154369, - 139303278 -}; - Main::Session *getSession(ID userId) { for (const auto &[index, account] : Core::App().domain().accounts()) { if (const auto session = account->maybeSession()) { @@ -125,12 +76,13 @@ ID getBareID(not_null peer) { return peer->id.value & PeerId::kChatTypeMask; } -bool isAyuGramRelated(ID peerId) { - return ayugram_devs.contains(peerId) || ayugram_channels.contains(peerId); +bool isExteraPeer(ID peerId) { + return RCManager::getInstance().developers().contains(peerId) || RCManager::getInstance().channels(). + contains(peerId); } -bool isExteraRelated(ID peerId) { - return extera_devs.contains(peerId) || extera_channels.contains(peerId); +bool isSupporterPeer(ID peerId) { + return RCManager::getInstance().supporters().contains(peerId); } bool isMessageHidden(const not_null item) { diff --git a/Telegram/SourceFiles/ayu/utils/telegram_helpers.h b/Telegram/SourceFiles/ayu/utils/telegram_helpers.h index e057ade78..d97172cd5 100644 --- a/Telegram/SourceFiles/ayu/utils/telegram_helpers.h +++ b/Telegram/SourceFiles/ayu/utils/telegram_helpers.h @@ -20,8 +20,8 @@ ID getDialogIdFromPeer(not_null peer); ID getBareID(not_null peer); -bool isAyuGramRelated(ID peerId); -bool isExteraRelated(ID peerId); +bool isExteraPeer(ID peerId); +bool isSupporterPeer(ID peerId); bool isMessageHidden(not_null item); diff --git a/Telegram/SourceFiles/info/profile/info_profile_cover.cpp b/Telegram/SourceFiles/info/profile/info_profile_cover.cpp index 7b2796cce..58d7f2fd2 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_cover.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_cover.cpp @@ -418,9 +418,9 @@ Cover::Cover( refreshNameGeometry(width()); }, _name->lifetime()); - if (isExteraRelated(getBareID(_peer)) || isAyuGramRelated(getBareID(_peer))) { + if (isExteraPeer(getBareID(_peer))) { _devBadge->setContent(Info::Profile::Badge::Content{BadgeType::Extera}); - } else if (false) { + } else if (isSupporterPeer(getBareID(_peer))) { _devBadge->setContent(Info::Profile::Badge::Content{BadgeType::ExteraSupporter}); } else { _devBadge->setContent(Info::Profile::Badge::Content{BadgeType::None});