From 4e1f2cbf8b85fffd2ad4e4e7c96409f98816d738 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 22 Feb 2022 15:58:16 +0300 Subject: [PATCH] Add td_api::searchOutgoingDocumentMessages. --- td/generate/scheme/td_api.tl | 8 +++- td/telegram/MessagesManager.cpp | 73 ++++++++++++++++++++++++++++++++- td/telegram/MessagesManager.h | 6 +++ td/telegram/Td.cpp | 9 +++- td/telegram/Td.h | 4 +- td/telegram/cli.cpp | 4 ++ 6 files changed, 100 insertions(+), 4 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index b4d98c66f..971410bc5 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -4439,9 +4439,15 @@ searchSecretMessages chat_id:int53 query:string offset:string limit:int32 filter //@description Searches for call messages. Returns the results in reverse chronological order (i. e., in order of decreasing message_id). For optimal performance, the number of returned messages is chosen by TDLib //@from_message_id Identifier of the message from which to search; use 0 to get results from the last message -//@limit The maximum number of messages to be returned; up to 100. For optimal performance, the number of returned messages is chosen by TDLib and can be smaller than the specified limit @only_missed If true, returns only messages with missed/declined calls +//@limit The maximum number of messages to be returned; up to 100. For optimal performance, the number of returned messages is chosen by TDLib and can be smaller than the specified limit +//@only_missed If true, returns only messages with missed/declined calls searchCallMessages from_message_id:int53 limit:int32 only_missed:Bool = Messages; +//@description Searches for outgoing messages with content of the type messageDocument in all chats except secret chats. Returns the results in reverse chronological order +//@query Query to search for in document file name and message caption +//@limit The maximum number of messages to be returned; up to 100 +searchOutgoingDocumentMessages query:string limit:int32 = FoundMessages; + //@description Deletes all call messages @revoke Pass true to delete the messages for all users deleteAllCallMessages revoke:Bool = Ok; diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 2bf14598b..b0ff02c2b 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -2636,6 +2636,45 @@ class GetAllScheduledMessagesQuery final : public Td::ResultHandler { } }; +class SearchSentMediaQuery final : public Td::ResultHandler { + Promise> promise_; + + public: + explicit SearchSentMediaQuery(Promise> &&promise) + : promise_(std::move(promise)) { + } + + void send(const string &query, int32 limit) { + send_query(G()->net_query_creator().create(telegram_api::messages_searchSentMedia( + query, telegram_api::make_object(), limit))); + } + + 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 info = td_->messages_manager_->get_messages_info(result_ptr.move_as_ok(), "SearchSentMediaQuery"); + td_->messages_manager_->get_channel_differences_if_needed( + std::move(info), + PromiseCreator::lambda([actor_id = td_->messages_manager_actor_.get(), + promise = std::move(promise_)](Result &&result) mutable { + if (result.is_error()) { + promise.set_error(result.move_as_error()); + } else { + auto info = result.move_as_ok(); + send_closure(actor_id, &MessagesManager::on_get_outgoing_document_messages, std::move(info.messages), + std::move(promise)); + } + })); + } + + void on_error(Status status) final { + promise_.set_error(std::move(status)); + } +}; + class GetRecentLocationsQuery final : public Td::ResultHandler { Promise> promise_; DialogId dialog_id_; @@ -10575,6 +10614,27 @@ void MessagesManager::on_failed_messages_search(int64 random_id) { found_messages_.erase(it); } +void MessagesManager::on_get_outgoing_document_messages(vector> &&messages, + Promise> &&promise) { + TRY_STATUS_PROMISE(promise, G()->close_status()); + + FoundMessages found_messages; + for (auto &message : messages) { + auto dialog_id = get_message_dialog_id(message); + auto full_message_id = on_get_message(std::move(message), false, dialog_id.get_type() == DialogType::Channel, false, + false, false, "on_get_outgoing_document_messages"); + if (full_message_id != FullMessageId()) { + CHECK(dialog_id == full_message_id.get_dialog_id()); + found_messages.full_message_ids.push_back(full_message_id); + } + } + auto result = get_found_messages_object(found_messages, "on_get_outgoing_document_messages"); + td::remove_if(result->messages_, + [](const auto &message) { return message->content_->get_id() != td_api::messageDocument::ID; }); + result->total_count_ = narrow_cast(result->messages_.size()); + promise.set_value(std::move(result)); +} + void MessagesManager::on_get_scheduled_server_messages(DialogId dialog_id, uint32 generation, vector> &&messages, bool is_not_modified) { @@ -22746,12 +22806,23 @@ std::pair> MessagesManager::search_call_messages(Me } } - LOG(DEBUG) << "Search call messages on server from " << from_message_id << " and with limit " << limit; td_->create_handler(std::move(promise)) ->send(DialogId(), "", DialogId(), from_message_id, 0, limit, filter, MessageId(), random_id); return result; } +void MessagesManager::search_outgoing_document_messages(const string &query, int32 limit, + Promise> &&promise) { + if (limit <= 0) { + return promise.set_error(Status::Error(400, "Parameter limit must be positive")); + } + if (limit > MAX_SEARCH_MESSAGES) { + limit = MAX_SEARCH_MESSAGES; + } + + td_->create_handler(std::move(promise))->send(query, limit); +} + void MessagesManager::search_dialog_recent_location_messages(DialogId dialog_id, int32 limit, Promise> &&promise) { LOG(INFO) << "Search recent location messages in " << dialog_id << " with limit " << limit; diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index e7b229b67..7e7577189 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -227,6 +227,9 @@ class MessagesManager final : public Actor { vector> &&messages, Promise &&promise); void on_failed_messages_search(int64 random_id); + void on_get_outgoing_document_messages(vector> &&messages, + Promise> &&promise); + void on_get_scheduled_server_messages(DialogId dialog_id, uint32 generation, vector> &&messages, bool is_not_modified); @@ -764,6 +767,9 @@ class MessagesManager final : public Actor { std::pair> search_call_messages(MessageId from_message_id, int32 limit, bool only_missed, int64 &random_id, bool use_db, Promise &&promise); + void search_outgoing_document_messages(const string &query, int32 limit, + Promise> &&promise); + void search_dialog_recent_location_messages(DialogId dialog_id, int32 limit, Promise> &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 17a4167e9..4d0a6c6ed 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5220,11 +5220,18 @@ void Td::on_request(uint64 id, td_api::searchMessages &request) { request.limit_, std::move(request.filter_), request.min_date_, request.max_date_); } -void Td::on_request(uint64 id, td_api::searchCallMessages &request) { +void Td::on_request(uint64 id, const td_api::searchCallMessages &request) { CHECK_IS_USER(); CREATE_REQUEST(SearchCallMessagesRequest, request.from_message_id_, request.limit_, request.only_missed_); } +void Td::on_request(uint64 id, td_api::searchOutgoingDocumentMessages &request) { + CHECK_IS_USER(); + CLEAN_INPUT_STRING(request.query_); + CREATE_REQUEST_PROMISE(); + messages_manager_->search_outgoing_document_messages(request.query_, request.limit_, std::move(promise)); +} + void Td::on_request(uint64 id, const td_api::deleteAllCallMessages &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 239708812..9d44115a5 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -614,7 +614,9 @@ class Td final : public Actor { void on_request(uint64 id, td_api::searchMessages &request); - void on_request(uint64 id, td_api::searchCallMessages &request); + void on_request(uint64 id, const td_api::searchCallMessages &request); + + void on_request(uint64 id, td_api::searchOutgoingDocumentMessages &request); void on_request(uint64 id, const td_api::deleteAllCallMessages &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 9948b5a9b..1f65cea15 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2211,6 +2211,10 @@ class CliClient final : public Actor { bool only_missed; get_args(args, limit, offset_message_id, only_missed); send_request(td_api::make_object(offset_message_id, as_limit(limit), only_missed)); + } else if (op == "sodm") { + SearchQuery query; + get_args(args, query); + send_request(td_api::make_object(query.query, query.limit)); } else if (op == "DeleteAllCallMessages") { bool revoke = as_bool(args); send_request(td_api::make_object(revoke));