feat: progress AyuSync implementation

This commit is contained in:
ZavaruKitsu 2023-07-09 22:08:25 +00:00
parent 32a2de1bef
commit 7323233b8d
4 changed files with 68 additions and 24 deletions

View file

@ -17,6 +17,8 @@
#include <QString> #include <QString>
#include <thread> #include <thread>
#include "core/sandbox.h"
namespace AyuSync namespace AyuSync
{ {
std::optional<ayu_sync_controller> controller = std::nullopt; std::optional<ayu_sync_controller> controller = std::nullopt;
@ -97,7 +99,7 @@ namespace AyuSync
if (type == "sync_force") if (type == "sync_force")
{ {
auto ev = p.template get<SyncForce>(); auto ev = p.get<SyncForce>();
onSyncForce(ev); onSyncForce(ev);
} }
else if (type == "sync_batch") else if (type == "sync_batch")
@ -106,7 +108,7 @@ namespace AyuSync
} }
else if (type == "sync_read") else if (type == "sync_read")
{ {
auto ev = p.template get<SyncRead>(); auto ev = p.get<SyncRead>();
onSyncRead(ev); onSyncRead(ev);
} }
else 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); dispatchToMainThread([=]
auto peer = PeerId(abs(ev.args.dialogId));
auto history = session->data().history(peer);
if (history)
{ {
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));
}
});
} }
} }

View file

@ -5,18 +5,20 @@
#include <string> #include <string>
#include <vector> #include <vector>
#define ID long long
class SyncEvent class SyncEvent
{ {
public: public:
std::string type = "sync_unspecified"; std::string type = "sync_unspecified";
long userId = 0; ID userId = 0;
}; };
class SyncBatch : public SyncEvent class SyncBatch : public SyncEvent
{ {
public: public:
std::string type = "sync_batch"; std::string type = "sync_batch";
long userId; ID userId;
class SyncBatchArgs class SyncBatchArgs
{ {
@ -31,12 +33,12 @@ class SyncRead : public SyncEvent
{ {
public: public:
std::string type = "sync_read"; std::string type = "sync_read";
long userId; ID userId;
class SyncReadArgs class SyncReadArgs
{ {
public: public:
long dialogId; ID dialogId;
int untilId; int untilId;
int unread; int unread;
}; };
@ -48,7 +50,7 @@ class SyncForce : public SyncEvent
{ {
public: public:
std::string type = "sync_force"; std::string type = "sync_force";
long userId; ID userId;
class SyncForceArgs class SyncForceArgs
{ {
@ -63,7 +65,7 @@ class SyncForceFinish : public SyncEvent
{ {
public: public:
std::string type = "sync_force_finish"; std::string type = "sync_force_finish";
long userId; ID userId;
class SyncForceFinishArgs class SyncForceFinishArgs
{ {

View file

@ -6,9 +6,17 @@
// Copyright @Radolyn, 2023 // Copyright @Radolyn, 2023
#include "telegram_helpers.h" #include "telegram_helpers.h"
#include <functional>
#include <QTimer>
#include "data/data_peer_id.h" #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()) for (auto& [index, account] : Core::App().domain().accounts())
{ {
@ -24,8 +32,31 @@ Main::Session* getSession(long userId)
return nullptr; return nullptr;
} }
PeerId dialogIdToPeerId(long dialogId) void dispatchToMainThread(std::function<void()> callback)
{ {
auto peerId = PeerId(); auto timer = new QTimer();
return peerId; 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<History*> 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)));
} }

View file

@ -7,9 +7,13 @@
#pragma once #pragma once
#include "ayu/sync/models.h"
#include "core/application.h" #include "core/application.h"
#include "main/main_account.h" #include "main/main_account.h"
#include "main/main_domain.h" #include "main/main_domain.h"
#include "main/main_session.h" #include "main/main_session.h"
Main::Session* getSession(long userId); Main::Session* getSession(ID userId);
void dispatchToMainThread(std::function<void()> callback);
not_null<History*> getHistoryFromDialogId(ID dialogId, Main::Session* session);