Add TranslationManager.

This commit is contained in:
levlam 2023-01-18 14:47:31 +03:00
parent 7288e90211
commit 8c43a7c0bb
8 changed files with 114 additions and 45 deletions

View File

@ -462,6 +462,7 @@ set(TDLIB_SOURCE
td/telegram/TopDialogCategory.cpp
td/telegram/TopDialogManager.cpp
td/telegram/TranscriptionInfo.cpp
td/telegram/TranslationManager.cpp
td/telegram/UpdatesManager.cpp
td/telegram/Usernames.cpp
td/telegram/Venue.cpp
@ -734,6 +735,7 @@ set(TDLIB_SOURCE
td/telegram/TopDialogCategory.h
td/telegram/TopDialogManager.h
td/telegram/TranscriptionInfo.h
td/telegram/TranslationManager.h
td/telegram/UniqueId.h
td/telegram/UpdatesManager.h
td/telegram/UserId.h

View File

@ -311,6 +311,7 @@ function split_file($file, $chunks, $undo) {
'theme_manager[_(-][^.]|ThemeManager' => "ThemeManager",
'TopDialogCategory|get_top_dialog_category' => 'TopDialogCategory',
'top_dialog_manager[_(-][^.]|TopDialogManager' => 'TopDialogManager',
'translation_manager[_(-][^.]|TranslationManager' => "TranslationManager",
'updates_manager[_(-][^.]|UpdatesManager|get_difference[)]|updateSentMessage|dummyUpdate' => 'UpdatesManager',
'WebPageId(Hash)?' => 'WebPageId',
'web_pages_manager[_(-][^.]|WebPagesManager' => 'WebPagesManager');

View File

@ -708,40 +708,6 @@ class GetMessageReadParticipantsQuery final : public Td::ResultHandler {
}
};
class TranslateTextQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::text>> promise_;
public:
explicit TranslateTextQuery(Promise<td_api::object_ptr<td_api::text>> &&promise) : promise_(std::move(promise)) {
}
void send(const string &text, const string &from_language_code, const string &to_language_code) {
int flags = telegram_api::messages_translateText::TEXT_MASK;
vector<telegram_api::object_ptr<telegram_api::textWithEntities>> texts;
texts.push_back(telegram_api::make_object<telegram_api::textWithEntities>(text, Auto()));
send_query(G()->net_query_creator().create(
telegram_api::messages_translateText(flags, nullptr, vector<int32>{0}, std::move(texts), to_language_code)));
}
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);
if (ptr->result_.empty()) {
return promise_.set_value(nullptr);
}
promise_.set_value(td_api::make_object<td_api::text>(ptr->result_[0]->text_));
}
void on_error(Status status) final {
promise_.set_error(std::move(status));
}
};
class ExportChannelMessageLinkQuery final : public Td::ResultHandler {
Promise<Unit> promise_;
ChannelId channel_id_;
@ -18993,12 +18959,6 @@ void MessagesManager::on_get_message_viewers(DialogId dialog_id, vector<UserId>
promise.set_value(td_->contacts_manager_->get_users_object(-1, user_ids));
}
void MessagesManager::translate_text(const string &text, const string &from_language_code,
const string &to_language_code,
Promise<td_api::object_ptr<td_api::text>> &&promise) {
td_->create_handler<TranslateTextQuery>(std::move(promise))->send(text, from_language_code, to_language_code);
}
void MessagesManager::recognize_speech(FullMessageId full_message_id, Promise<Unit> &&promise) {
auto m = get_message_force(full_message_id, "recognize_speech");
if (m == nullptr) {

View File

@ -637,9 +637,6 @@ class MessagesManager final : public Actor {
void get_message_viewers(FullMessageId full_message_id, Promise<td_api::object_ptr<td_api::users>> &&promise);
void translate_text(const string &text, const string &from_language_code, const string &to_language_code,
Promise<td_api::object_ptr<td_api::text>> &&promise);
void recognize_speech(FullMessageId full_message_id, Promise<Unit> &&promise);
void rate_speech_recognition(FullMessageId full_message_id, bool is_good, Promise<Unit> &&promise);

View File

@ -124,6 +124,7 @@
#include "td/telegram/ThemeManager.h"
#include "td/telegram/TopDialogCategory.h"
#include "td/telegram/TopDialogManager.h"
#include "td/telegram/TranslationManager.h"
#include "td/telegram/UpdatesManager.h"
#include "td/telegram/UserId.h"
#include "td/telegram/Version.h"
@ -3234,6 +3235,8 @@ void Td::dec_actor_refcnt() {
LOG(DEBUG) << "ThemeManager was cleared" << timer;
top_dialog_manager_.reset();
LOG(DEBUG) << "TopDialogManager was cleared" << timer;
translation_manager_.reset();
LOG(DEBUG) << "TranslationManager was cleared" << timer;
updates_manager_.reset();
LOG(DEBUG) << "UpdatesManager was cleared" << timer;
video_notes_manager_.reset();
@ -3426,6 +3429,8 @@ void Td::clear() {
LOG(DEBUG) << "ThemeManager actor was cleared" << timer;
top_dialog_manager_actor_.reset();
LOG(DEBUG) << "TopDialogManager actor was cleared" << timer;
translation_manager_actor_.reset();
LOG(DEBUG) << "TranslationManager actor was cleared" << timer;
updates_manager_actor_.reset();
LOG(DEBUG) << "UpdatesManager actor was cleared" << timer;
video_notes_manager_actor_.reset();
@ -3890,6 +3895,8 @@ void Td::init_managers() {
top_dialog_manager_ = make_unique<TopDialogManager>(this, create_reference());
top_dialog_manager_actor_ = register_actor("TopDialogManager", top_dialog_manager_.get());
G()->set_top_dialog_manager(top_dialog_manager_actor_.get());
translation_manager_ = make_unique<TranslationManager>(this, create_reference());
translation_manager_actor_ = register_actor("TranslationManager", translation_manager_.get());
updates_manager_ = make_unique<UpdatesManager>(this, create_reference());
updates_manager_actor_ = register_actor("UpdatesManager", updates_manager_.get());
G()->set_updates_manager(updates_manager_actor_.get());
@ -4699,8 +4706,8 @@ void Td::on_request(uint64 id, td_api::translateText &request) {
CLEAN_INPUT_STRING(request.from_language_code_);
CLEAN_INPUT_STRING(request.to_language_code_);
CREATE_REQUEST_PROMISE();
messages_manager_->translate_text(request.text_, request.from_language_code_, request.to_language_code_,
std::move(promise));
translation_manager_->translate_text(request.text_, request.from_language_code_, request.to_language_code_,
std::move(promise));
}
void Td::on_request(uint64 id, const td_api::recognizeSpeech &request) {

View File

@ -77,6 +77,7 @@ class StickersManager;
class StorageManager;
class ThemeManager;
class TopDialogManager;
class TranslationManager;
class UpdatesManager;
class VideoNotesManager;
class VideosManager;
@ -181,6 +182,8 @@ class Td final : public Actor {
ActorOwn<ThemeManager> theme_manager_actor_;
unique_ptr<TopDialogManager> top_dialog_manager_;
ActorOwn<TopDialogManager> top_dialog_manager_actor_;
unique_ptr<TranslationManager> translation_manager_;
ActorOwn<TranslationManager> translation_manager_actor_;
unique_ptr<UpdatesManager> updates_manager_;
ActorOwn<UpdatesManager> updates_manager_actor_;
unique_ptr<VideoNotesManager> video_notes_manager_;

View File

@ -0,0 +1,65 @@
//
// 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"
#include "td/telegram/Td.h"
#include "td/telegram/telegram_api.h"
#include "td/utils/buffer.h"
#include "td/utils/Status.h"
namespace td {
class TranslateTextQuery final : public Td::ResultHandler {
Promise<td_api::object_ptr<td_api::text>> promise_;
public:
explicit TranslateTextQuery(Promise<td_api::object_ptr<td_api::text>> &&promise) : promise_(std::move(promise)) {
}
void send(const string &text, const string &from_language_code, const string &to_language_code) {
int flags = telegram_api::messages_translateText::TEXT_MASK;
vector<telegram_api::object_ptr<telegram_api::textWithEntities>> texts;
texts.push_back(telegram_api::make_object<telegram_api::textWithEntities>(text, Auto()));
send_query(G()->net_query_creator().create(
telegram_api::messages_translateText(flags, nullptr, vector<int32>{}, std::move(texts), to_language_code)));
}
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);
if (ptr->result_.empty()) {
return promise_.set_value(nullptr);
}
promise_.set_value(td_api::make_object<td_api::text>(ptr->result_[0]->text_));
}
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();
}
void TranslationManager::translate_text(const string &text, const string &from_language_code,
const string &to_language_code,
Promise<td_api::object_ptr<td_api::text>> &&promise) {
td_->create_handler<TranslateTextQuery>(std::move(promise))->send(text, from_language_code, to_language_code);
}
} // namespace td

View File

@ -0,0 +1,34 @@
//
// 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)
//
#pragma once
#include "td/telegram/td_api.h"
#include "td/actor/actor.h"
#include "td/utils/common.h"
#include "td/utils/Promise.h"
namespace td {
class Td;
class TranslationManager final : public Actor {
public:
TranslationManager(Td *td, ActorShared<> parent);
void translate_text(const string &text, const string &from_language_code, const string &to_language_code,
Promise<td_api::object_ptr<td_api::text>> &&promise);
private:
void tear_down() final;
Td *td_;
ActorShared<> parent_;
};
} // namespace td