2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-01-01 01:35:39 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2022
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// 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/VoiceNotesManager.h"
|
|
|
|
|
2022-06-02 15:19:16 +02:00
|
|
|
#include "td/telegram/AccessRights.h"
|
|
|
|
#include "td/telegram/DialogId.h"
|
2022-06-02 16:52:12 +02:00
|
|
|
#include "td/telegram/Dimensions.h"
|
2021-05-27 20:15:30 +02:00
|
|
|
#include "td/telegram/files/FileManager.h"
|
2022-05-25 20:18:40 +02:00
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/MessagesManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/secret_api.h"
|
2021-05-27 20:15:30 +02:00
|
|
|
#include "td/telegram/Td.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/telegram_api.h"
|
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2022-05-25 20:18:40 +02:00
|
|
|
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_,
|
|
|
|
!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));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-26 19:40:43 +02:00
|
|
|
class RateTranscribedAudioQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
DialogId dialog_id_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit RateTranscribedAudioQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(FullMessageId full_message_id, int64 transcription_id, bool is_good) {
|
|
|
|
dialog_id_ = full_message_id.get_dialog_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_rateTranscribedAudio(
|
|
|
|
std::move(input_peer), full_message_id.get_message_id().get_server_message_id().get(), transcription_id,
|
|
|
|
is_good)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_result(BufferSlice packet) final {
|
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_rateTranscribedAudio>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
|
|
|
return on_error(result_ptr.move_as_error());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool result = result_ptr.ok();
|
|
|
|
LOG(INFO) << "Receive result for RateTranscribedAudioQuery: " << result;
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_error(Status status) final {
|
|
|
|
td_->messages_manager_->on_get_dialog_error(dialog_id_, status, "RateTranscribedAudioQuery");
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-05-27 18:07:37 +02:00
|
|
|
VoiceNotesManager::VoiceNotesManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
|
|
|
|
voice_note_transcription_timeout_.set_callback(on_voice_note_transcription_timeout_callback);
|
|
|
|
voice_note_transcription_timeout_.set_callback_data(static_cast<void *>(this));
|
|
|
|
}
|
|
|
|
|
2022-07-20 12:40:14 +02:00
|
|
|
VoiceNotesManager::~VoiceNotesManager() {
|
2022-07-22 20:21:30 +02:00
|
|
|
Scheduler::instance()->destroy_on_scheduler(G()->get_gc_scheduler_id(), voice_notes_, voice_note_messages_,
|
|
|
|
message_voice_notes_);
|
2022-07-20 12:40:14 +02:00
|
|
|
}
|
|
|
|
|
2022-05-27 18:07:37 +02:00
|
|
|
void VoiceNotesManager::tear_down() {
|
|
|
|
parent_.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoiceNotesManager::on_voice_note_transcription_timeout_callback(void *voice_notes_manager_ptr,
|
|
|
|
int64 transcription_id) {
|
|
|
|
if (G()->close_flag()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto voice_notes_manager = static_cast<VoiceNotesManager *>(voice_notes_manager_ptr);
|
|
|
|
send_closure_later(voice_notes_manager->actor_id(voice_notes_manager),
|
|
|
|
&VoiceNotesManager::on_pending_voice_note_transcription_failed, transcription_id,
|
|
|
|
Status::Error(500, "Timeout expired"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 20:14:32 +02:00
|
|
|
int32 VoiceNotesManager::get_voice_note_duration(FileId file_id) const {
|
|
|
|
auto it = voice_notes_.find(file_id);
|
2022-04-18 18:08:32 +02:00
|
|
|
if (it == voice_notes_.end()) {
|
|
|
|
return 0;
|
|
|
|
}
|
2018-09-27 20:14:32 +02:00
|
|
|
return it->second->duration;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-08-28 13:16:29 +02:00
|
|
|
tl_object_ptr<td_api::voiceNote> VoiceNotesManager::get_voice_note_object(FileId file_id) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!file_id.is_valid()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-08-28 13:16:29 +02:00
|
|
|
auto it = voice_notes_.find(file_id);
|
|
|
|
CHECK(it != voice_notes_.end());
|
|
|
|
auto voice_note = it->second.get();
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(voice_note != nullptr);
|
2022-07-25 12:47:06 +02:00
|
|
|
|
|
|
|
auto speech_recognition_result = [this, voice_note]() -> td_api::object_ptr<td_api::SpeechRecognitionResult> {
|
|
|
|
if (voice_note->is_transcribed) {
|
|
|
|
return td_api::make_object<td_api::speechRecognitionResultText>(voice_note->text);
|
|
|
|
}
|
|
|
|
if (speech_recognition_queries_.count(voice_note->file_id) != 0) {
|
|
|
|
return td_api::make_object<td_api::speechRecognitionResultPending>(voice_note->text);
|
|
|
|
}
|
|
|
|
if (voice_note->last_transcription_error.is_error()) {
|
|
|
|
return td_api::make_object<td_api::speechRecognitionResultError>(
|
|
|
|
td_api::make_object<td_api::error>(voice_note->last_transcription_error.error().code(),
|
|
|
|
voice_note->last_transcription_error.error().message().str()));
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}();
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
return make_tl_object<td_api::voiceNote>(voice_note->duration, voice_note->waveform, voice_note->mime_type,
|
2022-07-25 12:47:06 +02:00
|
|
|
std::move(speech_recognition_result),
|
2018-03-07 00:02:49 +01:00
|
|
|
td_->file_manager_->get_file_object(file_id));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-09-27 03:19:03 +02:00
|
|
|
FileId VoiceNotesManager::on_get_voice_note(unique_ptr<VoiceNote> new_voice_note, bool replace) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_id = new_voice_note->file_id;
|
2019-08-01 02:58:49 +02:00
|
|
|
CHECK(file_id.is_valid());
|
2018-01-05 16:50:35 +01:00
|
|
|
LOG(INFO) << "Receive voice note " << file_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &v = voice_notes_[file_id];
|
|
|
|
if (v == nullptr) {
|
|
|
|
v = std::move(new_voice_note);
|
|
|
|
} else if (replace) {
|
|
|
|
CHECK(v->file_id == new_voice_note->file_id);
|
|
|
|
if (v->mime_type != new_voice_note->mime_type) {
|
|
|
|
LOG(DEBUG) << "Voice note " << file_id << " info has changed";
|
|
|
|
v->mime_type = new_voice_note->mime_type;
|
|
|
|
}
|
|
|
|
if (v->duration != new_voice_note->duration || v->waveform != new_voice_note->waveform) {
|
|
|
|
LOG(DEBUG) << "Voice note " << file_id << " info has changed";
|
|
|
|
v->duration = new_voice_note->duration;
|
|
|
|
v->waveform = new_voice_note->waveform;
|
|
|
|
}
|
2022-05-25 20:18:40 +02:00
|
|
|
if (new_voice_note->is_transcribed && v->transcription_id == 0) {
|
|
|
|
CHECK(!v->is_transcribed);
|
|
|
|
CHECK(new_voice_note->transcription_id != 0);
|
|
|
|
v->is_transcribed = true;
|
|
|
|
v->transcription_id = new_voice_note->transcription_id;
|
|
|
|
v->text = std::move(new_voice_note->text);
|
2022-07-25 12:47:06 +02:00
|
|
|
v->last_transcription_error = Status::OK();
|
2022-05-25 20:18:40 +02:00
|
|
|
on_voice_note_transcription_updated(file_id);
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return file_id;
|
|
|
|
}
|
|
|
|
|
2022-05-25 20:18:40 +02:00
|
|
|
VoiceNotesManager::VoiceNote *VoiceNotesManager::get_voice_note(FileId file_id) {
|
|
|
|
auto voice_note = voice_notes_.find(file_id);
|
|
|
|
if (voice_note == voice_notes_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(voice_note->second->file_id == file_id);
|
|
|
|
return voice_note->second.get();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
const VoiceNotesManager::VoiceNote *VoiceNotesManager::get_voice_note(FileId file_id) const {
|
|
|
|
auto voice_note = voice_notes_.find(file_id);
|
|
|
|
if (voice_note == voice_notes_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(voice_note->second->file_id == file_id);
|
|
|
|
return voice_note->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
FileId VoiceNotesManager::dup_voice_note(FileId new_id, FileId old_id) {
|
|
|
|
const VoiceNote *old_voice_note = get_voice_note(old_id);
|
|
|
|
CHECK(old_voice_note != nullptr);
|
|
|
|
auto &new_voice_note = voice_notes_[new_id];
|
2022-02-09 22:59:52 +01:00
|
|
|
CHECK(new_voice_note == nullptr);
|
2022-07-25 12:47:06 +02:00
|
|
|
new_voice_note = make_unique<VoiceNote>();
|
2018-12-31 20:04:05 +01:00
|
|
|
new_voice_note->file_id = new_id;
|
2022-07-25 12:47:06 +02:00
|
|
|
new_voice_note->mime_type = old_voice_note->mime_type;
|
|
|
|
new_voice_note->duration = old_voice_note->duration;
|
|
|
|
new_voice_note->waveform = old_voice_note->waveform;
|
|
|
|
if (old_voice_note->is_transcribed) {
|
|
|
|
new_voice_note->is_transcribed = old_voice_note->is_transcribed;
|
|
|
|
new_voice_note->text = old_voice_note->text;
|
|
|
|
}
|
2018-12-31 20:04:05 +01:00
|
|
|
return new_id;
|
|
|
|
}
|
|
|
|
|
2021-08-26 17:50:28 +02:00
|
|
|
void VoiceNotesManager::merge_voice_notes(FileId new_id, FileId old_id, bool can_delete_old) {
|
|
|
|
CHECK(old_id.is_valid() && new_id.is_valid());
|
|
|
|
CHECK(new_id != old_id);
|
2018-01-05 16:50:35 +01:00
|
|
|
|
|
|
|
LOG(INFO) << "Merge voice notes " << new_id << " and " << old_id;
|
2018-12-31 20:04:05 +01:00
|
|
|
const VoiceNote *old_ = get_voice_note(old_id);
|
|
|
|
CHECK(old_ != nullptr);
|
|
|
|
|
|
|
|
auto new_it = voice_notes_.find(new_id);
|
|
|
|
if (new_it == voice_notes_.end()) {
|
|
|
|
auto &old = voice_notes_[old_id];
|
|
|
|
if (!can_delete_old) {
|
|
|
|
dup_voice_note(new_id, old_id);
|
|
|
|
} else {
|
|
|
|
old->file_id = new_id;
|
|
|
|
voice_notes_.emplace(new_id, std::move(old));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
VoiceNote *new_ = new_it->second.get();
|
|
|
|
CHECK(new_ != nullptr);
|
|
|
|
|
|
|
|
if (!old_->mime_type.empty() && old_->mime_type != new_->mime_type) {
|
|
|
|
LOG(INFO) << "Voice note has changed: mime_type = (" << old_->mime_type << ", " << new_->mime_type << ")";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LOG_STATUS(td_->file_manager_->merge(new_id, old_id));
|
|
|
|
if (can_delete_old) {
|
|
|
|
voice_notes_.erase(old_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoiceNotesManager::create_voice_note(FileId file_id, string mime_type, int32 duration, string waveform,
|
|
|
|
bool replace) {
|
2018-09-27 03:19:03 +02:00
|
|
|
auto v = make_unique<VoiceNote>();
|
2018-12-31 20:04:05 +01:00
|
|
|
v->file_id = file_id;
|
|
|
|
v->mime_type = std::move(mime_type);
|
2018-02-12 11:37:54 +01:00
|
|
|
v->duration = max(duration, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
v->waveform = std::move(waveform);
|
|
|
|
on_get_voice_note(std::move(v), replace);
|
|
|
|
}
|
|
|
|
|
2022-05-25 18:39:15 +02:00
|
|
|
void VoiceNotesManager::register_voice_note(FileId voice_note_file_id, FullMessageId full_message_id,
|
|
|
|
const char *source) {
|
|
|
|
if (full_message_id.get_message_id().is_scheduled() || !full_message_id.get_message_id().is_server()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Register voice note " << voice_note_file_id << " from " << full_message_id << " from " << source;
|
|
|
|
bool is_inserted = voice_note_messages_[voice_note_file_id].insert(full_message_id).second;
|
|
|
|
LOG_CHECK(is_inserted) << source << ' ' << voice_note_file_id << ' ' << full_message_id;
|
2022-05-25 20:18:40 +02:00
|
|
|
is_inserted = message_voice_notes_.emplace(full_message_id, voice_note_file_id).second;
|
|
|
|
CHECK(is_inserted);
|
2022-05-25 18:39:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void VoiceNotesManager::unregister_voice_note(FileId voice_note_file_id, FullMessageId full_message_id,
|
|
|
|
const char *source) {
|
|
|
|
if (full_message_id.get_message_id().is_scheduled() || !full_message_id.get_message_id().is_server()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LOG(INFO) << "Unregister voice note " << voice_note_file_id << " from " << full_message_id << " from " << source;
|
|
|
|
auto &message_ids = voice_note_messages_[voice_note_file_id];
|
|
|
|
auto is_deleted = message_ids.erase(full_message_id) > 0;
|
|
|
|
LOG_CHECK(is_deleted) << source << ' ' << voice_note_file_id << ' ' << full_message_id;
|
|
|
|
if (message_ids.empty()) {
|
|
|
|
voice_note_messages_.erase(voice_note_file_id);
|
|
|
|
}
|
2022-06-02 15:19:16 +02:00
|
|
|
is_deleted = message_voice_notes_.erase(full_message_id) > 0;
|
2022-05-25 20:18:40 +02:00
|
|
|
CHECK(is_deleted);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoiceNotesManager::recognize_speech(FullMessageId full_message_id, Promise<Unit> &&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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto file_id = it->second;
|
|
|
|
auto voice_note = get_voice_note(file_id);
|
|
|
|
CHECK(voice_note != nullptr);
|
|
|
|
if (voice_note->is_transcribed) {
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
auto &queries = speech_recognition_queries_[file_id];
|
|
|
|
queries.push_back(std::move(promise));
|
|
|
|
if (queries.size() == 1) {
|
|
|
|
td_->create_handler<TranscribeAudioQuery>()->send(file_id, full_message_id);
|
2022-07-25 12:47:06 +02:00
|
|
|
voice_note->last_transcription_error = Status::OK();
|
|
|
|
on_voice_note_transcription_updated(file_id);
|
2022-05-25 20:18:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoiceNotesManager::on_voice_note_transcribed(FileId file_id, string &&text, int64 transcription_id,
|
|
|
|
bool is_final) {
|
|
|
|
auto voice_note = get_voice_note(file_id);
|
|
|
|
CHECK(voice_note != nullptr);
|
|
|
|
CHECK(!voice_note->is_transcribed);
|
2022-05-25 20:48:31 +02:00
|
|
|
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;
|
2022-05-25 20:18:40 +02:00
|
|
|
voice_note->transcription_id = transcription_id;
|
|
|
|
voice_note->is_transcribed = is_final;
|
|
|
|
voice_note->text = std::move(text);
|
2022-07-25 12:47:06 +02:00
|
|
|
voice_note->last_transcription_error = Status::OK();
|
2022-05-25 20:18:40 +02:00
|
|
|
|
|
|
|
if (is_final) {
|
|
|
|
auto it = speech_recognition_queries_.find(file_id);
|
|
|
|
CHECK(it != speech_recognition_queries_.end());
|
|
|
|
CHECK(!it->second.empty());
|
|
|
|
auto promises = std::move(it->second);
|
|
|
|
speech_recognition_queries_.erase(it);
|
|
|
|
|
2022-07-25 12:47:06 +02:00
|
|
|
on_voice_note_transcription_updated(file_id);
|
2022-05-25 20:18:40 +02:00
|
|
|
set_promises(promises);
|
2022-05-25 20:48:31 +02:00
|
|
|
} else {
|
2022-07-25 12:47:06 +02:00
|
|
|
if (is_changed) {
|
|
|
|
on_voice_note_transcription_updated(file_id);
|
|
|
|
}
|
|
|
|
|
2022-05-25 20:48:31 +02:00
|
|
|
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);
|
2022-05-27 18:07:37 +02:00
|
|
|
voice_note_transcription_timeout_.set_timeout_in(transcription_id, TRANSCRIPTION_TIMEOUT);
|
2022-05-25 20:18:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VoiceNotesManager::on_voice_note_transcription_failed(FileId file_id, Status &&error) {
|
|
|
|
auto voice_note = get_voice_note(file_id);
|
|
|
|
CHECK(voice_note != nullptr);
|
|
|
|
CHECK(!voice_note->is_transcribed);
|
2022-07-25 12:47:06 +02:00
|
|
|
CHECK(pending_voice_note_transcription_queries_.count(voice_note->transcription_id) == 0);
|
2022-05-25 20:48:31 +02:00
|
|
|
|
2022-07-25 12:47:06 +02:00
|
|
|
voice_note->transcription_id = 0;
|
|
|
|
voice_note->text.clear();
|
|
|
|
voice_note->last_transcription_error = error.clone();
|
2022-05-25 20:18:40 +02:00
|
|
|
|
|
|
|
auto it = speech_recognition_queries_.find(file_id);
|
|
|
|
CHECK(it != speech_recognition_queries_.end());
|
|
|
|
CHECK(!it->second.empty());
|
|
|
|
auto promises = std::move(it->second);
|
|
|
|
speech_recognition_queries_.erase(it);
|
|
|
|
|
2022-07-25 12:47:06 +02:00
|
|
|
on_voice_note_transcription_updated(file_id);
|
2022-05-25 20:18:40 +02:00
|
|
|
fail_promises(promises, std::move(error));
|
|
|
|
}
|
|
|
|
|
2022-05-25 20:48:31 +02:00
|
|
|
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);
|
2022-05-27 18:07:37 +02:00
|
|
|
voice_note_transcription_timeout_.cancel_timeout(transcription_id);
|
2022-05-25 20:48:31 +02:00
|
|
|
|
|
|
|
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);
|
2022-05-27 18:07:37 +02:00
|
|
|
voice_note_transcription_timeout_.cancel_timeout(transcription_id);
|
|
|
|
|
2022-05-25 20:48:31 +02:00
|
|
|
on_voice_note_transcription_failed(file_id, std::move(error));
|
|
|
|
}
|
|
|
|
|
2022-05-25 20:18:40 +02:00
|
|
|
void VoiceNotesManager::on_voice_note_transcription_updated(FileId file_id) {
|
|
|
|
auto it = voice_note_messages_.find(file_id);
|
|
|
|
if (it != voice_note_messages_.end()) {
|
|
|
|
for (const auto &full_message_id : it->second) {
|
|
|
|
td_->messages_manager_->on_external_update_message_content(full_message_id);
|
|
|
|
}
|
|
|
|
}
|
2022-05-25 18:39:15 +02:00
|
|
|
}
|
|
|
|
|
2022-05-26 19:40:43 +02:00
|
|
|
void VoiceNotesManager::rate_speech_recognition(FullMessageId full_message_id, bool is_good, Promise<Unit> &&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"));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto file_id = it->second;
|
|
|
|
auto voice_note = get_voice_note(file_id);
|
|
|
|
CHECK(voice_note != nullptr);
|
|
|
|
if (!voice_note->is_transcribed) {
|
|
|
|
return promise.set_value(Unit());
|
|
|
|
}
|
|
|
|
CHECK(voice_note->transcription_id != 0);
|
|
|
|
td_->create_handler<RateTranscribedAudioQuery>(std::move(promise))
|
|
|
|
->send(full_message_id, voice_note->transcription_id, is_good);
|
|
|
|
}
|
|
|
|
|
2022-05-25 18:39:15 +02:00
|
|
|
SecretInputMedia VoiceNotesManager::get_secret_input_media(FileId voice_note_file_id,
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<telegram_api::InputEncryptedFile> input_file,
|
2022-05-11 06:46:06 +02:00
|
|
|
const string &caption, int32 layer) const {
|
2022-05-25 18:39:15 +02:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(voice_note_file_id);
|
2022-05-11 00:53:18 +02:00
|
|
|
if (!file_view.is_encrypted_secret() || file_view.encryption_key().empty()) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return SecretInputMedia{};
|
|
|
|
}
|
|
|
|
if (file_view.has_remote_location()) {
|
2019-11-17 20:41:28 +01:00
|
|
|
input_file = file_view.main_remote_location().as_input_encrypted_file();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (!input_file) {
|
|
|
|
return SecretInputMedia{};
|
|
|
|
}
|
2022-05-25 18:39:15 +02:00
|
|
|
|
|
|
|
auto *voice_note = get_voice_note(voice_note_file_id);
|
|
|
|
CHECK(voice_note != nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<tl_object_ptr<secret_api::DocumentAttribute>> attributes;
|
|
|
|
attributes.push_back(make_tl_object<secret_api::documentAttributeAudio>(
|
2018-12-04 19:18:07 +01:00
|
|
|
secret_api::documentAttributeAudio::VOICE_MASK | secret_api::documentAttributeAudio::WAVEFORM_MASK,
|
2018-12-31 20:04:05 +01:00
|
|
|
false /*ignored*/, voice_note->duration, "", "", BufferSlice(voice_note->waveform)));
|
2022-05-11 00:53:18 +02:00
|
|
|
|
|
|
|
return {std::move(input_file), BufferSlice(), Dimensions(), voice_note->mime_type, file_view,
|
2022-05-11 06:46:06 +02:00
|
|
|
std::move(attributes), caption, layer};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
tl_object_ptr<telegram_api::InputMedia> VoiceNotesManager::get_input_media(
|
2018-01-30 18:06:54 +01:00
|
|
|
FileId file_id, tl_object_ptr<telegram_api::InputFile> input_file) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_view = td_->file_manager_->get_file_view(file_id);
|
|
|
|
if (file_view.is_encrypted()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2019-11-17 20:41:28 +01:00
|
|
|
if (file_view.has_remote_location() && !file_view.main_remote_location().is_web() && input_file == nullptr) {
|
2020-12-18 15:43:23 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaDocument>(0, file_view.main_remote_location().as_input_document(), 0,
|
|
|
|
string());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (file_view.has_url()) {
|
2018-01-30 18:06:54 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaDocumentExternal>(0, file_view.url(), 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (input_file != nullptr) {
|
|
|
|
const VoiceNote *voice_note = get_voice_note(file_id);
|
|
|
|
CHECK(voice_note != nullptr);
|
|
|
|
|
|
|
|
vector<tl_object_ptr<telegram_api::DocumentAttribute>> attributes;
|
|
|
|
int32 flags = telegram_api::documentAttributeAudio::VOICE_MASK;
|
|
|
|
if (!voice_note->waveform.empty()) {
|
|
|
|
flags |= telegram_api::documentAttributeAudio::WAVEFORM_MASK;
|
|
|
|
}
|
|
|
|
attributes.push_back(make_tl_object<telegram_api::documentAttributeAudio>(
|
|
|
|
flags, false /*ignored*/, voice_note->duration, "", "", BufferSlice(voice_note->waveform)));
|
|
|
|
string mime_type = voice_note->mime_type;
|
|
|
|
if (mime_type != "audio/ogg" && mime_type != "audio/mpeg" && mime_type != "audio/mp4") {
|
|
|
|
mime_type = "audio/ogg";
|
|
|
|
}
|
|
|
|
return make_tl_object<telegram_api::inputMediaUploadedDocument>(
|
2020-06-22 01:02:21 +02:00
|
|
|
0, false /*ignored*/, false /*ignored*/, std::move(input_file), nullptr, mime_type, std::move(attributes),
|
2018-12-31 20:04:05 +01:00
|
|
|
vector<tl_object_ptr<telegram_api::InputDocument>>(), 0);
|
2019-02-12 02:50:30 +01:00
|
|
|
} else {
|
|
|
|
CHECK(!file_view.has_remote_location());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|