diff --git a/td/telegram/MessageContent.cpp b/td/telegram/MessageContent.cpp index 5244a9284..5a076c9f5 100644 --- a/td/telegram/MessageContent.cpp +++ b/td/telegram/MessageContent.cpp @@ -3722,6 +3722,9 @@ void register_message_content(Td *td, const MessageContent *content, FullMessage } return; } + case MessageContentType::VoiceNote: + return td->voice_notes_manager_->register_voice_note(static_cast(content)->file_id, + full_message_id, source); case MessageContentType::Poll: return td->poll_manager_->register_poll(static_cast(content)->poll_id, full_message_id, source); @@ -3750,6 +3753,12 @@ void reregister_message_content(Td *td, const MessageContent *old_content, const } break; } + case MessageContentType::VoiceNote: + if (static_cast(old_content)->file_id == + static_cast(new_content)->file_id) { + return; + } + break; case MessageContentType::Poll: if (static_cast(old_content)->poll_id == static_cast(new_content)->poll_id) { @@ -3784,6 +3793,9 @@ void unregister_message_content(Td *td, const MessageContent *content, FullMessa } return; } + case MessageContentType::VoiceNote: + return td->voice_notes_manager_->unregister_voice_note(static_cast(content)->file_id, + full_message_id, source); case MessageContentType::Poll: return td->poll_manager_->unregister_poll(static_cast(content)->poll_id, full_message_id, source); diff --git a/td/telegram/VoiceNotesManager.cpp b/td/telegram/VoiceNotesManager.cpp index 4f069ca01..bdaeb17c8 100644 --- a/td/telegram/VoiceNotesManager.cpp +++ b/td/telegram/VoiceNotesManager.cpp @@ -129,12 +129,36 @@ void VoiceNotesManager::create_voice_note(FileId file_id, string mime_type, int3 on_get_voice_note(std::move(v), replace); } -SecretInputMedia VoiceNotesManager::get_secret_input_media(FileId voice_file_id, +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; + auto voice_note = get_voice_note(voice_note_file_id); + CHECK(voice_note != nullptr); +} + +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); + } +} + +SecretInputMedia VoiceNotesManager::get_secret_input_media(FileId voice_note_file_id, tl_object_ptr input_file, const string &caption, int32 layer) const { - auto *voice_note = get_voice_note(voice_file_id); - CHECK(voice_note != nullptr); - auto file_view = td_->file_manager_->get_file_view(voice_file_id); + auto file_view = td_->file_manager_->get_file_view(voice_note_file_id); if (!file_view.is_encrypted_secret() || file_view.encryption_key().empty()) { return SecretInputMedia{}; } @@ -144,6 +168,9 @@ SecretInputMedia VoiceNotesManager::get_secret_input_media(FileId voice_file_id, if (!input_file) { return SecretInputMedia{}; } + + auto *voice_note = get_voice_note(voice_note_file_id); + CHECK(voice_note != nullptr); vector> attributes; attributes.push_back(make_tl_object( secret_api::documentAttributeAudio::VOICE_MASK | secret_api::documentAttributeAudio::WAVEFORM_MASK, diff --git a/td/telegram/VoiceNotesManager.h b/td/telegram/VoiceNotesManager.h index 0a5fa9668..9f610480b 100644 --- a/td/telegram/VoiceNotesManager.h +++ b/td/telegram/VoiceNotesManager.h @@ -7,6 +7,7 @@ #pragma once #include "td/telegram/files/FileId.h" +#include "td/telegram/FullMessageId.h" #include "td/telegram/SecretInputMedia.h" #include "td/telegram/td_api.h" #include "td/telegram/telegram_api.h" @@ -14,6 +15,7 @@ #include "td/utils/common.h" #include "td/utils/FlatHashMap.h" +#include "td/utils/FlatHashSet.h" namespace td { @@ -29,10 +31,14 @@ class VoiceNotesManager { void create_voice_note(FileId file_id, string mime_type, int32 duration, string waveform, bool replace); + void register_voice_note(FileId voice_note_file_id, FullMessageId full_message_id, const char *source); + + void unregister_voice_note(FileId voice_note_file_id, FullMessageId full_message_id, const char *source); + tl_object_ptr get_input_media(FileId file_id, tl_object_ptr input_file) const; - SecretInputMedia get_secret_input_media(FileId voice_file_id, + SecretInputMedia get_secret_input_media(FileId voice_note_file_id, tl_object_ptr input_file, const string &caption, int32 layer) const; @@ -65,6 +71,8 @@ class VoiceNotesManager { Td *td_; FlatHashMap, FileIdHash> voice_notes_; + + FlatHashMap, FileIdHash> voice_note_messages_; }; } // namespace td