From 8f4e9b2ed4a12c152ae4ea9cf688e6a0ebf681da Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 20 Sep 2020 18:17:47 +0300 Subject: [PATCH] Add td_api::getMessageEmbeddingCode. GitOrigin-RevId: cd26212418c3de5fc5046da456fad84f0f633d0d --- td/generate/scheme/td_api.tl | 10 ++++-- td/generate/scheme/td_api.tlo | Bin 182040 -> 182208 bytes td/telegram/MessagesManager.cpp | 53 ++++++++++++++++++++++++++++++++ td/telegram/MessagesManager.h | 2 ++ td/telegram/Td.cpp | 28 +++++++++++++++++ td/telegram/Td.h | 2 ++ td/telegram/cli.cpp | 8 +++++ 7 files changed, 101 insertions(+), 2 deletions(-) diff --git a/td/generate/scheme/td_api.tl b/td/generate/scheme/td_api.tl index b820ab93b..3d924c2d2 100644 --- a/td/generate/scheme/td_api.tl +++ b/td/generate/scheme/td_api.tl @@ -661,7 +661,7 @@ messageSendingStateFailed error_code:int32 error_message:string can_retry:Bool r //@description Describes a message -//@id Message identifier, unique for the chat to which the message belongs +//@id Message identifier; unique for the chat to which the message belongs //@sender_user_id Identifier of the user who sent the message; 0 if unknown. Currently, it is unknown for channel posts, for channel posts automatically forwarded to discussion group and for anonimously sent supergroup messages //@sender_chat_id Identifier of the chat on behalf of which the message was sent; 0 if none //@chat_id Chat identifier @@ -682,7 +682,7 @@ messageSendingStateFailed error_code:int32 error_message:string can_retry:Bool r //@interaction_info Information about interactions with the message; may be null //@reply_in_chat_id If non-zero, the identifier of the chat to which the replied message belongs; Currently, only messages in the Replies chat can have different reply_in_chat_id and chat_id //@reply_to_message_id If non-zero, the identifier of the message this message is replying to; can be the identifier of a deleted message -//@message_thread_id If non-zero, the identifier of the message thread the message belongs to +//@message_thread_id If non-zero, the identifier of the message thread the message belongs to; unique within the chat to which the message belongs //@ttl For self-destructing messages, the message's TTL (Time To Live), in seconds; 0 if none. TDLib will send updateDeleteMessages or updateMessageContent once the TTL expires //@ttl_expires_in Time left before the message expires, in seconds //@via_bot_user_id If non-zero, the user identifier of the bot through which this message was sent @@ -3702,6 +3702,12 @@ getPublicMessageLink chat_id:int53 message_id:int53 for_album:Bool for_comment:B //@for_comment Pass true to create a link to the message as a channel post comment, or from a message thread getMessageLink chat_id:int53 message_id:int53 for_album:Bool for_comment:Bool = HttpUrl; +//@description Returns an HTML code for embedding the message. Available only for messages in supergroups and channels with a username +//@chat_id Identifier of the chat to which the message belongs +//@message_id Identifier of the message +//@for_album Pass true to return an HTML code for embedding of the whole media album +getMessageEmbeddingCode chat_id:int53 message_id:int53 for_album:Bool = Text; + //@description Returns information about a public or private message link @url The message link in the format "https://t.me/c/...", or "tg://privatepost?...", or "https://t.me/username/...", or "tg://resolve?..." getMessageLinkInfo url:string = MessageLinkInfo; diff --git a/td/generate/scheme/td_api.tlo b/td/generate/scheme/td_api.tlo index 6dd40203e46be6dfbc238df75792156ff9970e43..8a73cfa618a9ef6ed34d53eca3f364b66a04d615 100644 GIT binary patch delta 78 zcmV-U0I~m=jtjt#3xI?Hv;xQo0fV>62m%vPAPLQ+Vi#v+bWLS*b75y?MQvhbWMpY> kXG3pfWtdoYB9_GgP?wA-0U(o*X$Y4vwE+;f+*txVZGHC~^8f$< delta 27 jcmX@m&po4$yP<_~3)2Y>#-{BjIhce4x3@$w+2jHMlFkXp diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 934f989c2..fa041ccce 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -16212,6 +16212,59 @@ string MessagesManager::get_message_link(FullMessageId full_message_id, bool for << dialog_id.get_channel_id().get() << "/" << m->message_id.get_server_message_id().get() << args; } +string MessagesManager::get_message_embedding_code(FullMessageId full_message_id, bool for_group, + Promise &&promise) { + auto dialog_id = full_message_id.get_dialog_id(); + auto d = get_dialog_force(dialog_id); + if (d == nullptr) { + promise.set_error(Status::Error(400, "Chat not found")); + return {}; + } + if (!have_input_peer(dialog_id, AccessRights::Read)) { + promise.set_error(Status::Error(400, "Can't access the chat")); + return {}; + } + if (dialog_id.get_type() != DialogType::Channel || + td_->contacts_manager_->get_channel_username(dialog_id.get_channel_id()).empty()) { + promise.set_error(Status::Error( + 400, "Message embedding code is available only for messages in public supergroups and channel chats")); + return {}; + } + + auto *m = get_message_force(d, full_message_id.get_message_id(), "get_message_embedding_code"); + if (m == nullptr) { + promise.set_error(Status::Error(400, "Message not found")); + return {}; + } + if (m->message_id.is_yet_unsent()) { + promise.set_error(Status::Error(400, "Message is yet unsent")); + return {}; + } + if (m->message_id.is_scheduled()) { + promise.set_error(Status::Error(400, "Message is scheduled")); + return {}; + } + if (!m->message_id.is_server()) { + promise.set_error(Status::Error(400, "Message is local")); + return {}; + } + + if (m->media_album_id == 0) { + for_group = true; // default is true + } + + auto &links = public_message_links_[for_group][dialog_id].links_; + auto it = links.find(m->message_id); + if (it == links.end()) { + td_->create_handler(std::move(promise)) + ->send(dialog_id.get_channel_id(), m->message_id, for_group, false); + return {}; + } + + promise.set_value(Unit()); + return it->second.second; +} + Result MessagesManager::get_message_link_info(Slice url) { if (url.empty()) { return Status::Error("URL must be non-empty"); diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index e0d2fe2e3..9ec22e50b 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -589,6 +589,8 @@ class MessagesManager : public Actor { std::pair get_public_message_link(FullMessageId full_message_id, bool for_group, bool for_comment, Promise &&promise); + string get_message_embedding_code(FullMessageId full_message_id, bool for_group, Promise &&promise); + void on_get_public_message_link(FullMessageId full_message_id, bool for_group, string url, string html); string get_message_link(FullMessageId full_message_id, bool for_group, bool for_comment, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index a48861a81..6e4760d4a 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -1165,6 +1165,29 @@ class GetMessageLinkRequest : public RequestActor<> { } }; +class GetMessageEmbeddingCodeRequest : public RequestActor<> { + FullMessageId full_message_id_; + bool for_group_; + + string html_; + + void do_run(Promise &&promise) override { + html_ = td->messages_manager_->get_message_embedding_code(full_message_id_, for_group_, std::move(promise)); + } + + void do_send_result() override { + send_result(make_tl_object(html_)); + } + + public: + GetMessageEmbeddingCodeRequest(ActorShared td, uint64 request_id, int64 dialog_id, int64 message_id, + bool for_group) + : RequestActor(std::move(td), request_id) + , full_message_id_(DialogId(dialog_id), MessageId(message_id)) + , for_group_(for_group) { + } +}; + class GetMessageLinkInfoRequest : public RequestActor { string url_; @@ -5148,6 +5171,11 @@ void Td::on_request(uint64 id, const td_api::getMessageLink &request) { request.for_comment_); } +void Td::on_request(uint64 id, const td_api::getMessageEmbeddingCode &request) { + CHECK_IS_USER(); + CREATE_REQUEST(GetMessageEmbeddingCodeRequest, request.chat_id_, request.message_id_, request.for_album_); +} + void Td::on_request(uint64 id, td_api::getMessageLinkInfo &request) { CLEAN_INPUT_STRING(request.url_); CREATE_REQUEST(GetMessageLinkInfoRequest, std::move(request.url_)); diff --git a/td/telegram/Td.h b/td/telegram/Td.h index 532e1aec6..4986d3be6 100644 --- a/td/telegram/Td.h +++ b/td/telegram/Td.h @@ -500,6 +500,8 @@ class Td final : public NetQueryCallback { void on_request(uint64 id, const td_api::getMessageLink &request); + void on_request(uint64 id, const td_api::getMessageEmbeddingCode &request); + void on_request(uint64 id, td_api::getMessageLinkInfo &request); void on_request(uint64 id, const td_api::getFile &request); diff --git a/td/telegram/cli.cpp b/td/telegram/cli.cpp index 85145d6f1..d38b0fd6e 100644 --- a/td/telegram/cli.cpp +++ b/td/telegram/cli.cpp @@ -2633,6 +2633,14 @@ class CliClient final : public Actor { std::tie(for_album, for_comment) = split(args); send_request(td_api::make_object(as_chat_id(chat_id), as_message_id(message_id), as_bool(for_album), as_bool(for_comment))); + } else if (op == "gmec") { + string chat_id; + string message_id; + string for_album; + std::tie(chat_id, args) = split(args); + std::tie(message_id, for_album) = split(args); + send_request(td_api::make_object(as_chat_id(chat_id), as_message_id(message_id), + as_bool(for_album))); } else if (op == "gmli") { send_request(td_api::make_object(args)); } else if (op == "gcmbd") {