implement real-time reposting from tg
This commit is contained in:
parent
f472cd3b6c
commit
abb0d8a11b
32
manager.cpp
32
manager.cpp
@ -65,6 +65,11 @@ void RepostManager::start() {
|
||||
spdlog::info("destination chat loaded");
|
||||
if (update.chat_->id_ == m_appConfig->tgSourceId)
|
||||
spdlog::info("source chat loaded");
|
||||
} else if (obj.get_id() == td_api::updateNewMessage::ID) {
|
||||
auto &update = (td_api::updateNewMessage&)obj;
|
||||
if (update.message_->chat_id_ == m_appConfig->tgSourceId) {
|
||||
on_tg_message(update);
|
||||
}
|
||||
}
|
||||
});
|
||||
spdlog::info("starting Telegram authentication");
|
||||
@ -214,7 +219,7 @@ void RepostManager::on_new_posts(std::vector<AbstractPost> vkPosts, std::vector<
|
||||
int vkIdx = vkPosts.size() - 1;
|
||||
int tgIdx = tgPosts.size() - 1;
|
||||
for (int i = 0; i < totalSize; ++i) {
|
||||
if (tgIdx < 0 || vkPosts[vkIdx].date < tgPosts[tgIdx].date) {
|
||||
if (tgIdx < 0 || vkIdx >= 0 && vkPosts[vkIdx].date < tgPosts[tgIdx].date) {
|
||||
mergedPosts.emplace_back(std::move(vkPosts[vkIdx--]));
|
||||
} else {
|
||||
mergedPosts.emplace_back(std::move(tgPosts[tgIdx--]));
|
||||
@ -370,14 +375,20 @@ void RepostManager::repost_timer_callback(uv_timer_t *h) {
|
||||
}
|
||||
|
||||
void RepostManager::check_timer_callback(uv_timer_t *h) {
|
||||
spdlog::debug("vk recheck timer");
|
||||
auto self = reinterpret_cast<RepostManager*>(h->data);
|
||||
self->recheck_vk_posts({});
|
||||
}
|
||||
|
||||
void RepostManager::recheck_vk_posts(std::function<void()> onDone) {
|
||||
spdlog::info("checking VK posts");
|
||||
NewPostFetcher *f = new NewPostFetcher(self, true, false);
|
||||
f->onDone = [self, f](std::vector<AbstractPost> &&vkPosts, std::vector<AbstractPost> &&tgPosts){
|
||||
delete f;
|
||||
NewPostFetcher *f = new NewPostFetcher(this, true, false);
|
||||
f->onDone = [this, f, onDone](std::vector<AbstractPost> &&vkPosts, std::vector<AbstractPost> &&tgPosts){
|
||||
spdlog::info("checked VK posts");
|
||||
self->on_new_posts(vkPosts, tgPosts);
|
||||
this->on_new_posts(vkPosts, tgPosts);
|
||||
if (onDone)
|
||||
onDone();
|
||||
delete f;
|
||||
};
|
||||
f->onError = [f](){
|
||||
delete f;
|
||||
@ -389,7 +400,6 @@ void RepostManager::check_timer_callback(uv_timer_t *h) {
|
||||
void RepostManager::repost(AbstractPost &post) {
|
||||
spdlog::debug("reposting (post length {})", post.text.length());
|
||||
std::string_view signature = posts::add_signature(post, m_appConfig);
|
||||
spdlog::debug(post.text);
|
||||
int signatureStart = post.text.length() - signature.length();
|
||||
int signatureLength = signature.length();
|
||||
spdlog::debug("post length {}, signature start {}, signature length {}", post.text.length(), signatureStart, signatureLength);
|
||||
@ -409,3 +419,13 @@ void RepostManager::repost(AbstractPost &post) {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void RepostManager::on_tg_message(td_api::updateNewMessage &update) {
|
||||
spdlog::info("received message from Telegram");
|
||||
uv_timer_stop(m_checkTimer);
|
||||
std::vector<td::tl::unique_ptr<td_api::message>> v;
|
||||
v.push_back(std::move(update.message_));
|
||||
recheck_vk_posts([this, post = to_abstract_posts(v)](){
|
||||
on_new_posts({}, post);
|
||||
});
|
||||
}
|
@ -46,6 +46,8 @@ namespace manager {
|
||||
void on_clients_ready();
|
||||
void load_more_telegram_chats();
|
||||
void on_new_posts(std::vector<AbstractPost> vkPosts, std::vector<AbstractPost> tgPosts);
|
||||
void on_tg_message(td_api::updateNewMessage &update);
|
||||
void recheck_vk_posts(std::function<void()> onDone);
|
||||
|
||||
void collect_all_vk_posts(std::function<void(std::vector<vk::Post>)> callback);
|
||||
void collect_all_tg_posts(std::function<void(std::vector<td::tl::unique_ptr<td_api::message>>)> callback);
|
||||
|
Loading…
Reference in New Issue
Block a user