Move invite link methods to DialogInviteLink.

This commit is contained in:
levlam 2021-01-13 18:30:58 +03:00
parent 1816a6c7a5
commit 4761d7385c
4 changed files with 44 additions and 41 deletions

View File

@ -1655,7 +1655,7 @@ class CheckDialogInviteLinkQuery : public Td::ResultHandler {
void send(const string &invite_link) { void send(const string &invite_link) {
invite_link_ = invite_link; invite_link_ = invite_link;
send_query(G()->net_query_creator().create( send_query(G()->net_query_creator().create(
telegram_api::messages_checkChatInvite(ContactsManager::get_dialog_invite_link_hash(invite_link_).str()))); telegram_api::messages_checkChatInvite(DialogInviteLink::get_dialog_invite_link_hash(invite_link_).str())));
} }
void on_result(uint64 id, BufferSlice packet) override { void on_result(uint64 id, BufferSlice packet) override {
@ -1687,7 +1687,7 @@ class ImportDialogInviteLinkQuery : public Td::ResultHandler {
void send(const string &invite_link) { void send(const string &invite_link) {
invite_link_ = invite_link; invite_link_ = invite_link;
send_query(G()->net_query_creator().create( send_query(G()->net_query_creator().create(
telegram_api::messages_importChatInvite(ContactsManager::get_dialog_invite_link_hash(invite_link).str()))); telegram_api::messages_importChatInvite(DialogInviteLink::get_dialog_invite_link_hash(invite_link).str())));
} }
void on_result(uint64 id, BufferSlice packet) override { void on_result(uint64 id, BufferSlice packet) override {
@ -2906,9 +2906,6 @@ class ContactsManager::UploadProfilePhotoCallback : public FileManager::UploadCa
} }
}; };
const CSlice ContactsManager::INVITE_LINK_URLS[3] = {"t.me/joinchat/", "telegram.me/joinchat/",
"telegram.dog/joinchat/"};
ContactsManager::ContactsManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) { ContactsManager::ContactsManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
upload_profile_photo_callback_ = std::make_shared<UploadProfilePhotoCallback>(); upload_profile_photo_callback_ = std::make_shared<UploadProfilePhotoCallback>();
@ -6606,7 +6603,7 @@ void ContactsManager::edit_dialog_invite_link(DialogId dialog_id, const string &
TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id)); TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id));
// if (!is_valid_invite_link(invite_link)) { // if (!DialogInviteLink::is_valid_invite_link(invite_link)) {
// return promise.set_error(Status::Error(400, "Wrong invite link")); // return promise.set_error(Status::Error(400, "Wrong invite link"));
// } // }
@ -6619,7 +6616,7 @@ void ContactsManager::check_dialog_invite_link(const string &invite_link, Promis
return promise.set_value(Unit()); return promise.set_value(Unit());
} }
if (!is_valid_invite_link(invite_link)) { if (!DialogInviteLink::is_valid_invite_link(invite_link)) {
return promise.set_error(Status::Error(400, "Wrong invite link")); return promise.set_error(Status::Error(400, "Wrong invite link"));
} }
@ -6627,7 +6624,7 @@ void ContactsManager::check_dialog_invite_link(const string &invite_link, Promis
} }
void ContactsManager::import_dialog_invite_link(const string &invite_link, Promise<DialogId> &&promise) { void ContactsManager::import_dialog_invite_link(const string &invite_link, Promise<DialogId> &&promise) {
if (!is_valid_invite_link(invite_link)) { if (!DialogInviteLink::is_valid_invite_link(invite_link)) {
return promise.set_error(Status::Error(400, "Wrong invite link")); return promise.set_error(Status::Error(400, "Wrong invite link"));
} }
@ -11485,32 +11482,6 @@ void ContactsManager::remove_dialog_access_by_invite_link(DialogId dialog_id) {
invite_link_info_expire_timeout_.cancel_timeout(dialog_id.get()); invite_link_info_expire_timeout_.cancel_timeout(dialog_id.get());
} }
bool ContactsManager::is_valid_invite_link(const string &invite_link) {
return !get_dialog_invite_link_hash(invite_link).empty();
}
Slice ContactsManager::get_dialog_invite_link_hash(const string &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 = Slice(invite_link).substr(url.size() + offset);
hash.truncate(hash.find('#'));
hash.truncate(hash.find('?'));
return hash;
}
}
return Slice();
}
bool ContactsManager::update_invite_link(string &invite_link, bool ContactsManager::update_invite_link(string &invite_link,
tl_object_ptr<telegram_api::chatInviteExported> &&exported_chat_invite) { tl_object_ptr<telegram_api::chatInviteExported> &&exported_chat_invite) {
string new_invite_link; string new_invite_link;
@ -11522,7 +11493,7 @@ bool ContactsManager::update_invite_link(string &invite_link,
if (!invite_link.empty()) { if (!invite_link.empty()) {
invite_link_infos_.erase(invite_link); invite_link_infos_.erase(invite_link);
} }
LOG_IF(ERROR, !new_invite_link.empty() && !is_valid_invite_link(new_invite_link)) LOG_IF(ERROR, !new_invite_link.empty() && !DialogInviteLink::is_valid_invite_link(new_invite_link))
<< "Unsupported invite link " << new_invite_link; << "Unsupported invite link " << new_invite_link;
invite_link = std::move(new_invite_link); invite_link = std::move(new_invite_link);

View File

@ -221,8 +221,6 @@ class ContactsManager : public Actor {
bool on_get_channel_error(ChannelId channel_id, const Status &status, const string &source); bool on_get_channel_error(ChannelId channel_id, const Status &status, const string &source);
static Slice get_dialog_invite_link_hash(const string &invite_link);
void on_get_dialog_invite_link_info(const string &invite_link, void on_get_dialog_invite_link_info(const string &invite_link,
tl_object_ptr<telegram_api::ChatInvite> &&chat_invite_ptr, tl_object_ptr<telegram_api::ChatInvite> &&chat_invite_ptr,
Promise<Unit> &&promise); Promise<Unit> &&promise);
@ -1044,8 +1042,6 @@ class ContactsManager : public Actor {
static constexpr int32 ACCOUNT_UPDATE_LAST_NAME = 1 << 1; static constexpr int32 ACCOUNT_UPDATE_LAST_NAME = 1 << 1;
static constexpr int32 ACCOUNT_UPDATE_ABOUT = 1 << 2; static constexpr int32 ACCOUNT_UPDATE_ABOUT = 1 << 2;
static const CSlice INVITE_LINK_URLS[3];
static bool have_input_peer_user(const User *u, AccessRights access_rights); static bool have_input_peer_user(const User *u, AccessRights access_rights);
static bool have_input_peer_chat(const Chat *c, AccessRights access_rights); static bool have_input_peer_chat(const Chat *c, AccessRights access_rights);
bool have_input_peer_channel(const Channel *c, ChannelId channel_id, AccessRights access_rights, bool have_input_peer_channel(const Channel *c, ChannelId channel_id, AccessRights access_rights,
@ -1352,8 +1348,6 @@ class ContactsManager : public Actor {
Status can_manage_dialog_invite_links(DialogId dialog_id); Status can_manage_dialog_invite_links(DialogId dialog_id);
static bool is_valid_invite_link(const string &invite_link);
bool update_invite_link(string &invite_link, tl_object_ptr<telegram_api::chatInviteExported> &&exported_chat_invite); bool update_invite_link(string &invite_link, tl_object_ptr<telegram_api::chatInviteExported> &&exported_chat_invite);
const DialogParticipant *get_chat_participant(ChatId chat_id, UserId user_id) const; const DialogParticipant *get_chat_participant(ChatId chat_id, UserId user_id) const;

View File

@ -9,8 +9,13 @@
#include "td/telegram/ContactsManager.h" #include "td/telegram/ContactsManager.h"
#include "td/telegram/Global.h" #include "td/telegram/Global.h"
#include "td/utils/misc.h"
namespace td { namespace td {
const CSlice DialogInviteLink::INVITE_LINK_URLS[3] = {"t.me/joinchat/", "telegram.me/joinchat/",
"telegram.dog/joinchat/"};
DialogInviteLink::DialogInviteLink(tl_object_ptr<telegram_api::chatInviteExported> exported_invite) { DialogInviteLink::DialogInviteLink(tl_object_ptr<telegram_api::chatInviteExported> exported_invite) {
if (exported_invite == nullptr) { if (exported_invite == nullptr) {
return; return;
@ -58,6 +63,32 @@ 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();
}
bool DialogInviteLink::is_expired() const { bool DialogInviteLink::is_expired() const {
return (expire_date_ != 0 && G()->unix_time() >= expire_date_) || (usage_limit_ != 0 && usage_count_ >= usage_limit_); return (expire_date_ != 0 && G()->unix_time() >= expire_date_) || (usage_limit_ != 0 && usage_count_ >= usage_limit_);
} }

View File

@ -11,6 +11,7 @@
#include "td/telegram/UserId.h" #include "td/telegram/UserId.h"
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/Slice.h"
#include "td/utils/StringBuilder.h" #include "td/utils/StringBuilder.h"
#include "td/utils/tl_helpers.h" #include "td/utils/tl_helpers.h"
@ -32,11 +33,17 @@ class DialogInviteLink {
friend StringBuilder &operator<<(StringBuilder &string_builder, const DialogInviteLink &invite_link); friend StringBuilder &operator<<(StringBuilder &string_builder, const DialogInviteLink &invite_link);
static const CSlice INVITE_LINK_URLS[3];
public: public:
DialogInviteLink() = default; DialogInviteLink() = default;
DialogInviteLink(tl_object_ptr<telegram_api::chatInviteExported> exported_invite); DialogInviteLink(tl_object_ptr<telegram_api::chatInviteExported> exported_invite);
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; td_api::object_ptr<td_api::chatInviteLink> get_chat_invite_link_object(const ContactsManager *contacts_manager) const;
bool is_valid() const { bool is_valid() const {