Add DialogInviteLink class.

This commit is contained in:
levlam 2021-01-13 16:28:24 +03:00
parent 87a2423f5a
commit b5cd03365d
6 changed files with 208 additions and 36 deletions

View File

@ -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

View File

@ -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<td_api::chatInviteLink>(
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<td_api::chatInviteLink>(
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<EditChatInviteLinkQuery>(std::move(promise))
->send(dialog_id, invite_link, expire_date, usage_limit, is_revoked);

View File

@ -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"

View File

@ -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;

View File

@ -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<telegram_api::chatInviteExported> 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<td_api::chatInviteLink> 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<td_api::chatInviteLink>(
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

View File

@ -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<telegram_api::chatInviteExported> exported_invite);
td_api::object_ptr<td_api::chatInviteLink> 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 <class StorerT>
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 <class ParserT>
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