From b5cd03365d1eb4a83546b3a9580c73d4b7e59bd8 Mon Sep 17 00:00:00 2001 From: levlam Date: Wed, 13 Jan 2021 16:28:24 +0300 Subject: [PATCH] Add DialogInviteLink class. --- CMakeLists.txt | 2 + td/telegram/ContactsManager.cpp | 42 ++--------- td/telegram/ContactsManager.h | 1 + td/telegram/DialogAdministrator.h | 2 +- td/telegram/DialogInviteLink.cpp | 85 +++++++++++++++++++++++ td/telegram/DialogInviteLink.h | 112 ++++++++++++++++++++++++++++++ 6 files changed, 208 insertions(+), 36 deletions(-) create mode 100644 td/telegram/DialogInviteLink.cpp create mode 100644 td/telegram/DialogInviteLink.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bb81b3b88..7faa9a005 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -291,6 +291,7 @@ set(TDLIB_SOURCE td/telegram/DialogDb.cpp td/telegram/DialogFilter.cpp td/telegram/DialogId.cpp + td/telegram/DialogInviteLink.cpp td/telegram/DialogLocation.cpp td/telegram/DialogParticipant.cpp td/telegram/DialogSource.cpp @@ -456,6 +457,7 @@ set(TDLIB_SOURCE td/telegram/DialogFilter.h td/telegram/DialogFilterId.h td/telegram/DialogId.h + td/telegram/DialogInviteLink.h td/telegram/DialogListId.h td/telegram/DialogLocation.h td/telegram/DialogParticipant.h diff --git a/td/telegram/ContactsManager.cpp b/td/telegram/ContactsManager.cpp index 2db8b22bf..00165411e 100644 --- a/td/telegram/ContactsManager.cpp +++ b/td/telegram/ContactsManager.cpp @@ -1571,21 +1571,8 @@ class ExportChatInviteLinkQuery : public Td::ResultHandler { auto ptr = result_ptr.move_as_ok(); LOG(INFO) << "Receive result for ExportChatInviteQuery: " << to_string(ptr); - int32 expire_date = 0; - if ((ptr->flags_ & telegram_api::chatInviteExported::EXPIRE_DATE_MASK) != 0) { - expire_date = ptr->expire_date_; - } - int32 usage_limit = 0; - if ((ptr->flags_ & telegram_api::chatInviteExported::USAGE_LIMIT_MASK) != 0) { - usage_limit = ptr->usage_limit_; - } - int32 usage_count = 0; - if ((ptr->flags_ & telegram_api::chatInviteExported::USAGE_MASK) != 0) { - usage_count = ptr->usage_; - } - auto invite_link = td_api::make_object( - ptr->link_, ptr->admin_id_, ptr->date_, expire_date, usage_limit, usage_count, ptr->expired_, ptr->revoked_); - promise_.set_value(std::move(invite_link)); + DialogInviteLink invite_link(std::move(ptr)); + promise_.set_value(invite_link.get_chat_invite_link_object(td->contacts_manager_.get())); } void on_error(uint64 id, Status status) override { @@ -1635,23 +1622,8 @@ class EditChatInviteLinkQuery : public Td::ResultHandler { td->contacts_manager_->on_get_users(std::move(result->users_), "EditChatInviteLinkQuery"); - auto recent_importers = std::move(result->recent_importers_); - auto ptr = std::move(result->invite_); - int32 expire_date = 0; - if ((ptr->flags_ & telegram_api::chatInviteExported::EXPIRE_DATE_MASK) != 0) { - expire_date = ptr->expire_date_; - } - int32 usage_limit = 0; - if ((ptr->flags_ & telegram_api::chatInviteExported::USAGE_LIMIT_MASK) != 0) { - usage_limit = ptr->usage_limit_; - } - int32 usage_count = 0; - if ((ptr->flags_ & telegram_api::chatInviteExported::USAGE_MASK) != 0) { - usage_count = ptr->usage_; - } - auto invite_link = td_api::make_object( - ptr->link_, ptr->admin_id_, ptr->date_, expire_date, usage_limit, usage_count, ptr->expired_, ptr->revoked_); - promise_.set_value(std::move(invite_link)); + DialogInviteLink invite_link(std::move(result->invite_)); + promise_.set_value(invite_link.get_chat_invite_link_object(td->contacts_manager_.get())); } void on_error(uint64 id, Status status) override { @@ -6602,9 +6574,9 @@ void ContactsManager::edit_dialog_invite_link(DialogId dialog_id, const string & TRY_STATUS_PROMISE(promise, can_manage_dialog_invite_links(dialog_id)); - if (!is_valid_invite_link(invite_link)) { - return promise.set_error(Status::Error(400, "Wrong invite link")); - } + // if (!is_valid_invite_link(invite_link)) { + // return promise.set_error(Status::Error(400, "Wrong invite link")); + // } td_->create_handler(std::move(promise)) ->send(dialog_id, invite_link, expire_date, usage_limit, is_revoked); diff --git a/td/telegram/ContactsManager.h b/td/telegram/ContactsManager.h index 15bd1eb8d..5dea92fe0 100644 --- a/td/telegram/ContactsManager.h +++ b/td/telegram/ContactsManager.h @@ -15,6 +15,7 @@ #include "td/telegram/Contact.h" #include "td/telegram/DialogAdministrator.h" #include "td/telegram/DialogId.h" +#include "td/telegram/DialogInviteLink.h" #include "td/telegram/DialogLocation.h" #include "td/telegram/DialogParticipant.h" #include "td/telegram/files/FileId.h" diff --git a/td/telegram/DialogAdministrator.h b/td/telegram/DialogAdministrator.h index 4a6a51e77..a46eeb116 100644 --- a/td/telegram/DialogAdministrator.h +++ b/td/telegram/DialogAdministrator.h @@ -22,7 +22,7 @@ class DialogAdministrator { string rank_; bool is_creator_ = false; - friend StringBuilder &operator<<(StringBuilder &string_builder, const DialogAdministrator &location); + friend StringBuilder &operator<<(StringBuilder &string_builder, const DialogAdministrator &administrator); public: DialogAdministrator() = default; diff --git a/td/telegram/DialogInviteLink.cpp b/td/telegram/DialogInviteLink.cpp new file mode 100644 index 000000000..e3441bd55 --- /dev/null +++ b/td/telegram/DialogInviteLink.cpp @@ -0,0 +1,85 @@ +// +// 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) +// +#include "td/telegram/DialogInviteLink.h" + +#include "td/telegram/ContactsManager.h" +#include "td/telegram/Global.h" + +namespace td { + +DialogInviteLink::DialogInviteLink(tl_object_ptr exported_invite) { + if (exported_invite == nullptr) { + return; + } + + invite_link_ = std::move(exported_invite->link_); + administrator_user_id_ = UserId(exported_invite->admin_id_); + if (!administrator_user_id_.is_valid()) { + LOG(ERROR) << "Receive invalid " << administrator_user_id_ << " as creator of a link " << invite_link_; + administrator_user_id_ = UserId(); + } + date_ = exported_invite->date_; + if (date_ < 1000000000) { + LOG(ERROR) << "Receive wrong date " << date_ << " as creation date of a link " << invite_link_; + date_ = 0; + } + if ((exported_invite->flags_ & telegram_api::chatInviteExported::EXPIRE_DATE_MASK) != 0) { + expire_date_ = exported_invite->expire_date_; + } + if ((exported_invite->flags_ & telegram_api::chatInviteExported::USAGE_LIMIT_MASK) != 0) { + usage_limit_ = exported_invite->usage_limit_; + } + if ((exported_invite->flags_ & telegram_api::chatInviteExported::USAGE_MASK) != 0) { + usage_count_ = exported_invite->usage_; + } +} + +bool DialogInviteLink::is_expired() const { + return (expire_date_ != 0 && G()->unix_time() >= expire_date_) || (usage_limit_ != 0 && usage_count_ >= usage_limit_); +} + +int32 DialogInviteLink::get_expire_time() const { + if (expire_date_ == 0) { + return 0; + } + if (usage_limit_ != 0 && usage_count_ >= usage_limit_) { + // already expired + return 0; + } + return td::max(expire_date_ - G()->unix_time(), 0); +} + +td_api::object_ptr DialogInviteLink::get_chat_invite_link_object( + const ContactsManager *contacts_manager) const { + CHECK(contacts_manager != nullptr); + if (!is_valid()) { + return nullptr; + } + + return td_api::make_object( + invite_link_, contacts_manager->get_user_id_object(administrator_user_id_, "get_chat_invite_link_object"), date_, + expire_date_, usage_limit_, usage_count_, is_expired(), is_revoked_); +} + +bool operator==(const DialogInviteLink &lhs, const DialogInviteLink &rhs) { + return lhs.invite_link_ == rhs.invite_link_ && lhs.administrator_user_id_ == rhs.administrator_user_id_ && + lhs.date_ == rhs.date_ && lhs.expire_date_ == rhs.expire_date_ && lhs.usage_limit_ == rhs.usage_limit_ && + lhs.usage_count_ == rhs.usage_count_ && lhs.is_revoked_ == rhs.is_revoked_; +} + +bool operator!=(const DialogInviteLink &lhs, const DialogInviteLink &rhs) { + return !(lhs == rhs); +} + +StringBuilder &operator<<(StringBuilder &string_builder, const DialogInviteLink &invite_link) { + return string_builder << "ChatInviteLink[" << invite_link.invite_link_ << " by " << invite_link.administrator_user_id_ + << " created at " << invite_link.date_ << " expiring at " << invite_link.expire_date_ + << " used by " << invite_link.usage_count_ << " with usage limit " << invite_link.usage_limit_ + << "]"; +} + +} // namespace td diff --git a/td/telegram/DialogInviteLink.h b/td/telegram/DialogInviteLink.h new file mode 100644 index 000000000..ffa96d842 --- /dev/null +++ b/td/telegram/DialogInviteLink.h @@ -0,0 +1,112 @@ +// +// 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/td_api.h" +#include "td/telegram/telegram_api.h" +#include "td/telegram/UserId.h" + +#include "td/utils/common.h" +#include "td/utils/StringBuilder.h" +#include "td/utils/tl_helpers.h" + +namespace td { + +class ContactsManager; + +class DialogInviteLink { + string invite_link_; + UserId administrator_user_id_; + int32 date_ = 0; + int32 expire_date_ = 0; + int32 usage_limit_ = 0; + int32 usage_count_ = 0; + bool is_revoked_ = false; + + friend bool operator==(const DialogInviteLink &lhs, const DialogInviteLink &rhs); + + friend StringBuilder &operator<<(StringBuilder &string_builder, const DialogInviteLink &invite_link); + + public: + DialogInviteLink() = default; + + DialogInviteLink(tl_object_ptr exported_invite); + + td_api::object_ptr get_chat_invite_link_object(const ContactsManager *contacts_manager) const; + + bool is_valid() const { + return !invite_link_.empty() && administrator_user_id_.is_valid() && date_ > 0; + } + + bool is_expired() const; + + int32 get_expire_time() const; + + UserId get_administrator_user_id() const { + return administrator_user_id_; + } + + template + void store(StorerT &storer) const { + using td::store; + bool has_expire_date = expire_date_ != 0; + bool has_usage_limit = usage_limit_ != 0; + bool has_usage_count = usage_count_ != 0; + BEGIN_STORE_FLAGS(); + STORE_FLAG(is_revoked_); + STORE_FLAG(has_expire_date); + STORE_FLAG(has_usage_limit); + STORE_FLAG(has_usage_count); + END_STORE_FLAGS(); + store(invite_link_, storer); + store(administrator_user_id_, storer); + store(date_, storer); + if (has_expire_date) { + store(expire_date_, storer); + } + if (has_usage_limit) { + store(usage_limit_, storer); + } + if (has_usage_count) { + store(usage_count_, storer); + } + } + + template + void parse(ParserT &parser) { + using td::parse; + bool has_expire_date; + bool has_usage_limit; + bool has_usage_count; + BEGIN_PARSE_FLAGS(); + PARSE_FLAG(is_revoked_); + PARSE_FLAG(has_expire_date); + PARSE_FLAG(has_usage_limit); + PARSE_FLAG(has_usage_count); + END_PARSE_FLAGS(); + parse(invite_link_, parser); + parse(administrator_user_id_, parser); + parse(date_, parser); + if (has_expire_date) { + parse(expire_date_, parser); + } + if (has_usage_limit) { + parse(usage_limit_, parser); + } + if (has_usage_count) { + parse(usage_count_, parser); + } + } +}; + +bool operator==(const DialogInviteLink &lhs, const DialogInviteLink &rhs); + +bool operator!=(const DialogInviteLink &lhs, const DialogInviteLink &rhs); + +StringBuilder &operator<<(StringBuilder &string_builder, const DialogInviteLink &invite_link); + +} // namespace td