Move TranscribeAudioQuery to TranscriptionInfo.cpp.

This commit is contained in:
levlam 2022-10-20 19:52:20 +03:00
parent 49c993b674
commit c9f8390bd5
6 changed files with 70 additions and 51 deletions

View File

@ -18828,6 +18828,11 @@ void MessagesManager::recognize_speech(FullMessageId full_message_id, Promise<Un
return promise.set_error(Status::Error(400, "Message not found")); return promise.set_error(Status::Error(400, "Message not found"));
} }
auto message_id = full_message_id.get_message_id();
if (message_id.is_scheduled() || !message_id.is_server()) {
return promise.set_error(Status::Error(400, "Message must be sent already"));
}
recognize_message_content_speech(td_, m->content.get(), full_message_id, std::move(promise)); recognize_message_content_speech(td_, m->content.get(), full_message_id, std::move(promise));
} }

View File

@ -18,6 +18,47 @@
namespace td { namespace td {
class TranscribeAudioQuery final : public Td::ResultHandler {
DialogId dialog_id_;
std::function<void(Result<telegram_api::object_ptr<telegram_api::updateTranscribedAudio>>)> handler_;
public:
void send(FullMessageId full_message_id,
std::function<void(Result<telegram_api::object_ptr<telegram_api::updateTranscribedAudio>>)> &&handler) {
dialog_id_ = full_message_id.get_dialog_id();
handler_ = std::move(handler);
auto input_peer = td_->messages_manager_->get_input_peer(dialog_id_, AccessRights::Read);
if (input_peer == nullptr) {
return on_error(Status::Error(400, "Can't access the chat"));
}
send_query(G()->net_query_creator().create(telegram_api::messages_transcribeAudio(
std::move(input_peer), full_message_id.get_message_id().get_server_message_id().get())));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_transcribeAudio>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for TranscribeAudioQuery: " << to_string(result);
if (result->transcription_id_ == 0) {
return on_error(Status::Error(500, "Receive no recognition identifier"));
}
auto update = telegram_api::make_object<telegram_api::updateTranscribedAudio>();
update->text_ = std::move(result->text_);
update->transcription_id_ = result->transcription_id_;
update->pending_ = result->pending_;
handler_(std::move(update));
}
void on_error(Status status) final {
td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "TranscribeAudioQuery");
handler_(std::move(status));
}
};
class RateTranscribedAudioQuery final : public Td::ResultHandler { class RateTranscribedAudioQuery final : public Td::ResultHandler {
Promise<Unit> promise_; Promise<Unit> promise_;
DialogId dialog_id_; DialogId dialog_id_;
@ -54,7 +95,9 @@ class RateTranscribedAudioQuery final : public Td::ResultHandler {
} }
}; };
bool TranscriptionInfo::start_recognize_speech(Promise<Unit> &&promise) { bool TranscriptionInfo::recognize_speech(
Td *td, FullMessageId full_message_id, Promise<Unit> &&promise,
std::function<void(Result<telegram_api::object_ptr<telegram_api::updateTranscribedAudio>>)> &&handler) {
if (is_transcribed_) { if (is_transcribed_) {
promise.set_value(Unit()); promise.set_value(Unit());
return false; return false;
@ -62,6 +105,7 @@ bool TranscriptionInfo::start_recognize_speech(Promise<Unit> &&promise) {
speech_recognition_queries_.push_back(std::move(promise)); speech_recognition_queries_.push_back(std::move(promise));
if (speech_recognition_queries_.size() == 1) { if (speech_recognition_queries_.size() == 1) {
last_transcription_error_ = Status::OK(); last_transcription_error_ = Status::OK();
td->create_handler<TranscribeAudioQuery>()->send(full_message_id, std::move(handler));
return true; return true;
} }
return false; return false;

View File

@ -31,7 +31,9 @@ class TranscriptionInfo {
return is_transcribed_; return is_transcribed_;
} }
bool start_recognize_speech(Promise<Unit> &&promise); bool recognize_speech(
Td *td, FullMessageId full_message_id, Promise<Unit> &&promise,
std::function<void(Result<telegram_api::object_ptr<telegram_api::updateTranscribedAudio>>)> &&handler);
vector<Promise<Unit>> on_final_transcription(string &&text, int64 transcription_id); vector<Promise<Unit>> on_final_transcription(string &&text, int64 transcription_id);

View File

@ -3660,6 +3660,7 @@ void UpdatesManager::on_update(tl_object_ptr<telegram_api::updateTranscribedAudi
if (it == pending_audio_transcriptions_.end()) { if (it == pending_audio_transcriptions_.end()) {
return promise.set_value(Unit()); return promise.set_value(Unit());
} }
// flags_, dialog_id_ and message_id_ must not be used
if (!update->pending_) { if (!update->pending_) {
auto on_update = std::move(it->second); auto on_update = std::move(it->second);
pending_audio_transcriptions_.erase(it); pending_audio_transcriptions_.erase(it);

View File

@ -6,7 +6,6 @@
// //
#include "td/telegram/VoiceNotesManager.h" #include "td/telegram/VoiceNotesManager.h"
#include "td/telegram/AccessRights.h"
#include "td/telegram/DialogId.h" #include "td/telegram/DialogId.h"
#include "td/telegram/Dimensions.h" #include "td/telegram/Dimensions.h"
#include "td/telegram/files/FileManager.h" #include "td/telegram/files/FileManager.h"
@ -18,48 +17,10 @@
#include "td/telegram/telegram_api.h" #include "td/telegram/telegram_api.h"
#include "td/telegram/UpdatesManager.h" #include "td/telegram/UpdatesManager.h"
#include "td/utils/buffer.h"
#include "td/utils/logging.h" #include "td/utils/logging.h"
namespace td { namespace td {
class TranscribeAudioQuery final : public Td::ResultHandler {
DialogId dialog_id_;
FileId file_id_;
public:
void send(FileId file_id, FullMessageId full_message_id) {
dialog_id_ = full_message_id.get_dialog_id();
file_id_ = file_id;
auto input_peer = td_->messages_manager_->get_input_peer(dialog_id_, AccessRights::Read);
if (input_peer == nullptr) {
return on_error(Status::Error(400, "Can't access the chat"));
}
send_query(G()->net_query_creator().create(telegram_api::messages_transcribeAudio(
std::move(input_peer), full_message_id.get_message_id().get_server_message_id().get())));
}
void on_result(BufferSlice packet) final {
auto result_ptr = fetch_result<telegram_api::messages_transcribeAudio>(packet);
if (result_ptr.is_error()) {
return on_error(result_ptr.move_as_error());
}
auto result = result_ptr.move_as_ok();
LOG(INFO) << "Receive result for TranscribeAudioQuery: " << to_string(result);
if (result->transcription_id_ == 0) {
return on_error(Status::Error(500, "Receive no recognition identifier"));
}
td_->voice_notes_manager_->on_voice_note_transcribed(file_id_, std::move(result->text_), result->transcription_id_,
true, !result->pending_);
}
void on_error(Status status) final {
td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "TranscribeAudioQuery");
td_->voice_notes_manager_->on_voice_note_transcription_failed(file_id_, std::move(status));
}
};
VoiceNotesManager::VoiceNotesManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { VoiceNotesManager::VoiceNotesManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
} }
@ -210,8 +171,12 @@ void VoiceNotesManager::recognize_speech(FullMessageId full_message_id, Promise<
if (voice_note->transcription_info == nullptr) { if (voice_note->transcription_info == nullptr) {
voice_note->transcription_info = make_unique<TranscriptionInfo>(); voice_note->transcription_info = make_unique<TranscriptionInfo>();
} }
if (voice_note->transcription_info->start_recognize_speech(std::move(promise))) {
td_->create_handler<TranscribeAudioQuery>()->send(file_id, full_message_id); auto handler = [actor_id = actor_id(this),
file_id](Result<telegram_api::object_ptr<telegram_api::updateTranscribedAudio>> r_update) {
send_closure(actor_id, &VoiceNotesManager::on_transcribed_audio_update, file_id, true, std::move(r_update));
};
if (voice_note->transcription_info->recognize_speech(td_, full_message_id, std::move(promise), std::move(handler))) {
on_voice_note_transcription_updated(file_id); on_voice_note_transcription_updated(file_id);
} }
} }
@ -235,19 +200,21 @@ void VoiceNotesManager::on_voice_note_transcribed(FileId file_id, string &&text,
td_->updates_manager_->subscribe_to_transcribed_audio_updates( td_->updates_manager_->subscribe_to_transcribed_audio_updates(
transcription_id, [actor_id = actor_id(this), transcription_id, [actor_id = actor_id(this),
file_id](Result<telegram_api::object_ptr<telegram_api::updateTranscribedAudio>> r_update) { file_id](Result<telegram_api::object_ptr<telegram_api::updateTranscribedAudio>> r_update) {
send_closure(actor_id, &VoiceNotesManager::on_transcribed_audio_update, file_id, std::move(r_update)); send_closure(actor_id, &VoiceNotesManager::on_transcribed_audio_update, file_id, false,
std::move(r_update));
}); });
} }
} }
} }
void VoiceNotesManager::on_transcribed_audio_update( void VoiceNotesManager::on_transcribed_audio_update(
FileId file_id, Result<telegram_api::object_ptr<telegram_api::updateTranscribedAudio>> r_update) { FileId file_id, bool is_initial, Result<telegram_api::object_ptr<telegram_api::updateTranscribedAudio>> r_update) {
if (r_update.is_error()) { if (r_update.is_error()) {
return on_voice_note_transcription_failed(file_id, r_update.move_as_error()); return on_voice_note_transcription_failed(file_id, r_update.move_as_error());
} }
auto update = r_update.move_as_ok(); auto update = r_update.move_as_ok();
on_voice_note_transcribed(file_id, std::move(update->text_), update->transcription_id_, false, !update->pending_); on_voice_note_transcribed(file_id, std::move(update->text_), update->transcription_id_, is_initial,
!update->pending_);
} }
void VoiceNotesManager::on_voice_note_transcription_failed(FileId file_id, Status &&error) { void VoiceNotesManager::on_voice_note_transcription_failed(FileId file_id, Status &&error) {

View File

@ -49,10 +49,6 @@ class VoiceNotesManager final : public Actor {
void rate_speech_recognition(FullMessageId full_message_id, bool is_good, Promise<Unit> &&promise); void rate_speech_recognition(FullMessageId full_message_id, bool is_good, Promise<Unit> &&promise);
void on_voice_note_transcribed(FileId file_id, string &&text, int64 transcription_id, bool is_initial, bool is_final);
void on_voice_note_transcription_failed(FileId file_id, Status &&error);
tl_object_ptr<telegram_api::InputMedia> get_input_media(FileId file_id, tl_object_ptr<telegram_api::InputMedia> get_input_media(FileId file_id,
tl_object_ptr<telegram_api::InputFile> input_file) const; tl_object_ptr<telegram_api::InputFile> input_file) const;
@ -87,11 +83,15 @@ class VoiceNotesManager final : public Actor {
FileId on_get_voice_note(unique_ptr<VoiceNote> new_voice_note, bool replace); FileId on_get_voice_note(unique_ptr<VoiceNote> new_voice_note, bool replace);
void on_voice_note_transcribed(FileId file_id, string &&text, int64 transcription_id, bool is_initial, bool is_final);
void on_voice_note_transcription_failed(FileId file_id, Status &&error);
void on_voice_note_transcription_updated(FileId file_id); void on_voice_note_transcription_updated(FileId file_id);
void on_voice_note_transcription_completed(FileId file_id); void on_voice_note_transcription_completed(FileId file_id);
void on_transcribed_audio_update(FileId file_id, void on_transcribed_audio_update(FileId file_id, bool is_initial,
Result<telegram_api::object_ptr<telegram_api::updateTranscribedAudio>> r_update); Result<telegram_api::object_ptr<telegram_api::updateTranscribedAudio>> r_update);
void tear_down() final; void tear_down() final;