tdlight/td/telegram/TranslationManager.cpp

122 lines
4.8 KiB
C++
Raw Normal View History

2023-01-18 12:47:31 +01:00
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include "td/telegram/TranslationManager.h"
#include "td/telegram/Global.h"
2023-01-18 14:34:49 +01:00
#include "td/telegram/MessageEntity.h"
2023-01-18 12:47:31 +01:00
#include "td/telegram/Td.h"
#include "td/telegram/telegram_api.h"
2023-01-18 14:34:49 +01:00
#include "td/utils/algorithm.h"
2023-01-18 12:47:31 +01:00
#include "td/utils/buffer.h"
#include "td/utils/Status.h"
namespace td {
class TranslateTextQuery final : public Td::ResultHandler {
2023-01-18 14:34:49 +01:00
Promise<vector<telegram_api::object_ptr<telegram_api::textWithEntities>>> promise_;
2023-01-18 12:47:31 +01:00
public:
2023-01-18 14:34:49 +01:00
explicit TranslateTextQuery(Promise<vector<telegram_api::object_ptr<telegram_api::textWithEntities>>> &&promise)
: promise_(std::move(promise)) {
2023-01-18 12:47:31 +01:00
}
2023-01-18 14:34:49 +01:00
void send(vector<FormattedText> &&texts, const string &to_language_code) {
2023-01-18 12:47:31 +01:00
int flags = telegram_api::messages_translateText::TEXT_MASK;
2023-01-18 14:34:49 +01:00
auto input_texts =
transform(std::move(texts), [contacts_manager = td_->contacts_manager_.get()](FormattedText &&text) {
return get_input_text_with_entities(contacts_manager, std::move(text), "TranslateTextQuery");
});
send_query(G()->net_query_creator().create(telegram_api::messages_translateText(
flags, nullptr, vector<int32>{}, std::move(input_texts), to_language_code)));
2023-01-18 12:47:31 +01:00
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_translateText>(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 TranslateTextQuery: " << to_string(ptr);
2023-01-18 14:34:49 +01:00
promise_.set_value(std::move(ptr->result_));
2023-01-18 12:47:31 +01:00
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
TranslationManager::TranslationManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
}
void TranslationManager::tear_down() {
parent_.reset();
}
2023-01-18 14:34:49 +01:00
void TranslationManager::translate_text(td_api::object_ptr<td_api::formattedText> &&text,
2023-01-18 12:47:31 +01:00
const string &to_language_code,
2023-01-18 14:34:49 +01:00
Promise<td_api::object_ptr<td_api::formattedText>> &&promise) {
if (text == nullptr) {
return promise.set_error(Status::Error(400, "Text must be non-empty"));
}
bool skip_bot_commands = true;
int32 max_media_timestamp = -1;
for (const auto &entity : text->entities_) {
if (entity == nullptr || entity->type_ == nullptr) {
continue;
}
switch (entity->type_->get_id()) {
case td_api::textEntityTypeBotCommand::ID:
skip_bot_commands = false;
break;
case td_api::textEntityTypeMediaTimestamp::ID:
max_media_timestamp =
td::max(max_media_timestamp,
static_cast<const td_api::textEntityTypeMediaTimestamp *>(entity->type_.get())->media_timestamp_);
break;
default:
// nothing to do
break;
}
}
2023-01-18 14:34:49 +01:00
TRY_RESULT_PROMISE(promise, entities, get_message_entities(td_->contacts_manager_.get(), std::move(text->entities_)));
TRY_STATUS_PROMISE(promise, fix_formatted_text(text->text_, entities, true, true, true, true, true));
vector<FormattedText> texts;
texts.push_back(FormattedText{std::move(text->text_), std::move(entities)});
auto query_promise = PromiseCreator::lambda(
[actor_id = actor_id(this), skip_bot_commands, max_media_timestamp, promise = std::move(promise)](
2023-01-18 14:34:49 +01:00
Result<vector<telegram_api::object_ptr<telegram_api::textWithEntities>>> result) mutable {
if (result.is_error()) {
return promise.set_error(result.move_as_error());
}
send_closure(actor_id, &TranslationManager::on_get_translated_texts, result.move_as_ok(), skip_bot_commands,
max_media_timestamp, std::move(promise));
2023-01-18 14:34:49 +01:00
});
td_->create_handler<TranslateTextQuery>(std::move(query_promise))->send(std::move(texts), to_language_code);
}
void TranslationManager::on_get_translated_texts(vector<telegram_api::object_ptr<telegram_api::textWithEntities>> texts,
bool skip_bot_commands, int32 max_media_timestamp,
2023-01-18 14:34:49 +01:00
Promise<td_api::object_ptr<td_api::formattedText>> &&promise) {
TRY_STATUS_PROMISE(promise, G()->close_status());
if (texts.size() != 1u) {
return promise.set_error(Status::Error(500, "Receive invalid number of results"));
}
auto formatted_text =
get_formatted_text(td_->contacts_manager_.get(), std::move(texts[0]), true, true, skip_bot_commands,
max_media_timestamp == -1, true, "on_get_translated_texts");
promise.set_value(get_formatted_text_object(formatted_text, skip_bot_commands, max_media_timestamp));
2023-01-18 12:47:31 +01:00
}
} // namespace td