From 92f9f8bdd0be2766eda1c48d645ab16e2ea9a1a8 Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 24 Sep 2022 22:17:17 +0300 Subject: [PATCH] Support extended media polling. --- td/telegram/InputInvoice.cpp | 4 +++ td/telegram/InputInvoice.h | 2 ++ td/telegram/MessageContent.cpp | 8 +++++ td/telegram/MessageContent.h | 2 ++ td/telegram/MessageExtendedMedia.h | 4 +++ td/telegram/MessagesManager.cpp | 56 ++++++++++++++++++++++++++++++ td/telegram/MessagesManager.h | 4 +++ 7 files changed, 80 insertions(+) diff --git a/td/telegram/InputInvoice.cpp b/td/telegram/InputInvoice.cpp index 11de7e3ce..0848f62d1 100644 --- a/td/telegram/InputInvoice.cpp +++ b/td/telegram/InputInvoice.cpp @@ -384,6 +384,10 @@ bool InputInvoice::update_extended_media(telegram_api::object_ptr get_product_description_object(const string &description) { FormattedText result; result.text = description; diff --git a/td/telegram/InputInvoice.h b/td/telegram/InputInvoice.h index 8a2f4da31..607d74306 100644 --- a/td/telegram/InputInvoice.h +++ b/td/telegram/InputInvoice.h @@ -97,6 +97,8 @@ struct InputInvoice { bool update_extended_media(telegram_api::object_ptr extended_media, DialogId owner_dialog_id, Td *td); + + bool need_poll_extended_media() const; }; bool operator==(const InputInvoice &lhs, const InputInvoice &rhs); diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 03243384b..fec66c5de 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -5709,6 +5709,14 @@ bool update_message_content_extended_media(MessageContent *content, owner_dialog_id, td); } +bool need_poll_message_content_extended_media(const MessageContent *content) { + CHECK(content != nullptr); + if (content->get_type() != MessageContentType::Invoice) { + return false; + } + return static_cast(content)->input_invoice.need_poll_extended_media(); +} + void get_message_content_animated_emoji_click_sticker(const MessageContent *content, FullMessageId full_message_id, Td *td, Promise> &&promise) { if (content->get_type() != MessageContentType::Text) { diff --git a/td/telegram/MessageContent.h b/td/telegram/MessageContent.h index 0703a199c..1fa8658d8 100644 --- a/td/telegram/MessageContent.h +++ b/td/telegram/MessageContent.h @@ -234,6 +234,8 @@ bool update_message_content_extended_media(MessageContent *content, telegram_api::object_ptr extended_media, DialogId owner_dialog_id, Td *td); +bool need_poll_message_content_extended_media(const MessageContent *content); + void get_message_content_animated_emoji_click_sticker(const MessageContent *content, FullMessageId full_message_id, Td *td, Promise> &&promise); diff --git a/td/telegram/MessageExtendedMedia.h b/td/telegram/MessageExtendedMedia.h index f20728832..cffc9b5cb 100644 --- a/td/telegram/MessageExtendedMedia.h +++ b/td/telegram/MessageExtendedMedia.h @@ -78,6 +78,10 @@ class MessageExtendedMedia { return type_ == Type::Unsupported && unsupported_version_ < CURRENT_VERSION; } + bool need_poll() const { + return type_ == Type::Preview; + } + bool has_media_timestamp() const { return type_ == Type::Video; } diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 0754ffb84..0f8afa79e 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -1956,6 +1956,42 @@ class GetMessagesViewsQuery final : public Td::ResultHandler { } }; +class GetExtendedMediaQuery final : public Td::ResultHandler { + DialogId dialog_id_; + vector message_ids_; + + public: + void send(DialogId dialog_id, vector &&message_ids) { + dialog_id_ = dialog_id; + message_ids_ = std::move(message_ids); + + auto input_peer = td_->messages_manager_->get_input_peer(dialog_id, AccessRights::Read); + if (input_peer == nullptr) { + return on_error(Status::Error(400, "Can't access the chat")); + } + + send_query(G()->net_query_creator().create(telegram_api::messages_getExtendedMedia( + std::move(input_peer), MessagesManager::get_server_message_ids(message_ids_)))); + } + + void on_result(BufferSlice packet) final { + auto result_ptr = fetch_result(packet); + if (result_ptr.is_error()) { + return on_error(result_ptr.move_as_error()); + } + + auto ptr = result_ptr.move_as_ok(); + LOG(INFO) << "Receive result for GetExtendedMediaQuery: " << to_string(ptr); + td_->updates_manager_->on_get_updates(std::move(ptr), Promise()); + td_->messages_manager_->finish_get_message_extended_media(dialog_id_, message_ids_); + } + + void on_error(Status status) final { + td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "GetExtendedMediaQuery"); + td_->messages_manager_->finish_get_message_extended_media(dialog_id_, message_ids_); + } +}; + class ReadMessagesContentsQuery final : public Td::ResultHandler { Promise promise_; @@ -12846,10 +12882,12 @@ void MessagesManager::on_update_viewed_messages_timeout(DialogId dialog_id) { CHECK(info != nullptr); vector reaction_message_ids; vector views_message_ids; + vector extended_media_message_ids; for (auto &message_it : info->message_id_to_view_id) { Message *m = get_message_force(d, message_it.first, "on_update_viewed_messages_timeout"); CHECK(m != nullptr); CHECK(m->message_id.is_valid()); + CHECK(m->message_id.is_server()); if (need_poll_message_reactions(d, m)) { reaction_message_ids.push_back(m->message_id); } @@ -12857,6 +12895,10 @@ void MessagesManager::on_update_viewed_messages_timeout(DialogId dialog_id) { m->has_get_message_views_query = true; views_message_ids.push_back(m->message_id); } + if (need_poll_message_content_extended_media(m->content.get()) && !m->has_get_extended_media_query) { + m->has_get_extended_media_query = true; + extended_media_message_ids.push_back(m->message_id); + } } if (!reaction_message_ids.empty()) { @@ -12865,6 +12907,9 @@ void MessagesManager::on_update_viewed_messages_timeout(DialogId dialog_id) { if (!views_message_ids.empty()) { td_->create_handler()->send(dialog_id, std::move(views_message_ids), false); } + if (!extended_media_message_ids.empty()) { + td_->create_handler()->send(dialog_id, std::move(extended_media_message_ids)); + } update_viewed_messages_timeout_.add_timeout_in(dialog_id.get(), UPDATE_VIEWED_MESSAGES_PERIOD); } @@ -21097,6 +21142,17 @@ void MessagesManager::finish_get_message_views(DialogId dialog_id, const vector< } } +void MessagesManager::finish_get_message_extended_media(DialogId dialog_id, const vector &message_ids) { + Dialog *d = get_dialog(dialog_id); + CHECK(d != nullptr); + for (auto message_id : message_ids) { + auto *m = get_message(d, message_id); + if (m != nullptr) { + m->has_get_extended_media_query = false; + } + } +} + 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, "open_message_content"); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 896c29444..654adbc0b 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -716,6 +716,8 @@ class MessagesManager final : public Actor { void finish_get_message_views(DialogId dialog_id, const vector &message_ids); + void finish_get_message_extended_media(DialogId dialog_id, const vector &message_ids); + Status open_message_content(FullMessageId full_message_id) TD_WARN_UNUSED_RESULT; void click_animated_emoji_message(FullMessageId full_message_id, @@ -1173,6 +1175,8 @@ class MessagesManager final : public Actor { bool has_get_message_views_query = false; bool need_view_counter_increment = false; + bool has_get_extended_media_query = false; + DialogId real_forward_from_dialog_id; // for resend_message MessageId real_forward_from_message_id; // for resend_message