diff --git a/td/telegram/MessageSender.cpp b/td/telegram/MessageSender.cpp index 8ff7ed527..7ff83f39e 100644 --- a/td/telegram/MessageSender.cpp +++ b/td/telegram/MessageSender.cpp @@ -102,7 +102,7 @@ Result get_message_sender_dialog_id(Td *td, if (allow_empty) { return DialogId(); } - return Status::Error(400, "Member identifier is not specified"); + return Status::Error(400, "Message sender must be non-empty"); } switch (message_sender_id->get_id()) { case td_api::messageSenderUser::ID: { @@ -126,8 +126,12 @@ Result get_message_sender_dialog_id(Td *td, } return Status::Error(400, "Invalid chat identifier specified"); } - if (check_access && !td->messages_manager_->have_dialog_force(dialog_id, "get_message_sender_dialog_id")) { - return Status::Error(400, "Unknown chat identifier specified"); + if (check_access) { + bool is_user = dialog_id.get_type() == DialogType::User; + if (is_user ? !td->contacts_manager_->have_user_force(dialog_id.get_user_id()) + : !td->messages_manager_->have_dialog_force(dialog_id, "get_message_sender_dialog_id")) { + return Status::Error(400, "Unknown chat identifier specified"); + } } return dialog_id; } diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 05c489f78..50b745215 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -19202,52 +19202,30 @@ void MessagesManager::toggle_dialog_is_marked_as_unread_on_server(DialogId dialo Status MessagesManager::toggle_message_sender_is_blocked(const td_api::object_ptr &sender, bool is_blocked) { - if (sender == nullptr) { - return Status::Error(400, "Message sender must be non-empty"); - } - - UserId sender_user_id; - DialogId dialog_id; - switch (sender->get_id()) { - case td_api::messageSenderUser::ID: - sender_user_id = UserId(static_cast(sender.get())->user_id_); + TRY_RESULT(dialog_id, get_message_sender_dialog_id(td_, sender, true, false)); + switch (dialog_id.get_type()) { + case DialogType::User: + if (dialog_id == get_my_dialog_id()) { + return Status::Error(400, is_blocked ? Slice("Can't block self") : Slice("Can't unblock self")); + } break; - case td_api::messageSenderChat::ID: { - auto sender_dialog_id = DialogId(static_cast(sender.get())->chat_id_); - if (!have_dialog_force(sender_dialog_id, "toggle_message_sender_is_blocked")) { - return Status::Error(400, "Sender chat not found"); - } - - switch (sender_dialog_id.get_type()) { - case DialogType::User: - sender_user_id = sender_dialog_id.get_user_id(); - break; - case DialogType::Chat: - return Status::Error(400, "Basic group chats can't be blocked"); - case DialogType::Channel: - dialog_id = sender_dialog_id; - break; - case DialogType::SecretChat: - sender_user_id = td_->contacts_manager_->get_secret_chat_user_id(sender_dialog_id.get_secret_chat_id()); - break; - case DialogType::None: - default: - UNREACHABLE(); + case DialogType::Chat: + return Status::Error(400, "Basic group chats can't be blocked"); + case DialogType::Channel: + // ok + break; + 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() || !td_->contacts_manager_->have_user_force(user_id)) { + return Status::Error(400, "The secret chat can't be blocked"); } + dialog_id = DialogId(user_id); break; } + case DialogType::None: default: UNREACHABLE(); } - if (!dialog_id.is_valid()) { - if (!td_->contacts_manager_->have_user_force(sender_user_id)) { - return Status::Error(400, "Sender user not found"); - } - dialog_id = DialogId(sender_user_id); - } - if (dialog_id == get_my_dialog_id()) { - return Status::Error(400, is_blocked ? Slice("Can't block self") : Slice("Can't unblock self")); - } Dialog *d = get_dialog_force(dialog_id, "toggle_message_sender_is_blocked"); if (!have_input_peer(dialog_id, AccessRights::Know)) {