diff --git a/td/telegram/CallbackQueriesManager.cpp b/td/telegram/CallbackQueriesManager.cpp index ad7ff82b0..d2827a313 100644 --- a/td/telegram/CallbackQueriesManager.cpp +++ b/td/telegram/CallbackQueriesManager.cpp @@ -243,7 +243,7 @@ int64 CallbackQueriesManager::send_callback_query(FullMessageId full_message_id, } auto dialog_id = full_message_id.get_dialog_id(); - td_->messages_manager_->have_dialog_force(dialog_id); + td_->messages_manager_->have_dialog_force(dialog_id, "send_callback_query"); if (!td_->messages_manager_->have_input_peer(dialog_id, AccessRights::Read)) { promise.set_error(Status::Error(5, "Can't access the chat")); return 0; diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index f89eda8fb..63f7bf469 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -6365,7 +6365,7 @@ void ContactsManager::set_channel_discussion_group(DialogId dialog_id, DialogId ChannelId broadcast_channel_id; telegram_api::object_ptr broadcast_input_channel; if (dialog_id.is_valid()) { - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "set_channel_discussion_group 1")) { return promise.set_error(Status::Error(400, "Chat not found")); } @@ -6395,7 +6395,7 @@ void ContactsManager::set_channel_discussion_group(DialogId dialog_id, DialogId ChannelId group_channel_id; telegram_api::object_ptr group_input_channel; if (discussion_dialog_id.is_valid()) { - if (!td_->messages_manager_->have_dialog_force(discussion_dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(discussion_dialog_id, "set_channel_discussion_group 2")) { return promise.set_error(Status::Error(400, "Discussion chat not found")); } if (discussion_dialog_id.get_type() != DialogType::Channel) { @@ -6435,7 +6435,7 @@ void ContactsManager::set_channel_location(DialogId dialog_id, const DialogLocat if (!dialog_id.is_valid()) { return promise.set_error(Status::Error(400, "Invalid chat identifier specified")); } - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "set_channel_location")) { return promise.set_error(Status::Error(400, "Chat not found")); } @@ -6467,7 +6467,7 @@ void ContactsManager::set_channel_slow_mode_delay(DialogId dialog_id, int32 slow if (!dialog_id.is_valid()) { return promise.set_error(Status::Error(400, "Invalid chat identifier specified")); } - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "set_channel_slow_mode_delay")) { return promise.set_error(Status::Error(400, "Chat not found")); } @@ -6495,7 +6495,7 @@ void ContactsManager::get_channel_statistics_dc_id(DialogId dialog_id, bool for_ if (!dialog_id.is_valid()) { return promise.set_error(Status::Error(400, "Invalid chat identifier specified")); } - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "get_channel_statistics_dc_id")) { return promise.set_error(Status::Error(400, "Chat not found")); } @@ -6716,7 +6716,7 @@ void ContactsManager::delete_channel(ChannelId channel_id, Promise &&promi } void ContactsManager::delete_dialog(DialogId dialog_id, Promise &&promise) { - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "delete_dialog")) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -7074,7 +7074,7 @@ td_api::object_ptr ContactsManager::get_can_ void ContactsManager::transfer_dialog_ownership(DialogId dialog_id, UserId user_id, const string &password, Promise &&promise) { - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "transfer_dialog_ownership")) { return promise.set_error(Status::Error(3, "Chat not found")); } if (!have_user_force(user_id)) { @@ -7126,7 +7126,7 @@ void ContactsManager::transfer_channel_ownership( } Status ContactsManager::can_manage_dialog_invite_links(DialogId dialog_id, bool creator_only) { - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "can_manage_dialog_invite_links")) { return Status::Error(3, "Chat not found"); } @@ -7866,7 +7866,7 @@ void ContactsManager::on_load_contacts_from_database(string value) { } void ContactsManager::on_get_contacts_finished(size_t expected_contact_count) { - LOG(INFO) << "Finished to get " << contacts_hints_.size() << " contacts out of " << expected_contact_count; + LOG(INFO) << "Finished to get " << contacts_hints_.size() << " contacts out of expected " << expected_contact_count; are_contacts_loaded_ = true; auto promises = std::move(load_contacts_queries_); load_contacts_queries_.clear(); @@ -9236,7 +9236,11 @@ void ContactsManager::on_load_user_full_from_database(UserId user_id, string val Dependencies dependencies; dependencies.user_ids.insert(user_id); - resolve_dependencies_force(td_, dependencies, "user_full"); + if (!resolve_dependencies_force(td_, dependencies, "user_full")) { + users_full_.erase(user_id); + G()->td_db()->get_sqlite_pmc()->erase(get_user_full_database_key(user_id), Auto()); + return; + } if (user_full->need_phone_number_privacy_exception && is_user_contact(user_id)) { user_full->need_phone_number_privacy_exception = false; @@ -9423,7 +9427,11 @@ void ContactsManager::on_load_chat_full_from_database(ChatId chat_id, string val dependencies.user_ids.insert(participant.inviter_user_id); } dependencies.user_ids.insert(chat_full->invite_link.get_creator_user_id()); - resolve_dependencies_force(td_, dependencies, "chat_full"); + if (!resolve_dependencies_force(td_, dependencies, "chat_full")) { + chats_full_.erase(chat_id); + G()->td_db()->get_sqlite_pmc()->erase(get_chat_full_database_key(chat_id), Auto()); + return; + } for (auto &participant : chat_full->participants) { get_bot_info_force(participant.user_id); @@ -9527,7 +9535,11 @@ void ContactsManager::on_load_channel_full_from_database(ChannelId channel_id, s dependencies.chat_ids.insert(channel_full->migrated_from_chat_id); dependencies.user_ids.insert(channel_full->bot_user_ids.begin(), channel_full->bot_user_ids.end()); dependencies.user_ids.insert(channel_full->invite_link.get_creator_user_id()); - resolve_dependencies_force(td_, dependencies, "channel_full"); + if (!resolve_dependencies_force(td_, dependencies, "channel_full")) { + channels_full_.erase(channel_id); + G()->td_db()->get_sqlite_pmc()->erase(get_channel_full_database_key(channel_id), Auto()); + return; + } for (auto &user_id : channel_full->bot_user_ids) { get_bot_info_force(user_id); @@ -14384,7 +14396,7 @@ std::pair> ContactsManager::search_among_users(const vecto void ContactsManager::add_dialog_participant(DialogId dialog_id, UserId user_id, int32 forward_limit, Promise &&promise) { - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "add_dialog_participant")) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -14409,7 +14421,7 @@ void ContactsManager::add_dialog_participants(DialogId dialog_id, const vectormessages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "add_dialog_participants")) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -14432,7 +14444,7 @@ void ContactsManager::set_dialog_participant_status(DialogId dialog_id, UserId u const tl_object_ptr &chat_member_status, Promise &&promise) { auto status = get_dialog_participant_status(chat_member_status); - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "set_dialog_participant_status")) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -14453,7 +14465,7 @@ void ContactsManager::set_dialog_participant_status(DialogId dialog_id, UserId u void ContactsManager::ban_dialog_participant(DialogId dialog_id, UserId user_id, int32 banned_until_date, bool revoke_messages, Promise &&promise) { - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "ban_dialog_participant")) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -14477,7 +14489,7 @@ DialogParticipant ContactsManager::get_dialog_participant(DialogId dialog_id, Us bool force, Promise &&promise) { LOG(INFO) << "Receive GetChatMember request to get " << user_id << " in " << dialog_id << " with random_id " << random_id; - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "get_dialog_participant")) { promise.set_error(Status::Error(3, "Chat not found")); return DialogParticipant(); } @@ -14571,7 +14583,7 @@ void ContactsManager::search_dialog_participants(DialogId dialog_id, const strin Promise &&promise) { LOG(INFO) << "Receive searchChatMembers request to search for \"" << query << "\" in " << dialog_id << " with filter " << filter; - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "search_dialog_participants")) { return promise.set_error(Status::Error(3, "Chat not found")); } if (limit < 0) { @@ -14861,7 +14873,7 @@ void ContactsManager::do_get_channel_participants(ChannelId channel_id, ChannelP vector ContactsManager::get_dialog_administrators(DialogId dialog_id, int left_tries, Promise &&promise) { LOG(INFO) << "Receive GetChatAdministrators request in " << dialog_id << " with " << left_tries << " left tries"; - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "get_dialog_administrators")) { promise.set_error(Status::Error(3, "Chat not found")); return {}; } diff --git a/td/telegram/Dependencies.cpp b/td/telegram/Dependencies.cpp index ca117eede..e7d58f5b3 100644 --- a/td/telegram/Dependencies.cpp +++ b/td/telegram/Dependencies.cpp @@ -51,38 +51,46 @@ void add_message_sender_dependencies(Dependencies &dependencies, DialogId dialog } } -void resolve_dependencies_force(Td *td, const Dependencies &dependencies, const char *source) { +bool resolve_dependencies_force(Td *td, const Dependencies &dependencies, const char *source) { + bool success = true; for (auto user_id : dependencies.user_ids) { if (user_id.is_valid() && !td->contacts_manager_->have_user_force(user_id)) { LOG(ERROR) << "Can't find " << user_id << " from " << source; + success = false; } } for (auto chat_id : dependencies.chat_ids) { if (chat_id.is_valid() && !td->contacts_manager_->have_chat_force(chat_id)) { LOG(ERROR) << "Can't find " << chat_id << " from " << source; + success = false; } } for (auto channel_id : dependencies.channel_ids) { if (channel_id.is_valid() && !td->contacts_manager_->have_channel_force(channel_id)) { LOG(ERROR) << "Can't find " << channel_id << " from " << source; + success = false; } } for (auto secret_chat_id : dependencies.secret_chat_ids) { if (secret_chat_id.is_valid() && !td->contacts_manager_->have_secret_chat_force(secret_chat_id)) { LOG(ERROR) << "Can't find " << secret_chat_id << " from " << source; + success = false; } } for (auto dialog_id : dependencies.dialog_ids) { - if (dialog_id.is_valid() && !td->messages_manager_->have_dialog_force(dialog_id)) { + if (dialog_id.is_valid() && !td->messages_manager_->have_dialog_force(dialog_id, source)) { LOG(ERROR) << "Can't find " << dialog_id << " from " << source; - td->messages_manager_->force_create_dialog(dialog_id, "resolve_dependencies_force"); + td->messages_manager_->force_create_dialog(dialog_id, "resolve_dependencies_force", true); + success = false; } } for (auto web_page_id : dependencies.web_page_ids) { if (web_page_id.is_valid()) { td->web_pages_manager_->have_web_page_force(web_page_id); + success = false; } } + return success; } } // namespace td diff --git a/td/telegram/Dependencies.h b/td/telegram/Dependencies.h index 4e1e5734d..711715b92 100644 --- a/td/telegram/Dependencies.h +++ b/td/telegram/Dependencies.h @@ -34,6 +34,6 @@ void add_dialog_dependencies(Dependencies &dependencies, DialogId dialog_id); void add_message_sender_dependencies(Dependencies &dependencies, DialogId dialog_id); -void resolve_dependencies_force(Td *td, const Dependencies &dependencies, const char *source); +bool resolve_dependencies_force(Td *td, const Dependencies &dependencies, const char *source); } // namespace td diff --git a/td/telegram/GroupCallManager.cpp b/td/telegram/GroupCallManager.cpp index 6fe233338..932d57e8f 100644 --- a/td/telegram/GroupCallManager.cpp +++ b/td/telegram/GroupCallManager.cpp @@ -790,7 +790,8 @@ void GroupCallManager::on_update_group_call_participant_order_timeout(GroupCallI auto new_order = get_real_participant_order(can_manage, participant, participants); if (new_order != participant.order) { participant.order = new_order; - send_update_group_call_participant(input_group_call_id, participant); + send_update_group_call_participant(input_group_call_id, participant, + "on_update_group_call_participant_order_timeout"); } } update_group_call_participant_order_timeout_.set_timeout_in(group_call_id.get(), @@ -932,7 +933,7 @@ DialogId GroupCallManager::get_group_call_participant_id( } case td_api::messageSenderChat::ID: { DialogId dialog_id(static_cast(message_sender.get())->chat_id_); - if (td_->messages_manager_->have_dialog_force(dialog_id)) { + if (td_->messages_manager_->have_dialog_force(dialog_id, "get_group_call_participant_id")) { return dialog_id; } break; @@ -1061,7 +1062,7 @@ void GroupCallManager::get_group_call_join_as(DialogId dialog_id, if (!dialog_id.is_valid()) { return promise.set_error(Status::Error(400, "Invalid chat identifier specified")); } - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "get_group_call_join_as")) { return promise.set_error(Status::Error(400, "Chat not found")); } if (!td_->messages_manager_->have_input_peer(dialog_id, AccessRights::Read)) { @@ -1075,7 +1076,7 @@ void GroupCallManager::create_voice_chat(DialogId dialog_id, Promisemessages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "create_voice_chat")) { return promise.set_error(Status::Error(400, "Chat not found")); } if (!td_->messages_manager_->have_input_peer(dialog_id, AccessRights::Read)) { @@ -1355,6 +1356,9 @@ void GroupCallManager::on_get_group_call_participants( } auto real_participant_count = participants->count_; + if (!group_call->is_joined) { + real_participant_count++; + } if (is_empty) { auto known_participant_count = participants_it != group_call_participants_.end() ? static_cast(participants_it->second->participants.size()) @@ -1584,6 +1588,7 @@ bool GroupCallManager::process_pending_group_call_participant_updates(InputGroup } }; + bool need_update = false; while (!pending_version_updates.empty()) { process_mute_updates(); @@ -1618,11 +1623,13 @@ bool GroupCallManager::process_pending_group_call_participant_updates(InputGroup diff += process_group_call_participant(input_group_call_id, std::move(participant)); } pending_version_updates.erase(it); - } else if (!group_call->syncing_participants) { + } else { // found a gap - LOG(INFO) << "Receive " << participants.size() << " group call participant updates with version " << version - << ", but current version is " << group_call->version; - sync_participants_timeout_.add_timeout_in(group_call->group_call_id.get(), 1.0); + if (!group_call->syncing_participants) { + LOG(INFO) << "Receive " << participants.size() << " group call participant updates with version " << version + << ", but current version is " << group_call->version; + sync_participants_timeout_.add_timeout_in(group_call->group_call_id.get(), 1.0); + } break; } } @@ -1637,10 +1644,10 @@ bool GroupCallManager::process_pending_group_call_participant_updates(InputGroup sync_participants_timeout_.cancel_timeout(group_call->group_call_id.get()); } - bool need_update = set_group_call_participant_count(group_call, group_call->participant_count + diff, - "process_pending_group_call_participant_updates"); + need_update |= set_group_call_participant_count(group_call, group_call->participant_count + diff, + "process_pending_group_call_participant_updates"); if (is_left && group_call->is_joined) { - on_group_call_left_impl(group_call, need_rejoin); + on_group_call_left_impl(group_call, need_rejoin, "process_pending_group_call_participant_updates"); need_update = true; } if (need_update) { @@ -1706,13 +1713,19 @@ GroupCallParticipantOrder GroupCallManager::get_real_participant_order( if (participant.is_self) { return participants->min_order; } + if (real_order.is_valid()) { + LOG(DEBUG) << "Order " << real_order << " of " << participant.dialog_id << " is less than last known order " + << participants->min_order; + } return GroupCallParticipantOrder(); } void GroupCallManager::process_group_call_participants( InputGroupCallId input_group_call_id, vector> &&participants, int32 version, const string &offset, bool is_load, bool is_sync) { - if (offset.empty() && is_load && !participants.empty() && participants[0]->self_) { + // if receive exactly one participant, then the current user is the only participant + // there are no reasons to process it independently + if (offset.empty() && is_load && participants.size() >= 2 && participants[0]->self_) { GroupCallParticipant participant(participants[0], version); if (participant.is_valid()) { process_my_group_call_participant(input_group_call_id, std::move(participant)); @@ -1787,7 +1800,6 @@ void GroupCallManager::process_group_call_participants( for (auto participant_it = group_participants.begin(); participant_it != group_participants.end();) { auto &participant = *participant_it; if (old_participant_dialog_ids.count(participant.dialog_id) == 0) { - CHECK(!participant.order.is_valid() || participant.order >= min_order); ++participant_it; continue; } @@ -1795,7 +1807,8 @@ void GroupCallManager::process_group_call_participants( if (participant.is_self) { if (participant.order != min_order) { participant.order = min_order; - send_update_group_call_participant(input_group_call_id, participant); + send_update_group_call_participant(input_group_call_id, participant, + "process_group_call_participants self"); } ++participant_it; continue; @@ -1805,7 +1818,7 @@ void GroupCallManager::process_group_call_participants( if (participant.order.is_valid()) { CHECK(participant.order >= participants_it->second->min_order); participant.order = GroupCallParticipantOrder(); - send_update_group_call_participant(input_group_call_id, participant); + send_update_group_call_participant(input_group_call_id, participant, "process_group_call_participants sync"); } on_remove_group_call_participant(input_group_call_id, participant.dialog_id); participant_it = group_participants.erase(participant_it); @@ -1829,7 +1842,8 @@ void GroupCallManager::process_group_call_participants( if (old_min_order > real_order && real_order >= min_order) { CHECK(!participant.order.is_valid() || participant.is_self); participant.order = real_order; - send_update_group_call_participant(input_group_call_id, participant); + send_update_group_call_participant(input_group_call_id, participant, + "process_group_call_participants load"); } } @@ -1858,7 +1872,8 @@ void GroupCallManager::update_group_call_participants_can_be_muted(InputGroupCal for (auto &participant : participants->participants) { if (update_group_call_participant_can_be_muted(can_manage, participants, participant) && participant.order.is_valid()) { - send_update_group_call_participant(input_group_call_id, participant); + send_update_group_call_participant(input_group_call_id, participant, + "update_group_call_participants_can_be_muted"); } } } @@ -1900,7 +1915,7 @@ int GroupCallManager::process_group_call_participant(InputGroupCallId input_grou auto can_self_unmute = !participant.get_is_muted_by_admin(); if (can_self_unmute != group_call->can_self_unmute) { group_call->can_self_unmute = can_self_unmute; - send_update_group_call(group_call, "process_group_call_participant"); + send_update_group_call(group_call, "process_group_call_participant 1"); } } } @@ -1913,7 +1928,7 @@ int GroupCallManager::process_group_call_participant(InputGroupCallId input_grou if (participant.joined_date == 0) { LOG(INFO) << "Remove " << old_participant; if (old_participant.order.is_valid()) { - send_update_group_call_participant(input_group_call_id, participant); + send_update_group_call_participant(input_group_call_id, participant, "process_group_call_participant remove"); } on_remove_group_call_participant(input_group_call_id, old_participant.dialog_id); remove_recent_group_call_speaker(input_group_call_id, old_participant.dialog_id); @@ -1939,7 +1954,7 @@ int GroupCallManager::process_group_call_participant(InputGroupCallId input_grou LOG(INFO) << "Edit " << old_participant << " to " << participant; if (old_participant != participant && (old_participant.order.is_valid() || participant.order.is_valid())) { - send_update_group_call_participant(input_group_call_id, participant); + send_update_group_call_participant(input_group_call_id, participant, "process_group_call_participant edit"); } on_participant_speaking_in_group_call(input_group_call_id, participant); old_participant = std::move(participant); @@ -1955,17 +1970,25 @@ int GroupCallManager::process_group_call_participant(InputGroupCallId input_grou CHECK(!participant.is_min); int diff = participant.is_just_joined ? 1 : 0; + participant.order = get_real_participant_order(can_manage, participant, participants); if (participant.is_just_joined) { LOG(INFO) << "Add new " << participant; } else { LOG(INFO) << "Receive new " << participant; } - participant.order = get_real_participant_order(can_manage, participant, participants); participant.is_just_joined = false; update_group_call_participant_can_be_muted(can_manage, participants, participant); participants->participants.push_back(std::move(participant)); if (participants->participants.back().order.is_valid()) { - send_update_group_call_participant(input_group_call_id, participants->participants.back()); + send_update_group_call_participant(input_group_call_id, participants->participants.back(), + "process_group_call_participant add"); + } else { + auto *group_call = get_group_call(input_group_call_id); + CHECK(group_call != nullptr && group_call->is_inited); + if (group_call->loaded_all_participants) { + group_call->loaded_all_participants = false; + send_update_group_call(group_call, "process_group_call_participant 2"); + } } on_add_group_call_participant(input_group_call_id, participants->participants.back().dialog_id); on_participant_speaking_in_group_call(input_group_call_id, participants->participants.back()); @@ -2003,7 +2026,7 @@ void GroupCallManager::on_update_dialog_about(DialogId dialog_id, const string & if ((from_server || participant->is_fake) && participant->about != about) { participant->about = about; if (participant->order.is_valid()) { - send_update_group_call_participant(input_group_call_id, *participant); + send_update_group_call_participant(input_group_call_id, *participant, "on_update_dialog_about"); } } } @@ -2134,7 +2157,7 @@ void GroupCallManager::join_group_call(GroupCallId group_call_id, DialogId as_di have_as_dialog_id = false; } } else { - if (!td_->messages_manager_->have_dialog_force(as_dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(as_dialog_id, "join_group_call")) { return promise.set_error(Status::Error(400, "Alias chat not found")); } } @@ -2946,7 +2969,7 @@ void GroupCallManager::toggle_group_call_participant_is_muted(GroupCallId group_ participant->pending_is_muted_generation = ++toggle_is_muted_generation_; if (participant->order.is_valid()) { - send_update_group_call_participant(input_group_call_id, *participant); + send_update_group_call_participant(input_group_call_id, *participant, "toggle_group_call_participant_is_muted"); } auto query_promise = PromiseCreator::lambda([actor_id = actor_id(this), input_group_call_id, dialog_id, @@ -2986,7 +3009,8 @@ void GroupCallManager::on_toggle_group_call_participant_is_muted(InputGroupCallI participant->server_is_muted_locally != participant->pending_is_muted_locally) { LOG(ERROR) << "Failed to mute/unmute " << dialog_id << " in " << input_group_call_id; if (participant->order.is_valid()) { - send_update_group_call_participant(input_group_call_id, *participant); + send_update_group_call_participant(input_group_call_id, *participant, + "on_toggle_group_call_participant_is_muted"); } } promise.set_value(Unit()); @@ -3036,7 +3060,7 @@ void GroupCallManager::set_group_call_participant_volume_level(GroupCallId group participant->pending_volume_level = volume_level; participant->pending_volume_level_generation = ++set_volume_level_generation_; if (participant->order.is_valid()) { - send_update_group_call_participant(input_group_call_id, *participant); + send_update_group_call_participant(input_group_call_id, *participant, "set_group_call_participant_volume_level"); } auto query_promise = PromiseCreator::lambda([actor_id = actor_id(this), input_group_call_id, dialog_id, @@ -3074,7 +3098,8 @@ void GroupCallManager::on_set_group_call_participant_volume_level(InputGroupCall LOG(ERROR) << "Failed to set volume level of " << dialog_id << " in " << input_group_call_id; participant->pending_volume_level = 0; if (participant->order.is_valid()) { - send_update_group_call_participant(input_group_call_id, *participant); + send_update_group_call_participant(input_group_call_id, *participant, + "on_set_group_call_participant_volume_level"); } } else { participant->pending_volume_level = 0; @@ -3131,7 +3156,8 @@ void GroupCallManager::toggle_group_call_participant_is_hand_raised(GroupCallId participant->pending_is_hand_raised = is_hand_raised; participant->pending_is_hand_raised_generation = ++toggle_is_hand_raised_generation_; if (participant->order.is_valid()) { - send_update_group_call_participant(input_group_call_id, *participant); + send_update_group_call_participant(input_group_call_id, *participant, + "toggle_group_call_participant_is_hand_raised"); } auto query_promise = PromiseCreator::lambda([actor_id = actor_id(this), input_group_call_id, dialog_id, @@ -3169,7 +3195,8 @@ void GroupCallManager::on_toggle_group_call_participant_is_hand_raised(InputGrou if (participant->get_is_hand_raised() != participant->pending_is_hand_raised) { LOG(ERROR) << "Failed to change raised hand state for " << dialog_id << " in " << input_group_call_id; if (participant->order.is_valid()) { - send_update_group_call_participant(input_group_call_id, *participant); + send_update_group_call_participant(input_group_call_id, *participant, + "on_toggle_group_call_participant_is_hand_raised"); } } promise.set_value(Unit()); @@ -3199,6 +3226,10 @@ void GroupCallManager::load_group_call_participants(GroupCallId group_call_id, i CHECK(participants_it->second != nullptr); next_offset = participants_it->second->next_offset; } + if (limit == 1 && next_offset.empty()) { + // prevent removing self as the first user and deducing that there are no more participants + limit = 2; + } td_->create_handler(std::move(promise)) ->send(input_group_call_id, std::move(next_offset), limit); } @@ -3254,12 +3285,12 @@ void GroupCallManager::on_group_call_left(InputGroupCallId input_group_call_id, return; } if (group_call->is_joined && group_call->audio_source == audio_source) { - on_group_call_left_impl(group_call, need_rejoin); + on_group_call_left_impl(group_call, need_rejoin, "on_group_call_left"); send_update_group_call(group_call, "on_group_call_left"); } } -void GroupCallManager::on_group_call_left_impl(GroupCall *group_call, bool need_rejoin) { +void GroupCallManager::on_group_call_left_impl(GroupCall *group_call, bool need_rejoin, const char *source) { if (!(group_call != nullptr && group_call->is_inited && group_call->is_joined)) { return; } @@ -3342,7 +3373,7 @@ void GroupCallManager::try_clear_group_call_participants(InputGroupCallId input_ if (participant.order.is_valid()) { CHECK(participant.order >= participants->min_order); participant.order = GroupCallParticipantOrder(); - send_update_group_call_participant(input_group_call_id, participant); + send_update_group_call_participant(input_group_call_id, participant, "try_clear_group_call_participants"); } on_remove_group_call_participant(input_group_call_id, participant.dialog_id); } @@ -3419,6 +3450,10 @@ InputGroupCallId GroupCallManager::update_group_call(const tl_object_ptrdialog_id.is_valid()) { group_call->dialog_id = dialog_id; } + if (call.is_active && join_params.empty() && !group_call->is_joined && + (group_call->need_rejoin || is_group_call_being_joined(input_group_call_id))) { + call.participant_count++; + } LOG(INFO) << "Update " << call.group_call_id << " with " << group_call->participant_count << " participants and version " << group_call->version; if (!group_call->is_inited) { @@ -3713,7 +3748,8 @@ DialogId GroupCallManager::set_group_call_participant_is_speaking_by_source(Inpu bool can_manage = can_manage_group_call(input_group_call_id); participant.order = get_real_participant_order(can_manage, participant, participants_it->second.get()); if (participant.order.is_valid()) { - send_update_group_call_participant(input_group_call_id, participant); + send_update_group_call_participant(input_group_call_id, participant, + "set_group_call_participant_is_speaking_by_source"); } } @@ -3738,6 +3774,7 @@ bool GroupCallManager::set_group_call_participant_count(GroupCall *group_call, i count = 0; } + bool result = false; auto input_group_call_id = get_input_group_call_id(group_call->group_call_id).ok(); if (need_group_call_participants(input_group_call_id, group_call)) { auto known_participant_count = @@ -3750,12 +3787,17 @@ bool GroupCallManager::set_group_call_participant_count(GroupCall *group_call, i } count = known_participant_count; } else if (group_call->loaded_all_participants && count > known_participant_count) { - count = known_participant_count; + if (group_call->joined_date_asc) { + group_call->loaded_all_participants = false; + result = true; + } else { + count = known_participant_count; + } } } if (group_call->participant_count == count) { - return false; + return result; } group_call->participant_count = count; @@ -3867,15 +3909,16 @@ void GroupCallManager::send_update_group_call(const GroupCall *group_call, const } void GroupCallManager::send_update_group_call_participant(GroupCallId group_call_id, - const GroupCallParticipant &participant) { + const GroupCallParticipant &participant, const char *source) { if (G()->shared_config().get_option_boolean("disable_group_calls")) { return; } + LOG(INFO) << "Send update about " << participant << " in " << group_call_id << " from " << source; send_closure(G()->td(), &Td::send_update, get_update_group_call_participant_object(group_call_id, participant)); } void GroupCallManager::send_update_group_call_participant(InputGroupCallId input_group_call_id, - const GroupCallParticipant &participant) { + const GroupCallParticipant &participant, const char *source) { if (G()->shared_config().get_option_boolean("disable_group_calls")) { return; } @@ -3883,7 +3926,7 @@ void GroupCallManager::send_update_group_call_participant(InputGroupCallId input if(!(group_call != nullptr && group_call->is_inited)) { return; } - send_update_group_call_participant(group_call->group_call_id, participant); + send_update_group_call_participant(group_call->group_call_id, participant, source); } } // namespace td diff --git a/td/telegram/GroupCallManager.h b/td/telegram/GroupCallManager.h index bf7d51ad2..62284b59d 100644 --- a/td/telegram/GroupCallManager.h +++ b/td/telegram/GroupCallManager.h @@ -263,7 +263,7 @@ class GroupCallManager : public Actor { void on_group_call_left(InputGroupCallId input_group_call_id, int32 audio_source, bool need_rejoin); - void on_group_call_left_impl(GroupCall *group_call, bool need_rejoin); + void on_group_call_left_impl(GroupCall *group_call, bool need_rejoin, const char *source); InputGroupCallId update_group_call(const tl_object_ptr &group_call_ptr, DialogId dialog_id); @@ -303,10 +303,11 @@ class GroupCallManager : public Actor { void send_update_group_call(const GroupCall *group_call, const char *source); - void send_update_group_call_participant(GroupCallId group_call_id, const GroupCallParticipant &participant); + void send_update_group_call_participant(GroupCallId group_call_id, const GroupCallParticipant &participant, + const char *source); - void send_update_group_call_participant(InputGroupCallId input_group_call_id, - const GroupCallParticipant &participant); + void send_update_group_call_participant(InputGroupCallId input_group_call_id, const GroupCallParticipant &participant, + const char *source); Td *td_; ActorShared<> parent_; diff --git a/td/telegram/GroupCallParticipantOrder.cpp b/td/telegram/GroupCallParticipantOrder.cpp index 08486c441..6809d16d3 100644 --- a/td/telegram/GroupCallParticipantOrder.cpp +++ b/td/telegram/GroupCallParticipantOrder.cpp @@ -24,6 +24,9 @@ bool GroupCallParticipantOrder::is_valid() const { } string GroupCallParticipantOrder::get_group_call_participant_order_object() const { + if (!is_valid()) { + return string(); + } return PSTRING() << lpad0(to_string(active_date), 10) << lpad0(to_string(raise_hand_rating), 19) << lpad0(to_string(joined_date), 10); } diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 702a29b28..820ebde16 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -16,6 +16,7 @@ #include "td/telegram/ChatId.h" #include "td/telegram/Contact.h" #include "td/telegram/ContactsManager.h" +#include "td/telegram/Dependencies.h" #include "td/telegram/Document.h" #include "td/telegram/DocumentsManager.h" #include "td/telegram/DocumentsManager.hpp" @@ -437,7 +438,7 @@ class MessageChatSetTtl : public MessageContent { class MessageUnsupported : public MessageContent { public: - static constexpr int32 CURRENT_VERSION = 5; + static constexpr int32 CURRENT_VERSION = 6; int32 version = CURRENT_VERSION; MessageUnsupported() = default; diff --git a/td/telegram/MessageContent.h b/td/telegram/MessageContent.h index 398d2d4c4..179f2c9d6 100644 --- a/td/telegram/MessageContent.h +++ b/td/telegram/MessageContent.h @@ -6,7 +6,6 @@ // #pragma once -#include "td/telegram/Dependencies.h" #include "td/telegram/DialogId.h" #include "td/telegram/files/FileId.h" #include "td/telegram/FullMessageId.h" @@ -36,6 +35,7 @@ namespace td { +struct Dependencies; class Game; struct Photo; class Td; diff --git a/td/telegram/MessageContentType.h b/td/telegram/MessageContentType.h index 097bcd7ee..167ae39ce 100644 --- a/td/telegram/MessageContentType.h +++ b/td/telegram/MessageContentType.h @@ -13,6 +13,7 @@ namespace td { +// increase MessageUnsupported::CURRENT_VERSION each time a new message content type is added enum class MessageContentType : int32 { None = -1, Text, diff --git a/td/telegram/MessageEntity.cpp b/td/telegram/MessageEntity.cpp index 79556ac5b..cf3ebb349 100644 --- a/td/telegram/MessageEntity.cpp +++ b/td/telegram/MessageEntity.cpp @@ -7,6 +7,7 @@ #include "td/telegram/MessageEntity.h" #include "td/telegram/ContactsManager.h" +#include "td/telegram/Dependencies.h" #include "td/telegram/misc.h" #include "td/telegram/SecretChatActor.h" diff --git a/td/telegram/MessageEntity.h b/td/telegram/MessageEntity.h index b63127787..4a4dae5e3 100644 --- a/td/telegram/MessageEntity.h +++ b/td/telegram/MessageEntity.h @@ -6,7 +6,6 @@ // #pragma once -#include "td/telegram/Dependencies.h" #include "td/telegram/DialogId.h" #include "td/telegram/UserId.h" @@ -25,6 +24,7 @@ namespace td { class ContactsManager; +struct Dependencies; class MessageEntity { public: diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 916947ec7..0f839e102 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -10,6 +10,7 @@ #include "td/telegram/ChatId.h" #include "td/telegram/ConfigShared.h" #include "td/telegram/ContactsManager.h" +#include "td/telegram/Dependencies.h" #include "td/telegram/DialogDb.h" #include "td/telegram/DialogFilter.h" #include "td/telegram/DialogFilter.hpp" @@ -6869,7 +6870,7 @@ void MessagesManager::on_update_read_channel_messages_contents( DialogId dialog_id = DialogId(channel_id); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "on_update_read_channel_messages_contents"); if (d == nullptr) { LOG(INFO) << "Receive read channel messages contents update in unknown " << dialog_id; return; @@ -6883,7 +6884,7 @@ void MessagesManager::on_update_read_channel_messages_contents( void MessagesManager::on_update_read_message_comments(DialogId dialog_id, MessageId message_id, MessageId max_message_id, MessageId last_read_inbox_message_id, MessageId last_read_outbox_message_id) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "on_update_read_message_comments"); if (d == nullptr) { LOG(INFO) << "Ignore update of read message comments in unknown " << dialog_id << " in updateReadDiscussion"; return; @@ -6909,17 +6910,17 @@ void MessagesManager::on_update_channel_too_long(tl_object_ptr 0) { - d = add_dialog(dialog_id); + d = add_dialog(dialog_id, "on_update_channel_too_long 5"); if (d == nullptr) { LOG(ERROR) << "Unknown dialog " << dialog_id; return; } CHECK(d->pts == pts); - update_dialog_pos(d, "on_update_channel_too_long"); + update_dialog_pos(d, "on_update_channel_too_long 6"); } } @@ -7002,7 +7003,7 @@ void MessagesManager::update_message_interaction_info(FullMessageId full_message int32 forward_count, bool has_reply_info, tl_object_ptr &&reply_info) { auto dialog_id = full_message_id.get_dialog_id(); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "update_message_interaction_info"); if (d == nullptr) { return; } @@ -7263,7 +7264,7 @@ void MessagesManager::on_read_channel_inbox(ChannelId channel_id, MessageId max_ } DialogId dialog_id(channel_id); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, source); if (d == nullptr) { LOG(INFO) << "Receive read inbox in unknown " << dialog_id << " from " << source; return; @@ -7358,7 +7359,7 @@ void MessagesManager::on_update_delete_scheduled_messages(DialogId dialog_id, return; } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "on_update_delete_scheduled_messages"); if (d == nullptr) { LOG(INFO) << "Skip updateDeleteScheduledMessages in unknown " << dialog_id; return; @@ -7400,7 +7401,7 @@ void MessagesManager::on_user_dialog_action(DialogId dialog_id, MessageId top_th LOG(ERROR) << "Receive " << action << " in thread of " << top_thread_message_id << " in " << dialog_id; return; } - const Dialog *d = get_dialog_force(dialog_id); + const Dialog *d = get_dialog_force(dialog_id, "on_user_dialog_action"); if (d != nullptr && d->active_group_call_id.is_valid()) { auto group_call_id = td_->group_call_manager_->get_group_call_id(d->active_group_call_id, dialog_id); td_->group_call_manager_->on_user_speaking_in_group_call(group_call_id, typing_dialog_id, date); @@ -7548,7 +7549,7 @@ void MessagesManager::add_pending_channel_update(DialogId dialog_id, tl_object_p // 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 these queries - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "add_pending_channel_update"); if (d == nullptr) { auto pts = load_channel_pts(dialog_id); if (pts > 0) { @@ -7559,7 +7560,7 @@ void MessagesManager::add_pending_channel_update(DialogId dialog_id, tl_object_p return; } - d = add_dialog(dialog_id); + d = add_dialog(dialog_id, "add_pending_channel_update"); if (d == nullptr) { LOG(ERROR) << "Unknown dialog " << dialog_id; return; @@ -7670,7 +7671,7 @@ void MessagesManager::add_pending_channel_update(DialogId dialog_id, tl_object_p 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); + const Dialog *d = get_dialog_force(dialog_id, "is_old_channel_update"); return new_pts <= (d == nullptr ? load_channel_pts(dialog_id) : d->pts); } @@ -7790,7 +7791,7 @@ void MessagesManager::process_channel_update(tl_object_ptr } auto dialog_id = DialogId(channel_id); - delete_dialog_messages(dialog_id, message_ids, true, false); + delete_dialog_messages(dialog_id, message_ids, true, false, "updateDeleteChannelMessages"); break; } case telegram_api::updateEditChannelMessage::ID: { @@ -8320,7 +8321,7 @@ void MessagesManager::repair_dialog_action_bar(Dialog *d, const char *source) { } void MessagesManager::hide_dialog_action_bar(DialogId dialog_id) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "hide_dialog_action_bar"); if (d == nullptr) { return; } @@ -8349,7 +8350,7 @@ void MessagesManager::hide_dialog_action_bar(Dialog *d) { } void MessagesManager::remove_dialog_action_bar(DialogId dialog_id, Promise &&promise) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "remove_dialog_action_bar"); if (d == nullptr) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -8360,7 +8361,7 @@ void MessagesManager::remove_dialog_action_bar(DialogId dialog_id, Promise if (dialog_id.get_type() == DialogType::SecretChat) { dialog_id = DialogId(td_->contacts_manager_->get_secret_chat_user_id(dialog_id.get_secret_chat_id())); - d = get_dialog_force(dialog_id); + d = get_dialog_force(dialog_id, "remove_dialog_action_bar 2"); if (d == nullptr) { return promise.set_error(Status::Error(3, "Chat with the user not found")); } @@ -8486,7 +8487,7 @@ bool MessagesManager::can_report_dialog(DialogId dialog_id) const { void MessagesManager::report_dialog(DialogId dialog_id, const vector &message_ids, ReportReason &&reason, Promise &&promise) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "report_dialog"); if (d == nullptr) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -8502,7 +8503,7 @@ void MessagesManager::report_dialog(DialogId dialog_id, const vector // report from action bar if (dialog_id.get_type() == DialogType::SecretChat) { auto user_dialog_id = DialogId(td_->contacts_manager_->get_secret_chat_user_id(dialog_id.get_secret_chat_id())); - user_d = get_dialog_force(user_dialog_id); + user_d = get_dialog_force(user_dialog_id, "report_dialog 2"); if (user_d == nullptr) { return promise.set_error(Status::Error(3, "Chat with the user not found")); } @@ -8546,7 +8547,7 @@ void MessagesManager::report_dialog(DialogId dialog_id, const vector void MessagesManager::report_dialog_photo(DialogId dialog_id, FileId file_id, ReportReason &&reason, Promise &&promise) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "report_dialog_photo"); if (d == nullptr) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -8583,7 +8584,7 @@ void MessagesManager::on_get_peer_settings(DialogId dialog_id, need_phone_number_privacy_exception); } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "on_get_peer_settings"); if (d == nullptr) { return; } @@ -8748,7 +8749,7 @@ void MessagesManager::fix_dialog_action_bar(Dialog *d) { void MessagesManager::get_dialog_statistics_url(DialogId dialog_id, const string ¶meters, bool is_dark, Promise> &&promise) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "get_dialog_statistics_url"); if (d == nullptr) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -8764,7 +8765,7 @@ void MessagesManager::get_dialog_statistics_url(DialogId dialog_id, const string } Result MessagesManager::get_login_button_url(DialogId dialog_id, MessageId message_id, int32 button_id) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "get_login_button_url"); if (d == nullptr) { return Status::Error(3, "Chat not found"); } @@ -9471,7 +9472,7 @@ void MessagesManager::after_get_difference() { void MessagesManager::on_get_empty_messages(DialogId dialog_id, vector empty_message_ids) { if (!empty_message_ids.empty()) { - delete_dialog_messages(dialog_id, std::move(empty_message_ids), true, true); + delete_dialog_messages(dialog_id, std::move(empty_message_ids), true, true, "on_get_empty_messages"); } } @@ -10306,10 +10307,11 @@ void MessagesManager::delete_messages_from_updates(const vector &mess } void MessagesManager::delete_dialog_messages(DialogId dialog_id, const vector &message_ids, - bool from_updates, bool skip_update_for_not_found_messages) { - Dialog *d = get_dialog_force(dialog_id); + bool from_updates, bool skip_update_for_not_found_messages, + const char *source) { + Dialog *d = get_dialog_force(dialog_id, "delete_dialog_messages"); if (d == nullptr) { - LOG(INFO) << "Ignore deleteChannelMessages for unknown " << dialog_id; + LOG(INFO) << "Ignore deleteChannelMessages for unknown " << dialog_id << " from " << source; CHECK(from_updates); CHECK(dialog_id.get_type() == DialogType::Channel); return; @@ -10321,7 +10323,7 @@ void MessagesManager::delete_dialog_messages(DialogId dialog_id, const vectordeleted_message_ids.count(message_id) != 0; - auto message = delete_message(d, message_id, true, &need_update_dialog_pos, "delete_dialog_messages"); + auto message = delete_message(d, message_id, true, &need_update_dialog_pos, source); if (message == nullptr) { if (!skip_update_for_not_found_messages && !was_already_deleted) { deleted_message_ids.push_back(message_id.get()); @@ -10339,14 +10341,14 @@ void MessagesManager::delete_dialog_messages(DialogId dialog_id, const vector &message_ids, bool is_pin) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "update_dialog_pinned_messages_from_updates"); if (d == nullptr) { LOG(INFO) << "Ignore updatePinnedMessages for unknown " << dialog_id; return; @@ -10577,7 +10579,7 @@ void MessagesManager::delete_messages(DialogId dialog_id, const vectorclose_flag()) { return promise.set_error(Status::Error(500, "Request aborted")); } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "delete_messages"); if (d == nullptr) { return promise.set_error(Status::Error(6, "Chat is not found")); } @@ -10639,11 +10641,11 @@ void MessagesManager::delete_messages(DialogId dialog_id, const vector deleted_message_ids; for (auto message_id : message_ids) { - need_update_chat_has_scheduled_messages |= message_id.is_scheduled(); auto m = delete_message(d, message_id, true, &need_update_dialog_pos, DELETE_MESSAGE_USER_REQUEST_SOURCE); if (m == nullptr) { LOG(INFO) << "Can't delete " << message_id << " because it is not found"; } else { + need_update_chat_has_scheduled_messages |= m->message_id.is_scheduled(); deleted_message_ids.push_back(m->message_id.get()); } } @@ -10658,13 +10660,32 @@ void MessagesManager::delete_messages(DialogId dialog_id, const vector random_ids; - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "delete_messages_from_server"); if (d == nullptr) { LOG(ERROR) << "Unknown dialog " << dialog_id; break; @@ -10800,7 +10821,7 @@ void MessagesManager::delete_dialog_history(DialogId dialog_id, bool remove_from LOG(INFO) << "Receive deleteChatHistory request to delete all messages in " << dialog_id << ", remove_from_chat_list is " << remove_from_dialog_list << ", revoke is " << revoke; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "delete_dialog_history"); if (d == nullptr) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -11052,7 +11073,7 @@ void MessagesManager::delete_dialog_messages_from_user(DialogId dialog_id, UserI LOG(INFO) << "Receive deleteChatMessagesFromUser request to delete all messages in " << dialog_id << " from the user " << user_id; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "delete_dialog_messages_from_user"); if (d == nullptr) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -11302,7 +11323,7 @@ void MessagesManager::delete_all_dialog_messages(Dialog *d, bool remove_from_dia void MessagesManager::on_dialog_deleted(DialogId dialog_id, Promise &&promise) { LOG(INFO) << "Delete " << dialog_id; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "on_dialog_deleted"); if (d == nullptr) { return promise.set_value(Unit()); } @@ -11338,7 +11359,7 @@ void MessagesManager::read_all_dialog_mentions(DialogId dialog_id, Promise return promise.set_error(Status::Error(3, "Method is not available for bots")); } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "read_all_dialog_mentions"); if (d == nullptr) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -11610,7 +11631,7 @@ void MessagesManager::repair_channel_server_unread_count(Dialog *d) { } LOG(INFO) << "Reload ChannelFull for " << d->dialog_id << " to repair unread message counts"; - get_dialog_info_full(d->dialog_id, Promise()); + get_dialog_info_full(d->dialog_id, Auto()); } void MessagesManager::read_history_inbox(DialogId dialog_id, MessageId max_message_id, int32 unread_count, @@ -11621,7 +11642,7 @@ void MessagesManager::read_history_inbox(DialogId dialog_id, MessageId max_messa return; } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "read_history_inbox"); if (d != nullptr) { if (d->need_repair_channel_server_unread_count) { d->need_repair_channel_server_unread_count = false; @@ -11710,7 +11731,7 @@ void MessagesManager::read_history_outbox(DialogId dialog_id, MessageId max_mess return; } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "read_history_outbox"); if (d != nullptr) { if (!max_message_id.is_valid()) { LOG(ERROR) << "Receive read outbox update in " << dialog_id << " with " << max_message_id; @@ -12079,7 +12100,7 @@ void MessagesManager::set_dialog_max_unavailable_message_id(DialogId dialog_id, bool from_update, const char *source) { CHECK(!max_unavailable_message_id.is_scheduled()); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, source); if (d != nullptr) { if (max_unavailable_message_id > d->last_new_message_id && from_update) { if (d->last_new_message_id.is_valid()) { @@ -12421,7 +12442,7 @@ void MessagesManager::ttl_loop(double now) { } } for (auto &it : to_delete) { - delete_dialog_messages(it.first, it.second, false, true); + delete_dialog_messages(it.first, it.second, false, true, "ttl_loop"); } ttl_update_timeout(now); } @@ -12652,7 +12673,7 @@ void MessagesManager::init() { } else { DialogId dialog_id(r_dialog_id.ok()); - const Dialog *d = get_dialog_force(dialog_id); + const Dialog *d = get_dialog_force(dialog_id, "init"); if (d != nullptr) { LOG(INFO) << "Loaded sponsored " << dialog_id; add_sponsored_dialog(d, r_source.move_as_ok()); @@ -13025,8 +13046,8 @@ void MessagesManager::ttl_db_on_result(Resultserver_time()); } @@ -13092,7 +13113,7 @@ void MessagesManager::delete_secret_messages(SecretChatId secret_chat_id, std::v CHECK(secret_chat_id.is_valid()); DialogId dialog_id(secret_chat_id); - if (!have_dialog_force(dialog_id)) { + if (!have_dialog_force(dialog_id, "delete_secret_messages")) { LOG(ERROR) << "Ignore delete secret messages in unknown " << dialog_id; promise.set_value(Unit()); return; @@ -13132,7 +13153,7 @@ void MessagesManager::finish_delete_secret_messages(DialogId dialog_id, std::vec LOG(INFO) << "Skip deletion of service " << message_id; } } - delete_dialog_messages(dialog_id, to_delete_message_ids, true, false); + delete_dialog_messages(dialog_id, to_delete_message_ids, true, false, "finish_delete_secret_messages"); } void MessagesManager::delete_secret_chat_history(SecretChatId secret_chat_id, bool remove_from_dialog_list, @@ -13142,7 +13163,7 @@ void MessagesManager::delete_secret_chat_history(SecretChatId secret_chat_id, bo CHECK(!last_message_id.is_scheduled()); DialogId dialog_id(secret_chat_id); - if (!have_dialog_force(dialog_id)) { + if (!have_dialog_force(dialog_id, "delete_secret_chat_history")) { LOG(ERROR) << "Ignore delete history in unknown " << dialog_id; promise.set_value(Unit()); return; @@ -13178,7 +13199,7 @@ void MessagesManager::read_secret_chat_outbox(SecretChatId secret_chat_id, int32 return; } auto dialog_id = DialogId(secret_chat_id); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "read_secret_chat_outbox"); if (d == nullptr) { return; } @@ -13221,7 +13242,7 @@ void MessagesManager::read_secret_chat_outbox_inner(DialogId dialog_id, int32 up void MessagesManager::open_secret_message(SecretChatId secret_chat_id, int64 random_id, Promise<> promise) { promise.set_value(Unit()); // TODO: set after event is saved DialogId dialog_id(secret_chat_id); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "open_secret_message"); if (d == nullptr) { LOG(ERROR) << "Ignore opening secret chat message in unknown " << dialog_id; return; @@ -13244,7 +13265,7 @@ void MessagesManager::open_secret_message(SecretChatId secret_chat_id, int64 ran void MessagesManager::on_update_secret_chat_state(SecretChatId secret_chat_id, SecretChatState state) { if (state == SecretChatState::Closed && !td_->auth_manager_->is_bot()) { DialogId dialog_id(secret_chat_id); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "on_update_secret_chat_state"); if (d != nullptr) { if (d->new_secret_chat_notification_id.is_valid()) { remove_new_secret_chat_notification(d, true); @@ -13281,7 +13302,7 @@ void MessagesManager::on_get_secret_message(SecretChatId secret_chat_id, UserId message_info.random_id = message->random_id_; message_info.ttl = message->ttl_; - Dialog *d = get_dialog_force(message_info.dialog_id); + Dialog *d = get_dialog_force(message_info.dialog_id, "on_get_secret_message"); if (d == nullptr) { LOG(ERROR) << "Ignore secret message in unknown " << message_info.dialog_id; pending_secret_message->success_promise.set_error(Status::Error(500, "Chat not found")); @@ -13371,7 +13392,7 @@ void MessagesManager::on_secret_chat_screenshot_taken(SecretChatId secret_chat_i message_info.flags = MESSAGE_FLAG_HAS_FROM_ID; message_info.content = create_screenshot_taken_message_content(); - Dialog *d = get_dialog_force(message_info.dialog_id); + Dialog *d = get_dialog_force(message_info.dialog_id, "on_secret_chat_screenshot_taken"); if (d == nullptr) { LOG(ERROR) << "Ignore secret message in unknown " << message_info.dialog_id; pending_secret_message->success_promise.set_error(Status::Error(500, "Chat not found")); @@ -13405,7 +13426,7 @@ void MessagesManager::on_secret_chat_ttl_changed(SecretChatId secret_chat_id, Us message_info.flags = MESSAGE_FLAG_HAS_FROM_ID; message_info.content = create_chat_set_ttl_message_content(ttl); - Dialog *d = get_dialog_force(message_info.dialog_id); + Dialog *d = get_dialog_force(message_info.dialog_id, "on_secret_chat_ttl_changed"); if (d == nullptr) { LOG(ERROR) << "Ignore secret message in unknown " << message_info.dialog_id; pending_secret_message->success_promise.set_error(Status::Error(500, "Chat not found")); @@ -13711,14 +13732,14 @@ std::pair> MessagesManager::creat } int32 ttl_period = message_info.ttl_period; - if (ttl_period < 0 || message_id.is_scheduled()) { + if (ttl_period < 0 || (message_id.is_scheduled() && ttl_period != 0)) { LOG(ERROR) << "Wrong TTL period = " << ttl_period << " received in " << message_id << " in " << dialog_id; ttl_period = 0; } int32 ttl = message_info.ttl; bool is_content_secret = is_secret_message_content(ttl, content_type); // should be calculated before TTL is adjusted - if (ttl < 0 || message_id.is_scheduled()) { + if (ttl < 0 || (message_id.is_scheduled() && ttl != 0)) { LOG(ERROR) << "Wrong TTL = " << ttl << " received in " << message_id << " in " << dialog_id; ttl = 0; } else if (ttl > 0) { @@ -13926,10 +13947,7 @@ FullMessageId MessagesManager::on_get_message(MessageInfo &&message_info, bool f unique_ptr old_message = delete_message(d, old_message_id, false, &need_update_dialog_pos, "add sent message"); if (old_message == nullptr) { - // message has already been deleted by the user or sent to inaccessible channel - // don't need to send update to the user, because the message has already been deleted - LOG(INFO) << "Delete already deleted sent " << new_message->message_id << " from server"; - delete_message_from_server(dialog_id, new_message->message_id, true); + delete_sent_message_from_server(dialog_id, new_message->message_id); being_readded_message_id_ = FullMessageId(); return FullMessageId(); } @@ -14683,11 +14701,11 @@ void MessagesManager::on_get_dialogs(FolderId folder_id, vectorparameters().use_message_db) { // TODO load dialog from database, DialogLoader // send_closure_later(actor_id(this), &MessagesManager::load_dialog_from_database, dialog_id, @@ -15674,7 +15692,7 @@ bool MessagesManager::load_dialog(DialogId dialog_id, int left_tries, Promise> MessagesManager::get_dialogs(DialogListId dia vector input_dialog_ids; for (auto &input_dialog_id : filter->pinned_dialog_ids) { auto dialog_id = input_dialog_id.get_dialog_id(); - if (!have_dialog_force(dialog_id)) { + if (!have_dialog_force(dialog_id, "get_dialogs")) { if (dialog_id.get_type() == DialogType::SecretChat) { if (have_dialog_info_force(dialog_id)) { force_create_dialog(dialog_id, "get_dialogs"); @@ -15901,7 +15919,7 @@ std::pair> MessagesManager::get_dialogs(DialogListId dia for (auto &pinned_dialog : list.pinned_dialogs_) { if (offset < pinned_dialog) { auto dialog_id = pinned_dialog.get_dialog_id(); - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "get_dialogs"); if (d == nullptr) { LOG(ERROR) << "Failed to load pinned " << dialog_id << " from " << dialog_list_id; if (dialog_id.get_type() != DialogType::SecretChat) { @@ -16100,7 +16118,7 @@ void MessagesManager::on_get_dialogs_from_database(FolderId folder_id, int32 lim size_t dialogs_skipped = 0; for (auto &dialog : dialogs.dialogs) { - Dialog *d = on_load_dialog_from_database(DialogId(), std::move(dialog)); + Dialog *d = on_load_dialog_from_database(DialogId(), std::move(dialog), "on_get_dialogs_from_database"); if (d == nullptr) { dialogs_skipped++; continue; @@ -16804,7 +16822,7 @@ void MessagesManager::block_message_sender_from_replies(MessageId message_id, bo bool delete_all_messages, bool report_spam, Promise &&promise) { auto dialog_id = DialogId(ContactsManager::get_replies_bot_user_id()); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "block_message_sender_from_replies"); if (d == nullptr) { return promise.set_error(Status::Error(400, "Chat not found")); } @@ -17002,7 +17020,7 @@ const MessagesManager::Message *MessagesManager::get_message(FullMessageId full_ } MessagesManager::Message *MessagesManager::get_message_force(FullMessageId full_message_id, const char *source) { - Dialog *d = get_dialog_force(full_message_id.get_dialog_id()); + Dialog *d = get_dialog_force(full_message_id.get_dialog_id(), source); if (d == nullptr) { return nullptr; } @@ -17050,7 +17068,7 @@ void MessagesManager::get_message_force_from_server(Dialog *d, MessageId message } void MessagesManager::get_message(FullMessageId full_message_id, Promise &&promise) { - Dialog *d = get_dialog_force(full_message_id.get_dialog_id()); + Dialog *d = get_dialog_force(full_message_id.get_dialog_id(), "get_message"); if (d == nullptr) { return promise.set_error(Status::Error(6, "Chat not found")); } @@ -17061,7 +17079,7 @@ void MessagesManager::get_message(FullMessageId full_message_id, Promise & FullMessageId MessagesManager::get_replied_message(DialogId dialog_id, MessageId message_id, bool force, Promise &&promise) { LOG(INFO) << "Get replied message to " << message_id << " in " << dialog_id; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "get_replied_message"); if (d == nullptr) { promise.set_error(Status::Error(6, "Chat not found")); return FullMessageId(); @@ -17092,7 +17110,7 @@ FullMessageId MessagesManager::get_replied_message(DialogId dialog_id, MessageId } force_create_dialog(dialog_id, "get_replied_message"); - d = get_dialog_force(dialog_id); + d = get_dialog_force(dialog_id, "get_replied_message"); if (d == nullptr) { promise.set_error(Status::Error(500, "Chat with replied message not found")); return {}; @@ -17139,7 +17157,7 @@ Result MessagesManager::get_top_thread_full_message_id(DialogId d void MessagesManager::get_message_thread(DialogId dialog_id, MessageId message_id, Promise &&promise) { LOG(INFO) << "Get message thread from " << message_id << " in " << dialog_id; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "get_message_thread"); if (d == nullptr) { return promise.set_error(Status::Error(400, "Chat not found")); } @@ -17259,7 +17277,7 @@ void MessagesManager::on_get_discussion_message(DialogId dialog_id, MessageId me if (G()->close_flag()) { return promise.set_error(Status::Error(500, "Request aborted")); } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "on_get_discussion_message"); if (d == nullptr) { LOG(ERROR) << "Unknown dialog " << dialog_id; return; @@ -17295,10 +17313,10 @@ void MessagesManager::on_get_discussion_message(DialogId dialog_id, MessageId me } if (expected_dialog_id != dialog_id && m->reply_info.is_comment && !result.message_ids.empty() && m->linked_top_thread_message_id != result.message_ids.back()) { - auto linked_d = get_dialog_force(expected_dialog_id); + auto linked_d = get_dialog_force(expected_dialog_id, "on_get_discussion_message 2"); CHECK(linked_d != nullptr); auto linked_message_id = result.message_ids.back(); - Message *linked_m = get_message_force(linked_d, linked_message_id, "on_get_discussion_message"); + Message *linked_m = get_message_force(linked_d, linked_message_id, "on_get_discussion_message 3"); CHECK(linked_m != nullptr && linked_m->message_id.is_server()); if (linked_m->top_thread_message_id == linked_m->message_id && is_active_message_reply_info(expected_dialog_id, linked_m->reply_info)) { @@ -17402,7 +17420,7 @@ void MessagesManager::reload_dialog_info_full(DialogId dialog_id) { } MessageId MessagesManager::get_dialog_pinned_message(DialogId dialog_id, Promise &&promise) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "get_dialog_pinned_message"); if (d == nullptr) { promise.set_error(Status::Error(6, "Chat not found")); return MessageId(); @@ -17433,7 +17451,7 @@ MessageId MessagesManager::get_dialog_pinned_message(DialogId dialog_id, Promise void MessagesManager::get_callback_query_message(DialogId dialog_id, MessageId message_id, int64 callback_query_id, Promise &&promise) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "get_callback_query_message"); if (d == nullptr) { return promise.set_error(Status::Error(6, "Chat not found")); } @@ -17449,7 +17467,7 @@ void MessagesManager::get_callback_query_message(DialogId dialog_id, MessageId m } bool MessagesManager::get_messages(DialogId dialog_id, const vector &message_ids, Promise &&promise) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "get_messages"); if (d == nullptr) { promise.set_error(Status::Error(6, "Chat not found")); return false; @@ -17543,7 +17561,7 @@ void MessagesManager::get_messages_from_server(vector &&message_i for (auto &it : scheduled_message_ids) { auto dialog_id = it.first; - have_dialog_force(dialog_id); + have_dialog_force(dialog_id, "get_messages_from_server"); auto input_peer = get_input_peer(dialog_id, AccessRights::Read); if (input_peer == nullptr) { LOG(ERROR) << "Can't find info about " << dialog_id << " to get a message from it"; @@ -17587,7 +17605,7 @@ bool MessagesManager::is_message_edited_recently(FullMessageId full_message_id, Result> MessagesManager::get_message_link(FullMessageId full_message_id, bool for_group, bool for_comment) { auto dialog_id = full_message_id.get_dialog_id(); - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "get_message_link"); if (d == nullptr) { return Status::Error(400, "Chat not found"); } @@ -17680,7 +17698,7 @@ Result> MessagesManager::get_message_link(FullMessageId string MessagesManager::get_message_embedding_code(FullMessageId full_message_id, bool for_group, Promise &&promise) { auto dialog_id = full_message_id.get_dialog_id(); - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "get_message_embedding_code"); if (d == nullptr) { promise.set_error(Status::Error(400, "Chat not found")); return {}; @@ -17943,7 +17961,7 @@ void MessagesManager::on_get_message_link_dialog(MessageLinkInfo &&info, Promise force_create_dialog(dialog_id, "on_get_message_link_dialog", true); } } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "on_get_message_link_dialog"); if (d == nullptr) { return promise.set_error(Status::Error(500, "Chat not found")); } @@ -17992,7 +18010,7 @@ void MessagesManager::on_get_message_link_discussion_message(MessageLinkInfo &&i CHECK(comment_dialog_id.is_valid()); info.comment_dialog_id = comment_dialog_id; - Dialog *d = get_dialog_force(comment_dialog_id); + Dialog *d = get_dialog_force(comment_dialog_id, "on_get_message_link_discussion_message"); if (d == nullptr) { return promise.set_error(Status::Error(500, "Chat not found")); } @@ -18085,7 +18103,7 @@ Result> MessagesManager::create_dialog_filter(DialogFil if (!dialog_id.is_valid()) { return Status::Error(400, "Invalid chat identifier specified"); } - const Dialog *d = get_dialog_force(dialog_id); + const Dialog *d = get_dialog_force(dialog_id, "create_dialog_filter"); if (d == nullptr) { return Status::Error(400, "Chat not found"); } @@ -18727,7 +18745,7 @@ Status MessagesManager::delete_dialog_reply_markup(DialogId dialog_id, MessageId return Status::Error(6, "Invalid message identifier specified"); } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "delete_dialog_reply_markup"); if (d == nullptr) { return Status::Error(6, "Chat not found"); } @@ -18779,7 +18797,7 @@ Status MessagesManager::set_dialog_draft_message(DialogId dialog_id, MessageId t return Status::Error(6, "Bots can't change chat draft message"); } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "set_dialog_draft_message"); if (d == nullptr) { return Status::Error(6, "Chat not found"); } @@ -18920,7 +18938,7 @@ Status MessagesManager::toggle_dialog_is_pinned(DialogListId dialog_list_id, Dia return Status::Error(6, "Bots can't change chat pin state"); } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "toggle_dialog_is_pinned"); if (d == nullptr) { return Status::Error(6, "Chat not found"); } @@ -19057,7 +19075,7 @@ Status MessagesManager::set_pinned_dialogs(DialogListId dialog_list_id, vector(sender.get())->chat_id_); - if (!have_dialog_force(sender_dialog_id)) { + if (!have_dialog_force(sender_dialog_id, "toggle_message_sender_is_blocked")) { return Status::Error(400, "Sender chat not found"); } @@ -19326,7 +19344,7 @@ Status MessagesManager::toggle_message_sender_is_blocked(const td_api::object_pt return Status::Error(5, is_blocked ? Slice("Can't block self") : Slice("Can't unblock self")); } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "toggle_message_sender_is_blocked"); if (d != nullptr) { if (is_blocked == d->is_blocked) { return Status::OK(); @@ -19388,7 +19406,7 @@ void MessagesManager::toggle_dialog_is_blocked_on_server(DialogId dialog_id, boo Status MessagesManager::toggle_dialog_silent_send_message(DialogId dialog_id, bool silent_send_message) { CHECK(!td_->auth_manager_->is_bot()); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "toggle_dialog_silent_send_message"); if (d == nullptr) { return Status::Error(6, "Chat not found"); } @@ -19475,7 +19493,7 @@ void MessagesManager::on_updated_dialog_notification_settings(DialogId dialog_id } Status MessagesManager::set_dialog_client_data(DialogId dialog_id, string &&client_data) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "set_dialog_client_data"); if (d == nullptr) { return Status::Error(6, "Chat not found"); } @@ -19536,7 +19554,7 @@ void MessagesManager::create_dialog(DialogId dialog_id, bool force, Promiseauth_manager_->is_bot() || dialog_id.get_type() == DialogType::SecretChat) { force_create_dialog(dialog_id, "create dialog"); } else { - const Dialog *d = get_dialog_force(dialog_id); + const Dialog *d = get_dialog_force(dialog_id, "create_dialog"); if (!is_dialog_inited(d)) { return send_get_dialog_query(dialog_id, std::move(promise), 0, "create_dialog"); } @@ -19669,9 +19687,9 @@ DialogId MessagesManager::migrate_dialog_to_megagroup(DialogId dialog_id, Promis } auto new_dialog_id = DialogId(channel_id); - Dialog *d = get_dialog_force(new_dialog_id); + Dialog *d = get_dialog_force(new_dialog_id, "migrate_dialog_to_megagroup"); if (d == nullptr) { - d = add_dialog(new_dialog_id); + d = add_dialog(new_dialog_id, "migrate_dialog_to_megagroup"); if (d->pts == 0) { d->pts = 1; if (is_debug_message_op_enabled()) { @@ -19686,7 +19704,7 @@ DialogId MessagesManager::migrate_dialog_to_megagroup(DialogId dialog_id, Promis } Status MessagesManager::open_dialog(DialogId dialog_id) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "open_dialog"); if (d == nullptr) { return Status::Error(3, "Chat not found"); } @@ -19696,7 +19714,7 @@ Status MessagesManager::open_dialog(DialogId dialog_id) { } Status MessagesManager::close_dialog(DialogId dialog_id) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "close_dialog"); if (d == nullptr) { return Status::Error(3, "Chat not found"); } @@ -19713,7 +19731,7 @@ Status MessagesManager::view_messages(DialogId dialog_id, MessageId top_thread_m const vector &message_ids, bool force_read) { CHECK(!td_->auth_manager_->is_bot()); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "view_messages"); if (d == nullptr) { return Status::Error(3, "Chat not found"); } @@ -19859,7 +19877,7 @@ Status MessagesManager::view_messages(DialogId dialog_id, MessageId top_thread_m Status MessagesManager::open_message_content(FullMessageId full_message_id) { auto dialog_id = full_message_id.get_dialog_id(); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "open_message_content"); if (d == nullptr) { return Status::Error(3, "Chat not found"); } @@ -20403,8 +20421,8 @@ vector MessagesManager::get_dialog_notification_settings_exceptions(No vector ordered_dialogs; auto my_dialog_id = get_my_dialog_id(); for (const auto &list : dialog_folders_) { - for (const auto &it : list.second.ordered_dialogs_) { - auto dialog_id = it.get_dialog_id(); + for (const auto &dialog_date : list.second.ordered_dialogs_) { + auto dialog_id = dialog_date.get_dialog_id(); if (filter_scope && get_dialog_notification_setting_scope(dialog_id) != scope) { continue; } @@ -20413,10 +20431,13 @@ vector MessagesManager::get_dialog_notification_settings_exceptions(No } const Dialog *d = get_dialog(dialog_id); + CHECK(d != nullptr); if (d == nullptr) { LOG(ERROR) << "Unknown dialog " << dialog_id; continue; } + LOG_CHECK(d->folder_id == list.first) + << list.first << ' ' << dialog_id << ' ' << d->folder_id << ' ' << d->order; if (d->order == DEFAULT_ORDER) { break; } @@ -20432,9 +20453,9 @@ vector MessagesManager::get_dialog_notification_settings_exceptions(No std::sort(ordered_dialogs.begin(), ordered_dialogs.end()); vector result; - for (auto &it : ordered_dialogs) { - CHECK(result.empty() || result.back() != it.get_dialog_id()); - result.push_back(it.get_dialog_id()); + for (auto &dialog_date : ordered_dialogs) { + CHECK(result.empty() || result.back() != dialog_date.get_dialog_id()); + result.push_back(dialog_date.get_dialog_id()); } promise.set_value(Unit()); return result; @@ -20462,7 +20483,7 @@ const ScopeNotificationSettings *MessagesManager::get_scope_notification_setting } DialogNotificationSettings *MessagesManager::get_dialog_notification_settings(DialogId dialog_id, bool force) { - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "get_dialog_notification_settings"); if (d == nullptr) { return nullptr; } @@ -20660,7 +20681,7 @@ tl_object_ptr MessagesManager::get_dialog_history(DialogId dia return nullptr; } - const Dialog *d = get_dialog_force(dialog_id); + const Dialog *d = get_dialog_force(dialog_id, "get_dialog_history"); if (d == nullptr) { promise.set_error(Status::Error(6, "Chat not found")); return nullptr; @@ -21094,7 +21115,7 @@ std::pair> MessagesManager::get_message_thread_histo CHECK(0 < limit && limit <= MAX_GET_HISTORY); CHECK(-limit < offset && offset <= 0); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "get_message_thread_history"); if (d == nullptr) { promise.set_error(Status::Error(6, "Chat not found")); return {}; @@ -21282,7 +21303,7 @@ std::pair> MessagesManager::search_dialog_messages( } from_message_id = from_message_id.get_next_server_message_id(); - const Dialog *d = get_dialog_force(dialog_id); + const Dialog *d = get_dialog_force(dialog_id, "search_dialog_messages"); if (d == nullptr) { promise.set_error(Status::Error(6, "Chat not found")); return result; @@ -21532,7 +21553,7 @@ std::pair> MessagesManager::search_dialog_recent_locati limit = MAX_SEARCH_MESSAGES; } - const Dialog *d = get_dialog_force(dialog_id); + const Dialog *d = get_dialog_force(dialog_id, "search_dialog_recent_location_messages"); if (d == nullptr) { promise.set_error(Status::Error(6, "Chat not found")); return result; @@ -22014,7 +22035,7 @@ MessagesManager::FoundMessages MessagesManager::offline_search_messages(DialogId promise.set_value(Unit()); return {}; } - if (dialog_id != DialogId() && !have_dialog_force(dialog_id)) { + if (dialog_id != DialogId() && !have_dialog_force(dialog_id, "offline_search_messages")) { promise.set_error(Status::Error(400, "Chat not found")); return {}; } @@ -22072,8 +22093,9 @@ void MessagesManager::on_messages_db_fts_result(Result resu res.reserve(fts_result.messages.size()); for (auto &message : fts_result.messages) { - auto m = on_get_message_from_database(message.dialog_id, get_dialog_force(message.dialog_id), message.data, false, - "on_messages_db_fts_result"); + auto m = on_get_message_from_database(message.dialog_id, + get_dialog_force(message.dialog_id, "on_messages_db_fts_result"), + message.data, false, "on_messages_db_fts_result"); if (m != nullptr) { res.push_back(FullMessageId(message.dialog_id, m->message_id)); } @@ -22106,8 +22128,9 @@ void MessagesManager::on_messages_db_calls_result(Result res.reserve(calls_result.messages.size()); for (auto &message : calls_result.messages) { - auto m = on_get_message_from_database(message.dialog_id, get_dialog_force(message.dialog_id), message.data, false, - "on_messages_db_calls_result"); + auto m = on_get_message_from_database(message.dialog_id, + get_dialog_force(message.dialog_id, "on_messages_db_calls_result"), + message.data, false, "on_messages_db_calls_result"); if (m != nullptr && first_db_message_id <= m->message_id) { res.push_back(FullMessageId(message.dialog_id, m->message_id)); @@ -22188,7 +22211,7 @@ std::pair> MessagesManager::search_messages( } int64 MessagesManager::get_dialog_message_by_date(DialogId dialog_id, int32 date, Promise &&promise) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "get_dialog_message_by_date"); if (d == nullptr) { promise.set_error(Status::Error(5, "Chat not found")); return 0; @@ -22367,7 +22390,7 @@ int32 MessagesManager::get_dialog_message_count(DialogId dialog_id, MessageSearc LOG(INFO) << "Get " << (return_local ? "local " : "") << "number of messages in " << dialog_id << " filtered by " << filter; - const Dialog *d = get_dialog_force(dialog_id); + const Dialog *d = get_dialog_force(dialog_id, "get_dialog_message_count"); if (d == nullptr) { promise.set_error(Status::Error(6, "Chat not found")); return -1; @@ -22463,7 +22486,6 @@ void MessagesManager::preload_older_messages(const Dialog *d, MessageId min_mess unique_ptr MessagesManager::parse_message(DialogId dialog_id, const BufferSlice &value, bool is_scheduled) { - LOG(INFO) << "Loaded message of size " << value.size() << " from database"; auto m = make_unique(); auto status = log_event_parse(*m, value.as_slice()); @@ -22480,6 +22502,7 @@ unique_ptr MessagesManager::parse_message(DialogId dia return nullptr; } + LOG(INFO) << "Loaded " << m->message_id << " in " << dialog_id << " of size " << value.size() << " from database"; return m; } @@ -22612,7 +22635,7 @@ void MessagesManager::on_get_history_from_database(DialogId dialog_id, MessageId is_first = false; pos++; } - resolve_dependencies_force(td_, dependencies, "get_history"); + resolve_dependencies_force(td_, dependencies, "on_get_history_from_database"); if (from_the_end && !last_added_message_id.is_valid() && last_received_message_id < d->first_database_message_id && !d->have_full_history) { @@ -22798,7 +22821,7 @@ vector MessagesManager::get_dialog_scheduled_messages(DialogId dialog return {}; } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "get_dialog_scheduled_messages"); if (d == nullptr) { promise.set_error(Status::Error(6, "Chat not found")); return {}; @@ -22930,7 +22953,7 @@ void MessagesManager::on_get_scheduled_messages_from_database(DialogId dialog_id added_message_ids.push_back(m->message_id); } } - resolve_dependencies_force(td_, dependencies, "get_scheduled_messages"); + resolve_dependencies_force(td_, dependencies, "on_get_scheduled_messages_from_database"); // for (auto message_id : added_message_ids) { // send_update_new_message(d, get_message(d, message_id)); @@ -22962,7 +22985,7 @@ MessagesManager::FoundMessages MessagesManager::get_message_public_forwards(Full } auto dialog_id = full_message_id.get_dialog_id(); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "get_message_public_forwards"); if (d == nullptr) { promise.set_error(Status::Error(5, "Chat not found")); return {}; @@ -23831,7 +23854,7 @@ Result MessagesManager::send_message(DialogId dialog_id, MessageId to return Status::Error(5, "Can't send message without content"); } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "send_message"); if (d == nullptr) { return Status::Error(5, "Chat not found"); } @@ -23908,7 +23931,7 @@ Result MessagesManager::process_input_message_content( } DialogId from_dialog_id(input_message->from_chat_id_); - Dialog *from_dialog = get_dialog_force(from_dialog_id); + Dialog *from_dialog = get_dialog_force(from_dialog_id, "send_message copy"); if (from_dialog == nullptr) { return Status::Error(400, "Chat to copy message from not found"); } @@ -24069,7 +24092,7 @@ Result> MessagesManager::send_message_group( return Status::Error(4, "There are no messages to send"); } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "send_message_group"); if (d == nullptr) { return Status::Error(5, "Chat not found"); } @@ -24721,7 +24744,7 @@ Result MessagesManager::send_bot_start_message(UserId bot_user_id, Di TRY_RESULT(bot_data, td_->contacts_manager_->get_bot_data(bot_user_id)); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "send_bot_start_message"); if (d == nullptr) { return Status::Error(5, "Chat not found"); } @@ -24877,7 +24900,7 @@ Result MessagesManager::send_inline_query_result_message(DialogId dia bool hide_via_bot) { LOG(INFO) << "Begin to send inline query result message to " << dialog_id << " in reply to " << reply_to_message_id; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "send_inline_query_result_message"); if (d == nullptr) { return Status::Error(5, "Chat not found"); } @@ -25274,7 +25297,7 @@ void MessagesManager::edit_message_text(FullMessageId full_message_id, LOG(INFO) << "Begin to edit text of " << full_message_id; auto dialog_id = full_message_id.get_dialog_id(); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "edit_message_text"); if (d == nullptr) { return promise.set_error(Status::Error(5, "Chat not found")); } @@ -25329,7 +25352,7 @@ void MessagesManager::edit_message_live_location(FullMessageId full_message_id, int32 proximity_alert_radius, Promise &&promise) { LOG(INFO) << "Begin to edit live location of " << full_message_id; auto dialog_id = full_message_id.get_dialog_id(); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "edit_message_live_location"); if (d == nullptr) { return promise.set_error(Status::Error(5, "Chat not found")); } @@ -25492,7 +25515,7 @@ void MessagesManager::edit_message_media(FullMessageId full_message_id, LOG(INFO) << "Begin to edit media of " << full_message_id; auto dialog_id = full_message_id.get_dialog_id(); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "edit_message_media"); if (d == nullptr) { return promise.set_error(Status::Error(5, "Chat not found")); } @@ -25569,7 +25592,7 @@ void MessagesManager::edit_message_caption(FullMessageId full_message_id, LOG(INFO) << "Begin to edit caption of " << full_message_id; auto dialog_id = full_message_id.get_dialog_id(); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "edit_message_caption"); if (d == nullptr) { return promise.set_error(Status::Error(5, "Chat not found")); } @@ -25621,7 +25644,7 @@ void MessagesManager::edit_message_reply_markup(FullMessageId full_message_id, LOG(INFO) << "Begin to edit reply markup of " << full_message_id; auto dialog_id = full_message_id.get_dialog_id(); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "edit_message_reply_markup"); if (d == nullptr) { return promise.set_error(Status::Error(5, "Chat not found")); } @@ -25850,7 +25873,7 @@ void MessagesManager::edit_message_scheduling_state( LOG(INFO) << "Begin to reschedule " << full_message_id << " to " << schedule_date; auto dialog_id = full_message_id.get_dialog_id(); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "edit_message_scheduling_state"); if (d == nullptr) { return promise.set_error(Status::Error(5, "Chat not found")); } @@ -26030,7 +26053,7 @@ void MessagesManager::set_game_score(FullMessageId full_message_id, bool edit_me LOG(INFO) << "Begin to set game score of " << user_id << " in " << full_message_id; auto dialog_id = full_message_id.get_dialog_id(); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "set_game_score"); if (d == nullptr) { return promise.set_error(Status::Error(5, "Chat not found")); } @@ -26086,7 +26109,7 @@ int64 MessagesManager::get_game_high_scores(FullMessageId full_message_id, UserI LOG(INFO) << "Begin to get game high scores of " << user_id << " in " << full_message_id; auto dialog_id = full_message_id.get_dialog_id(); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "get_game_high_scores"); if (d == nullptr) { promise.set_error(Status::Error(5, "Chat not found")); return 0; @@ -26456,7 +26479,7 @@ Result> MessagesManager::forward_messages(DialogId to_dialog_i return Status::Error(4, "There are no messages to forward"); } - Dialog *from_dialog = get_dialog_force(from_dialog_id); + Dialog *from_dialog = get_dialog_force(from_dialog_id, "forward_messages from"); if (from_dialog == nullptr) { return Status::Error(5, "Chat to forward messages from not found"); } @@ -26467,7 +26490,7 @@ Result> MessagesManager::forward_messages(DialogId to_dialog_i return Status::Error(7, "Can't forward messages from secret chats"); } - Dialog *to_dialog = get_dialog_force(to_dialog_id); + Dialog *to_dialog = get_dialog_force(to_dialog_id, "forward_messages to"); if (to_dialog == nullptr) { return Status::Error(5, "Chat to forward messages to not found"); } @@ -26761,7 +26784,7 @@ Result> MessagesManager::resend_messages(DialogId dialog_id, v return Status::Error(4, "There are no messages to resend"); } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "resend_messages"); if (d == nullptr) { return Status::Error(400, "Chat not found"); } @@ -26882,7 +26905,7 @@ Status MessagesManager::send_screenshot_taken_notification_message(DialogId dial LOG(INFO) << "Begin to send notification about taken screenshot in " << dialog_id; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "send_screenshot_taken_notification_message"); if (d == nullptr) { return Status::Error(5, "Chat not found"); } @@ -26965,7 +26988,7 @@ Result MessagesManager::add_local_message( } LOG(INFO) << "Begin to add local message to " << dialog_id << " in reply to " << reply_to_message_id; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "add_local_message"); if (d == nullptr) { return Status::Error(5, "Chat not found"); } @@ -27000,7 +27023,7 @@ Result MessagesManager::add_local_message( if (sender_dialog_id.get_type() != DialogType::Channel) { return Status::Error(400, "Sender chat must be a supergroup or channel"); } - if (!have_dialog_force(sender_dialog_id)) { + if (!have_dialog_force(sender_dialog_id, "add_local_message")) { return Status::Error(400, "Sender chat not found"); } break; @@ -27111,7 +27134,7 @@ void MessagesManager::get_message_file_type(const string &message_file_head, } Status MessagesManager::can_import_messages(DialogId dialog_id) { - if (!have_dialog_force(dialog_id)) { + if (!have_dialog_force(dialog_id, "can_import_messages")) { return Status::Error(400, "Chat not found"); } @@ -27293,7 +27316,13 @@ bool MessagesManager::on_update_message_id(int64 random_id, MessageId new_messag being_sent_messages_.erase(it); + if (!have_message_force({dialog_id, old_message_id}, "on_update_message_id")) { + delete_sent_message_from_server(dialog_id, new_message_id); + return true; + } + LOG(INFO) << "Save correspondence from " << new_message_id << " in " << dialog_id << " to " << old_message_id; + CHECK(old_message_id.is_yet_unsent()); update_message_ids_[FullMessageId(dialog_id, new_message_id)] = old_message_id; return true; } @@ -27317,7 +27346,13 @@ bool MessagesManager::on_update_scheduled_message_id(int64 random_id, ScheduledS being_sent_messages_.erase(it); + if (!have_message_force({dialog_id, old_message_id}, "on_update_scheduled_message_id")) { + delete_sent_message_from_server(dialog_id, MessageId(new_message_id, std::numeric_limits::max())); + return true; + } + LOG(INFO) << "Save correspondence from " << new_message_id << " in " << dialog_id << " to " << old_message_id; + CHECK(old_message_id.is_yet_unsent()); update_scheduled_message_ids_[dialog_id][new_message_id] = old_message_id; return true; } @@ -27417,11 +27452,11 @@ Result MessagesManager::get_messag return Status::Error("Ignore notification sent to bot"); } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "get_message_push_notification_info"); if (d == nullptr) { return Status::Error(406, "Ignore notification in unknown chat"); } - if (sender_dialog_id.is_valid() && !have_dialog_force(sender_dialog_id)) { + if (sender_dialog_id.is_valid() && !have_dialog_force(sender_dialog_id, "get_message_push_notification_info")) { return Status::Error(406, "Ignore notification sent by unknown chat"); } @@ -27476,10 +27511,10 @@ Result MessagesManager::get_messag if (contains_mention) { if (sender_user_id.is_valid()) { settings_dialog_id = DialogId(sender_user_id); - settings_dialog = get_dialog_force(settings_dialog_id); + settings_dialog = get_dialog_force(settings_dialog_id, "get_message_push_notification_info 2"); } else if (sender_dialog_id.is_valid()) { settings_dialog_id = sender_dialog_id; - settings_dialog = get_dialog_force(settings_dialog_id); + settings_dialog = get_dialog_force(settings_dialog_id, "get_message_push_notification_info 3"); } } @@ -27558,7 +27593,7 @@ MessagesManager::MessageNotificationGroup MessagesManager::get_message_notificat auto r_value = G()->td_db()->get_dialog_db_sync()->get_notification_group(group_id); if (r_value.is_ok()) { VLOG(notifications) << "Loaded " << r_value.ok() << " from database by " << group_id; - d = get_dialog_force(r_value.ok().dialog_id); + d = get_dialog_force(r_value.ok().dialog_id, "get_message_notification_group_force"); } else { CHECK(r_value.error().message() == "Not found"); VLOG(notifications) << "Failed to load " << group_id << " from database"; @@ -27860,7 +27895,7 @@ vector MessagesManager::get_message_notification_group_key vector result; for (auto &group_key : group_keys) { CHECK(group_key.dialog_id.is_valid()); - const Dialog *d = get_dialog_force(group_key.dialog_id); + const Dialog *d = get_dialog_force(group_key.dialog_id, "get_message_notification_group_keys_from_database"); if (d == nullptr || (d->message_notification_group.group_id != group_key.group_id && d->mention_notification_group.group_id != group_key.group_id)) { continue; @@ -28076,7 +28111,7 @@ void MessagesManager::on_get_message_notifications_from_database(DialogId dialog void MessagesManager::remove_message_notification(DialogId dialog_id, NotificationGroupId group_id, NotificationId notification_id) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "remove_message_notification"); if (d == nullptr) { LOG(ERROR) << "Can't find " << dialog_id; return; @@ -28123,7 +28158,7 @@ void MessagesManager::remove_message_notification(DialogId dialog_id, Notificati void MessagesManager::remove_message_notifications_by_message_ids(DialogId dialog_id, const vector &message_ids) { VLOG(notifications) << "Trying to remove notification about " << message_ids << " in " << dialog_id; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "remove_message_notifications_by_message_ids"); if (d == nullptr) { return; } @@ -28176,7 +28211,7 @@ void MessagesManager::do_remove_message_notification(DialogId dialog_id, bool fr void MessagesManager::remove_message_notifications(DialogId dialog_id, NotificationGroupId group_id, NotificationId max_notification_id, MessageId max_message_id) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "remove_message_notifications"); if (d == nullptr) { LOG(ERROR) << "Can't find " << dialog_id; return; @@ -28389,10 +28424,10 @@ bool MessagesManager::add_new_message_notification(Dialog *d, Message *m, bool f // have a mention, so use notification settings from the dialog with the sender if (m->sender_user_id.is_valid()) { settings_dialog_id = DialogId(m->sender_user_id); - settings_dialog = get_dialog_force(settings_dialog_id); + settings_dialog = get_dialog_force(settings_dialog_id, "add_new_message_notification 1"); } else if (m->sender_dialog_id.is_valid()) { settings_dialog_id = m->sender_dialog_id; - settings_dialog = get_dialog_force(settings_dialog_id); + settings_dialog = get_dialog_force(settings_dialog_id, "add_new_message_notification 2"); } } @@ -29134,10 +29169,7 @@ FullMessageId MessagesManager::on_send_message_success(int64 random_id, MessageI being_readded_message_id_ = {dialog_id, old_message_id}; unique_ptr sent_message = delete_message(d, old_message_id, false, &need_update_dialog_pos, source); if (sent_message == nullptr) { - // message has already been deleted by the user or sent to inaccessible channel - // don't need to send update to the user, because the message has already been deleted - LOG(INFO) << "Delete already deleted sent " << new_message_id << " from server"; - delete_message_from_server(dialog_id, new_message_id, true); + delete_sent_message_from_server(dialog_id, new_message_id); being_readded_message_id_ = FullMessageId(); return {}; } @@ -29694,13 +29726,13 @@ void MessagesManager::on_update_dialog_draft_message(DialogId dialog_id, LOG(ERROR) << "Receive update chat draft in invalid " << dialog_id; return; } - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "on_update_dialog_draft_message"); if (d == nullptr) { LOG(INFO) << "Ignore update chat draft in unknown " << dialog_id; if (!have_input_peer(dialog_id, AccessRights::Read)) { LOG(ERROR) << "Have no read access to " << dialog_id << " to repair chat draft message"; } else { - send_get_dialog_query(dialog_id, Promise(), 0, "on_update_dialog_draft_message"); + send_get_dialog_query(dialog_id, Auto(), 0, "on_update_dialog_draft_message"); } return; } @@ -29759,7 +29791,7 @@ void MessagesManager::on_update_dialog_is_pinned(FolderId folder_id, DialogId di return; } - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "on_update_dialog_is_pinned"); if (d == nullptr) { LOG(INFO) << "Can't apply updateDialogPinned in " << folder_id << " with unknown " << dialog_id; on_update_pinned_dialogs(folder_id); @@ -29811,7 +29843,7 @@ void MessagesManager::on_update_dialog_is_marked_as_unread(DialogId dialog_id, b return; } - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "on_update_dialog_is_marked_as_unread"); if (d == nullptr) { // nothing to do return; @@ -29869,7 +29901,7 @@ void MessagesManager::on_update_dialog_is_blocked(DialogId dialog_id, bool is_bl td_->contacts_manager_->on_update_user_is_blocked(dialog_id.get_user_id(), is_blocked); } - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "on_update_dialog_is_blocked"); if (d == nullptr) { // nothing to do return; @@ -29940,7 +29972,7 @@ void MessagesManager::on_update_dialog_last_pinned_message_id(DialogId dialog_id return; } - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "on_update_dialog_last_pinned_message_id"); if (d == nullptr) { // nothing to do return; @@ -30025,7 +30057,7 @@ void MessagesManager::on_update_dialog_has_scheduled_server_messages(DialogId di return; } - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "on_update_dialog_has_scheduled_server_messages"); if (d == nullptr) { // nothing to do return; @@ -30079,7 +30111,7 @@ void MessagesManager::set_dialog_has_scheduled_database_messages_impl(Dialog *d, } void MessagesManager::on_update_dialog_folder_id(DialogId dialog_id, FolderId folder_id) { - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "on_update_dialog_folder_id"); if (d == nullptr) { // nothing to do return; @@ -30112,8 +30144,7 @@ void MessagesManager::set_dialog_folder_id(Dialog *d, FolderId folder_id) { } DialogDate dialog_date(d->order, d->dialog_id); - auto *folder = get_dialog_folder(d->folder_id); - if (folder->ordered_dialogs_.erase(dialog_date) == 0) { + if (get_dialog_folder(d->folder_id)->ordered_dialogs_.erase(dialog_date) == 0) { LOG_IF(ERROR, d->order != DEFAULT_ORDER) << d->dialog_id << " not found in the chat list"; } @@ -30207,7 +30238,7 @@ void MessagesManager::on_update_dialog_group_call(DialogId dialog_id, bool has_a } void MessagesManager::on_update_dialog_group_call_id(DialogId dialog_id, InputGroupCallId input_group_call_id) { - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "on_update_dialog_group_call_id"); if (d == nullptr) { // nothing to do return; @@ -30231,7 +30262,7 @@ void MessagesManager::on_update_dialog_group_call_id(DialogId dialog_id, InputGr void MessagesManager::on_update_dialog_default_join_group_call_as_dialog_id(DialogId dialog_id, DialogId default_join_as_dialog_id, bool force) { - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "on_update_dialog_default_join_group_call_as_dialog_id"); if (d == nullptr) { // nothing to do return; @@ -30259,7 +30290,7 @@ void MessagesManager::on_update_dialog_default_join_group_call_as_dialog_id(Dial } void MessagesManager::on_update_dialog_message_ttl_setting(DialogId dialog_id, MessageTtlSetting message_ttl_setting) { - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "on_update_dialog_message_ttl_setting"); if (d == nullptr) { // nothing to do return; @@ -30347,7 +30378,7 @@ void MessagesManager::on_dialog_bots_updated(DialogId dialog_id, vector return; } - auto d = from_database ? get_dialog(dialog_id) : get_dialog_force(dialog_id); + auto d = from_database ? get_dialog(dialog_id) : get_dialog_force(dialog_id, "on_dialog_bots_updated"); if (d == nullptr || d->reply_markup_message_id == MessageId()) { return; } @@ -30518,9 +30549,9 @@ DialogId MessagesManager::search_public_dialog(const string &username_to_search, if (dialog_id.is_valid()) { if (have_input_peer(dialog_id, AccessRights::Read)) { if (td_->auth_manager_->is_bot()) { - force_create_dialog(dialog_id, "search public dialog", true); + force_create_dialog(dialog_id, "search_public_dialog", true); } else { - const Dialog *d = get_dialog_force(dialog_id); + const Dialog *d = get_dialog_force(dialog_id, "search_public_dialog"); if (!is_dialog_inited(d)) { send_get_dialog_query(dialog_id, std::move(promise), 0, "search_public_dialog"); return DialogId(); @@ -30532,7 +30563,7 @@ DialogId MessagesManager::search_public_dialog(const string &username_to_search, } else { // bot username maybe known despite there is no access_hash if (force || dialog_id.get_type() != DialogType::User) { - force_create_dialog(dialog_id, "search public dialog", true); + force_create_dialog(dialog_id, "search_public_dialog", true); promise.set_value(Unit()); return dialog_id; } @@ -30866,7 +30897,7 @@ bool MessagesManager::is_dialog_action_unneeded(DialogId dialog_id) const { void MessagesManager::send_dialog_action(DialogId dialog_id, MessageId top_thread_message_id, DialogAction action, Promise &&promise) { - if (!have_dialog_force(dialog_id)) { + if (!have_dialog_force(dialog_id, "send_dialog_action")) { return promise.set_error(Status::Error(5, "Chat not found")); } if (top_thread_message_id != MessageId() && @@ -31013,7 +31044,7 @@ void MessagesManager::clear_active_dialog_actions(DialogId dialog_id) { vector MessagesManager::get_dialog_lists_to_add_dialog(DialogId dialog_id) { vector result; - const Dialog *d = get_dialog_force(dialog_id); + const Dialog *d = get_dialog_force(dialog_id, "get_dialog_lists_to_add_dialog"); if (d == nullptr || d->order == DEFAULT_ORDER || !have_input_peer(dialog_id, AccessRights::Read)) { return result; } @@ -31053,7 +31084,7 @@ void MessagesManager::add_dialog_to_list(DialogId dialog_id, DialogListId dialog LOG(INFO) << "Receive addChatToList request to add " << dialog_id << " to " << dialog_list_id; CHECK(!td_->auth_manager_->is_bot()); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "add_dialog_to_list"); if (d == nullptr) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -31182,7 +31213,7 @@ void MessagesManager::set_dialog_photo(DialogId dialog_id, const tl_object_ptr &&promise) { LOG(INFO) << "Receive setChatPhoto request to change photo of " << dialog_id; - if (!have_dialog_force(dialog_id)) { + if (!have_dialog_force(dialog_id, "set_dialog_photo")) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -31297,7 +31328,7 @@ void MessagesManager::upload_dialog_photo(DialogId dialog_id, FileId file_id, bo void MessagesManager::set_dialog_title(DialogId dialog_id, const string &title, Promise &&promise) { LOG(INFO) << "Receive setChatTitle request to change title of " << dialog_id << " to \"" << title << '"'; - if (!have_dialog_force(dialog_id)) { + if (!have_dialog_force(dialog_id, "set_dialog_title")) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -31346,7 +31377,7 @@ void MessagesManager::set_dialog_message_ttl_setting(DialogId dialog_id, int32 t return promise.set_error(Status::Error(400, "Message auto-delete time can't be negative")); } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "set_dialog_message_ttl_setting"); if (d == nullptr) { return promise.set_error(Status::Error(400, "Chat not found")); } @@ -31413,7 +31444,7 @@ void MessagesManager::set_dialog_permissions(DialogId dialog_id, LOG(INFO) << "Receive setChatPermissions request to change permissions of " << dialog_id << " to " << to_string(permissions); - if (!have_dialog_force(dialog_id)) { + if (!have_dialog_force(dialog_id, "set_dialog_permissions")) { return promise.set_error(Status::Error(3, "Chat not found")); } if (!have_input_peer(dialog_id, AccessRights::Write)) { @@ -31467,7 +31498,7 @@ void MessagesManager::set_dialog_description(DialogId dialog_id, const string &d LOG(INFO) << "Receive setChatDescription request to set description of " << dialog_id << " to \"" << description << '"'; - if (!have_dialog_force(dialog_id)) { + if (!have_dialog_force(dialog_id, "set_dialog_description")) { return promise.set_error(Status::Error(3, "Chat not found")); } @@ -31523,7 +31554,7 @@ Status MessagesManager::can_pin_messages(DialogId dialog_id) const { void MessagesManager::pin_dialog_message(DialogId dialog_id, MessageId message_id, bool disable_notification, bool only_for_self, bool is_unpin, Promise &&promise) { - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "pin_dialog_message"); if (d == nullptr) { return promise.set_error(Status::Error(400, "Chat not found")); } @@ -31554,7 +31585,7 @@ void MessagesManager::pin_dialog_message(DialogId dialog_id, MessageId message_i } void MessagesManager::unpin_all_dialog_messages(DialogId dialog_id, Promise &&promise) { - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "unpin_all_dialog_messages"); if (d == nullptr) { return promise.set_error(Status::Error(6, "Chat not found")); } @@ -31656,10 +31687,10 @@ tl_object_ptr MessagesManager::get_ch flags |= telegram_api::channelAdminLogEventsFilter::SETTINGS_MASK; } if (filters->invite_link_changes_) { - flags |= telegram_api::channelAdminLogEventsFilter::GROUP_CALL_MASK; + flags |= telegram_api::channelAdminLogEventsFilter::INVITES_MASK; } if (filters->voice_chat_changes_) { - flags |= telegram_api::channelAdminLogEventsFilter::INVITES_MASK; + flags |= telegram_api::channelAdminLogEventsFilter::GROUP_CALL_MASK; } return make_tl_object( @@ -31676,7 +31707,7 @@ int64 MessagesManager::get_dialog_event_log(DialogId dialog_id, const string &qu return 0; } - if (!have_dialog_force(dialog_id)) { + if (!have_dialog_force(dialog_id, "get_dialog_event_log")) { promise.set_error(Status::Error(3, "Chat not found")); return 0; } @@ -32231,14 +32262,14 @@ MessagesManager::Message *MessagesManager::on_get_message_from_database(DialogId } force_create_dialog(dialog_id, source); - d = get_dialog_force(dialog_id); + d = get_dialog_force(dialog_id, source); if (d == nullptr) { LOG(ERROR) << "Unknown dialog " << dialog_id; return nullptr; } } - if (!have_input_peer(d->dialog_id, AccessRights::Read)) { + if (!have_input_peer(dialog_id, AccessRights::Read)) { return nullptr; } @@ -32261,7 +32292,10 @@ MessagesManager::Message *MessagesManager::on_get_message_from_database(DialogId Dependencies dependencies; add_message_dependencies(dependencies, m.get()); - resolve_dependencies_force(td_, dependencies, "on_get_message_from_database"); + if (!resolve_dependencies_force(td_, dependencies, "on_get_message_from_database")) { + FullMessageId full_message_id{dialog_id, m->message_id}; + get_message_from_server(full_message_id, Auto()); + } m->have_previous = false; m->have_next = false; @@ -32271,7 +32305,7 @@ MessagesManager::Message *MessagesManager::on_get_message_from_database(DialogId auto result = add_message_to_dialog(d, std::move(m), false, &need_update, &need_update_dialog_pos, source); if (need_update_dialog_pos) { LOG(ERROR) << "Need update dialog pos after load " << (result == nullptr ? MessageId() : result->message_id) - << " in " << d->dialog_id << " from " << source; + << " in " << dialog_id << " from " << source; send_update_chat_last_message(d, source); } return result; @@ -32300,9 +32334,9 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(DialogId dialog return nullptr; } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, source); if (d == nullptr) { - d = add_dialog(dialog_id); + d = add_dialog(dialog_id, "add_message_to_dialog"); *need_update_dialog_pos = true; } else { CHECK(d->dialog_id == dialog_id); @@ -32337,6 +32371,13 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq } } } + if (message->sender_user_id == ContactsManager::get_anonymous_bot_user_id() && + !message->sender_dialog_id.is_valid() && dialog_id.get_type() == DialogType::Channel && + !is_broadcast_channel(dialog_id)) { + message->sender_user_id = UserId(); + message->sender_dialog_id = dialog_id; + } + if (!message->top_thread_message_id.is_valid() && !is_broadcast_channel(dialog_id) && is_visible_message_reply_info(dialog_id, message.get()) && !message_id.is_scheduled()) { message->top_thread_message_id = message_id; @@ -32344,6 +32385,9 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq if (!message_id.is_scheduled() && message_id <= d->last_clear_history_message_id) { LOG(INFO) << "Skip adding cleared " << message_id << " to " << dialog_id << " from " << source; + if (message->from_database) { + delete_message_from_database(d, message_id, message.get(), true); + } debug_add_message_to_dialog_fail_reason_ = "cleared full history"; return nullptr; } @@ -32472,6 +32516,9 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq } LOG(INFO) << "Process MessageChatDeleteHistory in " << message_id << " in " << dialog_id << " with date " << message->date << " from " << source; + if (message_id > d->max_unavailable_message_id) { + set_dialog_max_unavailable_message_id(dialog_id, message_id, false, "message chat delete history"); + } CHECK(!message->from_database); debug_add_message_to_dialog_fail_reason_ = "skip adding MessageChatDeleteHistory"; return nullptr; @@ -33035,6 +33082,7 @@ MessagesManager::Message *MessagesManager::add_message_to_dialog(Dialog *d, uniq add_notification_id_to_message_id_correspondence(d, m->notification_id, m->message_id); } + result_message->debug_source = source; d->being_added_message_id = MessageId(); if (!td_->auth_manager_->is_bot() && from_update && d->reply_markup_message_id != MessageId()) { @@ -33410,7 +33458,9 @@ void MessagesManager::delete_message_from_database(Dialog *d, MessageId message_ if (message_id.is_any_server()) { auto old_message_id = find_old_message_id(d->dialog_id, message_id); if (old_message_id.is_valid()) { - LOG(WARNING) << "Sent " << FullMessageId{d->dialog_id, message_id} << " was deleted before it was received"; + bool have_old_message = get_message(d, old_message_id) != nullptr; + LOG(WARNING) << "Sent " << FullMessageId{d->dialog_id, message_id} + << " was deleted before it was received. Have old message = " << have_old_message; send_closure_later(actor_id(this), &MessagesManager::delete_messages, d->dialog_id, vector{old_message_id}, false, Promise()); delete_update_message_id(d->dialog_id, message_id); @@ -33690,6 +33740,9 @@ bool MessagesManager::update_message(Dialog *d, Message *old_message, unique_ptr if (new_message->is_mention_notification_disabled) { old_message->is_mention_notification_disabled = true; } + if (!new_message->from_database) { + old_message->from_database = false; + } if (old_message->ttl_period != new_message->ttl_period) { if (old_message->ttl_period != 0 || !message_id.is_yet_unsent()) { @@ -34056,8 +34109,8 @@ MessagesManager::Dialog *MessagesManager::get_dialog_by_message_id(MessageId mes G()->td_db()->get_messages_db_sync()->get_message_by_unique_message_id(message_id.get_server_message_id()); if (r_value.is_ok()) { DialogId dialog_id(r_value.ok().first); - Message *m = on_get_message_from_database(dialog_id, get_dialog_force(dialog_id), r_value.ok().second, false, - "get_dialog_by_message_id"); + Message *m = on_get_message_from_database(dialog_id, get_dialog_force(dialog_id, "get_dialog_by_message_id"), + r_value.ok().second, false, "get_dialog_by_message_id"); if (m != nullptr) { CHECK(m->message_id == message_id); CHECK(message_id_to_dialog_id_[message_id] == dialog_id); @@ -34120,7 +34173,7 @@ void MessagesManager::force_create_dialog(DialogId dialog_id, const char *source bool force_update_dialog_pos) { LOG_CHECK(dialog_id.is_valid()) << source; LOG_CHECK(is_inited_) << dialog_id << ' ' << source << ' ' << expect_no_access << ' ' << force_update_dialog_pos; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, source); if (d == nullptr) { LOG(INFO) << "Force create " << dialog_id << " from " << source; if (loaded_dialogs_.count(dialog_id) > 0) { @@ -34128,7 +34181,7 @@ void MessagesManager::force_create_dialog(DialogId dialog_id, const char *source return; } - d = add_dialog(dialog_id); + d = add_dialog(dialog_id, "force_create_dialog"); update_dialog_pos(d, "force_create_dialog"); if (dialog_id.get_type() == DialogType::SecretChat && !d->notification_settings.is_synchronized && @@ -34139,7 +34192,7 @@ void MessagesManager::force_create_dialog(DialogId dialog_id, const char *source auto secret_chat_id = dialog_id.get_secret_chat_id(); { auto user_id = td_->contacts_manager_->get_secret_chat_user_id(secret_chat_id); - Dialog *user_d = get_dialog_force(DialogId(user_id)); + Dialog *user_d = get_dialog_force(DialogId(user_id), source); if (user_d != nullptr && user_d->notification_settings.is_synchronized) { VLOG(notifications) << "Copy notification settings from " << user_d->dialog_id << " to " << dialog_id; auto new_notification_settings = user_d->notification_settings; @@ -34188,7 +34241,7 @@ void MessagesManager::force_create_dialog(DialogId dialog_id, const char *source } } -MessagesManager::Dialog *MessagesManager::add_dialog(DialogId dialog_id) { +MessagesManager::Dialog *MessagesManager::add_dialog(DialogId dialog_id, const char *source) { LOG(DEBUG) << "Creating " << dialog_id; CHECK(!have_dialog(dialog_id)); @@ -34197,7 +34250,7 @@ MessagesManager::Dialog *MessagesManager::add_dialog(DialogId dialog_id) { auto r_value = G()->td_db()->get_dialog_db_sync()->get_dialog(dialog_id); if (r_value.is_ok()) { LOG(INFO) << "Synchronously loaded " << dialog_id << " from database"; - return add_new_dialog(parse_dialog(dialog_id, r_value.ok()), true); + return add_new_dialog(parse_dialog(dialog_id, r_value.ok()), true, source); } } @@ -34205,12 +34258,13 @@ MessagesManager::Dialog *MessagesManager::add_dialog(DialogId dialog_id) { d->dialog_id = dialog_id; invalidate_message_indexes(d.get()); - return add_new_dialog(std::move(d), false); + return add_new_dialog(std::move(d), false, source); } -MessagesManager::Dialog *MessagesManager::add_new_dialog(unique_ptr &&d, bool is_loaded_from_database) { +MessagesManager::Dialog *MessagesManager::add_new_dialog(unique_ptr &&d, bool is_loaded_from_database, + const char *source) { auto dialog_id = d->dialog_id; - LOG_CHECK(is_inited_) << dialog_id << ' ' << is_loaded_from_database; + LOG_CHECK(is_inited_) << dialog_id << ' ' << is_loaded_from_database << ' ' << source; switch (dialog_id.get_type()) { case DialogType::User: if (dialog_id == get_my_dialog_id() && d->last_read_inbox_message_id == MessageId::max() && @@ -34346,7 +34400,7 @@ void MessagesManager::fix_new_dialog(Dialog *d, unique_ptr &&last_datab if (user_id.is_valid()) { force_create_dialog(DialogId(user_id), "add chat with user to load/store action_bar and is_blocked"); - Dialog *user_d = get_dialog_force(DialogId(user_id)); + Dialog *user_d = get_dialog_force(DialogId(user_id), "fix_new_dialog"); if (user_d != nullptr && d->is_blocked != user_d->is_blocked) { set_dialog_is_blocked(d, user_d->is_blocked); } @@ -34669,9 +34723,13 @@ void MessagesManager::fix_new_dialog(Dialog *d, unique_ptr &&last_datab << ", max_notification_message_id = " << d->max_notification_message_id; if (d->messages != nullptr) { - CHECK(d->messages->message_id == last_message_id); - CHECK(d->messages->left == nullptr); - CHECK(d->messages->right == nullptr); + auto get_debug_source = [](const unique_ptr &message) { + return message->debug_source != nullptr ? message->debug_source : "null"; + }; + LOG_CHECK(d->messages->message_id == last_message_id) + << d->messages->message_id << ' ' << last_message_id << ' ' << get_debug_source(d->messages); + LOG_CHECK(d->messages->left == nullptr) << get_debug_source(d->messages->left); + LOG_CHECK(d->messages->right == nullptr) << get_debug_source(d->messages->right); } } @@ -35024,7 +35082,10 @@ void MessagesManager::update_dialog_lists( if (d->folder_id != FolderId::main()) { LOG(INFO) << "Change folder of " << dialog_id << " to " << FolderId::main(); + DialogDate dialog_date(d->order, dialog_id); + get_dialog_folder(d->folder_id)->ordered_dialogs_.erase(dialog_date); do_set_dialog_folder_id(d, FolderId::main()); + get_dialog_folder(d->folder_id)->ordered_dialogs_.insert(dialog_date); } } @@ -35290,11 +35351,11 @@ const MessagesManager::Dialog *MessagesManager::get_dialog(DialogId dialog_id) c return it == dialogs_.end() ? nullptr : it->second.get(); } -bool MessagesManager::have_dialog_force(DialogId dialog_id) { - return loaded_dialogs_.count(dialog_id) > 0 || get_dialog_force(dialog_id) != nullptr; +bool MessagesManager::have_dialog_force(DialogId dialog_id, const char *source) { + return loaded_dialogs_.count(dialog_id) > 0 || get_dialog_force(dialog_id, source) != nullptr; } -MessagesManager::Dialog *MessagesManager::get_dialog_force(DialogId dialog_id) { +MessagesManager::Dialog *MessagesManager::get_dialog_force(DialogId dialog_id, const char *source) { // TODO remove most usages of that function, preload dialog asynchronously if possible auto it = dialogs_.find(dialog_id); if (it != dialogs_.end()) { @@ -35307,12 +35368,13 @@ MessagesManager::Dialog *MessagesManager::get_dialog_force(DialogId dialog_id) { auto r_value = G()->td_db()->get_dialog_db_sync()->get_dialog(dialog_id); if (r_value.is_ok()) { - LOG(INFO) << "Loaded " << dialog_id << " from database"; - auto d = on_load_dialog_from_database(dialog_id, r_value.move_as_ok()); + LOG(INFO) << "Loaded " << dialog_id << " from database from " << source; + auto d = on_load_dialog_from_database(dialog_id, r_value.move_as_ok(), source); LOG_CHECK(d == nullptr || d->dialog_id == dialog_id) << d->dialog_id << " " << dialog_id; return d; } else { - LOG(INFO) << "Failed to load " << dialog_id << " from database: " << r_value.error().message(); + LOG(INFO) << "Failed to load " << dialog_id << " from database from " << source << ": " + << r_value.error().message(); return nullptr; } } @@ -35362,12 +35424,15 @@ unique_ptr MessagesManager::parse_dialog(DialogId dialo if (d->draft_message != nullptr) { add_formatted_text_dependencies(dependencies, &d->draft_message->input_message_text.text); } - resolve_dependencies_force(td_, dependencies, "parse_dialog"); + if (!resolve_dependencies_force(td_, dependencies, "parse_dialog")) { + send_get_dialog_query(dialog_id, Auto(), 0, "parse_dialog"); + } return d; } -MessagesManager::Dialog *MessagesManager::on_load_dialog_from_database(DialogId dialog_id, const BufferSlice &value) { +MessagesManager::Dialog *MessagesManager::on_load_dialog_from_database(DialogId dialog_id, const BufferSlice &value, + const char *source) { CHECK(G()->parameters().use_message_db); if (!dialog_id.is_valid()) { @@ -35388,8 +35453,8 @@ MessagesManager::Dialog *MessagesManager::on_load_dialog_from_database(DialogId return old_d; } - LOG(INFO) << "Add new " << dialog_id << " from database"; - return add_new_dialog(parse_dialog(dialog_id, value), true); + LOG(INFO) << "Add new " << dialog_id << " from database from " << source; + return add_new_dialog(parse_dialog(dialog_id, value), true, source); } const DialogFilter *MessagesManager::get_server_dialog_filter(DialogFilterId dialog_filter_id) const { @@ -35717,7 +35782,8 @@ void MessagesManager::set_channel_pts(Dialog *d, int32 new_pts, const char *sour CHECK(d->dialog_id.get_type() == DialogType::Channel); LOG_IF(ERROR, running_get_channel_difference(d->dialog_id)) - << "Set pts of " << d->dialog_id << " to " << new_pts << " while running getChannelDifference"; + << "Set pts of " << d->dialog_id << " to " << new_pts << " from " << source + << " while running getChannelDifference"; if (is_debug_message_op_enabled()) { d->debug_message_op.emplace_back(Dialog::MessageOp::SetPts, new_pts, source); @@ -35725,7 +35791,7 @@ void MessagesManager::set_channel_pts(Dialog *d, int32 new_pts, const char *sour // TODO delete_first_messages support in channels if (new_pts == std::numeric_limits::max()) { - LOG(ERROR) << "Update " << d->dialog_id << " pts to -1"; + LOG(ERROR) << "Update " << d->dialog_id << " pts to -1 from " << source; G()->td_db()->get_binlog_pmc()->erase(get_channel_pts_key(d->dialog_id)); d->pts = std::numeric_limits::max(); if (d->pending_read_channel_inbox_pts != 0) { @@ -35735,9 +35801,10 @@ void MessagesManager::set_channel_pts(Dialog *d, int32 new_pts, const char *sour } if (new_pts > d->pts || (0 < new_pts && new_pts < d->pts - 99999)) { // pts can only go up or drop cardinally if (new_pts < d->pts - 99999) { - LOG(WARNING) << "Pts of " << d->dialog_id << " decreases from " << d->pts << " to " << new_pts; + LOG(WARNING) << "Pts of " << d->dialog_id << " decreases from " << d->pts << " to " << new_pts << " from " + << source; } else { - LOG(INFO) << "Update " << d->dialog_id << " pts to " << new_pts; + LOG(INFO) << "Update " << d->dialog_id << " pts to " << new_pts << " from " << source; } d->pts = new_pts; @@ -35756,7 +35823,8 @@ void MessagesManager::set_channel_pts(Dialog *d, int32 new_pts, const char *sour G()->td_db()->get_binlog_pmc()->set(get_channel_pts_key(d->dialog_id), to_string(new_pts)); } } else if (new_pts < d->pts) { - LOG(ERROR) << "Receive wrong pts " << new_pts << " in " << d->dialog_id << ". Current pts is " << d->pts; + LOG(ERROR) << "Receive wrong pts " << new_pts << " in " << d->dialog_id << " from " << source << ". Current pts is " + << d->pts; } } @@ -35771,7 +35839,7 @@ bool MessagesManager::need_channel_difference_to_add_message(DialogId dialog_id, return false; } - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "need_channel_difference_to_add_message"); if (d == nullptr || d->last_new_message_id == MessageId()) { return false; } @@ -36105,7 +36173,7 @@ void MessagesManager::on_get_channel_difference( LOG(INFO) << "----- END GET CHANNEL DIFFERENCE----- for " << dialog_id; CHECK(active_get_channel_differencies_.count(dialog_id) == 1); active_get_channel_differencies_.erase(dialog_id); - auto d = get_dialog_force(dialog_id); + auto d = get_dialog_force(dialog_id, "on_get_channel_difference"); if (difference_ptr == nullptr) { bool have_access = have_input_peer(dialog_id, AccessRights::Read); @@ -36150,7 +36218,7 @@ void MessagesManager::on_get_channel_difference( bool need_update_dialog_pos = false; if (d == nullptr) { - d = add_dialog(dialog_id); + d = add_dialog(dialog_id, "on_get_channel_difference"); need_update_dialog_pos = true; } @@ -36559,7 +36627,7 @@ void MessagesManager::try_hide_distance(DialogId dialog_id, const Message *m) { case DialogType::SecretChat: { auto user_id = td_->contacts_manager_->get_secret_chat_user_id(dialog_id.get_secret_chat_id()); if (user_id.is_valid()) { - d = get_dialog_force(DialogId(user_id)); + d = get_dialog_force(DialogId(user_id), "try_hide_distance"); } break; } @@ -36584,7 +36652,7 @@ MessagesManager::Message *MessagesManager::continue_send_message(DialogId dialog CHECK(m != nullptr); CHECK(m->content != nullptr); - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "continue_send_message"); if (d == nullptr) { LOG(ERROR) << "Can't find " << dialog_id << " to continue send a message"; binlog_erase(G()->td_db()->get_binlog(), log_event_id); @@ -36791,13 +36859,13 @@ void MessagesManager::on_binlog_events(vector &&events) { } resolve_dependencies_force(td_, dependencies, "ForwardMessagesLogEvent"); - Dialog *to_dialog = get_dialog_force(to_dialog_id); + Dialog *to_dialog = get_dialog_force(to_dialog_id, "ForwardMessagesLogEvent to"); if (to_dialog == nullptr) { LOG(ERROR) << "Can't find " << to_dialog_id << " to forward messages to"; binlog_erase(G()->td_db()->get_binlog(), event.id_); continue; } - Dialog *from_dialog = get_dialog_force(from_dialog_id); + Dialog *from_dialog = get_dialog_force(from_dialog_id, "ForwardMessagesLogEvent from"); if (from_dialog == nullptr) { LOG(ERROR) << "Can't find " << from_dialog_id << " to forward messages from"; binlog_erase(G()->td_db()->get_binlog(), event.id_); @@ -36856,7 +36924,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); log_event.id_ = event.id_; - Dialog *d = get_dialog_force(log_event.full_message_id_.get_dialog_id()); + Dialog *d = get_dialog_force(log_event.full_message_id_.get_dialog_id(), "DeleteMessageLogEvent"); if (d != nullptr) { auto message_id = log_event.full_message_id_.get_message_id(); if (message_id.is_valid_scheduled() && message_id.is_scheduled_server()) { @@ -36879,7 +36947,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); auto dialog_id = log_event.dialog_id_; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "DeleteMessagesFromServerLogEvent"); if (d == nullptr || !have_input_peer(dialog_id, AccessRights::Read)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -36900,7 +36968,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); auto dialog_id = log_event.dialog_id_; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "DeleteScheduledMessagesFromServerLogEvent"); if (d == nullptr || !have_input_peer(dialog_id, AccessRights::Read)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -36924,7 +36992,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); auto dialog_id = log_event.dialog_id_; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "DeleteDialogHistoryFromServerLogEvent"); if (d == nullptr || !have_input_peer(dialog_id, AccessRights::Read)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -36986,7 +37054,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); auto dialog_id = log_event.dialog_id_; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "ReadHistoryOnServerLogEvent"); if (d == nullptr || !have_input_peer(dialog_id, AccessRights::Read)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -37037,7 +37105,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); auto dialog_id = log_event.dialog_id_; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "ReadMessageThreadHistoryOnServerLogEvent"); if (d == nullptr || !have_input_peer(dialog_id, AccessRights::Read)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -37063,7 +37131,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); auto dialog_id = log_event.dialog_id_; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "ReadMessageContentsOnServerLogEvent"); if (d == nullptr || !have_input_peer(dialog_id, AccessRights::Read)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -37082,7 +37150,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); auto dialog_id = log_event.dialog_id_; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "ReadAllDialogMentionsOnServerLogEvent"); if (d == nullptr || !have_input_peer(dialog_id, AccessRights::Read)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -37101,7 +37169,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); auto dialog_id = log_event.dialog_id_; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "ToggleDialogIsPinnedOnServerLogEvent"); if (d == nullptr || !have_input_peer(dialog_id, AccessRights::Read)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -37121,7 +37189,7 @@ void MessagesManager::on_binlog_events(vector &&events) { vector dialog_ids; for (auto &dialog_id : log_event.dialog_ids_) { - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "ReorderPinnedDialogsOnServerLogEvent"); if (d != nullptr && have_input_peer(dialog_id, AccessRights::Read)) { dialog_ids.push_back(dialog_id); } @@ -37146,7 +37214,7 @@ void MessagesManager::on_binlog_events(vector &&events) { auto dialog_id = log_event.dialog_id_; bool have_info = dialog_id.get_type() == DialogType::User ? td_->contacts_manager_->have_user_force(dialog_id.get_user_id()) - : have_dialog_force(dialog_id); + : have_dialog_force(dialog_id, "ToggleDialogIsMarkedAsUnreadOnServerLogEvent"); if (!have_info || !have_input_peer(dialog_id, AccessRights::Know)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -37165,7 +37233,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); auto dialog_id = log_event.dialog_id_; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "ToggleDialogIsBlockedOnServerLogEvent"); if (d == nullptr || !have_input_peer(dialog_id, AccessRights::Read)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -37184,7 +37252,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); auto dialog_id = log_event.dialog_id_; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "SaveDialogDraftMessageOnServerLogEvent"); if (d == nullptr || !have_input_peer(dialog_id, AccessRights::Write)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -37204,7 +37272,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); auto dialog_id = log_event.dialog_id_; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "UpdateDialogNotificationSettingsOnServerLogEvent"); if (d == nullptr || !have_input_peer(dialog_id, AccessRights::Read)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -37238,7 +37306,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); auto dialog_id = log_event.dialog_id_; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "ChangeDialogReportSpamStateOnServerLogEvent"); if (d == nullptr || !have_input_peer(dialog_id, AccessRights::Read)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -37257,7 +37325,7 @@ void MessagesManager::on_binlog_events(vector &&events) { log_event_parse(log_event, event.data_).ensure(); auto dialog_id = log_event.dialog_id_; - Dialog *d = get_dialog_force(dialog_id); + Dialog *d = get_dialog_force(dialog_id, "SetDialogFolderIdOnServerLogEvent"); if (d == nullptr || !have_input_peer(dialog_id, AccessRights::Read)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; @@ -37283,14 +37351,14 @@ void MessagesManager::on_binlog_events(vector &&events) { add_dialog_dependencies(dependencies, dialog_id); resolve_dependencies_force(td_, dependencies, "GetDialogFromServerLogEvent"); - get_dialog_force(dialog_id); // load it if exists + get_dialog_force(dialog_id, "GetDialogFromServerLogEvent"); // load it if exists if (!have_input_peer(dialog_id, AccessRights::Read)) { binlog_erase(G()->td_db()->get_binlog(), event.id_); break; } - send_get_dialog_query(dialog_id, Promise(), event.id_, "GetDialogFromServerLogEvent"); + send_get_dialog_query(dialog_id, Auto(), event.id_, "GetDialogFromServerLogEvent"); break; } case LogEvent::HandlerType::UnpinAllDialogMessagesOnServer: { @@ -37420,7 +37488,7 @@ bool MessagesManager::load_recently_found_dialogs(Promise &promise) { CHECK(dialog_id.is_valid()); // TODO use asynchronous load // get_dialog(dialog_id, resolve_recently_found_dialogs_multipromise_.get_promise()); - get_dialog_force(dialog_id); + get_dialog_force(dialog_id, "load_recently_found_dialogs"); } } resolve_recently_found_dialogs_multipromise_.get_promise().set_value(Unit()); @@ -37434,7 +37502,7 @@ bool MessagesManager::load_recently_found_dialogs(Promise &promise) { } Status MessagesManager::add_recently_found_dialog(DialogId dialog_id) { - if (!have_dialog_force(dialog_id)) { + if (!have_dialog_force(dialog_id, "add_recently_found_dialog")) { return Status::Error(5, "Chat not found"); } if (add_recently_found_dialog_internal(dialog_id)) { @@ -37445,7 +37513,7 @@ Status MessagesManager::add_recently_found_dialog(DialogId dialog_id) { } Status MessagesManager::remove_recently_found_dialog(DialogId dialog_id) { - if (!have_dialog_force(dialog_id)) { + if (!have_dialog_force(dialog_id, "remove_recently_found_dialog")) { return Status::Error(5, "Chat not found"); } if (remove_recently_found_dialog_internal(dialog_id)) { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 3efa4b0b5..cf0529f59 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -8,7 +8,6 @@ #include "td/telegram/AccessRights.h" #include "td/telegram/ChannelId.h" -#include "td/telegram/Dependencies.h" #include "td/telegram/DialogAction.h" #include "td/telegram/DialogDate.h" #include "td/telegram/DialogDb.h" @@ -81,17 +80,12 @@ namespace td { struct BinlogEvent; - +struct Dependencies; class DialogFilter; - class DraftMessage; - struct InputMessageContent; - class MessageContent; - class MultiSequenceDispatcher; - class Td; class MessagesManager : public Actor { @@ -508,7 +502,7 @@ class MessagesManager : public Actor { tl_object_ptr get_chat_events_object(int64 random_id); bool have_dialog(DialogId dialog_id) const; - bool have_dialog_force(DialogId dialog_id); + bool have_dialog_force(DialogId dialog_id, const char *source = "have_dialog_force"); bool have_dialog_info(DialogId dialog_id) const; bool have_dialog_info_force(DialogId dialog_id) const; @@ -1118,6 +1112,8 @@ class MessagesManager : public Actor { int32 last_edit_pts = 0; + const char *debug_source = nullptr; + unique_ptr left; unique_ptr right; @@ -1824,7 +1820,7 @@ class MessagesManager : public Actor { void delete_messages_from_updates(const vector &message_ids); void delete_dialog_messages(DialogId dialog_id, const vector &message_ids, bool from_updates, - bool skip_update_for_not_found_messages); + bool skip_update_for_not_found_messages, const char *source); void update_dialog_pinned_messages_from_updates(DialogId dialog_id, const vector &message_ids, bool is_pin); @@ -1920,7 +1916,7 @@ class MessagesManager : public Actor { void do_delete_all_dialog_messages(Dialog *d, unique_ptr &message, bool is_permanently_deleted, vector &deleted_message_ids); - void delete_message_from_server(DialogId dialog_id, MessageId message_ids, bool revoke); + void delete_sent_message_from_server(DialogId dialog_id, MessageId message_id); void delete_messages_from_server(DialogId dialog_id, vector message_ids, bool revoke, uint64 log_event_id, Promise &&promise); @@ -2416,9 +2412,9 @@ class MessagesManager : public Actor { MessageId get_message_id_by_random_id(Dialog *d, int64 random_id, const char *source); - Dialog *add_dialog(DialogId dialog_id); + Dialog *add_dialog(DialogId dialog_id, const char *source); - Dialog *add_new_dialog(unique_ptr &&d, bool is_loaded_from_database); + Dialog *add_new_dialog(unique_ptr &&d, bool is_loaded_from_database, const char *source); void fix_new_dialog(Dialog *d, unique_ptr &&last_database_message, MessageId last_database_message_id, int64 order, int32 last_clear_history_date, MessageId last_clear_history_message_id, @@ -2440,9 +2436,9 @@ class MessagesManager : public Actor { Dialog *get_dialog(DialogId dialog_id); const Dialog *get_dialog(DialogId dialog_id) const; - Dialog *get_dialog_force(DialogId dialog_id); + Dialog *get_dialog_force(DialogId dialog_id, const char *source = "get_dialog_force"); - Dialog *on_load_dialog_from_database(DialogId dialog_id, const BufferSlice &value); + Dialog *on_load_dialog_from_database(DialogId dialog_id, const BufferSlice &value, const char *source); void on_get_dialogs_from_database(FolderId folder_id, int32 limit, DialogDbGetDialogsResult &&dialogs, Promise &&promise); diff --git a/td/telegram/PrivacyManager.cpp b/td/telegram/PrivacyManager.cpp index 399c292ab..7f9618b9e 100644 --- a/td/telegram/PrivacyManager.cpp +++ b/td/telegram/PrivacyManager.cpp @@ -151,7 +151,7 @@ void PrivacyManager::UserPrivacySettingRule::set_chat_ids(const vector &d auto td = G()->td().get_actor_unsafe(); for (auto dialog_id_int : dialog_ids) { DialogId dialog_id(dialog_id_int); - if (!td->messages_manager_->have_dialog_force(dialog_id)) { + if (!td->messages_manager_->have_dialog_force(dialog_id, "UserPrivacySettingRule::set_chat_ids")) { LOG(ERROR) << "Ignore not found " << dialog_id; continue; } diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index c14b3c4e9..6d4173de3 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3191,6 +3191,10 @@ bool Td::is_online() const { } void Td::set_is_bot_online(bool is_bot_online) { + if (G()->shared_config().get_option_integer("session_count") > 1) { + is_bot_online = false; + } + if (is_bot_online == is_bot_online_) { return; } @@ -6376,7 +6380,7 @@ void Td::on_request(uint64 id, const td_api::leaveChat &request) { CREATE_OK_REQUEST_PROMISE(); DialogId dialog_id(request.chat_id_); td_api::object_ptr new_status = td_api::make_object(); - if (dialog_id.get_type() == DialogType::Channel && messages_manager_->have_dialog_force(dialog_id)) { + if (dialog_id.get_type() == DialogType::Channel && messages_manager_->have_dialog_force(dialog_id, "leaveChat")) { auto status = contacts_manager_->get_channel_status(dialog_id.get_channel_id()); if (status.is_creator()) { if (!status.is_member()) { @@ -7213,7 +7217,7 @@ void Td::on_request(uint64 id, const td_api::resetAllNotificationSettings &reque void Td::on_request(uint64 id, const td_api::getMapThumbnailFile &request) { DialogId dialog_id(request.chat_id_); - if (!messages_manager_->have_dialog_force(dialog_id)) { + if (!messages_manager_->have_dialog_force(dialog_id, "getMapThumbnailFile")) { dialog_id = DialogId(); } diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 52eacfeec..3ca0835ed 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -2823,9 +2823,9 @@ void UpdatesManager::on_update(tl_object_ptr update, Promise &&promise) { DialogId dialog_id(ChatId(update->chat_id_)); - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "updateGroupCall")) { dialog_id = DialogId(ChannelId(update->chat_id_)); - if (!td_->messages_manager_->have_dialog_force(dialog_id)) { + if (!td_->messages_manager_->have_dialog_force(dialog_id, "updateGroupCall")) { dialog_id = DialogId(); } } diff --git a/tddb/td/db/SqliteKeyValue.cpp b/tddb/td/db/SqliteKeyValue.cpp index fe9e4d882..c51325b2d 100644 --- a/tddb/td/db/SqliteKeyValue.cpp +++ b/tddb/td/db/SqliteKeyValue.cpp @@ -66,7 +66,7 @@ SqliteKeyValue::SeqNo SqliteKeyValue::set(Slice key, Slice value) { set_stmt_.bind_blob(2, value).ensure(); auto status = set_stmt_.step(); if (status.is_error()) { - LOG(FATAL) << "Failed to set \"" << key << '"'; + LOG(FATAL) << "Failed to set \"" << key << "\": " << status.error(); } // set_stmt_.step().ensure(); set_stmt_.reset(); diff --git a/tdutils/td/utils/Status.cpp b/tdutils/td/utils/Status.cpp index bb3f6e780..2d9f7f3c8 100644 --- a/tdutils/td/utils/Status.cpp +++ b/tdutils/td/utils/Status.cpp @@ -42,12 +42,16 @@ string winerror_to_string(int code) { wchar_t wbuf[size]; auto res_size = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nullptr, code, 0, wbuf, size - 1, nullptr); if (res_size == 0) { - return "Unknown windows error"; + return "Unknown Windows error"; } while (res_size != 0 && (wbuf[res_size - 1] == '\n' || wbuf[res_size - 1] == '\r')) { res_size--; } - return from_wstring(wbuf, res_size).ok(); + auto error_message = from_wstring(wbuf, res_size); + if (error_message.is_error()) { + return "Invalid Windows error"; + } + return error_message.move_as_ok(); } #endif diff --git a/tdutils/td/utils/port/CxCli.h b/tdutils/td/utils/port/CxCli.h index 9e01984e8..a22b2fe38 100644 --- a/tdutils/td/utils/port/CxCli.h +++ b/tdutils/td/utils/port/CxCli.h @@ -85,7 +85,11 @@ inline std::string string_to_unmanaged(String^ str) { if (!str) { return std::string(); } - return td::from_wstring(str->Data(), str->Length()).ok(); + auto r_unmanaged_str = td::from_wstring(str->Data(), str->Length()); + if (r_unmanaged_str.is_error()) { + return std::string(); + } + return r_unmanaged_str.move_as_ok(); } inline String^ string_from_unmanaged(const std::string &from) { diff --git a/tdutils/td/utils/port/FromApp.h b/tdutils/td/utils/port/FromApp.h index 71cdfb9bb..db607b543 100644 --- a/tdutils/td/utils/port/FromApp.h +++ b/tdutils/td/utils/port/FromApp.h @@ -76,8 +76,8 @@ inline BOOL DeleteFileFromAppW(_In_ LPCWSTR lpFileName) { } inline BOOL MoveFileExFromAppW(_In_ LPCWSTR lpExistingFileName, _In_ LPCWSTR lpNewFileName, _In_ DWORD dwFlags) { - auto func = get_from_app_function<4>("MoveFileFromAppW", &MoveFile); - if (func == &MoveFile || (dwFlags & ~MOVEFILE_REPLACE_EXISTING) != 0) { + auto func = get_from_app_function<4>("MoveFileFromAppW", static_cast(nullptr)); + if (func == nullptr || (dwFlags & ~MOVEFILE_REPLACE_EXISTING) != 0) { // if can't find MoveFileFromAppW or have unsupported flags, call MoveFileEx directly return MoveFileEx(lpExistingFileName, lpNewFileName, dwFlags); } diff --git a/tdutils/td/utils/port/SocketFd.cpp b/tdutils/td/utils/port/SocketFd.cpp index 83d418a0c..b74a078f9 100644 --- a/tdutils/td/utils/port/SocketFd.cpp +++ b/tdutils/td/utils/port/SocketFd.cpp @@ -393,7 +393,8 @@ class SocketFdImpl { int native_fd = get_native_fd().socket(); TRY_RESULT(slices_size, narrow_cast_safe(slices.size())); auto write_res = detail::skip_eintr([&] { -#ifdef MSG_NOSIGNAL + // sendmsg can erroneously return 2^32 - 1 on Android 5.1 and Android 6.0, so it must not be used there +#if defined(MSG_NOSIGNAL) && !TD_ANDROID msghdr msg; std::memset(&msg, 0, sizeof(msg)); msg.msg_iov = const_cast(slices.begin()); @@ -412,7 +413,7 @@ class SocketFdImpl { } left -= slice.iov_len; } - LOG(FATAL) << "Receive " << result << " as writev response, but tried to write only " << result - left + LOG(FATAL) << "Receive " << write_res << " as writev response, but tried to write only " << result - left << " bytes"; } return write_finish(); @@ -430,7 +431,7 @@ class SocketFdImpl { }); if (write_res >= 0) { auto result = narrow_cast(write_res); - LOG_CHECK(result <= slice.size()) << "Receive " << result << " as write response, but tried to write only " + LOG_CHECK(result <= slice.size()) << "Receive " << write_res << " as write response, but tried to write only " << slice.size() << " bytes"; return result; } diff --git a/tdutils/td/utils/port/path.cpp b/tdutils/td/utils/port/path.cpp index 83ac617ad..865467641 100644 --- a/tdutils/td/utils/port/path.cpp +++ b/tdutils/td/utils/port/path.cpp @@ -470,7 +470,7 @@ CSlice get_temporary_dir() { } auto rs = from_wstring(buf); LOG_IF(FATAL, rs.is_error()) << "GetTempPathW failed: " << rs.error(); - temporary_dir = rs.ok(); + temporary_dir = rs.move_as_ok(); } if (temporary_dir.size() > 1 && temporary_dir.back() == TD_DIR_SLASH) { temporary_dir.pop_back(); diff --git a/tdutils/td/utils/port/wstring_convert.cpp b/tdutils/td/utils/port/wstring_convert.cpp index 2f53d5192..45ad32065 100644 --- a/tdutils/td/utils/port/wstring_convert.cpp +++ b/tdutils/td/utils/port/wstring_convert.cpp @@ -18,7 +18,7 @@ namespace td { Result to_wstring(CSlice slice) { if (!check_utf8(slice)) { - return Status::Error("Wrong encoding"); + return Status::Error("Wrong string encoding"); } size_t wstring_len = utf8_utf16_length(slice); @@ -65,7 +65,7 @@ Result from_wstring(const wchar_t *begin, size_t size) { } } - return Status::Error("Wrong encoding"); + return Status::Error("Wrong wstring encoding"); } result_len += 1 + (cur >= 0x80) + (cur >= 0x800); }