tdlight/td/telegram/DialogInviteLink.cpp

137 lines
5.9 KiB
C++
Raw Normal View History

2021-01-13 14:28:24 +01:00
//
2022-12-31 22:28:08 +01:00
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
2021-01-13 14:28:24 +01:00
//
// 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/LinkManager.h"
2021-01-13 14:28:24 +01:00
2021-01-27 00:57:59 +01:00
#include "td/utils/logging.h"
2023-03-06 18:36:56 +01:00
#include "td/utils/misc.h"
2022-06-02 15:19:16 +02:00
#include "td/utils/SliceBuilder.h"
2021-01-13 14:28:24 +01:00
namespace td {
DialogInviteLink::DialogInviteLink(tl_object_ptr<telegram_api::ExportedChatInvite> exported_invite_ptr,
bool allow_truncated, const char *source) {
2022-04-19 16:43:24 +02:00
if (exported_invite_ptr == nullptr) {
return;
}
if (exported_invite_ptr->get_id() != telegram_api::chatInviteExported::ID) {
2023-05-22 18:31:58 +02:00
CHECK(exported_invite_ptr->get_id() == telegram_api::chatInvitePublicJoinRequests::ID);
2022-05-25 16:17:14 +02:00
Slice slice(source);
if (slice != "channelAdminLogEventActionParticipantJoinByRequest" && slice != "updateChatParticipant" &&
slice != "updateChannelParticipant" && slice != "updateBotChatInviteRequester") {
LOG(ERROR) << "Receive from " << source << ' ' << to_string(exported_invite_ptr);
}
2021-01-13 14:28:24 +01:00
return;
}
2022-04-19 16:43:24 +02:00
auto exported_invite = move_tl_object_as<telegram_api::chatInviteExported>(exported_invite_ptr);
2021-01-13 14:28:24 +01:00
invite_link_ = std::move(exported_invite->link_);
2021-10-27 12:21:50 +02:00
title_ = std::move(exported_invite->title_);
creator_user_id_ = UserId(exported_invite->admin_id_);
2021-10-27 12:21:50 +02:00
date_ = exported_invite->date_;
expire_date_ = exported_invite->expire_date_;
usage_limit_ = exported_invite->usage_limit_;
usage_count_ = exported_invite->usage_;
edit_date_ = exported_invite->start_date_;
request_count_ = exported_invite->requested_;
2021-11-04 07:52:27 +01:00
creates_join_request_ = exported_invite->request_needed_;
2021-10-27 12:21:50 +02:00
is_revoked_ = exported_invite->revoked_;
is_permanent_ = exported_invite->permanent_;
string full_source = PSTRING() << "invite link " << invite_link_ << " from " << source;
LOG_IF(ERROR, !is_valid_invite_link(invite_link_, allow_truncated)) << "Unsupported " << full_source;
if (!creator_user_id_.is_valid()) {
LOG(ERROR) << "Receive invalid " << creator_user_id_ << " as creator of " << full_source;
creator_user_id_ = UserId();
2021-01-13 14:28:24 +01:00
}
2021-10-27 14:02:50 +02:00
if (date_ != 0 && date_ < 1000000000) {
LOG(ERROR) << "Receive wrong date " << date_ << " as a creation date of " << full_source;
2021-01-13 14:28:24 +01:00
date_ = 0;
}
2021-10-27 14:02:50 +02:00
if (expire_date_ != 0 && expire_date_ < 1000000000) {
LOG(ERROR) << "Receive wrong date " << expire_date_ << " as an expire date of " << full_source;
2021-10-27 12:21:50 +02:00
expire_date_ = 0;
2021-01-13 14:28:24 +01:00
}
2021-10-27 12:21:50 +02:00
if (usage_limit_ < 0) {
LOG(ERROR) << "Receive wrong usage limit " << usage_limit_ << " for " << full_source;
2021-10-27 12:21:50 +02:00
usage_limit_ = 0;
2021-01-13 14:28:24 +01:00
}
2021-10-27 12:21:50 +02:00
if (usage_count_ < 0) {
LOG(ERROR) << "Receive wrong usage count " << usage_count_ << " for " << full_source;
2021-10-27 12:21:50 +02:00
usage_count_ = 0;
2021-01-13 14:28:24 +01:00
}
2021-10-27 14:02:50 +02:00
if (edit_date_ != 0 && edit_date_ < 1000000000) {
LOG(ERROR) << "Receive wrong date " << edit_date_ << " as an edit date of " << full_source;
2021-10-27 12:21:50 +02:00
edit_date_ = 0;
}
2021-10-27 12:21:50 +02:00
if (request_count_ < 0) {
LOG(ERROR) << "Receive wrong pending join request count " << request_count_ << " for " << full_source;
2021-10-27 12:21:50 +02:00
request_count_ = 0;
}
2021-01-13 16:16:33 +01:00
2021-10-27 12:21:50 +02:00
if (is_permanent_ && (!title_.empty() || expire_date_ > 0 || usage_limit_ > 0 || edit_date_ > 0 ||
2021-11-04 07:52:27 +01:00
request_count_ > 0 || creates_join_request_)) {
LOG(ERROR) << "Receive wrong permanent " << full_source << ' ' << *this;
2021-10-27 12:21:50 +02:00
title_.clear();
2021-01-13 16:16:33 +01:00
expire_date_ = 0;
usage_limit_ = 0;
edit_date_ = 0;
request_count_ = 0;
2021-11-04 07:52:27 +01:00
creates_join_request_ = false;
2021-01-13 16:16:33 +01:00
}
2021-11-04 07:52:27 +01:00
if (creates_join_request_ && usage_limit_ > 0) {
LOG(ERROR) << "Receive wrong permanent " << full_source << ' ' << *this;
usage_limit_ = 0;
}
2021-01-13 14:28:24 +01:00
}
bool DialogInviteLink::is_valid_invite_link(Slice invite_link, bool allow_truncated) {
if (allow_truncated && ends_with(invite_link, "...")) {
invite_link.remove_suffix(3);
}
return !LinkManager::get_dialog_invite_link_hash(invite_link).empty();
}
2021-01-13 14:28:24 +01:00
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>(
2021-10-27 12:21:50 +02:00
invite_link_, title_, contacts_manager->get_user_id_object(creator_user_id_, "get_chat_invite_link_object"),
2021-11-04 07:52:27 +01:00
date_, edit_date_, expire_date_, usage_limit_, usage_count_, request_count_, creates_join_request_, is_permanent_,
is_revoked_);
2021-01-13 14:28:24 +01:00
}
bool operator==(const DialogInviteLink &lhs, const DialogInviteLink &rhs) {
2021-10-27 12:21:50 +02:00
return lhs.invite_link_ == rhs.invite_link_ && lhs.title_ == rhs.title_ &&
lhs.creator_user_id_ == rhs.creator_user_id_ && lhs.date_ == rhs.date_ && lhs.edit_date_ == rhs.edit_date_ &&
lhs.expire_date_ == rhs.expire_date_ && lhs.usage_limit_ == rhs.usage_limit_ &&
lhs.usage_count_ == rhs.usage_count_ && lhs.request_count_ == rhs.request_count_ &&
2021-11-04 07:52:27 +01:00
lhs.creates_join_request_ == rhs.creates_join_request_ && lhs.is_permanent_ == rhs.is_permanent_ &&
2021-10-27 12:21:50 +02:00
lhs.is_revoked_ == rhs.is_revoked_;
2021-01-13 14:28:24 +01:00
}
bool operator!=(const DialogInviteLink &lhs, const DialogInviteLink &rhs) {
return !(lhs == rhs);
}
StringBuilder &operator<<(StringBuilder &string_builder, const DialogInviteLink &invite_link) {
2021-10-27 12:21:50 +02:00
return string_builder << "ChatInviteLink[" << invite_link.invite_link_ << '(' << invite_link.title_ << ')'
2021-11-04 07:52:27 +01:00
<< (invite_link.creates_join_request_ ? " creating join request" : "") << " by "
<< invite_link.creator_user_id_ << " created at " << invite_link.date_ << " edited at "
<< invite_link.edit_date_ << " expiring at " << invite_link.expire_date_ << " used by "
<< invite_link.usage_count_ << " with usage limit " << invite_link.usage_limit_ << " and "
2022-06-20 16:56:06 +02:00
<< invite_link.request_count_ << " pending join requests]";
2021-01-13 14:28:24 +01:00
}
} // namespace td