From 7323233b8da523a027e06f43350e4f364a18c10e Mon Sep 17 00:00:00 2001 From: ZavaruKitsu Date: Sun, 9 Jul 2023 22:08:25 +0000 Subject: [PATCH] feat: progress AyuSync implementation --- .../ayu/sync/ayu_sync_controller.cpp | 33 +++++++++------- Telegram/SourceFiles/ayu/sync/models.h | 14 ++++--- .../ayu/sync/utils/telegram_helpers.cpp | 39 +++++++++++++++++-- .../ayu/sync/utils/telegram_helpers.h | 6 ++- 4 files changed, 68 insertions(+), 24 deletions(-) diff --git a/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.cpp b/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.cpp index a3ab024ef..4dd01a981 100644 --- a/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.cpp +++ b/Telegram/SourceFiles/ayu/sync/ayu_sync_controller.cpp @@ -17,6 +17,8 @@ #include #include +#include "core/sandbox.h" + namespace AyuSync { std::optional controller = std::nullopt; @@ -97,7 +99,7 @@ namespace AyuSync if (type == "sync_force") { - auto ev = p.template get(); + auto ev = p.get(); onSyncForce(ev); } else if (type == "sync_batch") @@ -106,7 +108,7 @@ namespace AyuSync } else if (type == "sync_read") { - auto ev = p.template get(); + auto ev = p.get(); onSyncRead(ev); } else @@ -115,24 +117,29 @@ namespace AyuSync } } - void ayu_sync_controller::onSyncForce(SyncForce ev) + DECLSPEC_NOINLINE void ayu_sync_controller::onSyncForce(SyncForce ev) { } - void ayu_sync_controller::onSyncBatch(json ev) + DECLSPEC_NOINLINE void ayu_sync_controller::onSyncBatch(json ev) { } - void ayu_sync_controller::onSyncRead(SyncRead ev) + DECLSPEC_NOINLINE 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) + dispatchToMainThread([=] { - history->inboxRead(ev.args.untilId, ev.args.unread); - } + auto session = getSession(ev.userId); + auto history = getHistoryFromDialogId(ev.args.dialogId, session); + + if (history->folderKnown()) + { + history->inboxRead(ev.args.untilId, ev.args.unread); + } + else + { + LOG(("Unknown dialog %1").arg(ev.args.dialogId)); + } + }); } } diff --git a/Telegram/SourceFiles/ayu/sync/models.h b/Telegram/SourceFiles/ayu/sync/models.h index 8575b7bb6..dd368da5f 100644 --- a/Telegram/SourceFiles/ayu/sync/models.h +++ b/Telegram/SourceFiles/ayu/sync/models.h @@ -5,18 +5,20 @@ #include #include +#define ID long long + class SyncEvent { public: std::string type = "sync_unspecified"; - long userId = 0; + ID userId = 0; }; class SyncBatch : public SyncEvent { public: std::string type = "sync_batch"; - long userId; + ID userId; class SyncBatchArgs { @@ -31,12 +33,12 @@ class SyncRead : public SyncEvent { public: std::string type = "sync_read"; - long userId; + ID userId; class SyncReadArgs { public: - long dialogId; + ID dialogId; int untilId; int unread; }; @@ -48,7 +50,7 @@ class SyncForce : public SyncEvent { public: std::string type = "sync_force"; - long userId; + ID userId; class SyncForceArgs { @@ -63,7 +65,7 @@ class SyncForceFinish : public SyncEvent { public: std::string type = "sync_force_finish"; - long userId; + ID userId; class SyncForceFinishArgs { diff --git a/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.cpp b/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.cpp index 7027aa388..7bdf45ac0 100644 --- a/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.cpp +++ b/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.cpp @@ -6,9 +6,17 @@ // Copyright @Radolyn, 2023 #include "telegram_helpers.h" +#include +#include #include "data/data_peer_id.h" -Main::Session* getSession(long userId) +#include "ayu/sync/models.h" + +#include "data/data_session.h" +#include "history/history.h" + + +Main::Session* getSession(ID userId) { for (auto& [index, account] : Core::App().domain().accounts()) { @@ -24,8 +32,31 @@ Main::Session* getSession(long userId) return nullptr; } -PeerId dialogIdToPeerId(long dialogId) +void dispatchToMainThread(std::function callback) { - auto peerId = PeerId(); - return peerId; + auto timer = new QTimer(); + timer->moveToThread(qApp->thread()); + timer->setSingleShot(true); + QObject::connect(timer, &QTimer::timeout, [=]() + { + callback(); + timer->deleteLater(); + }); + QMetaObject::invokeMethod(timer, "start", Qt::QueuedConnection, Q_ARG(int, 0)); +} + +not_null getHistoryFromDialogId(ID dialogId, Main::Session* session) +{ + if (dialogId > 0) + { + return session->data().history(peerFromUser(dialogId)); + } + + auto history = session->data().history(peerFromChannel(abs(dialogId))); + if (history->folderKnown()) + { + return history; + } + + return session->data().history(peerFromChat(abs(dialogId))); } diff --git a/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.h b/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.h index 68417a6f5..d7c1a0228 100644 --- a/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.h +++ b/Telegram/SourceFiles/ayu/sync/utils/telegram_helpers.h @@ -7,9 +7,13 @@ #pragma once +#include "ayu/sync/models.h" + #include "core/application.h" #include "main/main_account.h" #include "main/main_domain.h" #include "main/main_session.h" -Main::Session* getSession(long userId); +Main::Session* getSession(ID userId); +void dispatchToMainThread(std::function callback); +not_null getHistoryFromDialogId(ID dialogId, Main::Session* session);