From 2268ae87a4ea33fe9a34e69f2665b100124df14c Mon Sep 17 00:00:00 2001 From: levlam Date: Tue, 21 Feb 2023 22:35:20 +0300 Subject: [PATCH] Add td_api::webApp. --- CMakeLists.txt | 3 ++ td/generate/scheme/td_api.tl | 8 ++++ td/telegram/Game.cpp | 2 +- td/telegram/WebApp.cpp | 81 ++++++++++++++++++++++++++++++++++++ td/telegram/WebApp.h | 62 +++++++++++++++++++++++++++ td/telegram/WebApp.hpp | 58 ++++++++++++++++++++++++++ 6 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 td/telegram/WebApp.cpp create mode 100644 td/telegram/WebApp.h create mode 100644 td/telegram/WebApp.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4745f689a..f023be490 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -476,6 +476,7 @@ set(TDLIB_SOURCE td/telegram/VideoNotesManager.cpp td/telegram/VideosManager.cpp td/telegram/VoiceNotesManager.cpp + td/telegram/WebApp.cpp td/telegram/WebPageBlock.cpp td/telegram/WebPagesManager.cpp @@ -758,6 +759,7 @@ set(TDLIB_SOURCE td/telegram/VideoNotesManager.h td/telegram/VideosManager.h td/telegram/VoiceNotesManager.h + td/telegram/WebApp.h td/telegram/WebPageBlock.h td/telegram/WebPageId.h td/telegram/WebPagesManager.h @@ -810,6 +812,7 @@ set(TDLIB_SOURCE td/telegram/VideoNotesManager.hpp td/telegram/VideosManager.hpp td/telegram/VoiceNotesManager.hpp + td/telegram/WebApp.hpp ${TL_TD_SCHEME_SOURCE} diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index 9a935803a..d12dc7a21 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -472,6 +472,14 @@ venue location:location title:string address:string provider:string id:string ty //@animation Game animation; may be null game id:int64 short_name:string title:string text:formattedText description:string photo:photo animation:animation = Game; +//@description Describes a web app +//@short_name Web app short name. To share a web app use the URL https://t.me/{bot_username}/{app_short_name}?startapp={start_parameter} +//@title Web App title +//@param_description Web App description +//@photo Web App photo +//@animation Web App animation; may be null +webApp short_name:string title:string description:string photo:photo animation:animation = WebApp; + //@description Describes a poll //@id Unique poll identifier //@question Poll question; 1-300 characters diff --git a/td/telegram/Game.cpp b/td/telegram/Game.cpp index 9907ecd42..1014c274e 100644 --- a/td/telegram/Game.cpp +++ b/td/telegram/Game.cpp @@ -26,7 +26,7 @@ Game::Game(Td *td, UserId bot_user_id, tl_object_ptr &&game, id_ = game->id_; access_hash_ = game->access_hash_; bot_user_id_ = bot_user_id.is_valid() ? bot_user_id : UserId(); - short_name_ = game->short_name_; + short_name_ = std::move(game->short_name_); text_ = std::move(text); } diff --git a/td/telegram/WebApp.cpp b/td/telegram/WebApp.cpp new file mode 100644 index 000000000..a0bf27666 --- /dev/null +++ b/td/telegram/WebApp.cpp @@ -0,0 +1,81 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// 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/WebApp.h" + +#include "td/telegram/AnimationsManager.h" +#include "td/telegram/Document.h" +#include "td/telegram/DocumentsManager.h" +#include "td/telegram/misc.h" +#include "td/telegram/Photo.h" +#include "td/telegram/Td.h" + +#include "td/utils/common.h" +#include "td/utils/logging.h" + +namespace td { + +WebApp::WebApp(Td *td, telegram_api::object_ptr &&web_app, DialogId owner_dialog_id) + : id_(web_app->id_) + , access_hash_(web_app->access_hash_) + , short_name_(std::move(web_app->short_name_)) + , title_(std::move(web_app->title_)) + , description_(std::move(web_app->description_)) + , hash_(web_app->hash_) { + CHECK(td != nullptr); + photo_ = get_photo(td, std::move(web_app->photo_), owner_dialog_id); + if (photo_.is_empty()) { + LOG(ERROR) << "Receive empty photo for Web App " << short_name_ << '/' << title_; + photo_.id = 0; // to prevent null photo in td_api + } + if (web_app->document_ != nullptr) { + int32 document_id = web_app->document_->get_id(); + if (document_id == telegram_api::document::ID) { + auto parsed_document = td->documents_manager_->on_get_document( + move_tl_object_as(web_app->document_), owner_dialog_id); + if (parsed_document.type == Document::Type::Animation) { + animation_file_id_ = parsed_document.file_id; + } else { + LOG(ERROR) << "Receive non-animation document for Web App " << short_name_ << '/' << title_; + } + } + } +} + +bool WebApp::is_empty() const { + return short_name_.empty(); +} + +vector WebApp::get_file_ids(const Td *td) const { + auto result = photo_get_file_ids(photo_); + Document(Document::Type::Animation, animation_file_id_).append_file_ids(td, result); + return result; +} + +td_api::object_ptr WebApp::get_web_app_object(Td *td) const { + return td_api::make_object(short_name_, title_, description_, + get_photo_object(td->file_manager_.get(), photo_), + td->animations_manager_->get_animation_object(animation_file_id_)); +} + +bool operator==(const WebApp &lhs, const WebApp &rhs) { + return lhs.id_ == rhs.id_ && lhs.access_hash_ == rhs.access_hash_ && lhs.short_name_ == rhs.short_name_ && + lhs.title_ == rhs.title_ && lhs.description_ == rhs.description_ && lhs.photo_ == rhs.photo_ && + lhs.animation_file_id_ == rhs.animation_file_id_ && lhs.hash_ == rhs.hash_; +} + +bool operator!=(const WebApp &lhs, const WebApp &rhs) { + return !(lhs == rhs); +} + +StringBuilder &operator<<(StringBuilder &string_builder, const WebApp &web_app) { + return string_builder << "WebApp[ID = " << web_app.id_ << ", access_hash = " << web_app.access_hash_ + << ", short_name = " << web_app.short_name_ << ", title = " << web_app.title_ + << ", description = " << web_app.description_ << ", photo = " << web_app.photo_ + << ", animation_file_id = " << web_app.animation_file_id_ << "]"; +} + +} // namespace td diff --git a/td/telegram/WebApp.h b/td/telegram/WebApp.h new file mode 100644 index 000000000..15e8ef78d --- /dev/null +++ b/td/telegram/WebApp.h @@ -0,0 +1,62 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// 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/DialogId.h" +#include "td/telegram/files/FileId.h" +#include "td/telegram/Photo.h" +#include "td/telegram/td_api.h" +#include "td/telegram/telegram_api.h" + +#include "td/utils/common.h" +#include "td/utils/Status.h" +#include "td/utils/StringBuilder.h" + +namespace td { + +class ContactsManager; +class Td; + +class WebApp { + int64 id_ = 0; + int64 access_hash_ = 0; + string short_name_; + string title_; + string description_; + Photo photo_; + FileId animation_file_id_; + int64 hash_ = 0; + + friend bool operator==(const WebApp &lhs, const WebApp &rhs); + friend bool operator!=(const WebApp &lhs, const WebApp &rhs); + + friend StringBuilder &operator<<(StringBuilder &string_builder, const WebApp &web_app); + + public: + WebApp() = default; + + WebApp(Td *td, telegram_api::object_ptr &&web_app, DialogId owner_dialog_id); + + bool is_empty() const; + + vector get_file_ids(const Td *td) const; + + td_api::object_ptr get_web_app_object(Td *td) const; + + template + void store(StorerT &storer) const; + + template + void parse(ParserT &parser); +}; + +bool operator==(const WebApp &lhs, const WebApp &rhs); +bool operator!=(const WebApp &lhs, const WebApp &rhs); + +StringBuilder &operator<<(StringBuilder &string_builder, const WebApp &web_app); + +} // namespace td diff --git a/td/telegram/WebApp.hpp b/td/telegram/WebApp.hpp new file mode 100644 index 000000000..f386558c9 --- /dev/null +++ b/td/telegram/WebApp.hpp @@ -0,0 +1,58 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023 +// +// 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/WebApp.h" + +#include "td/telegram/AnimationsManager.hpp" +#include "td/telegram/Photo.hpp" +#include "td/telegram/Td.h" +#include "td/telegram/Version.h" + +#include "td/utils/tl_helpers.h" + +namespace td { + +template +void WebApp::store(StorerT &storer) const { + using td::store; + bool has_animation = animation_file_id_.is_valid(); + BEGIN_STORE_FLAGS(); + STORE_FLAG(has_animation); + END_STORE_FLAGS(); + store(id_, storer); + store(access_hash_, storer); + store(short_name_, storer); + store(title_, storer); + store(description_, storer); + store(photo_, storer); + if (has_animation) { + storer.context()->td().get_actor_unsafe()->animations_manager_->store_animation(animation_file_id_, storer); + } + store(hash_, storer); +} + +template +void WebApp::parse(ParserT &parser) { + using td::parse; + bool has_animation; + BEGIN_PARSE_FLAGS(); + PARSE_FLAG(has_animation); + END_PARSE_FLAGS(); + parse(id_, parser); + parse(access_hash_, parser); + parse(short_name_, parser); + parse(title_, parser); + parse(description_, parser); + parse(photo_, parser); + if (has_animation) { + animation_file_id_ = parser.context()->td().get_actor_unsafe()->animations_manager_->parse_animation(parser); + } + parse(hash_, parser); +} + +} // namespace td