From d9a9693747d41c4277738999eb725142034a8266 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 25 May 2022 21:48:31 +0300 Subject: [PATCH] Support updateTranscribeAudio. --- td/telegram/UpdatesManager.cpp | 8 ++++-- td/telegram/UpdatesManager.h | 4 +-- td/telegram/VoiceNotesManager.cpp | 43 ++++++++++++++++++++++++++++--- td/telegram/VoiceNotesManager.h | 5 ++++ 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/td/telegram/UpdatesManager.cpp b/td/telegram/UpdatesManager.cpp index 9c8c1e898..a5102ec33 100644 --- a/td/telegram/UpdatesManager.cpp +++ b/td/telegram/UpdatesManager.cpp @@ -53,6 +53,7 @@ #include "td/telegram/TdDb.h" #include "td/telegram/telegram_api.hpp" #include "td/telegram/ThemeManager.h" +#include "td/telegram/VoiceNotesManager.h" #include "td/telegram/WebPagesManager.h" #include "td/actor/MultiPromise.h" @@ -3431,9 +3432,12 @@ void UpdatesManager::on_update(tl_object_ptr td_->notification_settings_manager_->reload_saved_ringtones(std::move(promise)); } -// unsupported updates - void UpdatesManager::on_update(tl_object_ptr update, Promise &&promise) { + td_->voice_notes_manager_->on_update_transcribed_audio(std::move(update->text_), update->transcription_id_, + !update->pending_); + promise.set_value(Unit()); } +// unsupported updates + } // namespace td diff --git a/td/telegram/UpdatesManager.h b/td/telegram/UpdatesManager.h index 15c43ba32..b637bb4a7 100644 --- a/td/telegram/UpdatesManager.h +++ b/td/telegram/UpdatesManager.h @@ -526,9 +526,9 @@ class UpdatesManager final : public Actor { void on_update(tl_object_ptr update, Promise &&promise); - // unsupported updates - void on_update(tl_object_ptr update, Promise &&promise); + + // unsupported updates }; } // namespace td diff --git a/td/telegram/VoiceNotesManager.cpp b/td/telegram/VoiceNotesManager.cpp index 861fc40bd..89e291d97 100644 --- a/td/telegram/VoiceNotesManager.cpp +++ b/td/telegram/VoiceNotesManager.cpp @@ -241,12 +241,13 @@ void VoiceNotesManager::on_voice_note_transcribed(FileId file_id, string &&text, auto voice_note = get_voice_note(file_id); CHECK(voice_note != nullptr); CHECK(!voice_note->is_transcribed); - CHECK(voice_note->transcription_id == 0); + CHECK(voice_note->transcription_id == 0 || voice_note->transcription_id == transcription_id); + bool is_changed = voice_note->is_transcribed != is_final || voice_note->text != text; voice_note->transcription_id = transcription_id; voice_note->is_transcribed = is_final; voice_note->text = std::move(text); - if (!voice_note->text.empty() || is_final) { + if (is_changed) { on_voice_note_transcription_updated(file_id); } @@ -258,6 +259,13 @@ void VoiceNotesManager::on_voice_note_transcribed(FileId file_id, string &&text, speech_recognition_queries_.erase(it); set_promises(promises); + } else { + if (pending_voice_note_transcription_queries_.count(transcription_id) != 0) { + on_pending_voice_note_transcription_failed(transcription_id, + Status::Error(500, "Receive duplicate recognition identifier")); + } + bool is_inserted = pending_voice_note_transcription_queries_.emplace(transcription_id, file_id).second; + CHECK(is_inserted); } } @@ -265,7 +273,15 @@ void VoiceNotesManager::on_voice_note_transcription_failed(FileId file_id, Statu auto voice_note = get_voice_note(file_id); CHECK(voice_note != nullptr); CHECK(!voice_note->is_transcribed); - CHECK(voice_note->transcription_id == 0); + + if (voice_note->transcription_id != 0) { + CHECK(pending_voice_note_transcription_queries_.count(voice_note->transcription_id) == 0); + voice_note->transcription_id = 0; + if (!voice_note->text.empty()) { + voice_note->text.clear(); + on_voice_note_transcription_updated(file_id); + } + } auto it = speech_recognition_queries_.find(file_id); CHECK(it != speech_recognition_queries_.end()); @@ -276,6 +292,27 @@ void VoiceNotesManager::on_voice_note_transcription_failed(FileId file_id, Statu fail_promises(promises, std::move(error)); } +void VoiceNotesManager::on_update_transcribed_audio(string &&text, int64 transcription_id, bool is_final) { + auto it = pending_voice_note_transcription_queries_.find(transcription_id); + if (it == pending_voice_note_transcription_queries_.end()) { + return; + } + auto file_id = it->second; + pending_voice_note_transcription_queries_.erase(it); + + on_voice_note_transcribed(file_id, std::move(text), transcription_id, is_final); +} + +void VoiceNotesManager::on_pending_voice_note_transcription_failed(int64 transcription_id, Status &&error) { + auto it = pending_voice_note_transcription_queries_.find(transcription_id); + if (it == pending_voice_note_transcription_queries_.end()) { + return; + } + auto file_id = it->second; + pending_voice_note_transcription_queries_.erase(it); + on_voice_note_transcription_failed(file_id, std::move(error)); +} + void VoiceNotesManager::on_voice_note_transcription_updated(FileId file_id) { auto it = voice_note_messages_.find(file_id); if (it != voice_note_messages_.end()) { diff --git a/td/telegram/VoiceNotesManager.h b/td/telegram/VoiceNotesManager.h index baf148de3..a67d479fa 100644 --- a/td/telegram/VoiceNotesManager.h +++ b/td/telegram/VoiceNotesManager.h @@ -40,6 +40,8 @@ class VoiceNotesManager { void recognize_speech(FullMessageId full_message_id, Promise &&promise); + void on_update_transcribed_audio(string &&text, int64 transcription_id, bool is_final); + void on_voice_note_transcribed(FileId file_id, string &&text, int64 transcription_id, bool is_final); void on_voice_note_transcription_failed(FileId file_id, Status &&error); @@ -80,12 +82,15 @@ class VoiceNotesManager { FileId on_get_voice_note(unique_ptr new_voice_note, bool replace); + void on_pending_voice_note_transcription_failed(int64 transcription_id, Status &&error); + void on_voice_note_transcription_updated(FileId file_id); Td *td_; FlatHashMap, FileIdHash> voice_notes_; FlatHashMap>, FileIdHash> speech_recognition_queries_; + FlatHashMap pending_voice_note_transcription_queries_; FlatHashMap, FileIdHash> voice_note_messages_; FlatHashMap message_voice_notes_;