feat: progress AyuSync implementation

This commit is contained in:
ZavaruKitsu 2023-07-09 17:28:52 +00:00
parent 0813121f58
commit 3286eacf75
7 changed files with 110 additions and 3 deletions

View file

@ -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

View file

@ -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));

View file

@ -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 <QString>
#include <thread>
@ -73,6 +78,39 @@ namespace AyuSync {
auto userId = p["userId"].get<long>();
auto type = p["type"].get<std::string>();
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<SyncForce>();
onSyncForce(ev);
} else if (type == "sync_batch") {
onSyncBatch(p);
} else if (type == "sync_read") {
auto ev = p.template get<SyncRead>();
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);
}
}
}

View file

@ -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();

View file

@ -4,15 +4,18 @@
#include <vector>
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<SyncEvent> 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)

View file

@ -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;
}

View file

@ -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);