From 5832a59ee5159826184a9b2a8c44311537e74302 Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 27 May 2021 20:47:04 +0300 Subject: [PATCH] Move get_message_link_info to LinkManager. --- CMakeLists.txt | 1 + td/telegram/LinkManager.cpp | 141 +++++++++++++++++++++++++++++++- td/telegram/LinkManager.h | 3 + td/telegram/MessageLinkInfo.h | 30 +++++++ td/telegram/MessagesManager.cpp | 141 +------------------------------- td/telegram/MessagesManager.h | 15 +--- td/telegram/Td.cpp | 9 +- 7 files changed, 181 insertions(+), 159 deletions(-) create mode 100644 td/telegram/MessageLinkInfo.h diff --git a/CMakeLists.txt b/CMakeLists.txt index dccd12c66..71925fff6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -524,6 +524,7 @@ set(TDLIB_SOURCE td/telegram/MessageCopyOptions.h td/telegram/MessageEntity.h td/telegram/MessageId.h + td/telegram/MessageLinkInfo.h td/telegram/MessageReplyInfo.h td/telegram/MessagesDb.h td/telegram/MessageSearchFilter.h diff --git a/td/telegram/LinkManager.cpp b/td/telegram/LinkManager.cpp index 12e6788af..2e86d4c53 100644 --- a/td/telegram/LinkManager.cpp +++ b/td/telegram/LinkManager.cpp @@ -592,7 +592,7 @@ void LinkManager::get_link_login_url(const string &url, bool allow_write_access, } string LinkManager::get_dialog_invite_link_hash(Slice invite_link) { - auto link_info = LinkManager::get_link_info(invite_link); + auto link_info = get_link_info(invite_link); if (!link_info.is_internal_) { return string(); } @@ -617,4 +617,143 @@ string LinkManager::get_dialog_invite_link_hash(Slice invite_link) { return string(); } +Result LinkManager::get_message_link_info(Slice url) { + if (url.empty()) { + return Status::Error("URL must be non-empty"); + } + auto link_info = get_link_info(url); + if (!link_info.is_internal_) { + return Status::Error("Invalid message link URL"); + } + url = link_info.query_; + + Slice username; + Slice channel_id_slice; + Slice message_id_slice; + Slice comment_message_id_slice = "0"; + bool is_single = false; + bool for_comment = false; + if (link_info.is_tg_) { + // resolve?domain=username&post=12345&single + // privatepost?channel=123456789&msg_id=12345 + + bool is_resolve = false; + if (begins_with(url, "resolve")) { + url = url.substr(7); + is_resolve = true; + } else if (begins_with(url, "privatepost")) { + url = url.substr(11); + } else { + return Status::Error("Wrong message link URL"); + } + + if (begins_with(url, "/")) { + url = url.substr(1); + } + if (!begins_with(url, "?")) { + return Status::Error("Wrong message link URL"); + } + url = url.substr(1); + + auto args = full_split(url, '&'); + for (auto arg : args) { + auto key_value = split(arg, '='); + if (is_resolve) { + if (key_value.first == "domain") { + username = key_value.second; + } + if (key_value.first == "post") { + message_id_slice = key_value.second; + } + } else { + if (key_value.first == "channel") { + channel_id_slice = key_value.second; + } + if (key_value.first == "msg_id") { + message_id_slice = key_value.second; + } + } + if (key_value.first == "single") { + is_single = true; + } + if (key_value.first == "comment") { + comment_message_id_slice = key_value.second; + } + if (key_value.first == "thread") { + for_comment = true; + } + } + } else { + // /c/123456789/12345 + // /username/12345?single + + CHECK(!url.empty() && url[0] == '/'); + url.remove_prefix(1); + + auto username_end_pos = url.find('/'); + if (username_end_pos == Slice::npos) { + return Status::Error("Wrong message link URL"); + } + username = url.substr(0, username_end_pos); + url = url.substr(username_end_pos + 1); + if (username == "c") { + username = Slice(); + auto channel_id_end_pos = url.find('/'); + if (channel_id_end_pos == Slice::npos) { + return Status::Error("Wrong message link URL"); + } + channel_id_slice = url.substr(0, channel_id_end_pos); + url = url.substr(channel_id_end_pos + 1); + } + + auto query_pos = url.find('?'); + message_id_slice = url.substr(0, query_pos); + if (query_pos != Slice::npos) { + auto args = full_split(url.substr(query_pos + 1), '&'); + for (auto arg : args) { + auto key_value = split(arg, '='); + if (key_value.first == "single") { + is_single = true; + } + if (key_value.first == "comment") { + comment_message_id_slice = key_value.second; + } + if (key_value.first == "thread") { + for_comment = true; + } + } + } + } + + ChannelId channel_id; + if (username.empty()) { + auto r_channel_id = to_integer_safe(channel_id_slice); + if (r_channel_id.is_error() || !ChannelId(r_channel_id.ok()).is_valid()) { + return Status::Error("Wrong channel ID"); + } + channel_id = ChannelId(r_channel_id.ok()); + } + + auto r_message_id = to_integer_safe(message_id_slice); + if (r_message_id.is_error() || !ServerMessageId(r_message_id.ok()).is_valid()) { + return Status::Error("Wrong message ID"); + } + + auto r_comment_message_id = to_integer_safe(comment_message_id_slice); + if (r_comment_message_id.is_error() || + !(r_comment_message_id.ok() == 0 || ServerMessageId(r_comment_message_id.ok()).is_valid())) { + return Status::Error("Wrong comment message ID"); + } + + MessageLinkInfo info; + info.username = username.str(); + info.channel_id = channel_id; + info.message_id = MessageId(ServerMessageId(r_message_id.ok())); + info.comment_message_id = MessageId(ServerMessageId(r_comment_message_id.ok())); + info.is_single = is_single; + info.for_comment = for_comment; + LOG(INFO) << "Have link to " << info.message_id << " in chat @" << info.username << "/" << channel_id.get(); + return std::move(info); +} + } // namespace td diff --git a/td/telegram/LinkManager.h b/td/telegram/LinkManager.h index 9e9d1b149..39bb25803 100644 --- a/td/telegram/LinkManager.h +++ b/td/telegram/LinkManager.h @@ -8,6 +8,7 @@ #include "td/telegram/DialogId.h" #include "td/telegram/MessageId.h" +#include "td/telegram/MessageLinkInfo.h" #include "td/telegram/td_api.h" #include "td/actor/actor.h" @@ -84,6 +85,8 @@ class LinkManager : public Actor { static string get_dialog_invite_link_hash(Slice invite_link); + static Result get_message_link_info(Slice url); + private: void tear_down() override; diff --git a/td/telegram/MessageLinkInfo.h b/td/telegram/MessageLinkInfo.h new file mode 100644 index 000000000..a580ebe1b --- /dev/null +++ b/td/telegram/MessageLinkInfo.h @@ -0,0 +1,30 @@ +// +// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021 +// +// 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/ChannelId.h" +#include "td/telegram/DialogId.h" +#include "td/telegram/MessageId.h" + +#include "td/utils/common.h" + +namespace td { + +struct MessageLinkInfo { + string username; + // or + ChannelId channel_id; + + MessageId message_id; + bool is_single = false; + + DialogId comment_dialog_id; + MessageId comment_message_id; + bool for_comment = false; +}; + +} // namespace td \ No newline at end of file diff --git a/td/telegram/MessagesManager.cpp b/td/telegram/MessagesManager.cpp index 59d398679..81ae75cdd 100644 --- a/td/telegram/MessagesManager.cpp +++ b/td/telegram/MessagesManager.cpp @@ -17375,147 +17375,8 @@ void MessagesManager::on_get_public_message_link(FullMessageId full_message_id, std::move(url), std::move(html)}; } -Result MessagesManager::get_message_link_info(Slice url) { - if (url.empty()) { - return Status::Error("URL must be non-empty"); - } - auto link_info = LinkManager::get_link_info(url); - if (!link_info.is_internal_) { - return Status::Error("Invalid message link URL"); - } - url = link_info.query_; - - Slice username; - Slice channel_id_slice; - Slice message_id_slice; - Slice comment_message_id_slice = "0"; - bool is_single = false; - bool for_comment = false; - if (link_info.is_tg_) { - // resolve?domain=username&post=12345&single - // privatepost?channel=123456789&msg_id=12345 - - bool is_resolve = false; - if (begins_with(url, "resolve")) { - url = url.substr(7); - is_resolve = true; - } else if (begins_with(url, "privatepost")) { - url = url.substr(11); - } else { - return Status::Error("Wrong message link URL"); - } - - if (begins_with(url, "/")) { - url = url.substr(1); - } - if (!begins_with(url, "?")) { - return Status::Error("Wrong message link URL"); - } - url = url.substr(1); - - auto args = full_split(url, '&'); - for (auto arg : args) { - auto key_value = split(arg, '='); - if (is_resolve) { - if (key_value.first == "domain") { - username = key_value.second; - } - if (key_value.first == "post") { - message_id_slice = key_value.second; - } - } else { - if (key_value.first == "channel") { - channel_id_slice = key_value.second; - } - if (key_value.first == "msg_id") { - message_id_slice = key_value.second; - } - } - if (key_value.first == "single") { - is_single = true; - } - if (key_value.first == "comment") { - comment_message_id_slice = key_value.second; - } - if (key_value.first == "thread") { - for_comment = true; - } - } - } else { - // /c/123456789/12345 - // /username/12345?single - - CHECK(!url.empty() && url[0] == '/'); - url.remove_prefix(1); - - auto username_end_pos = url.find('/'); - if (username_end_pos == Slice::npos) { - return Status::Error("Wrong message link URL"); - } - username = url.substr(0, username_end_pos); - url = url.substr(username_end_pos + 1); - if (username == "c") { - username = Slice(); - auto channel_id_end_pos = url.find('/'); - if (channel_id_end_pos == Slice::npos) { - return Status::Error("Wrong message link URL"); - } - channel_id_slice = url.substr(0, channel_id_end_pos); - url = url.substr(channel_id_end_pos + 1); - } - - auto query_pos = url.find('?'); - message_id_slice = url.substr(0, query_pos); - if (query_pos != Slice::npos) { - auto args = full_split(url.substr(query_pos + 1), '&'); - for (auto arg : args) { - auto key_value = split(arg, '='); - if (key_value.first == "single") { - is_single = true; - } - if (key_value.first == "comment") { - comment_message_id_slice = key_value.second; - } - if (key_value.first == "thread") { - for_comment = true; - } - } - } - } - - ChannelId channel_id; - if (username.empty()) { - auto r_channel_id = to_integer_safe(channel_id_slice); - if (r_channel_id.is_error() || !ChannelId(r_channel_id.ok()).is_valid()) { - return Status::Error("Wrong channel ID"); - } - channel_id = ChannelId(r_channel_id.ok()); - } - - auto r_message_id = to_integer_safe(message_id_slice); - if (r_message_id.is_error() || !ServerMessageId(r_message_id.ok()).is_valid()) { - return Status::Error("Wrong message ID"); - } - - auto r_comment_message_id = to_integer_safe(comment_message_id_slice); - if (r_comment_message_id.is_error() || - !(r_comment_message_id.ok() == 0 || ServerMessageId(r_comment_message_id.ok()).is_valid())) { - return Status::Error("Wrong comment message ID"); - } - - MessageLinkInfo info; - info.username = username.str(); - info.channel_id = channel_id; - info.message_id = MessageId(ServerMessageId(r_message_id.ok())); - info.comment_message_id = MessageId(ServerMessageId(r_comment_message_id.ok())); - info.is_single = is_single; - info.for_comment = for_comment; - LOG(INFO) << "Have link to " << info.message_id << " in chat @" << info.username << "/" << channel_id.get(); - return std::move(info); -} - void MessagesManager::get_message_link_info(Slice url, Promise &&promise) { - auto r_message_link_info = get_message_link_info(url); + auto r_message_link_info = LinkManager::get_message_link_info(url); if (r_message_link_info.is_error()) { return promise.set_error(Status::Error(400, r_message_link_info.error().message())); } diff --git a/td/telegram/MessagesManager.h b/td/telegram/MessagesManager.h index 7b229ecb8..1ff16af00 100644 --- a/td/telegram/MessagesManager.h +++ b/td/telegram/MessagesManager.h @@ -28,6 +28,7 @@ #include "td/telegram/MessageContentType.h" #include "td/telegram/MessageCopyOptions.h" #include "td/telegram/MessageId.h" +#include "td/telegram/MessageLinkInfo.h" #include "td/telegram/MessageReplyInfo.h" #include "td/telegram/MessagesDb.h" #include "td/telegram/MessageSearchFilter.h" @@ -579,18 +580,6 @@ class MessagesManager : public Actor { void on_get_public_message_link(FullMessageId full_message_id, bool for_group, string url, string html); - struct MessageLinkInfo { - string username; - // or - ChannelId channel_id; - - MessageId message_id; - bool is_single = false; - - DialogId comment_dialog_id; - MessageId comment_message_id; - bool for_comment = false; - }; void get_message_link_info(Slice url, Promise &&promise); td_api::object_ptr get_message_link_info_object(const MessageLinkInfo &info) const; @@ -2621,8 +2610,6 @@ class MessagesManager : public Actor { void ttl_db_loop(double server_now); void ttl_db_on_result(Result>, int32>> r_result, bool dummy); - static Result get_message_link_info(Slice url); - void on_get_message_link_dialog(MessageLinkInfo &&info, Promise &&promise); void on_get_message_link_message(MessageLinkInfo &&info, DialogId dialog_id, Promise &&promise); diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 794261bed..a6aa47d4c 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -55,6 +55,7 @@ #include "td/telegram/MessageCopyOptions.h" #include "td/telegram/MessageEntity.h" #include "td/telegram/MessageId.h" +#include "td/telegram/MessageLinkInfo.h" #include "td/telegram/MessageSearchFilter.h" #include "td/telegram/MessagesManager.h" #include "td/telegram/misc.h" @@ -1174,12 +1175,12 @@ class GetMessageEmbeddingCodeRequest : public RequestActor<> { } }; -class GetMessageLinkInfoRequest : public RequestActor { +class GetMessageLinkInfoRequest : public RequestActor { string url_; - MessagesManager::MessageLinkInfo message_link_info_; + MessageLinkInfo message_link_info_; - void do_run(Promise &&promise) override { + void do_run(Promise &&promise) override { if (get_tries() < 2) { promise.set_value(std::move(message_link_info_)); return; @@ -1187,7 +1188,7 @@ class GetMessageLinkInfoRequest : public RequestActormessages_manager_->get_message_link_info(url_, std::move(promise)); } - void do_set_result(MessagesManager::MessageLinkInfo &&result) override { + void do_set_result(MessageLinkInfo &&result) override { message_link_info_ = std::move(result); }