From 8228c2e7b6197bcc21dd96996a8416f5ac3d6ebb Mon Sep 17 00:00:00 2001 From: levlam Date: Mon, 6 Nov 2023 13:20:16 +0300 Subject: [PATCH] Allow to change reply quote in resendMessages. --- td/generate/scheme/td_api.tl | 3 ++- td/telegram/MessageInputReplyTo.h | 8 ++++++++ td/telegram/MessagesManager.cpp | 16 ++++++++++++++-- td/telegram/MessagesManager.h | 3 ++- td/telegram/Td.cpp | 5 +++-- td/telegram/Td.h | 2 +- td/telegram/cli.cpp | 6 ++++-- 7 files changed, 34 insertions(+), 9 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 290a1b6e4..351df8380 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -7063,7 +7063,8 @@ forwardMessages chat_id:int53 message_thread_id:int53 from_chat_id:int53 message //-If a message is re-sent, the corresponding failed to send message is deleted. Returns the sent messages in the same order as the message identifiers passed in message_ids. If a message can't be re-sent, null will be returned instead of the message //@chat_id Identifier of the chat to send messages //@message_ids Identifiers of the messages to resend. Message identifiers must be in a strictly increasing order -resendMessages chat_id:int53 message_ids:vector = Messages; +//@quote New manually chosen quote from the message to reply; pass null if none. Ignored if more than one message is re-sent, or if messageSendingStateFailed.need_another_reply_quote == false +resendMessages chat_id:int53 message_ids:vector quote:formattedText = Messages; //@description Adds a local message to a chat. The message is persistent across application restarts only if the message database is used. Returns the added message //@chat_id Target chat diff --git a/td/telegram/MessageInputReplyTo.h b/td/telegram/MessageInputReplyTo.h index 1154ce15a..186a691b1 100644 --- a/td/telegram/MessageInputReplyTo.h +++ b/td/telegram/MessageInputReplyTo.h @@ -62,6 +62,14 @@ class MessageInputReplyTo { return !is_empty(); } + bool has_quote() const { + return !quote_.text.empty(); + } + + void set_quote(FormattedText &"e) { + quote_ = std::move(quote); + } + StoryFullId get_story_full_id() const { return story_full_id_; } diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 4457f9f47..ca3077c7d 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -28272,7 +28272,8 @@ Result> MessagesManager::forward_messages( return get_messages_object(-1, std::move(result), false); } -Result> MessagesManager::resend_messages(DialogId dialog_id, vector message_ids) { +Result> MessagesManager::resend_messages(DialogId dialog_id, vector message_ids, + td_api::object_ptr &"e) { if (message_ids.empty()) { return Status::Error(400, "There are no messages to resend"); } @@ -28361,6 +28362,17 @@ Result> MessagesManager::resend_messages(DialogId dialog_id, v auto need_another_sender = message->send_error_code == 400 && message->send_error_message == CSlice("SEND_AS_PEER_INVALID"); + auto need_another_reply_quote = + message->send_error_code == 400 && message->send_error_message == CSlice("QUOTE_TEXT_INVALID"); + if (need_another_reply_quote && message_ids.size() == 1 && quote != nullptr) { + CHECK(message->input_reply_to.is_valid()); + CHECK(message->input_reply_to.has_quote()); // checked in on_send_message_fail + auto r_quote = + get_formatted_text(td_, get_my_dialog_id(), std::move(quote), td_->auth_manager_->is_bot(), true, true, true); + if (r_quote.is_ok()) { + message->input_reply_to.set_quote(r_quote.move_as_ok()); + } + } MessageSendOptions options(message->disable_notification, message->from_background, message->update_stickersets_order, message->noforwards, false, get_message_schedule_date(message.get()), message->sending_id); @@ -31211,7 +31223,7 @@ void MessagesManager::on_send_message_fail(int64 random_id, Status error) { } if (error.message() == "QUOTE_TEXT_INVALID") { auto *input_reply_to = get_message_input_reply_to(m); - if (input_reply_to != nullptr && !input_reply_to->is_empty()) { + if (input_reply_to != nullptr && !input_reply_to->is_empty() && input_reply_to->has_quote()) { auto reply_message_full_id = input_reply_to->get_reply_message_full_id(dialog_id); if (reply_message_full_id.get_message_id().is_valid()) { get_message_from_server(reply_message_full_id, Auto(), "QUOTE_TEXT_INVALID"); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 4a27642c8..fe9a77012 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -465,7 +465,8 @@ class MessagesManager final : public Actor { tl_object_ptr &&options, bool in_game_share, vector &©_options) TD_WARN_UNUSED_RESULT; - Result> resend_messages(DialogId dialog_id, vector message_ids) TD_WARN_UNUSED_RESULT; + Result> resend_messages(DialogId dialog_id, vector message_ids, + td_api::object_ptr &"e) TD_WARN_UNUSED_RESULT; void set_dialog_message_ttl(DialogId dialog_id, int32 ttl, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 49f010478..259d3d80b 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -5911,9 +5911,10 @@ void Td::on_request(uint64 id, td_api::forwardMessages &request) { } } -void Td::on_request(uint64 id, const td_api::resendMessages &request) { +void Td::on_request(uint64 id, td_api::resendMessages &request) { DialogId dialog_id(request.chat_id_); - auto r_message_ids = messages_manager_->resend_messages(dialog_id, MessageId::get_message_ids(request.message_ids_)); + auto r_message_ids = messages_manager_->resend_messages(dialog_id, MessageId::get_message_ids(request.message_ids_), + std::move(request.quote_)); if (r_message_ids.is_error()) { return send_closure(actor_id(this), &Td::send_error, id, r_message_ids.move_as_error()); } diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 8e20b54fb..1fb104c1c 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -856,7 +856,7 @@ class Td final : public Actor { void on_request(uint64 id, td_api::forwardMessages &request); - void on_request(uint64 id, const td_api::resendMessages &request); + void on_request(uint64 id, td_api::resendMessages &request); void on_request(uint64 id, td_api::getWebPagePreview &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index c14d45806..b624c41b0 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -3715,8 +3715,10 @@ class CliClient final : public Actor { } else if (op == "resend") { ChatId chat_id; string message_ids; - get_args(args, chat_id, message_ids); - send_request(td_api::make_object(chat_id, as_message_ids(message_ids))); + string quote; + get_args(args, chat_id, message_ids, quote); + send_request( + td_api::make_object(chat_id, as_message_ids(message_ids), as_formatted_text(quote))); } else if (op == "csc" || op == "CreateSecretChat") { send_request(td_api::make_object(as_secret_chat_id(args))); } else if (op == "cnsc" || op == "CreateNewSecretChat") {