From 660acfef2262beb8eee83d32b554c3d4c316f259 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 4 Feb 2020 04:06:20 +0300 Subject: [PATCH] Allow outdated unacceptable updates to avoid getDifference after trying to apply every such pending update. GitOrigin-RevId: 61944ec2b4e81fa2ca61bee5887220964e0c9cbc --- td/telegram/MessagesManager.cpp | 11 +++++++++-- td/telegram/MessagesManager.h | 2 ++ td/telegram/UpdatesManager.cpp | 19 +++++++++++++------ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 75f562514..9a53fab1b 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -6127,7 +6127,7 @@ void MessagesManager::cancel_user_dialog_action(DialogId dialog_id, const Messag void MessagesManager::add_pending_channel_update(DialogId dialog_id, tl_object_ptr &&update, int32 new_pts, int32 pts_count, const char *source, - bool is_postponed_udpate) { + bool is_postponed_update) { LOG(INFO) << "Receive from " << source << " pending " << to_string(update); CHECK(update != nullptr); CHECK(dialog_id.get_type() == DialogType::Channel); @@ -6166,7 +6166,7 @@ void MessagesManager::add_pending_channel_update(DialogId dialog_id, tl_object_p } else { int32 old_pts = d->pts; if (new_pts <= old_pts) { // very old or unuseful update - if (new_pts < old_pts - 19999 && !is_postponed_udpate) { + if (new_pts < old_pts - 19999 && !is_postponed_update) { // restore channel pts after delete_first_messages LOG(ERROR) << "Restore pts in " << d->dialog_id << " from " << source << " after delete_first_messages from " << old_pts << " to " << new_pts << " is temporarily disabled, pts_count = " << pts_count @@ -6245,6 +6245,13 @@ void MessagesManager::add_pending_channel_update(DialogId dialog_id, tl_object_p set_channel_pts(d, new_pts, source); } +bool MessagesManager::is_old_channel_update(DialogId dialog_id, int32 new_pts) { + CHECK(dialog_id.get_type() == DialogType::Channel); + + const Dialog *d = get_dialog_force(dialog_id); + return new_pts <= (d == nullptr ? load_channel_pts(dialog_id) : d->pts); +} + void MessagesManager::set_get_difference_timeout(double timeout) { if (!pts_gap_timeout_.has_timeout()) { LOG(INFO) << "Gap in pts has found, current pts is " << td_->updates_manager_->get_pts(); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 28d4bd2cd..3a5cee1fc 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -663,6 +663,8 @@ class MessagesManager : public Actor { void add_pending_channel_update(DialogId dialog_id, tl_object_ptr &&update, int32 new_pts, int32 pts_count, const char *source, bool is_postponed_update = false); + bool is_old_channel_update(DialogId dialog_id, int32 new_pts); + bool is_update_about_username_change_received(DialogId dialog_id) const; void on_dialog_bots_updated(DialogId dialog_id, vector bot_user_ids); diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 0b89378f4..67246f7e9 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -1173,16 +1173,23 @@ void UpdatesManager::on_pending_updates(vectorget_id() != telegram_api::messageService::ID) { + if (message_ptr != nullptr) { auto dialog_id = td_->messages_manager_->get_message_dialog_id(*message_ptr); if (dialog_id.get_type() == DialogType::Channel) { - LOG(INFO) << "Replace update about new message with updateChannelTooLong in " << dialog_id; auto channel_id = dialog_id.get_channel_id(); if (td_->contacts_manager_->have_channel_force(channel_id)) { - update = telegram_api::make_object( - telegram_api::updateChannelTooLong::PTS_MASK, channel_id.get(), pts); - continue; + if (td_->messages_manager_->is_old_channel_update(dialog_id, pts)) { + // the update will be ignored anyway, so there is no reason to replace it or force get_difference + LOG(INFO) << "Allow an outdated unacceptable update from " << source; + continue; + } + if ((*message_ptr)->get_id() != telegram_api::messageService::ID) { + // don't replace service messages, because they can be about bot's kicking + LOG(INFO) << "Replace update about new message with updateChannelTooLong in " << dialog_id; + update = telegram_api::make_object( + telegram_api::updateChannelTooLong::PTS_MASK, channel_id.get(), pts); + continue; + } } } else { LOG(ERROR) << "Update is not from a channel: " << to_string(update);