From 17cc3b93f0ccddf49bbf8082c41b71adf1080136 Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 27 Sep 2019 03:21:55 +0300 Subject: [PATCH] Add td_api::pageBlockVoiceNote. GitOrigin-RevId: 4f5fdd595abba78863cf2e825c492b4ee99a11a3 --- td/generate/scheme/td_api.tl | 3 + td/generate/scheme/td_api.tlo | Bin 158112 -> 158240 bytes td/telegram/WebPageBlock.cpp | 128 +++++++++++++++++++++++++------- td/telegram/WebPageBlock.h | 13 ++-- td/telegram/WebPagesManager.cpp | 20 ++++- 5 files changed, 128 insertions(+), 36 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index da361d34..28aa6921 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -853,6 +853,9 @@ pageBlockPhoto photo:photo caption:pageBlockCaption url:string = PageBlock; //@description A video @video Video file; may be null @caption Video caption @need_autoplay True, if the video should be played automatically @is_looped True, if the video should be looped pageBlockVideo video:video caption:pageBlockCaption need_autoplay:Bool is_looped:Bool = PageBlock; +//@description A voice note @voice_note Voice note; may be null @caption Voice note caption +pageBlockVoiceNote voice_note:voiceNote caption:pageBlockCaption = PageBlock; + //@description A page cover @cover Cover pageBlockCover cover:PageBlock = PageBlock; diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index 4e59a570f65ad45e0568b08167b87b5b682efa60..ced7d907e0aca538d438160bca34efd380c6dedd 100644 GIT binary patch delta 845 zcmaJX|Xaq6IG&|=_%WH*!|4i0M2 z*Wm`Ik`{ymg>;e=#K|FGgtio+5a{ZVp<{FR^1l{aeABz%*Zbx29<|Sl#UPFe-YT;*7qJUo# ziOndC0qQT(IqTDsDNk;m1*5G-$*~AVR{u~HJf z%#A@{jzZhGRH)>wLQ%O`7ppoytkBin^%o*pehPosAk$!#6UeL_hhh2y`2+gr`dRGt z#@nN!UZR(sx8E!k=InAI5)58@fzfKErTWDl@p+s$HRi>U4tB=F?(D4HDHb^D>0TJ% z=3Ii_q?EvCG3bS9F^q6};UVoMoy7P*bo+jKD@krQi84>js?_bQ3Xfx|M!E?NuQ;}& z(Yu~&F#q~fCp8SQj1H^Yp^i+*iq%2;$=(N*o#=QM@O7&X-}$WdbU)mW9BrJU@#!~> C;xn57 delta 785 zcmaJ>^YM+R>qd51^c^LNRW-Y`wmQ3+ zTiyr+V(0bfy^fdj!-=UU!C_gW7fH7|5|H4~xPTO|7Q2}0$R2Nv`5vYZAj*yloi$IrSqbCZuc^RfCI7W0k->*VVpB$9nDz^F zpRGB`RAzSSZnSloI`(AhsQFl1MurbE6!{e> z3OvwFKi##{kE-Y#D8Td}COQ2&KvV26QorbY_+MJlh)!zAdl%Gb+lGc3$M$v7Rdp&Njpo@W DX$e2R diff --git a/td/telegram/WebPageBlock.cpp b/td/telegram/WebPageBlock.cpp index efa6dd85..4370cb49 100644 --- a/td/telegram/WebPageBlock.cpp +++ b/td/telegram/WebPageBlock.cpp @@ -25,6 +25,8 @@ #include "td/telegram/Version.h" #include "td/telegram/VideosManager.h" #include "td/telegram/VideosManager.hpp" +#include "td/telegram/VoiceNotesManager.h" +#include "td/telegram/VoiceNotesManager.hpp" #include "td/telegram/WebPageId.h" #include "td/utils/common.h" @@ -1401,8 +1403,10 @@ class WebPageBlockAudio : public WebPageBlock { using ::td::store; bool has_empty_audio = !audio_file_id.is_valid(); + bool is_voice_note_repaired = true; BEGIN_STORE_FLAGS(); STORE_FLAG(has_empty_audio); + STORE_FLAG(is_voice_note_repaired); END_STORE_FLAGS(); if (!has_empty_audio) { @@ -1416,17 +1420,23 @@ class WebPageBlockAudio : public WebPageBlock { using ::td::parse; bool has_empty_audio; + bool is_voice_note_repaired; if (parser.version() >= static_cast(Version::FixPageBlockAudioEmptyFile)) { BEGIN_PARSE_FLAGS(); PARSE_FLAG(has_empty_audio); + PARSE_FLAG(is_voice_note_repaired); END_PARSE_FLAGS(); } else { has_empty_audio = false; + is_voice_note_repaired = false; } if (!has_empty_audio) { audio_file_id = parser.context()->td().get_actor_unsafe()->audios_manager_->parse_audio(parser); } else { + if (!is_voice_note_repaired) { + parser.set_error("Trying to repair WebPageBlockVoiceNote"); + } audio_file_id = FileId(); } parse(caption, parser); @@ -1631,6 +1641,64 @@ class WebPageBlockMap : public WebPageBlock { } }; +class WebPageBlockVoiceNote : public WebPageBlock { + FileId voice_note_file_id; + WebPageBlockCaption caption; + + public: + WebPageBlockVoiceNote() = default; + WebPageBlockVoiceNote(FileId voice_note_file_id, WebPageBlockCaption &&caption) + : voice_note_file_id(voice_note_file_id), caption(std::move(caption)) { + } + + Type get_type() const override { + return Type::VoiceNote; + } + + void append_file_ids(vector &file_ids) const override { + Document(Document::Type::VoiceNote, voice_note_file_id).append_file_ids(G()->td().get_actor_unsafe(), file_ids); + caption.append_file_ids(file_ids); + } + + td_api::object_ptr get_page_block_object() const override { + return make_tl_object( + G()->td().get_actor_unsafe()->voice_notes_manager_->get_voice_note_object(voice_note_file_id), + caption.get_page_block_caption_object()); + } + + template + void store(StorerT &storer) const { + using ::td::store; + + bool has_empty_voice_note = !voice_note_file_id.is_valid(); + BEGIN_STORE_FLAGS(); + STORE_FLAG(has_empty_voice_note); + END_STORE_FLAGS(); + + if (!has_empty_voice_note) { + storer.context()->td().get_actor_unsafe()->voice_notes_manager_->store_voice_note(voice_note_file_id, storer); + } + store(caption, storer); + } + + template + void parse(ParserT &parser) { + using ::td::parse; + + bool has_empty_voice_note; + BEGIN_PARSE_FLAGS(); + PARSE_FLAG(has_empty_voice_note); + END_PARSE_FLAGS(); + + if (!has_empty_voice_note) { + voice_note_file_id = parser.context()->td().get_actor_unsafe()->voice_notes_manager_->parse_voice_note(parser); + } else { + voice_note_file_id = FileId(); + } + parse(caption, parser); + } +}; + vector get_rich_texts(vector> &&rich_text_ptrs, const std::unordered_map &documents); @@ -1770,7 +1838,8 @@ unique_ptr get_web_page_block(Td *td, tl_object_ptr &audios, const std::unordered_map &documents, const std::unordered_map &photos, - const std::unordered_map &videos) { + const std::unordered_map &videos, + const std::unordered_map &voice_notes) { CHECK(page_block_ptr != nullptr); switch (page_block_ptr->get_id()) { case telegram_api::pageBlockUnsupported::ID: @@ -1833,8 +1902,8 @@ unique_ptr get_web_page_block(Td *td, tl_object_ptr(list_item_ptr); - item.page_blocks = - get_web_page_blocks(td, std::move(list_item->blocks_), animations, audios, documents, photos, videos); + item.page_blocks = get_web_page_blocks(td, std::move(list_item->blocks_), animations, audios, documents, + photos, videos, voice_notes); break; } } @@ -1861,8 +1930,8 @@ unique_ptr get_web_page_block(Td *td, tl_object_ptr(list_item_ptr); item.label = std::move(list_item->num_); - item.page_blocks = - get_web_page_blocks(td, std::move(list_item->blocks_), animations, audios, documents, photos, videos); + item.page_blocks = get_web_page_blocks(td, std::move(list_item->blocks_), animations, audios, documents, + photos, videos, voice_notes); break; } } @@ -1928,7 +1997,8 @@ unique_ptr get_web_page_block(Td *td, tl_object_ptr(page_block_ptr); - auto cover = get_web_page_block(td, std::move(page_block->cover_), animations, audios, documents, photos, videos); + auto cover = get_web_page_block(td, std::move(page_block->cover_), animations, audios, documents, photos, videos, + voice_notes); if (cover == nullptr) { return nullptr; } @@ -1967,20 +2037,21 @@ unique_ptr get_web_page_block(Td *td, tl_object_ptr( std::move(page_block->url_), std::move(page_block->author_), std::move(author_photo), page_block->date_, - get_web_page_blocks(td, std::move(page_block->blocks_), animations, audios, documents, photos, videos), + get_web_page_blocks(td, std::move(page_block->blocks_), animations, audios, documents, photos, videos, + voice_notes), get_page_block_caption(std::move(page_block->caption_), documents)); } case telegram_api::pageBlockCollage::ID: { auto page_block = move_tl_object_as(page_block_ptr); - return td::make_unique( - get_web_page_blocks(td, std::move(page_block->items_), animations, audios, documents, photos, videos), - get_page_block_caption(std::move(page_block->caption_), documents)); + return td::make_unique(get_web_page_blocks(td, std::move(page_block->items_), animations, + audios, documents, photos, videos, voice_notes), + get_page_block_caption(std::move(page_block->caption_), documents)); } case telegram_api::pageBlockSlideshow::ID: { auto page_block = move_tl_object_as(page_block_ptr); - return td::make_unique( - get_web_page_blocks(td, std::move(page_block->items_), animations, audios, documents, photos, videos), - get_page_block_caption(std::move(page_block->caption_), documents)); + return td::make_unique(get_web_page_blocks(td, std::move(page_block->items_), animations, + audios, documents, photos, videos, voice_notes), + get_page_block_caption(std::move(page_block->caption_), documents)); } case telegram_api::pageBlockChannel::ID: { auto page_block = move_tl_object_as(page_block_ptr); @@ -2014,6 +2085,12 @@ unique_ptr get_web_page_block(Td *td, tl_object_ptr(page_block_ptr); + auto voice_note_it = voice_notes.find(page_block->audio_id_); + if (voice_note_it != voice_notes.end()) { + return make_unique(voice_note_it->second, + get_page_block_caption(std::move(page_block->caption_), documents)); + } + auto it = audios.find(page_block->audio_id_); FileId audio_file_id; if (it != audios.end()) { @@ -2063,10 +2140,10 @@ unique_ptr get_web_page_block(Td *td, tl_object_ptr(page_block_ptr); auto is_open = (page_block->flags_ & telegram_api::pageBlockDetails::OPEN_MASK) != 0; - return td::make_unique( - get_rich_text(std::move(page_block->title_), documents), - get_web_page_blocks(td, std::move(page_block->blocks_), animations, audios, documents, photos, videos), - is_open); + return td::make_unique(get_rich_text(std::move(page_block->title_), documents), + get_web_page_blocks(td, std::move(page_block->blocks_), animations, + audios, documents, photos, videos, voice_notes), + is_open); } case telegram_api::pageBlockRelatedArticles::ID: { auto page_block = move_tl_object_as(page_block_ptr); @@ -2181,6 +2258,8 @@ void WebPageBlock::call_impl(Type type, const WebPageBlock *ptr, F &&f) { return f(static_cast(ptr)); case Type::Map: return f(static_cast(ptr)); + case Type::VoiceNote: + return f(static_cast(ptr)); default: UNREACHABLE(); } @@ -2233,17 +2312,16 @@ void parse(unique_ptr &block, LogEventParser &parser) { parse_web_page_block(block, parser); } -vector> get_web_page_blocks(Td *td, - vector> page_block_ptrs, - const std::unordered_map &animations, - const std::unordered_map &audios, - const std::unordered_map &documents, - const std::unordered_map &photos, - const std::unordered_map &videos) { +vector> get_web_page_blocks( + Td *td, vector> page_block_ptrs, + const std::unordered_map &animations, const std::unordered_map &audios, + const std::unordered_map &documents, const std::unordered_map &photos, + const std::unordered_map &videos, const std::unordered_map &voice_notes) { vector> result; result.reserve(page_block_ptrs.size()); for (auto &page_block_ptr : page_block_ptrs) { - auto page_block = get_web_page_block(td, std::move(page_block_ptr), animations, audios, documents, photos, videos); + auto page_block = + get_web_page_block(td, std::move(page_block_ptr), animations, audios, documents, photos, videos, voice_notes); if (page_block != nullptr) { result.push_back(std::move(page_block)); } diff --git a/td/telegram/WebPageBlock.h b/td/telegram/WebPageBlock.h index ef28ee01..6c491fe0 100644 --- a/td/telegram/WebPageBlock.h +++ b/td/telegram/WebPageBlock.h @@ -52,6 +52,7 @@ class WebPageBlock { Details, RelatedArticles, Map, + VoiceNote, Size }; @@ -91,13 +92,11 @@ void store(const unique_ptr &block, LogEventStorerUnsafe &storer); void parse(unique_ptr &block, LogEventParser &parser); -vector> get_web_page_blocks(Td *td, - vector> page_block_ptrs, - const std::unordered_map &animations, - const std::unordered_map &audios, - const std::unordered_map &documents, - const std::unordered_map &photos, - const std::unordered_map &videos); +vector> get_web_page_blocks( + Td *td, vector> page_block_ptrs, + const std::unordered_map &animations, const std::unordered_map &audios, + const std::unordered_map &documents, const std::unordered_map &photos, + const std::unordered_map &videos, const std::unordered_map &voice_notes); vector> get_page_block_objects( const vector> &page_blocks); diff --git a/td/telegram/WebPagesManager.cpp b/td/telegram/WebPagesManager.cpp index f9331e38..3bada42e 100644 --- a/td/telegram/WebPagesManager.cpp +++ b/td/telegram/WebPagesManager.cpp @@ -834,10 +834,11 @@ void WebPagesManager::on_load_web_page_instant_view_from_database(WebPageId web_ WebPageInstantView result; if (!value.empty()) { - if (log_event_parse(result, value).is_error()) { + auto status = log_event_parse(result, value); + if (status.is_error()) { result = WebPageInstantView(); - LOG(ERROR) << "Erase instant view in " << web_page_id << " from database"; + LOG(ERROR) << "Erase instant view in " << web_page_id << " from database because of " << status.message(); G()->td_db()->get_sqlite_pmc()->erase(get_web_page_instant_view_database_key(web_page_id), Auto()); } } @@ -1199,6 +1200,7 @@ void WebPagesManager::on_get_web_page_instant_view(WebPage *web_page, tl_object_ std::unordered_map audios; std::unordered_map documents; std::unordered_map videos; + std::unordered_map voice_notes; for (auto &document_ptr : page->documents_) { if (document_ptr->get_id() == telegram_api::document::ID) { auto document = move_tl_object_as(document_ptr); @@ -1212,6 +1214,8 @@ void WebPagesManager::on_get_web_page_instant_view(WebPage *web_page, tl_object_ documents.emplace(document_id, parsed_document.file_id); } else if (parsed_document.type == Document::Type::Video) { videos.emplace(document_id, parsed_document.file_id); + } else if (parsed_document.type == Document::Type::VoiceNote) { + voice_notes.emplace(document_id, parsed_document.file_id); } else { LOG(ERROR) << "Receive document of the wrong type: " << parsed_document; } @@ -1249,12 +1253,20 @@ void WebPagesManager::on_get_web_page_instant_view(WebPage *web_page, tl_object_ LOG(ERROR) << "Video has no remote location"; } } + if (web_page->document.type == Document::Type::VoiceNote) { + auto file_view = td_->file_manager_->get_file_view(web_page->document.file_id); + if (file_view.has_remote_location()) { + voice_notes.emplace(file_view.remote_location().get_id(), web_page->document.file_id); + } else { + LOG(ERROR) << "Voice note has no remote location"; + } + } LOG(INFO) << "Receive a web page instant view with " << page->blocks_.size() << " blocks, " << animations.size() << " animations, " << audios.size() << " audios, " << documents.size() << " documents, " << photos.size() - << " photos and " << videos.size() << " videos"; + << " photos, " << videos.size() << " videos and " << voice_notes.size() << " voice notes"; web_page->instant_view.page_blocks = - get_web_page_blocks(td_, std::move(page->blocks_), animations, audios, documents, photos, videos); + get_web_page_blocks(td_, std::move(page->blocks_), animations, audios, documents, photos, videos, voice_notes); web_page->instant_view.is_v2 = (page->flags_ & telegram_api::page::V2_MASK) != 0; web_page->instant_view.is_rtl = (page->flags_ & telegram_api::page::RTL_MASK) != 0; web_page->instant_view.hash = hash;