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 <thread>
#include "core/sandbox.h"
namespace AyuSync
{
std::optional<ayu_sync_controller> controller = std::nullopt;
@ -97,7 +99,7 @@ namespace AyuSync
if (type == "sync_force")
{
auto ev = p.template get<SyncForce>();
auto ev = p.get<SyncForce>();
onSyncForce(ev);
}
else if (type == "sync_batch")
@ -106,7 +108,7 @@ namespace AyuSync
}
else if (type == "sync_read")
{
auto ev = p.template get<SyncRead>();
auto ev = p.get<SyncRead>();
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));
}
});
}
}

View file

@ -5,18 +5,20 @@
#include <string>
#include <vector>
#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
{

View file

@ -6,9 +6,17 @@
// Copyright @Radolyn, 2023
#include "telegram_helpers.h"
#include <functional>
#include <QTimer>
#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<void()> 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<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
#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<void()> callback);
not_null<History*> getHistoryFromDialogId(ID dialogId, Main::Session* session);