Register voice note messages.

This commit is contained in:
levlam 2022-05-25 19:39:15 +03:00
parent 44832189e3
commit de4d3e7620
3 changed files with 52 additions and 5 deletions

View File

@ -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<const MessageVoiceNote *>(content)->file_id,
full_message_id, source);
case MessageContentType::Poll:
return td->poll_manager_->register_poll(static_cast<const MessagePoll *>(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<const MessageVoiceNote *>(old_content)->file_id ==
static_cast<const MessageVoiceNote *>(new_content)->file_id) {
return;
}
break;
case MessageContentType::Poll:
if (static_cast<const MessagePoll *>(old_content)->poll_id ==
static_cast<const MessagePoll *>(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<const MessageVoiceNote *>(content)->file_id,
full_message_id, source);
case MessageContentType::Poll:
return td->poll_manager_->unregister_poll(static_cast<const MessagePoll *>(content)->poll_id, full_message_id,
source);

View File

@ -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<telegram_api::InputEncryptedFile> 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<tl_object_ptr<secret_api::DocumentAttribute>> attributes;
attributes.push_back(make_tl_object<secret_api::documentAttributeAudio>(
secret_api::documentAttributeAudio::VOICE_MASK | secret_api::documentAttributeAudio::WAVEFORM_MASK,

View File

@ -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<telegram_api::InputMedia> get_input_media(FileId file_id,
tl_object_ptr<telegram_api::InputFile> 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<telegram_api::InputEncryptedFile> input_file,
const string &caption, int32 layer) const;
@ -65,6 +71,8 @@ class VoiceNotesManager {
Td *td_;
FlatHashMap<FileId, unique_ptr<VoiceNote>, FileIdHash> voice_notes_;
FlatHashMap<FileId, FlatHashSet<FullMessageId, FullMessageIdHash>, FileIdHash> voice_note_messages_;
};
} // namespace td