implement last post id tracking
This commit is contained in:
parent
393df64bc8
commit
182ddffe41
36
manager.cpp
36
manager.cpp
@ -71,6 +71,10 @@ void RepostManager::on_clients_ready() {
|
||||
vkState.needRequest = false;
|
||||
mgr->collect_all_vk_posts([this](auto posts){
|
||||
spdlog::info("fetched all {} VK posts", posts.size());
|
||||
if (posts.size() > 0) {
|
||||
spdlog::info("last vk post id is now {}", posts[0].id);
|
||||
mgr->m_appState->vkLastPostId = posts[0].id;
|
||||
}
|
||||
vkState.ready = true;
|
||||
std::vector<AbstractPost> aposts = mgr->to_abstract_posts(posts);
|
||||
vkState.posts.reserve(vkState.posts.size() + aposts.size());
|
||||
@ -95,6 +99,11 @@ void RepostManager::on_clients_ready() {
|
||||
} else {
|
||||
tgState.needRequest = false;
|
||||
mgr->collect_all_tg_posts([this](auto posts){
|
||||
spdlog::info("fetched all {} TG posts", posts.size());
|
||||
if (posts.size() > 0) {
|
||||
spdlog::info("last telegram post id is now {}", posts[0]->id_);
|
||||
mgr->m_appState->tgLastPostId = posts[0]->id_;
|
||||
}
|
||||
tgState.ready = true;
|
||||
std::vector<AbstractPost> aposts = mgr->to_abstract_posts(posts);
|
||||
tgState.posts.reserve(tgState.posts.size() + aposts.size());
|
||||
@ -108,8 +117,13 @@ void RepostManager::on_clients_ready() {
|
||||
}
|
||||
void check_vk_posts(std::vector<vk::Post> posts) {
|
||||
spdlog::info("fetched {} VK posts", posts.size());
|
||||
long oldLastPostId = mgr->m_appState->vkLastPostId;
|
||||
if (posts.size() > 0) {
|
||||
spdlog::info("last vk post id is now {}", posts[0].id);
|
||||
mgr->m_appState->vkLastPostId = posts[0].id;
|
||||
}
|
||||
std::vector<AbstractPost> aposts = mgr->to_abstract_posts(posts);
|
||||
if (mgr->drop_posts_older_than(aposts, mgr->m_appState->vkLastPostId)) {
|
||||
if (mgr->drop_posts_older_than(aposts, oldLastPostId)) {
|
||||
spdlog::info("found last remembered VK post");
|
||||
vkState.ready = true;
|
||||
}
|
||||
@ -121,8 +135,14 @@ void RepostManager::on_clients_ready() {
|
||||
fetch();
|
||||
}
|
||||
void check_tg_posts(std::vector<td::tl::unique_ptr<td_api::message>> posts) {
|
||||
spdlog::info("fetched {} TG posts", posts.size());
|
||||
long oldLastPostId = mgr->m_appState->tgLastPostId;
|
||||
if (posts.size() > 0) {
|
||||
spdlog::info("last telegram post id is now {}", posts[0]->id_);
|
||||
mgr->m_appState->tgLastPostId = posts[0]->id_;
|
||||
}
|
||||
std::vector<AbstractPost> aposts = mgr->to_abstract_posts(posts);
|
||||
if (mgr->drop_posts_older_than(aposts, mgr->m_appState->tgLastPostId)) {
|
||||
if (mgr->drop_posts_older_than(aposts, oldLastPostId)) {
|
||||
spdlog::info("found last remembered TG post");
|
||||
tgState.ready = true;
|
||||
}
|
||||
@ -198,8 +218,11 @@ void RepostManager::collect_vk_posts_from__intermediate(int offset, int count, s
|
||||
for (auto i = chunk->posts.begin(), end = chunk->posts.end(); i != end; ++i) {
|
||||
intermediateResult->emplace_back(std::move(*i));
|
||||
}
|
||||
if (count > chunkSize)
|
||||
if (count > chunkSize) {
|
||||
collect_vk_posts_from__intermediate(offset + chunkSize, count - chunkSize, intermediateResult, callback);
|
||||
} else {
|
||||
callback(std::move(*intermediateResult.get()));
|
||||
}
|
||||
} else {
|
||||
spdlog::error("failed to get {} VK posts at offset {}: error {}", count, offset, err);
|
||||
}
|
||||
@ -232,8 +255,11 @@ void RepostManager::collect_tg_posts_from__intermediate(long from, int count, st
|
||||
oldestId = (*i)->id_;
|
||||
intermediateResult->emplace_back(std::move(*i));
|
||||
}
|
||||
if (count > chunkSize)
|
||||
if (count > chunkSize) {
|
||||
collect_tg_posts_from__intermediate(oldestId, count - chunkSize, intermediateResult, callback);
|
||||
} else {
|
||||
callback(std::move(*intermediateResult.get()));
|
||||
}
|
||||
} else {
|
||||
auto &err = (td_api::error&)*obj;
|
||||
spdlog::error("failed to get posts: {} {}", err.code_, err.message_);
|
||||
@ -277,6 +303,6 @@ std::vector<AbstractPost> RepostManager::to_abstract_posts(std::vector<td::tl::u
|
||||
void RepostManager::repost_all(std::vector<AbstractPost> posts) {
|
||||
for (auto &post : posts) {
|
||||
auto content = td_api::make_object<td_api::inputMessageText>(td_api::make_object<td_api::formattedText>(post.text + "\nDate: " + std::to_string(post.date), std::vector<td_api::object_ptr<td_api::textEntity>>()), nullptr, false);
|
||||
m_tg.send_query(td_api::make_object<td_api::sendMessage>(m_appConfig->tgDestinationId, 0, nullptr, nullptr, nullptr, std::move(content)), {});
|
||||
//m_tg.send_query(td_api::make_object<td_api::sendMessage>(m_appConfig->tgDestinationId, 0, nullptr, nullptr, nullptr, std::move(content)), {});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user