From 46a0b0036dcca3b57ae695ed54650d5dbd48a223 Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 9 Apr 2019 18:52:53 +0300 Subject: [PATCH] Add Document.hpp. GitOrigin-RevId: e26a6aa44453c2a5e2216d91782e94f5bda9686d --- CMakeLists.txt | 1 + td/telegram/Document.cpp | 3 +- td/telegram/Document.h | 2 +- td/telegram/Document.hpp | 98 +++++++++++++++++++++++++++++++++ td/telegram/WebPagesManager.cpp | 67 +--------------------- 5 files changed, 104 insertions(+), 67 deletions(-) create mode 100644 td/telegram/Document.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 45a935301..6be026faa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -617,6 +617,7 @@ set(TDLIB_SOURCE td/telegram/AnimationsManager.hpp td/telegram/AudiosManager.hpp td/telegram/AuthManager.hpp + td/telegram/Document.hpp td/telegram/DocumentsManager.hpp td/telegram/DraftMessage.hpp td/telegram/FileReferenceManager.hpp diff --git a/td/telegram/Document.cpp b/td/telegram/Document.cpp index e53480f62..92e26e927 100644 --- a/td/telegram/Document.cpp +++ b/td/telegram/Document.cpp @@ -31,9 +31,8 @@ StringBuilder &operator<<(StringBuilder &string_builder, const Document &documen return "Unreachable"; } }(); - + return string_builder << '[' << type << ' ' << document.file_id << ']'; } - } // namespace td diff --git a/td/telegram/Document.h b/td/telegram/Document.h index b439b4427..5ca243692 100644 --- a/td/telegram/Document.h +++ b/td/telegram/Document.h @@ -21,7 +21,7 @@ struct Document { FileId file_id; Document() = default; - Document(Type type, FileId file_id): type(type), file_id(file_id) { + Document(Type type, FileId file_id) : type(type), file_id(file_id) { } }; diff --git a/td/telegram/Document.hpp b/td/telegram/Document.hpp new file mode 100644 index 000000000..a0a5011fa --- /dev/null +++ b/td/telegram/Document.hpp @@ -0,0 +1,98 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019 +// +// 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) +// +#pragma once + +#include "td/telegram/Document.h" + +#include "td/telegram/AnimationsManager.h" +#include "td/telegram/AudiosManager.h" +#include "td/telegram/DocumentsManager.h" +#include "td/telegram/files/FileId.hpp" +#include "td/telegram/StickersManager.h" +#include "td/telegram/Td.h" +#include "td/telegram/VideosManager.h" +#include "td/telegram/VideoNotesManager.h" +#include "td/telegram/VoiceNotesManager.h" + +#include "td/utils/logging.h" +#include "td/utils/tl_helpers.h" + +namespace td { + +template +void store(const Document &document, StorerT &storer) { + Td *td = storer.context()->td().get_actor_unsafe(); + CHECK(td != nullptr); + + store(document.type, storer); + switch (document.type) { + case Document::Type::Animation: + td->animations_manager_->store_animation(document.file_id, storer); + break; + case Document::Type::Audio: + td->audios_manager_->store_audio(document.file_id, storer); + break; + case Document::Type::General: + td->documents_manager_->store_document(document.file_id, storer); + break; + case Document::Type::Sticker: + td->stickers_manager_->store_sticker(document.file_id, false, storer); + break; + case Document::Type::Video: + td->videos_manager_->store_video(document.file_id, storer); + break; + case Document::Type::VideoNote: + td->video_notes_manager_->store_video_note(document.file_id, storer); + break; + case Document::Type::VoiceNote: + td->voice_notes_manager_->store_voice_note(document.file_id, storer); + break; + case Document::Type::Unknown: + default: + UNREACHABLE(); + } +} + +template +void parse(Document &document, ParserT &parser) { + Td *td = parser.context()->td().get_actor_unsafe(); + CHECK(td != nullptr); + + parse(document.type, parser); + switch (document.type) { + case Document::Type::Animation: + document.file_id = td->animations_manager_->parse_animation(parser); + break; + case Document::Type::Audio: + document.file_id = td->audios_manager_->parse_audio(parser); + break; + case Document::Type::General: + document.file_id = td->documents_manager_->parse_document(parser); + break; + case Document::Type::Sticker: + document.file_id = td->stickers_manager_->parse_sticker(false, parser); + break; + case Document::Type::Video: + document.file_id = td->videos_manager_->parse_video(parser); + break; + case Document::Type::VideoNote: + document.file_id = td->video_notes_manager_->parse_video_note(parser); + break; + case Document::Type::VoiceNote: + document.file_id = td->voice_notes_manager_->parse_voice_note(parser); + break; + case Document::Type::Unknown: + default: + UNREACHABLE(); + } + if (!document.file_id.is_valid()) { + LOG(ERROR) << "Parse invalid document.file_id"; + document = Document(); + } +} + +} // namespace td diff --git a/td/telegram/WebPagesManager.cpp b/td/telegram/WebPagesManager.cpp index 630e1de12..a3e2e2164 100644 --- a/td/telegram/WebPagesManager.cpp +++ b/td/telegram/WebPagesManager.cpp @@ -16,6 +16,7 @@ #include "td/telegram/ChannelId.h" #include "td/telegram/ContactsManager.h" #include "td/telegram/Document.h" +#include "td/telegram/Document.hpp" #include "td/telegram/DocumentsManager.h" #include "td/telegram/DocumentsManager.hpp" #include "td/telegram/FileReferenceManager.h" @@ -280,36 +281,7 @@ class WebPagesManager::WebPage { store(author, storer); } if (has_document) { - Td *td = storer.context()->td().get_actor_unsafe(); - CHECK(td != nullptr); - - store(document.type, storer); - switch (document.type) { - case Document::Type::Animation: - td->animations_manager_->store_animation(document.file_id, storer); - break; - case Document::Type::Audio: - td->audios_manager_->store_audio(document.file_id, storer); - break; - case Document::Type::General: - td->documents_manager_->store_document(document.file_id, storer); - break; - case Document::Type::Sticker: - td->stickers_manager_->store_sticker(document.file_id, false, storer); - break; - case Document::Type::Video: - td->videos_manager_->store_video(document.file_id, storer); - break; - case Document::Type::VideoNote: - td->video_notes_manager_->store_video_note(document.file_id, storer); - break; - case Document::Type::VoiceNote: - td->voice_notes_manager_->store_voice_note(document.file_id, storer); - break; - case Document::Type::Unknown: - default: - UNREACHABLE(); - } + store(document, storer); } } @@ -382,40 +354,7 @@ class WebPagesManager::WebPage { parse(author, parser); } if (has_document) { - Td *td = parser.context()->td().get_actor_unsafe(); - CHECK(td != nullptr); - - parse(document.type, parser); - switch (document.type) { - case Document::Type::Animation: - document.file_id = td->animations_manager_->parse_animation(parser); - break; - case Document::Type::Audio: - document.file_id = td->audios_manager_->parse_audio(parser); - break; - case Document::Type::General: - document.file_id = td->documents_manager_->parse_document(parser); - break; - case Document::Type::Sticker: - document.file_id = td->stickers_manager_->parse_sticker(false, parser); - break; - case Document::Type::Video: - document.file_id = td->videos_manager_->parse_video(parser); - break; - case Document::Type::VideoNote: - document.file_id = td->video_notes_manager_->parse_video_note(parser); - break; - case Document::Type::VoiceNote: - document.file_id = td->voice_notes_manager_->parse_voice_note(parser); - break; - case Document::Type::Unknown: - default: - UNREACHABLE(); - } - if (!document.file_id.is_valid()) { - LOG(ERROR) << "Parse invalid document.file_id"; - document = Document(); - } + parse(document, parser); } if (has_instant_view) {