1 //
2 // Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 #include "td/telegram/DialogInviteLink.h"
8
9 #include "td/telegram/ContactsManager.h"
10 #include "td/telegram/LinkManager.h"
11
12 #include "td/utils/logging.h"
13
14 namespace td {
15
DialogInviteLink(tl_object_ptr<telegram_api::chatInviteExported> exported_invite)16 DialogInviteLink::DialogInviteLink(tl_object_ptr<telegram_api::chatInviteExported> exported_invite) {
17 if (exported_invite == nullptr) {
18 return;
19 }
20
21 invite_link_ = std::move(exported_invite->link_);
22 title_ = std::move(exported_invite->title_);
23 creator_user_id_ = UserId(exported_invite->admin_id_);
24 date_ = exported_invite->date_;
25 expire_date_ = exported_invite->expire_date_;
26 usage_limit_ = exported_invite->usage_limit_;
27 usage_count_ = exported_invite->usage_;
28 edit_date_ = exported_invite->start_date_;
29 request_count_ = exported_invite->requested_;
30 creates_join_request_ = exported_invite->request_needed_;
31 is_revoked_ = exported_invite->revoked_;
32 is_permanent_ = exported_invite->permanent_;
33
34 LOG_IF(ERROR, !is_valid_invite_link(invite_link_)) << "Unsupported invite link " << invite_link_;
35 if (!creator_user_id_.is_valid()) {
36 LOG(ERROR) << "Receive invalid " << creator_user_id_ << " as creator of a link " << invite_link_;
37 creator_user_id_ = UserId();
38 }
39 if (date_ != 0 && date_ < 1000000000) {
40 LOG(ERROR) << "Receive wrong date " << date_ << " as a creation date of a link " << invite_link_;
41 date_ = 0;
42 }
43 if (expire_date_ != 0 && expire_date_ < 1000000000) {
44 LOG(ERROR) << "Receive wrong date " << expire_date_ << " as an expire date of a link " << invite_link_;
45 expire_date_ = 0;
46 }
47 if (usage_limit_ < 0) {
48 LOG(ERROR) << "Receive wrong usage limit " << usage_limit_ << " for a link " << invite_link_;
49 usage_limit_ = 0;
50 }
51 if (usage_count_ < 0) {
52 LOG(ERROR) << "Receive wrong usage count " << usage_count_ << " for a link " << invite_link_;
53 usage_count_ = 0;
54 }
55 if (edit_date_ != 0 && edit_date_ < 1000000000) {
56 LOG(ERROR) << "Receive wrong date " << edit_date_ << " as an edit date of a link " << invite_link_;
57 edit_date_ = 0;
58 }
59 if (request_count_ < 0) {
60 LOG(ERROR) << "Receive wrong pending join request count " << request_count_ << " for a link " << invite_link_;
61 request_count_ = 0;
62 }
63
64 if (is_permanent_ && (!title_.empty() || expire_date_ > 0 || usage_limit_ > 0 || edit_date_ > 0 ||
65 request_count_ > 0 || creates_join_request_)) {
66 LOG(ERROR) << "Receive wrong permanent " << *this;
67 title_.clear();
68 expire_date_ = 0;
69 usage_limit_ = 0;
70 edit_date_ = 0;
71 request_count_ = 0;
72 creates_join_request_ = false;
73 }
74 if (creates_join_request_ && usage_limit_ > 0) {
75 LOG(ERROR) << "Receive wrong permanent " << *this;
76 usage_limit_ = 0;
77 }
78 }
79
is_valid_invite_link(Slice invite_link)80 bool DialogInviteLink::is_valid_invite_link(Slice invite_link) {
81 return !LinkManager::get_dialog_invite_link_hash(invite_link).empty();
82 }
83
get_chat_invite_link_object(const ContactsManager * contacts_manager) const84 td_api::object_ptr<td_api::chatInviteLink> DialogInviteLink::get_chat_invite_link_object(
85 const ContactsManager *contacts_manager) const {
86 CHECK(contacts_manager != nullptr);
87 if (!is_valid()) {
88 return nullptr;
89 }
90
91 return td_api::make_object<td_api::chatInviteLink>(
92 invite_link_, title_, contacts_manager->get_user_id_object(creator_user_id_, "get_chat_invite_link_object"),
93 date_, edit_date_, expire_date_, usage_limit_, usage_count_, request_count_, creates_join_request_, is_permanent_,
94 is_revoked_);
95 }
96
operator ==(const DialogInviteLink & lhs,const DialogInviteLink & rhs)97 bool operator==(const DialogInviteLink &lhs, const DialogInviteLink &rhs) {
98 return lhs.invite_link_ == rhs.invite_link_ && lhs.title_ == rhs.title_ &&
99 lhs.creator_user_id_ == rhs.creator_user_id_ && lhs.date_ == rhs.date_ && lhs.edit_date_ == rhs.edit_date_ &&
100 lhs.expire_date_ == rhs.expire_date_ && lhs.usage_limit_ == rhs.usage_limit_ &&
101 lhs.usage_count_ == rhs.usage_count_ && lhs.request_count_ == rhs.request_count_ &&
102 lhs.creates_join_request_ == rhs.creates_join_request_ && lhs.is_permanent_ == rhs.is_permanent_ &&
103 lhs.is_revoked_ == rhs.is_revoked_;
104 }
105
operator !=(const DialogInviteLink & lhs,const DialogInviteLink & rhs)106 bool operator!=(const DialogInviteLink &lhs, const DialogInviteLink &rhs) {
107 return !(lhs == rhs);
108 }
109
operator <<(StringBuilder & string_builder,const DialogInviteLink & invite_link)110 StringBuilder &operator<<(StringBuilder &string_builder, const DialogInviteLink &invite_link) {
111 return string_builder << "ChatInviteLink[" << invite_link.invite_link_ << '(' << invite_link.title_ << ')'
112 << (invite_link.creates_join_request_ ? " creating join request" : "") << " by "
113 << invite_link.creator_user_id_ << " created at " << invite_link.date_ << " edited at "
114 << invite_link.edit_date_ << " expiring at " << invite_link.expire_date_ << " used by "
115 << invite_link.usage_count_ << " with usage limit " << invite_link.usage_limit_ << " and "
116 << invite_link.request_count_ << "pending join requests]";
117 }
118
119 } // namespace td
120