From 073541718d6ce02784a0e49b698f6877d69b24a2 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 27 Sep 2018 20:23:48 +0300 Subject: [PATCH] Separate method for InlineQueriesManager::create_inline_message_content. GitOrigin-RevId: 6ea0152a182ef09cc1960f1fd16b081c1654dc1e --- td/telegram/InlineQueriesManager.cpp | 61 +++++++++++++++------------- td/telegram/InlineQueriesManager.h | 4 ++ 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/td/telegram/InlineQueriesManager.cpp b/td/telegram/InlineQueriesManager.cpp index 3494b997..843657cc 100644 --- a/td/telegram/InlineQueriesManager.cpp +++ b/td/telegram/InlineQueriesManager.cpp @@ -317,10 +317,9 @@ Result> InlineQueriesManager: return Status::Error(400, "Unallowed inline message content type"); } -bool InlineQueriesManager::register_inline_message_content( - int64 query_id, const string &result_id, FileId file_id, - tl_object_ptr &&inline_message, int32 allowed_media_content_id, Photo *photo, - Game *game) { +InlineMessageContent InlineQueriesManager::create_inline_message_content( + FileId file_id, tl_object_ptr &&inline_message, int32 allowed_media_content_id, + Photo *photo, Game *game) { CHECK(inline_message != nullptr); CHECK((allowed_media_content_id == td_api::inputMessagePhoto::ID) == (photo != nullptr)); CHECK((allowed_media_content_id == td_api::inputMessageGame::ID) == (game != nullptr)); @@ -328,9 +327,9 @@ bool InlineQueriesManager::register_inline_message_content( allowed_media_content_id != td_api::inputMessageGame::ID && allowed_media_content_id != -1) == file_id.is_valid()); - unique_ptr message_content; + InlineMessageContent result; tl_object_ptr reply_markup; - bool disable_web_page_preview = false; + result.disable_web_page_preview = false; switch (inline_message->get_id()) { case telegram_api::botInlineMessageText::ID: { auto inline_message_text = move_tl_object_as(inline_message); @@ -343,14 +342,14 @@ bool InlineQueriesManager::register_inline_message_content( break; } - disable_web_page_preview = + result.disable_web_page_preview = (inline_message_text->flags_ & telegram_api::botInlineMessageText::NO_WEBPAGE_MASK) != 0; WebPageId web_page_id; - if (!disable_web_page_preview) { + if (!result.disable_web_page_preview) { web_page_id = td_->web_pages_manager_->get_web_page_by_url(get_first_url(inline_message_text->message_, entities)); } - message_content = make_unique( + result.message_content = make_unique( FormattedText{std::move(inline_message_text->message_), std::move(entities)}, web_page_id); reply_markup = std::move(inline_message_text->reply_markup_); break; @@ -358,17 +357,17 @@ bool InlineQueriesManager::register_inline_message_content( case telegram_api::botInlineMessageMediaGeo::ID: { auto inline_message_geo = move_tl_object_as(inline_message); if (inline_message_geo->period_ > 0) { - message_content = + result.message_content = make_unique(Location(inline_message_geo->geo_), inline_message_geo->period_); } else { - message_content = make_unique(Location(inline_message_geo->geo_)); + result.message_content = make_unique(Location(inline_message_geo->geo_)); } reply_markup = std::move(inline_message_geo->reply_markup_); break; } case telegram_api::botInlineMessageMediaVenue::ID: { auto inline_message_venue = move_tl_object_as(inline_message); - message_content = make_unique( + result.message_content = make_unique( Venue(inline_message_venue->geo_, std::move(inline_message_venue->title_), std::move(inline_message_venue->address_), std::move(inline_message_venue->provider_), std::move(inline_message_venue->venue_id_), std::move(inline_message_venue->venue_type_))); @@ -377,7 +376,7 @@ bool InlineQueriesManager::register_inline_message_content( } case telegram_api::botInlineMessageMediaContact::ID: { auto inline_message_contact = move_tl_object_as(inline_message); - message_content = make_unique( + result.message_content = make_unique( Contact(std::move(inline_message_contact->phone_number_), std::move(inline_message_contact->first_name_), std::move(inline_message_contact->last_name_), std::move(inline_message_contact->vcard_), 0)); reply_markup = std::move(inline_message_contact->reply_markup_); @@ -391,41 +390,47 @@ bool InlineQueriesManager::register_inline_message_content( reply_markup = std::move(input_message_media_auto->reply_markup_); if (allowed_media_content_id == td_api::inputMessageAnimation::ID) { - message_content = make_unique(file_id, std::move(caption)); + result.message_content = make_unique(file_id, std::move(caption)); } else if (allowed_media_content_id == td_api::inputMessageAudio::ID) { - message_content = make_unique(file_id, std::move(caption)); + result.message_content = make_unique(file_id, std::move(caption)); } else if (allowed_media_content_id == td_api::inputMessageDocument::ID) { - message_content = make_unique(file_id, std::move(caption)); + result.message_content = make_unique(file_id, std::move(caption)); } else if (allowed_media_content_id == td_api::inputMessageGame::ID) { CHECK(game != nullptr); // TODO game->set_short_name(std::move(caption)); - message_content = make_unique(std::move(*game)); + result.message_content = make_unique(std::move(*game)); } else if (allowed_media_content_id == td_api::inputMessagePhoto::ID) { - message_content = make_unique(std::move(*photo), std::move(caption)); + result.message_content = make_unique(std::move(*photo), std::move(caption)); } else if (allowed_media_content_id == td_api::inputMessageSticker::ID) { - message_content = make_unique(file_id); + result.message_content = make_unique(file_id); } else if (allowed_media_content_id == td_api::inputMessageVideo::ID) { - message_content = make_unique(file_id, std::move(caption)); + result.message_content = make_unique(file_id, std::move(caption)); } else if (allowed_media_content_id == td_api::inputMessageVoiceNote::ID) { - message_content = make_unique(file_id, std::move(caption), true); + result.message_content = make_unique(file_id, std::move(caption), true); } else { input_message_media_auto->reply_markup_ = std::move(reply_markup); input_message_media_auto->message_ = std::move(caption.text); - inline_message = std::move(input_message_media_auto); + LOG(WARNING) << "Unallowed bot inline message " << to_string(input_message_media_auto); } break; } default: UNREACHABLE(); } - if (message_content != nullptr) { - inline_message_contents_[query_id][result_id] = { - std::move(message_content), - get_reply_markup(std::move(reply_markup), td_->auth_manager_->is_bot(), true, false), disable_web_page_preview}; + result.message_reply_markup = get_reply_markup(std::move(reply_markup), td_->auth_manager_->is_bot(), true, false); + return result; +} + +bool InlineQueriesManager::register_inline_message_content( + int64 query_id, const string &result_id, FileId file_id, + tl_object_ptr &&inline_message, int32 allowed_media_content_id, Photo *photo, + Game *game) { + InlineMessageContent content = + create_inline_message_content(file_id, std::move(inline_message), allowed_media_content_id, photo, game); + if (content.message_content != nullptr) { + inline_message_contents_[query_id].emplace(result_id, std::move(content)); return true; } - - LOG(WARNING) << "Unallowed bot inline message " << to_string(inline_message); return false; } diff --git a/td/telegram/InlineQueriesManager.h b/td/telegram/InlineQueriesManager.h index 5d010047..cc24668a 100644 --- a/td/telegram/InlineQueriesManager.h +++ b/td/telegram/InlineQueriesManager.h @@ -101,6 +101,10 @@ class InlineQueriesManager : public Actor { tl_object_ptr &&reply_markup_ptr, int32 allowed_media_content_id) const TD_WARN_UNUSED_RESULT; // TODO make static + InlineMessageContent create_inline_message_content(FileId file_id, + tl_object_ptr &&inline_message, + int32 allowed_media_content_id, Photo *photo, Game *game); + bool register_inline_message_content(int64 query_id, const string &result_id, FileId file_id, tl_object_ptr &&inline_message, int32 allowed_media_content_id, Photo *photo = nullptr, Game *game = nullptr);