From c941f488a00892f8d64461bbebf3a6f570212aaa Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 19 Oct 2022 17:56:18 +0300 Subject: [PATCH] Proxy speech recognition methods through MessagesManager. --- td/telegram/MessageContent.cpp | 20 ++++++++++++++++++++ td/telegram/MessageContent.h | 6 ++++++ td/telegram/MessagesManager.cpp | 18 ++++++++++++++++++ td/telegram/MessagesManager.h | 4 ++++ td/telegram/Td.cpp | 7 +++---- td/telegram/VoiceNotesManager.cpp | 16 ++-------------- 6 files changed, 53 insertions(+), 18 deletions(-) diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index f14c55a64..faa6678c1 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -6173,4 +6173,24 @@ void update_used_hashtags(Td *td, const MessageContent *content) { } } +void recognize_message_content_speech(Td *td, const MessageContent *content, FullMessageId full_message_id, + Promise &&promise) { + switch (content->get_type()) { + case MessageContentType::VoiceNote: + return td->voice_notes_manager_->recognize_speech(full_message_id, std::move(promise)); + default: + return promise.set_error(Status::Error(400, "Invalid message specified")); + } +} + +void rate_message_content_speech_recognition(Td *td, const MessageContent *content, FullMessageId full_message_id, + bool is_good, Promise &&promise) { + switch (content->get_type()) { + case MessageContentType::VoiceNote: + return td->voice_notes_manager_->rate_speech_recognition(full_message_id, is_good, std::move(promise)); + default: + return promise.set_error(Status::Error(400, "Invalid message specified")); + } +} + } // namespace td diff --git a/td/telegram/MessageContent.h b/td/telegram/MessageContent.h index 1fa8658d8..2d759cb4f 100644 --- a/td/telegram/MessageContent.h +++ b/td/telegram/MessageContent.h @@ -264,4 +264,10 @@ void on_dialog_used(TopDialogCategory category, DialogId dialog_id, int32 date); void update_used_hashtags(Td *td, const MessageContent *content); +void recognize_message_content_speech(Td *td, const MessageContent *content, FullMessageId full_message_id, + Promise &&promise); + +void rate_message_content_speech_recognition(Td *td, const MessageContent *content, FullMessageId full_message_id, + bool is_good, Promise &&promise); + } // namespace td diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index a23eee5e5..342e7ffc2 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -18822,6 +18822,24 @@ void MessagesManager::translate_text(const string &text, const string &from_lang td_->create_handler(std::move(promise))->send(text, from_language_code, to_language_code); } +void MessagesManager::recognize_speech(FullMessageId full_message_id, Promise &&promise) { + auto m = get_message_force(full_message_id, "recognize_speech"); + if (m == nullptr) { + return promise.set_error(Status::Error(400, "Message not found")); + } + + recognize_message_content_speech(td_, m->content.get(), full_message_id, std::move(promise)); +} + +void MessagesManager::rate_speech_recognition(FullMessageId full_message_id, bool is_good, Promise &&promise) { + auto m = get_message_force(full_message_id, "rate_speech_recognition"); + if (m == nullptr) { + return promise.set_error(Status::Error(400, "Message not found")); + } + + rate_message_content_speech_recognition(td_, m->content.get(), full_message_id, is_good, std::move(promise)); +} + void MessagesManager::get_dialog_info_full(DialogId dialog_id, Promise &&promise, const char *source) { switch (dialog_id.get_type()) { case DialogType::User: diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index c025a388c..ff042cc39 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -649,6 +649,10 @@ class MessagesManager final : public Actor { void translate_text(const string &text, const string &from_language_code, const string &to_language_code, Promise> &&promise); + void recognize_speech(FullMessageId full_message_id, Promise &&promise); + + void rate_speech_recognition(FullMessageId full_message_id, bool is_good, Promise &&promise); + bool is_message_edited_recently(FullMessageId full_message_id, int32 seconds); bool is_deleted_secret_chat(DialogId dialog_id) const; diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index b3271ec46..3efe6b2be 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -4689,15 +4689,14 @@ void Td::on_request(uint64 id, td_api::translateText &request) { void Td::on_request(uint64 id, const td_api::recognizeSpeech &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - voice_notes_manager_->recognize_speech({DialogId(request.chat_id_), MessageId(request.message_id_)}, - std::move(promise)); + messages_manager_->recognize_speech({DialogId(request.chat_id_), MessageId(request.message_id_)}, std::move(promise)); } void Td::on_request(uint64 id, const td_api::rateSpeechRecognition &request) { CHECK_IS_USER(); CREATE_OK_REQUEST_PROMISE(); - voice_notes_manager_->rate_speech_recognition({DialogId(request.chat_id_), MessageId(request.message_id_)}, - request.is_good_, std::move(promise)); + messages_manager_->rate_speech_recognition({DialogId(request.chat_id_), MessageId(request.message_id_)}, + request.is_good_, std::move(promise)); } void Td::on_request(uint64 id, const td_api::getFile &request) { diff --git a/td/telegram/VoiceNotesManager.cpp b/td/telegram/VoiceNotesManager.cpp index 182e487f2..af2de051d 100644 --- a/td/telegram/VoiceNotesManager.cpp +++ b/td/telegram/VoiceNotesManager.cpp @@ -272,14 +272,8 @@ void VoiceNotesManager::unregister_voice_note(FileId voice_note_file_id, FullMes } void VoiceNotesManager::recognize_speech(FullMessageId full_message_id, Promise &&promise) { - if (!td_->messages_manager_->have_message_force(full_message_id, "recognize_speech")) { - return promise.set_error(Status::Error(400, "Message not found")); - } - auto it = message_voice_notes_.find(full_message_id); - if (it == message_voice_notes_.end()) { - return promise.set_error(Status::Error(400, "Invalid message specified")); - } + CHECK(it != message_voice_notes_.end()); auto file_id = it->second; auto voice_note = get_voice_note(file_id); @@ -396,14 +390,8 @@ void VoiceNotesManager::on_voice_note_transcription_completed(FileId file_id) { } void VoiceNotesManager::rate_speech_recognition(FullMessageId full_message_id, bool is_good, Promise &&promise) { - if (!td_->messages_manager_->have_message_force(full_message_id, "rate_speech_recognition")) { - return promise.set_error(Status::Error(400, "Message not found")); - } - auto it = message_voice_notes_.find(full_message_id); - if (it == message_voice_notes_.end()) { - return promise.set_error(Status::Error(400, "Invalid message specified")); - } + CHECK(it != message_voice_notes_.end()); auto file_id = it->second; auto voice_note = get_voice_note(file_id);