diff options
| author | levlam <levlam@telegram.org> | 2026-04-09 15:58:48 +0300 |
|---|---|---|
| committer | levlam <levlam@telegram.org> | 2026-04-09 15:58:48 +0300 |
| commit | c3a6ecea6312a6940fcf6890ed046841932d3f4f (patch) | |
| tree | 893776c2f846edb1faef2eb41af0c92603354594 | |
| parent | aea46d7b1413c663b88243cbf398401d2c27738d (diff) | |
Add TranslationManager.InputText.
| -rw-r--r-- | td/telegram/MessagesManager.cpp | 8 | ||||
| -rw-r--r-- | td/telegram/TranslationManager.cpp | 69 | ||||
| -rw-r--r-- | td/telegram/TranslationManager.h | 11 |
3 files changed, 47 insertions, 41 deletions
diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 5044e96de..8394bcb78 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -14836,14 +14836,16 @@ void MessagesManager::translate_message_text(MessageFullId message_full_id, cons return promise.set_value(td_api::make_object<td_api::formattedText>()); } - auto skip_bot_commands = need_skip_bot_commands(message_full_id.get_dialog_id(), m); - auto max_media_timestamp = get_message_max_media_timestamp(m); + TranslationManager::InputText input_text; + input_text.text_ = *text; + input_text.skip_bot_commands_ = need_skip_bot_commands(message_full_id.get_dialog_id(), m); + input_text.max_media_timestamp_ = get_message_max_media_timestamp(m); auto dialog_id = message_full_id.get_dialog_id(); auto has_autotranslation = dialog_id.get_type() == DialogType::Channel && td_->dialog_manager_->have_input_peer(dialog_id, false, AccessRights::Read) && m->message_id.is_server() && td_->chat_manager_->get_channel_autotranslation(dialog_id.get_channel_id()); - td_->translation_manager_->translate_text(*text, skip_bot_commands, max_media_timestamp, + td_->translation_manager_->translate_text(std::move(input_text), has_autotranslation ? message_full_id : MessageFullId(), to_language_code, tone, std::move(promise)); } diff --git a/td/telegram/TranslationManager.cpp b/td/telegram/TranslationManager.cpp index 0f63f1fc2..bda54e923 100644 --- a/td/telegram/TranslationManager.cpp +++ b/td/telegram/TranslationManager.cpp @@ -89,10 +89,10 @@ class ComposeMessageWithAiQuery final : public Td::ResultHandler { : promise_(std::move(promise)) { } - void send(FormattedText &&text, const string &translate_to_language_code, string tone, bool emojify, - bool skip_bot_commands, int32 max_media_timestamp) { - skip_bot_commands_ = skip_bot_commands; - max_media_timestamp_ = max_media_timestamp; + void send(const TranslationManager::InputText &text, const string &translate_to_language_code, string tone, + bool emojify) { + skip_bot_commands_ = text.skip_bot_commands_; + max_media_timestamp_ = text.max_media_timestamp_; int32 flags = 0; if (!translate_to_language_code.empty()) { @@ -103,7 +103,7 @@ class ComposeMessageWithAiQuery final : public Td::ResultHandler { } send_query(G()->net_query_creator().create(telegram_api::messages_composeMessageWithAI( flags, false, emojify, - get_input_text_with_entities(td_->user_manager_.get(), std::move(text), "ComposeMessageWithAiQuery"), + get_input_text_with_entities(td_->user_manager_.get(), text.text_, "ComposeMessageWithAiQuery"), translate_to_language_code, tone))); } @@ -136,14 +136,14 @@ class ProofreadMessageWithAiQuery final : public Td::ResultHandler { : promise_(std::move(promise)) { } - void send(FormattedText &&text, bool skip_bot_commands, int32 max_media_timestamp) { - skip_bot_commands_ = skip_bot_commands; - max_media_timestamp_ = max_media_timestamp; + void send(const TranslationManager::InputText &text) { + skip_bot_commands_ = text.skip_bot_commands_; + max_media_timestamp_ = text.max_media_timestamp_; send_query(G()->net_query_creator().create(telegram_api::messages_composeMessageWithAI( 0, true, false, - get_input_text_with_entities(td_->user_manager_.get(), std::move(text), "ProofreadMessageWithAiQuery"), - string(), string()))); + get_input_text_with_entities(td_->user_manager_.get(), text.text_, "ProofreadMessageWithAiQuery"), string(), + string()))); } void on_result(BufferSlice packet) final { @@ -196,8 +196,7 @@ void TranslationManager::translate_text(td_api::object_ptr<td_api::formattedText if (text == nullptr) { return promise.set_error(400, "Text must be non-empty"); } - bool skip_bot_commands = true; - int32 max_media_timestamp = -1; + InputText input_text; for (const auto &entity : text->entities_) { if (entity == nullptr || entity->type_ == nullptr) { continue; @@ -205,11 +204,11 @@ void TranslationManager::translate_text(td_api::object_ptr<td_api::formattedText switch (entity->type_->get_id()) { case td_api::textEntityTypeBotCommand::ID: - skip_bot_commands = false; + input_text.skip_bot_commands_ = false; break; case td_api::textEntityTypeMediaTimestamp::ID: - max_media_timestamp = - td::max(max_media_timestamp, + input_text.max_media_timestamp_ = + td::max(input_text.max_media_timestamp_, static_cast<const td_api::textEntityTypeMediaTimestamp *>(entity->type_.get())->media_timestamp_); break; default: @@ -220,24 +219,24 @@ void TranslationManager::translate_text(td_api::object_ptr<td_api::formattedText TRY_RESULT_PROMISE(promise, entities, get_message_entities(td_->user_manager_.get(), std::move(text->entities_))); TRY_STATUS_PROMISE(promise, fix_formatted_text(text->text_, entities, true, true, true, true, true, true)); + input_text.text_ = FormattedText{std::move(text->text_), std::move(entities)}; - translate_text(FormattedText{std::move(text->text_), std::move(entities)}, skip_bot_commands, max_media_timestamp, - MessageFullId(), to_language_code, tone, std::move(promise)); + translate_text(std::move(input_text), MessageFullId(), to_language_code, tone, std::move(promise)); } -void TranslationManager::translate_text(FormattedText text, bool skip_bot_commands, int32 max_media_timestamp, - MessageFullId message_full_id, const string &to_language_code, +void TranslationManager::translate_text(InputText &&text, MessageFullId message_full_id, const string &to_language_code, const string &tone, Promise<td_api::object_ptr<td_api::formattedText>> &&promise) { vector<FormattedText> texts; - texts.push_back(std::move(text)); + texts.push_back(std::move(text.text_)); if (tone != string() && tone != "formal" && tone != "neutral" && tone != "casual") { return promise.set_error(400, "Invalid tone specified"); } auto query_promise = PromiseCreator::lambda( - [actor_id = actor_id(this), skip_bot_commands, max_media_timestamp, promise = std::move(promise)]( + [actor_id = actor_id(this), skip_bot_commands = text.skip_bot_commands_, + max_media_timestamp = text.max_media_timestamp_, promise = std::move(promise)]( Result<vector<telegram_api::object_ptr<telegram_api::textWithEntities>>> result) mutable { if (result.is_error()) { return promise.set_error(result.move_as_error()); @@ -274,8 +273,7 @@ void TranslationManager::compose_message_with_ai(td_api::object_ptr<td_api::form return promise.set_error(400, "Text must be non-empty"); } - bool skip_bot_commands = true; - int32 max_media_timestamp = -1; + InputText input_text; for (const auto &entity : text->entities_) { if (entity == nullptr || entity->type_ == nullptr) { continue; @@ -283,11 +281,11 @@ void TranslationManager::compose_message_with_ai(td_api::object_ptr<td_api::form switch (entity->type_->get_id()) { case td_api::textEntityTypeBotCommand::ID: - skip_bot_commands = false; + input_text.skip_bot_commands_ = false; break; case td_api::textEntityTypeMediaTimestamp::ID: - max_media_timestamp = - td::max(max_media_timestamp, + input_text.max_media_timestamp_ = + td::max(input_text.max_media_timestamp_, static_cast<const td_api::textEntityTypeMediaTimestamp *>(entity->type_.get())->media_timestamp_); break; default: @@ -298,9 +296,10 @@ void TranslationManager::compose_message_with_ai(td_api::object_ptr<td_api::form TRY_RESULT_PROMISE(promise, entities, get_message_entities(td_->user_manager_.get(), std::move(text->entities_))); TRY_STATUS_PROMISE(promise, fix_formatted_text(text->text_, entities, true, true, true, true, true, true)); + input_text.text_ = FormattedText{std::move(text->text_), std::move(entities)}; + td_->create_handler<ComposeMessageWithAiQuery>(std::move(promise)) - ->send(FormattedText{std::move(text->text_), std::move(entities)}, translate_to_language_code, tone, emojify, - skip_bot_commands, max_media_timestamp); + ->send(input_text, translate_to_language_code, tone, emojify); } void TranslationManager::proofread_message_with_ai(td_api::object_ptr<td_api::formattedText> &&text, @@ -309,8 +308,7 @@ void TranslationManager::proofread_message_with_ai(td_api::object_ptr<td_api::fo return promise.set_error(400, "Text must be non-empty"); } - bool skip_bot_commands = true; - int32 max_media_timestamp = -1; + InputText input_text; for (const auto &entity : text->entities_) { if (entity == nullptr || entity->type_ == nullptr) { continue; @@ -318,11 +316,11 @@ void TranslationManager::proofread_message_with_ai(td_api::object_ptr<td_api::fo switch (entity->type_->get_id()) { case td_api::textEntityTypeBotCommand::ID: - skip_bot_commands = false; + input_text.skip_bot_commands_ = false; break; case td_api::textEntityTypeMediaTimestamp::ID: - max_media_timestamp = - td::max(max_media_timestamp, + input_text.max_media_timestamp_ = + td::max(input_text.max_media_timestamp_, static_cast<const td_api::textEntityTypeMediaTimestamp *>(entity->type_.get())->media_timestamp_); break; default: @@ -333,8 +331,9 @@ void TranslationManager::proofread_message_with_ai(td_api::object_ptr<td_api::fo TRY_RESULT_PROMISE(promise, entities, get_message_entities(td_->user_manager_.get(), std::move(text->entities_))); TRY_STATUS_PROMISE(promise, fix_formatted_text(text->text_, entities, true, true, true, true, true, true)); - td_->create_handler<ProofreadMessageWithAiQuery>(std::move(promise)) - ->send(FormattedText{std::move(text->text_), std::move(entities)}, skip_bot_commands, max_media_timestamp); + input_text.text_ = FormattedText{std::move(text->text_), std::move(entities)}; + + td_->create_handler<ProofreadMessageWithAiQuery>(std::move(promise))->send(input_text); } string TranslationManager::get_ai_compose_styles_key() { diff --git a/td/telegram/TranslationManager.h b/td/telegram/TranslationManager.h index 39332a0b5..04b8abe48 100644 --- a/td/telegram/TranslationManager.h +++ b/td/telegram/TranslationManager.h @@ -26,12 +26,17 @@ class TranslationManager final : public Actor { void on_authorization_success(); + struct InputText { + FormattedText text_; + bool skip_bot_commands_ = true; + int32 max_media_timestamp_ = -1; + }; + void translate_text(td_api::object_ptr<td_api::formattedText> &&text, const string &to_language_code, const string &tone, Promise<td_api::object_ptr<td_api::formattedText>> &&promise); - void translate_text(FormattedText text, bool skip_bot_commands, int32 max_media_timestamp, - MessageFullId message_full_id, const string &to_language_code, const string &tone, - Promise<td_api::object_ptr<td_api::formattedText>> &&promise); + void translate_text(InputText &&text, MessageFullId message_full_id, const string &to_language_code, + const string &tone, Promise<td_api::object_ptr<td_api::formattedText>> &&promise); void compose_message_with_ai(td_api::object_ptr<td_api::formattedText> &&text, const string &translate_to_language_code, const string &tone, bool emojify, |
