Support updateTranscribeAudio.

This commit is contained in:
levlam 2022-05-25 21:48:31 +03:00
parent 151654eeea
commit d9a9693747
4 changed files with 53 additions and 7 deletions

View File

@ -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<telegram_api::updateSavedRingtones>
td_->notification_settings_manager_->reload_saved_ringtones(std::move(promise));
}
// unsupported updates
void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateTranscribedAudio> update, Promise<Unit> &&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

View File

@ -526,9 +526,9 @@ class UpdatesManager final : public Actor {
void on_update(tl_object_ptr<telegram_api::updateSavedRingtones> update, Promise<Unit> &&promise);
// unsupported updates
void on_update(tl_object_ptr<telegram_api::updateTranscribedAudio> update, Promise<Unit> &&promise);
// unsupported updates
};
} // namespace td

View File

@ -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()) {

View File

@ -40,6 +40,8 @@ class VoiceNotesManager {
void recognize_speech(FullMessageId full_message_id, Promise<Unit> &&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<VoiceNote> 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<FileId, unique_ptr<VoiceNote>, FileIdHash> voice_notes_;
FlatHashMap<FileId, vector<Promise<Unit>>, FileIdHash> speech_recognition_queries_;
FlatHashMap<int64, FileId> pending_voice_note_transcription_queries_;
FlatHashMap<FileId, FlatHashSet<FullMessageId, FullMessageIdHash>, FileIdHash> voice_note_messages_;
FlatHashMap<FullMessageId, FileId, FullMessageIdHash> message_voice_notes_;