From 3286eacf75f41cc056e6f0a9cc1cd172bb8bd85b Mon Sep 17 00:00:00 2001 From: ZavaruKitsu Date: Sun, 9 Jul 2023 17:28:52 +0000 Subject: [PATCH] feat: progress AyuSync implementation --- Telegram/CMakeLists.txt | 2 + Telegram/SourceFiles/ayu/ayu_lang.cpp | 4 +- .../ayu/sync/ayu_sync_controller.cpp | 40 ++++++++++++++++++- .../ayu/sync/ayu_sync_controller.h | 5 +++ Telegram/SourceFiles/ayu/sync/models.h | 21 ++++++++++ .../ayu/sync/utils/telegram_helpers.cpp | 26 ++++++++++++ .../ayu/sync/utils/telegram_helpers.h | 15 +++++++ 7 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.cpp create mode 100644 Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index c0bfa0f24..c2559975d 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -123,6 +123,8 @@ PRIVATE ayu/sync/utils/ayu_pipe_wrapper.cpp ayu/sync/utils/ayu_pipe_wrapper.h ayu/sync/utils/process_utils.hpp + ayu/sync/utils/telegram_helpers.cpp + ayu/sync/utils/telegram_helpers.h ayu/libs/pipe.hpp ayu/libs/json.hpp ayu/libs/json_ext.hpp diff --git a/Telegram/SourceFiles/ayu/ayu_lang.cpp b/Telegram/SourceFiles/ayu/ayu_lang.cpp index e6c9e1183..7343f75f6 100644 --- a/Telegram/SourceFiles/ayu/ayu_lang.cpp +++ b/Telegram/SourceFiles/ayu/ayu_lang.cpp @@ -42,10 +42,10 @@ void CustomLangPack::fetchCustomLangPack(const QString &langPackId, const QStrin QUrl url; if (!finalLangPackId.isEmpty() && !langPackBaseId.isEmpty() && !needFallback) { - url.setUrl(qsl("https://raw.githubusercontent.com/AyuGram/Languages/main/values/langs/%1/Shared.json").arg( + url.setUrl(qsl("https://raw.githubusercontent.com/AyuGram/Languages/l10n_main/values/langs/%1/Shared.json").arg( finalLangPackId)); } else { - url.setUrl(qsl("https://raw.githubusercontent.com/AyuGram/Languages/main/values/langs/%1/Shared.json").arg( + url.setUrl(qsl("https://raw.githubusercontent.com/AyuGram/Languages/l10n_main/values/langs/%1/Shared.json").arg( needFallback ? langPackBaseId : finalLangPackId)); } _chkReply = networkManager.get(QNetworkRequest(url)); diff --git a/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.cpp b/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.cpp index 4e7a8c371..be0acbf3e 100644 --- a/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.cpp +++ b/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.cpp @@ -8,6 +8,11 @@ #include "ayu_sync_controller.h" #include "ayu/libs/process.hpp" #include "ayu/sync/utils/process_utils.hpp" +#include "ayu/sync/models.h" +#include "data/data_session.h" +#include "history/history.h" +#include "ayu/sync/utils/telegram_helpers.h" + #include #include @@ -73,6 +78,39 @@ namespace AyuSync { auto userId = p["userId"].get(); auto type = p["type"].get(); - LOG(("userId: %1, type: %1").arg(userId).arg(type.c_str())); + LOG(("userId: %1, type: %2").arg(userId).arg(type.c_str())); + + // todo: check if account exists + + if (type == "sync_force") { + auto ev = p.template get(); + onSyncForce(ev); + } else if (type == "sync_batch") { + onSyncBatch(p); + } else if (type == "sync_read") { + auto ev = p.template get(); + onSyncRead(ev); + } else { + LOG(("Unknown sync type: %1").arg(type.c_str())); + } + } + + void ayu_sync_controller::onSyncForce(SyncForce ev) { + + } + + void ayu_sync_controller::onSyncBatch(json ev) { + + } + + void ayu_sync_controller::onSyncRead(SyncRead ev) { + auto session = getSession(ev.userId); + + auto peer = PeerId(abs(ev.args.dialogId)); + + auto history = session->data().history(peer); + if (history) { + history->inboxRead(ev.args.untilId, ev.args.unread); + } } } \ No newline at end of file diff --git a/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.h b/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.h index bdc1cf1d7..04dd6c32f 100644 --- a/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.h +++ b/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.h @@ -8,6 +8,7 @@ #pragma once #include "ayu/libs/json.hpp" +#include "models.h" #include "utils/ayu_pipe_wrapper.h" using json = nlohmann::json; @@ -26,6 +27,10 @@ namespace AyuSync { public: void initializeAgent(); + void onSyncForce(SyncForce ev); + void onSyncBatch(json ev); + void onSyncRead(SyncRead ev); + void invokeHandler(json p); private: void receiver(); diff --git a/Telegram/SourceFiles/ayu/sync/models.h b/Telegram/SourceFiles/ayu/sync/models.h index 827994c49..8bac6b26f 100644 --- a/Telegram/SourceFiles/ayu/sync/models.h +++ b/Telegram/SourceFiles/ayu/sync/models.h @@ -4,15 +4,18 @@ #include class SyncEvent { +public: std::string type = "sync_unspecified"; long userId = 0; }; class SyncBatch : public SyncEvent { +public: std::string type = "sync_batch"; long userId; class SyncBatchArgs { + public: std::vector events; }; @@ -20,10 +23,12 @@ class SyncBatch : public SyncEvent { }; class SyncRead : public SyncEvent { +public: std::string type = "sync_read"; long userId; class SyncReadArgs { + public: long dialogId; int untilId; int unread; @@ -33,10 +38,12 @@ class SyncRead : public SyncEvent { }; class SyncForce : public SyncEvent { +public: std::string type = "sync_force"; long userId; class SyncForceArgs { + public: int fromDate; }; @@ -44,12 +51,26 @@ class SyncForce : public SyncEvent { }; class SyncForceFinish : public SyncEvent { +public: std::string type = "sync_force_finish"; long userId; class SyncForceFinishArgs { + public: + short dummy; // required to be JSON serializable }; SyncForceFinishArgs args; }; + +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SyncEvent, type, userId) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SyncBatch::SyncBatchArgs, events) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SyncBatch, type, userId, args) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SyncRead::SyncReadArgs, dialogId, untilId, unread) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SyncRead, type, userId, args) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SyncForce::SyncForceArgs, fromDate) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SyncForce, type, userId, args) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SyncForceFinish::SyncForceFinishArgs, dummy) +NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(SyncForceFinish, type, userId, args) + diff --git a/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.cpp b/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.cpp new file mode 100644 index 000000000..abf52a477 --- /dev/null +++ b/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.cpp @@ -0,0 +1,26 @@ +// 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, 2023 + +#include "telegram_helpers.h" +#include "data/data_peer_id.h" + +Main::Session* getSession(long userId) { + for (auto &[index, account] : Core::App().domain().accounts()) { + if (const auto session = account->maybeSession()) { + if (session->userId().bare == userId) { + return session; + } + } + } + + return nullptr; +} + +PeerId dialogIdToPeerId(long dialogId) { + auto peerId = PeerId(); + return peerId; +} \ No newline at end of file diff --git a/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.h b/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.h new file mode 100644 index 000000000..4d13ae085 --- /dev/null +++ b/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.h @@ -0,0 +1,15 @@ +// 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, 2023 + +#pragma once + +#include "core/application.h" +#include "main/main_account.h" +#include "main/main_session.h" +#include "main/main_domain.h" + +Main::Session* getSession(long userId);