94 lines
4.0 KiB
C++
94 lines
4.0 KiB
C++
#pragma once
|
|
|
|
#include "config.h"
|
|
#include "posts.h"
|
|
#include "state.h"
|
|
#include "td/tl/TlObject.h"
|
|
#include "tg.h"
|
|
#include "vk.h"
|
|
#include <functional>
|
|
#include <queue>
|
|
#include <vector>
|
|
|
|
namespace manager {
|
|
namespace td_api = td::td_api;
|
|
|
|
using posts::AbstractPost;
|
|
|
|
class RepostManager;
|
|
|
|
struct NewPostFetcher {
|
|
struct fetcher_state {
|
|
int sourceIndex;
|
|
bool ready = false;
|
|
bool needRequest = true;
|
|
long offset = 0, count = 3;
|
|
std::vector<AbstractPost> posts;
|
|
};
|
|
|
|
bool working = false;
|
|
RepostManager *mgr;
|
|
std::vector<fetcher_state> vkState, tgState;
|
|
std::function<void(std::vector<AbstractPost>&&)> onDone;
|
|
std::function<void()> onError;
|
|
|
|
inline NewPostFetcher(RepostManager *m) : mgr(m) {};
|
|
void fetch(bool fetchVk, bool fetchTg, decltype(onDone) onDone, decltype(onError) onError);
|
|
private:
|
|
void reset_state();
|
|
void continue_fetch();
|
|
void check_vk_posts(int index, std::vector<vk::Post> posts);
|
|
void check_tg_posts(int index, std::vector<td::tl::unique_ptr<td_api::message>> posts);
|
|
};
|
|
|
|
class RepostManager {
|
|
friend struct NewPostFetcher;
|
|
public:
|
|
RepostManager(uv_loop_t *eventLoop, tg::AuthCodeProvider tgCodeProvider, tg::PasswordProvider tgPasswordProvider, state::AppState *appState, config::AppConfig *config);
|
|
RepostManager(RepostManager&) = delete;
|
|
~RepostManager();
|
|
void start();
|
|
private:
|
|
void on_clients_ready();
|
|
void load_more_telegram_chats();
|
|
void on_new_posts(std::vector<AbstractPost> posts);
|
|
void on_tg_message(td_api::updateNewMessage &update);
|
|
bool recheck_vk_posts(std::function<void()> onDone);
|
|
|
|
void collect_all_vk_posts(const std::variant<long, std::string> wall, std::function<void(std::vector<vk::Post>)> callback);
|
|
void collect_all_tg_posts(long channel, std::function<void(std::vector<td::tl::unique_ptr<td_api::message>>)> callback);
|
|
void collect_last_vk_posts(const std::variant<long, std::string> wall, int count, std::function<void(std::vector<vk::Post>)> callback);
|
|
void collect_last_tg_posts(long channel, int count, std::function<void(std::vector<td::tl::unique_ptr<td_api::message>>)> callback);
|
|
void collect_vk_posts_from(const std::variant<long, std::string> wall, int offset, int count, std::function<void(std::vector<vk::Post>)> callback);
|
|
void collect_tg_posts_from(long channel, long from, int count, std::function<void(std::vector<td::tl::unique_ptr<td_api::message>>)> callback);
|
|
|
|
void collect_vk_posts_from__intermediate(const std::variant<long, std::string> wall, int offset, int count, std::shared_ptr<std::vector<vk::Post>> intermediateResult, std::function<void(std::vector<vk::Post>)> callback);
|
|
void collect_tg_posts_from__intermediate(long channel, long from, int count, std::shared_ptr<std::vector<td::tl::unique_ptr<td_api::message>>> intermediateResult, std::function<void(std::vector<td::tl::unique_ptr<td_api::message>>)> callback);
|
|
|
|
bool drop_posts_older_than(std::vector<AbstractPost> &posts, long lastPostId);
|
|
|
|
std::optional<AbstractPost> to_abstract_post(const vk::Post &post, int sourceIndex);
|
|
std::optional<AbstractPost> to_abstract_post(const td_api::message &post, int sourceIndex);
|
|
std::vector<AbstractPost> to_abstract_posts(std::vector<vk::Post> &posts, int sourceIndex);
|
|
std::vector<AbstractPost> to_abstract_posts(std::vector<td::tl::unique_ptr<td_api::message>> &posts, int sourceIndex);
|
|
|
|
void enqueue_for_repost(std::vector<AbstractPost> posts);
|
|
static void repost_timer_callback(uv_timer_t *h);
|
|
static void check_timer_callback(uv_timer_t *h);
|
|
void repost(AbstractPost &post);
|
|
|
|
state::AppState *m_appState;
|
|
config::AppConfig *m_appConfig;
|
|
vk::VKClient m_vk;
|
|
tg::TelegramClient m_tg;
|
|
NewPostFetcher m_fetcher;
|
|
std::queue<AbstractPost> m_repostQueue;
|
|
std::queue<AbstractPost> m_unprocessedTgPosts;
|
|
uv_timer_t *m_repostTimer = nullptr;
|
|
bool m_checkTimerStarted = false;
|
|
uv_timer_t *m_checkTimer = nullptr;
|
|
int m_nRequiredChats;
|
|
int m_nLoadedRequiredChats = 0;
|
|
};
|
|
}
|