Add MessagesManager::check_pts_update.

This commit is contained in:
levlam 2021-01-11 23:31:25 +03:00
parent cdbed1844a
commit efec8787d8
2 changed files with 40 additions and 53 deletions

View File

@ -6087,7 +6087,7 @@ bool MessagesManager::is_allowed_useless_update(const tl_object_ptr<telegram_api
return false;
}
bool MessagesManager::check_update_dialog_id(const tl_object_ptr<telegram_api::Update> &update, DialogId dialog_id) {
bool MessagesManager::check_pts_update_dialog_id(DialogId dialog_id) {
switch (dialog_id.get_type()) {
case DialogType::User:
case DialogType::Chat:
@ -6095,7 +6095,6 @@ bool MessagesManager::check_update_dialog_id(const tl_object_ptr<telegram_api::U
case DialogType::Channel:
case DialogType::SecretChat:
case DialogType::None:
LOG(ERROR) << "Receive update in wrong " << dialog_id << ": " << oneline(to_string(update));
return false;
default:
UNREACHABLE();
@ -6160,6 +6159,39 @@ int32 MessagesManager::get_min_pending_pts() const {
return result;
}
bool MessagesManager::check_pts_update(const tl_object_ptr<telegram_api::Update> &update) {
CHECK(update != nullptr);
switch (update->get_id()) {
case dummyUpdate::ID:
case updateSentMessage::ID:
case telegram_api::updateReadMessagesContents::ID:
case telegram_api::updateDeleteMessages::ID:
return true;
case telegram_api::updateNewMessage::ID: {
auto update_new_message = static_cast<const telegram_api::updateNewMessage *>(update.get());
return check_pts_update_dialog_id(get_message_dialog_id(update_new_message->message_));
}
case telegram_api::updateReadHistoryInbox::ID: {
auto update_read_history_inbox = static_cast<const telegram_api::updateReadHistoryInbox *>(update.get());
return check_pts_update_dialog_id(DialogId(update_read_history_inbox->peer_));
}
case telegram_api::updateReadHistoryOutbox::ID: {
auto update_read_history_outbox = static_cast<const telegram_api::updateReadHistoryOutbox *>(update.get());
return check_pts_update_dialog_id(DialogId(update_read_history_outbox->peer_));
}
case telegram_api::updateEditMessage::ID: {
auto update_edit_message = static_cast<const telegram_api::updateEditMessage *>(update.get());
return check_pts_update_dialog_id(get_message_dialog_id(update_edit_message->message_));
}
case telegram_api::updatePinnedMessages::ID: {
auto update_pinned_messages = static_cast<const telegram_api::updatePinnedMessages *>(update.get());
return check_pts_update_dialog_id(DialogId(update_pinned_messages->peer_));
}
default:
return false;
}
}
void MessagesManager::add_pending_update(tl_object_ptr<telegram_api::Update> &&update, int32 new_pts, int32 pts_count,
bool force_apply, Promise<Unit> &&promise, const char *source) {
// do not try to run getDifference from this function
@ -6176,57 +6208,10 @@ void MessagesManager::add_pending_update(tl_object_ptr<telegram_api::Update> &&u
// TODO need to save all updates that can change result of running queries not associated with pts (for example
// getHistory) and apply them to result of this queries
switch (update->get_id()) {
case dummyUpdate::ID:
case updateSentMessage::ID:
case telegram_api::updateReadMessagesContents::ID:
case telegram_api::updateDeleteMessages::ID:
// nothing to check
break;
case telegram_api::updateNewMessage::ID: {
auto update_new_message = static_cast<const telegram_api::updateNewMessage *>(update.get());
DialogId dialog_id = get_message_dialog_id(update_new_message->message_);
if (!check_update_dialog_id(update, dialog_id)) {
if (!check_pts_update(update)) {
LOG(ERROR) << "Receive wrong pts update from " << source << ": " << oneline(to_string(update));
return promise.set_value(Unit());
}
break;
}
case telegram_api::updateReadHistoryInbox::ID: {
auto update_read_history_inbox = static_cast<const telegram_api::updateReadHistoryInbox *>(update.get());
auto dialog_id = DialogId(update_read_history_inbox->peer_);
if (!check_update_dialog_id(update, dialog_id)) {
return promise.set_value(Unit());
}
break;
}
case telegram_api::updateReadHistoryOutbox::ID: {
auto update_read_history_outbox = static_cast<const telegram_api::updateReadHistoryOutbox *>(update.get());
auto dialog_id = DialogId(update_read_history_outbox->peer_);
if (!check_update_dialog_id(update, dialog_id)) {
return promise.set_value(Unit());
}
break;
}
case telegram_api::updateEditMessage::ID: {
auto update_edit_message = static_cast<const telegram_api::updateEditMessage *>(update.get());
DialogId dialog_id = get_message_dialog_id(update_edit_message->message_);
if (!check_update_dialog_id(update, dialog_id)) {
return promise.set_value(Unit());
}
break;
}
case telegram_api::updatePinnedMessages::ID: {
auto update_pinned_messages = static_cast<const telegram_api::updatePinnedMessages *>(update.get());
auto dialog_id = DialogId(update_pinned_messages->peer_);
if (!check_update_dialog_id(update, dialog_id)) {
return promise.set_value(Unit());
}
break;
}
default:
LOG(ERROR) << "Receive unexpected update " << oneline(to_string(update)) << "from " << source;
return;
}
if (force_apply) {
CHECK(pending_pts_updates_.empty());

View File

@ -1826,7 +1826,9 @@ class MessagesManager : public Actor {
bool can_set_game_score(DialogId dialog_id, const Message *m) const;
bool check_update_dialog_id(const tl_object_ptr<telegram_api::Update> &update, DialogId dialog_id);
static bool check_pts_update_dialog_id(DialogId dialog_id);
static bool check_pts_update(const tl_object_ptr<telegram_api::Update> &update);
void process_update(tl_object_ptr<telegram_api::Update> &&update);