From 7ffd9ad7c90491e1ab8187f188e2d47c49520d5a Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 15 Jan 2020 02:44:24 +0300 Subject: [PATCH] Move checks for Game message content in dup_message_content. GitOrigin-RevId: e9d5ad1216c5178b4990b4f17bc5c808cd70cb6f --- td/telegram/MessageContent.cpp | 18 +++++++++++++----- td/telegram/MessageContent.h | 6 +++--- td/telegram/MessagesManager.cpp | 30 ++++++++---------------------- td/telegram/MessagesManager.h | 4 ++-- 4 files changed, 26 insertions(+), 32 deletions(-) diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 9fa40153..0e5af193 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -4091,15 +4091,22 @@ unique_ptr dup_message_content(Td *td, DialogId dialog_id, const CHECK(result->file_id.is_valid()); return std::move(result); } - case MessageContentType::Game: - return make_unique(*static_cast(content)); + case MessageContentType::Game: { + auto result = make_unique(*static_cast(content)); + if (type != MessageContentDupType::Forward && type != MessageContentDupType::SendViaBot && + !result->game.get_bot_user_id().is_valid()) { + LOG(INFO) << "Can't send/copy game without bot_user_id"; + return nullptr; + } + return std::move(result); + } case MessageContentType::Invoice: return make_unique(*static_cast(content)); case MessageContentType::LiveLocation: - if (to_secret || type != MessageContentDupType::Send) { - return make_unique(Location(static_cast(content)->location)); - } else { + if (!to_secret && (type == MessageContentDupType::Send || type == MessageContentDupType::SendViaBot)) { return make_unique(*static_cast(content)); + } else { + return make_unique(Location(static_cast(content)->location)); } case MessageContentType::Location: return make_unique(*static_cast(content)); @@ -5088,6 +5095,7 @@ void add_message_content_dependencies(Dependencies &dependencies, const MessageC case MessageContentType::PassportDataReceived: break; case MessageContentType::Poll: + // no need to add poll dependencies, because they are forcely loaded with the poll break; default: UNREACHABLE(); diff --git a/td/telegram/MessageContent.h b/td/telegram/MessageContent.h index f0d9a9e3..5fe7232c 100644 --- a/td/telegram/MessageContent.h +++ b/td/telegram/MessageContent.h @@ -178,10 +178,10 @@ vector get_message_content_added_user_ids(const MessageContent *content) UserId get_message_content_deleted_user_id(const MessageContent *content); -UserId get_message_content_game_bot_user_id(const MessageContent *content); - int32 get_message_content_live_location_period(const MessageContent *content); +UserId get_message_content_game_bot_user_id(const MessageContent *content); + bool get_message_content_poll_is_closed(const Td *td, const MessageContent *content); bool get_message_content_poll_is_anonymous(const Td *td, const MessageContent *content); @@ -221,7 +221,7 @@ unique_ptr get_message_content(Td *td, FormattedText message_tex DialogId owner_dialog_id, bool is_content_read, UserId via_bot_user_id, int32 *ttl); -enum class MessageContentDupType : int32 { Send, Forward, Copy, CopyWithoutCaption }; +enum class MessageContentDupType : int32 { Send, SendViaBot, Forward, Copy, CopyWithoutCaption }; unique_ptr dup_message_content(Td *td, DialogId dialog_id, const MessageContent *content, MessageContentDupType type); diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index f7f7798b..0ff2400a 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -17767,8 +17767,8 @@ Status MessagesManager::can_send_message(DialogId dialog_id) const { return Status::OK(); } -Status MessagesManager::can_send_message_content(DialogId dialog_id, const MessageContent *content, bool is_forward, - bool is_via_bot) const { +Status MessagesManager::can_send_message_content(DialogId dialog_id, const MessageContent *content, + bool is_forward) const { auto dialog_type = dialog_id.get_type(); int32 secret_chat_layer = std::numeric_limits::max(); if (dialog_type == DialogType::SecretChat) { @@ -17786,7 +17786,7 @@ Status MessagesManager::can_send_message_content(DialogId dialog_id, const Messa auto content_type = content->get_type(); switch (dialog_type) { case DialogType::User: - if (content_type == MessageContentType::Poll && !is_forward && !td_->auth_manager_->is_bot() && !is_via_bot && + if (content_type == MessageContentType::Poll && !is_forward && !td_->auth_manager_->is_bot() && !td_->contacts_manager_->is_user_bot(dialog_id.get_user_id()) && dialog_id != get_my_dialog_id()) { return Status::Error(400, "Polls can't be sent to the private chat"); } @@ -17846,9 +17846,6 @@ Status MessagesManager::can_send_message_content(DialogId dialog_id, const Messa if (!can_send_games) { return Status::Error(400, "Not enough rights to send games to the chat"); } - if (!is_forward && !is_via_bot && !get_message_content_game_bot_user_id(content).is_valid()) { - return Status::Error(400, "Games can't be copied"); - } break; case MessageContentType::Invoice: if (!is_forward) { @@ -19168,12 +19165,12 @@ Result MessagesManager::send_inline_query_result_message(DialogId dia } TRY_STATUS(can_use_send_message_options(send_message_options, content->message_content, 0)); - TRY_STATUS(can_send_message_content(dialog_id, content->message_content.get(), false, true)); + TRY_STATUS(can_send_message_content(dialog_id, content->message_content.get(), false)); bool need_update_dialog_pos = false; Message *m = get_message_to_send( d, get_reply_to_message_id(d, reply_to_message_id), send_message_options, - dup_message_content(td_, dialog_id, content->message_content.get(), MessageContentDupType::Send), + dup_message_content(td_, dialog_id, content->message_content.get(), MessageContentDupType::SendViaBot), &need_update_dialog_pos, nullptr, true); m->hide_via_bot = hide_via_bot; if (!hide_via_bot) { @@ -20678,17 +20675,13 @@ Result> MessagesManager::forward_messages(DialogId to_dialog_i continue; } - auto content_type = content->get_type(); - bool is_game = content_type == MessageContentType::Game; if (need_copy) { - if (is_game && !get_message_content_game_bot_user_id(content.get()).is_valid()) { - LOG(INFO) << "Can't copy game from " << message_id; - continue; - } copied_messages.push_back({std::move(content), forwarded_message->disable_web_page_preview, i}); continue; } + auto content_type = content->get_type(); + bool is_game = content_type == MessageContentType::Game; unique_ptr forward_info; if (!is_game && content_type != MessageContentType::Audio) { DialogId saved_from_dialog_id; @@ -20903,13 +20896,6 @@ Result> MessagesManager::resend_messages(DialogId dialog_id, v continue; } - if (content->get_type() == MessageContentType::Game && - !get_message_content_game_bot_user_id(content.get()).is_valid()) { - // must not happen - LOG(ERROR) << "Can't resend game from " << m->message_id; - continue; - } - new_contents[i] = std::move(content); if (m->media_album_id != 0) { @@ -29337,7 +29323,7 @@ void MessagesManager::on_binlog_events(vector &&events) { add_message_dependencies(dependencies, dialog_id, m.get()); resolve_dependencies_force(dependencies); - m->content = dup_message_content(td_, dialog_id, m->content.get(), MessageContentDupType::Send); + m->content = dup_message_content(td_, dialog_id, m->content.get(), MessageContentDupType::SendViaBot); auto result_message = continue_send_message(dialog_id, std::move(m), event.id_); if (result_message != nullptr) { diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 8b9c0d35..8a55df4e 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -1530,8 +1530,8 @@ class MessagesManager : public Actor { Status can_send_message(DialogId dialog_id) const TD_WARN_UNUSED_RESULT; - Status can_send_message_content(DialogId dialog_id, const MessageContent *content, bool is_forward, - bool is_via_bot = false) const TD_WARN_UNUSED_RESULT; + Status can_send_message_content(DialogId dialog_id, const MessageContent *content, + bool is_forward) const TD_WARN_UNUSED_RESULT; static bool can_resend_message(const Message *m);