Move get_dialog_invite_link_hash to LinkManager.

This commit is contained in:
levlam 2021-05-27 20:31:06 +03:00
parent ffa1da2cac
commit 2d534e48d6
6 changed files with 38 additions and 35 deletions

View File

@ -25,6 +25,7 @@
#include "td/telegram/GroupCallManager.h"
#include "td/telegram/InlineQueriesManager.h"
#include "td/telegram/InputGroupCallId.h"
#include "td/telegram/LinkManager.h"
#include "td/telegram/logevent/LogEvent.h"
#include "td/telegram/logevent/LogEventHelper.h"
#include "td/telegram/MessagesManager.h"
@ -2139,7 +2140,7 @@ class CheckChatInviteQuery : public Td::ResultHandler {
void send(const string &invite_link) {
invite_link_ = invite_link;
send_query(G()->net_query_creator().create(
telegram_api::messages_checkChatInvite(DialogInviteLink::get_dialog_invite_link_hash(invite_link_).str())));
telegram_api::messages_checkChatInvite(LinkManager::get_dialog_invite_link_hash(invite_link_))));
}
void on_result(uint64 id, BufferSlice packet) override {
@ -2171,7 +2172,7 @@ class ImportChatInviteQuery : public Td::ResultHandler {
void send(const string &invite_link) {
invite_link_ = invite_link;
send_query(G()->net_query_creator().create(
telegram_api::messages_importChatInvite(DialogInviteLink::get_dialog_invite_link_hash(invite_link).str())));
telegram_api::messages_importChatInvite(LinkManager::get_dialog_invite_link_hash(invite_link_))));
}
void on_result(uint64 id, BufferSlice packet) override {

View File

@ -7,18 +7,13 @@
#include "td/telegram/DialogInviteLink.h"
#include "td/telegram/ContactsManager.h"
#include "td/telegram/LinkManager.h"
#include "td/utils/logging.h"
#include "td/utils/misc.h"
namespace td {
const CSlice DialogInviteLink::INVITE_LINK_URLS[12] = {
"t.me/joinchat/", "telegram.me/joinchat/", "telegram.dog/joinchat/",
"t.me/+", "telegram.me/+", "telegram.dog/+",
"t.me/ ", "telegram.me/ ", "telegram.dog/ ",
"t.me/%20", "telegram.me/%20", "telegram.dog/%20"};
DialogInviteLink::DialogInviteLink(tl_object_ptr<telegram_api::chatInviteExported> exported_invite) {
if (exported_invite == nullptr) {
return;
@ -76,29 +71,7 @@ DialogInviteLink::DialogInviteLink(tl_object_ptr<telegram_api::chatInviteExporte
}
bool DialogInviteLink::is_valid_invite_link(Slice invite_link) {
return !get_dialog_invite_link_hash(invite_link).empty();
}
Slice DialogInviteLink::get_dialog_invite_link_hash(Slice invite_link) {
auto lower_cased_invite_link_str = to_lower(invite_link);
Slice lower_cased_invite_link = lower_cased_invite_link_str;
size_t offset = 0;
if (begins_with(lower_cased_invite_link, "https://")) {
offset = 8;
} else if (begins_with(lower_cased_invite_link, "http://")) {
offset = 7;
}
lower_cased_invite_link.remove_prefix(offset);
for (auto &url : INVITE_LINK_URLS) {
if (begins_with(lower_cased_invite_link, url)) {
Slice hash = invite_link.substr(url.size() + offset);
hash.truncate(hash.find('#'));
hash.truncate(hash.find('?'));
return hash;
}
}
return Slice();
return !LinkManager::get_dialog_invite_link_hash(invite_link).empty();
}
td_api::object_ptr<td_api::chatInviteLink> DialogInviteLink::get_chat_invite_link_object(

View File

@ -34,8 +34,6 @@ class DialogInviteLink {
friend StringBuilder &operator<<(StringBuilder &string_builder, const DialogInviteLink &invite_link);
static const CSlice INVITE_LINK_URLS[12];
public:
DialogInviteLink() = default;
@ -43,8 +41,6 @@ class DialogInviteLink {
static bool is_valid_invite_link(Slice invite_link);
static Slice get_dialog_invite_link_hash(Slice invite_link);
td_api::object_ptr<td_api::chatInviteLink> get_chat_invite_link_object(const ContactsManager *contacts_manager) const;
bool is_valid() const {

View File

@ -591,4 +591,30 @@ void LinkManager::get_link_login_url(const string &url, bool allow_write_access,
->send(url, DialogId(), MessageId(), 0, allow_write_access);
}
string LinkManager::get_dialog_invite_link_hash(Slice invite_link) {
auto link_info = LinkManager::get_link_info(invite_link);
if (!link_info.is_internal_) {
return string();
}
const auto url_query = parse_url_query(link_info.query_);
const auto &path = url_query.path_;
if (link_info.is_tg_) {
if (path.size() == 1 && path[0] == "join" && !url_query.get_arg("invite").empty()) {
// join?invite=abcdef
return url_query.get_arg("invite").str();
}
} else {
if (path.size() >= 2 && path[0] == "joinchat" && !path[1].empty()) {
// /joinchat/<link>
return path[1];
}
if (path.size() >= 1 && path[0].size() >= 2 && (path[0][0] == ' ' || path[0][0] == '+')) {
// /+<link>
return path[0].substr(1);
}
}
return string();
}
} // namespace td

View File

@ -82,6 +82,8 @@ class LinkManager : public Actor {
void get_link_login_url(const string &url, bool allow_write_access,
Promise<td_api::object_ptr<td_api::httpUrl>> &&promise);
static string get_dialog_invite_link_hash(Slice invite_link);
private:
void tear_down() override;

View File

@ -17383,6 +17383,7 @@ Result<MessagesManager::MessageLinkInfo> MessagesManager::get_message_link_info(
if (!link_info.is_internal_) {
return Status::Error("Invalid message link URL");
}
url = link_info.query_;
Slice username;
Slice channel_id_slice;
@ -17443,6 +17444,10 @@ Result<MessagesManager::MessageLinkInfo> MessagesManager::get_message_link_info(
} 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");