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 #pragma once
8 
9 #include "td/telegram/td_api.h"
10 #include "td/telegram/telegram_api.h"
11 #include "td/telegram/UserId.h"
12 
13 #include "td/utils/common.h"
14 #include "td/utils/Slice.h"
15 #include "td/utils/StringBuilder.h"
16 #include "td/utils/tl_helpers.h"
17 
18 namespace td {
19 
20 class ContactsManager;
21 
22 class DialogInviteLink {
23   string invite_link_;
24   string title_;
25   UserId creator_user_id_;
26   int32 date_ = 0;
27   int32 edit_date_ = 0;
28   int32 expire_date_ = 0;
29   int32 usage_limit_ = 0;
30   int32 usage_count_ = 0;
31   int32 request_count_ = 0;
32   bool creates_join_request_ = false;
33   bool is_revoked_ = false;
34   bool is_permanent_ = false;
35 
36   friend bool operator==(const DialogInviteLink &lhs, const DialogInviteLink &rhs);
37 
38   friend StringBuilder &operator<<(StringBuilder &string_builder, const DialogInviteLink &invite_link);
39 
40  public:
41   DialogInviteLink() = default;
42 
43   explicit DialogInviteLink(tl_object_ptr<telegram_api::chatInviteExported> exported_invite);
44 
45   static bool is_valid_invite_link(Slice invite_link);
46 
47   td_api::object_ptr<td_api::chatInviteLink> get_chat_invite_link_object(const ContactsManager *contacts_manager) const;
48 
is_valid()49   bool is_valid() const {
50     return !invite_link_.empty() && creator_user_id_.is_valid() && date_ > 0;
51   }
52 
is_permanent()53   bool is_permanent() const {
54     return is_permanent_;
55   }
56 
get_invite_link()57   const string &get_invite_link() const {
58     return invite_link_;
59   }
60 
get_creator_user_id()61   UserId get_creator_user_id() const {
62     return creator_user_id_;
63   }
64 
65   template <class StorerT>
store(StorerT & storer)66   void store(StorerT &storer) const {
67     using td::store;
68     bool has_expire_date = expire_date_ != 0;
69     bool has_usage_limit = usage_limit_ != 0;
70     bool has_usage_count = usage_count_ != 0;
71     bool has_edit_date = edit_date_ != 0;
72     bool has_request_count = request_count_ != 0;
73     bool has_title = !title_.empty();
74     BEGIN_STORE_FLAGS();
75     STORE_FLAG(is_revoked_);
76     STORE_FLAG(is_permanent_);
77     STORE_FLAG(has_expire_date);
78     STORE_FLAG(has_usage_limit);
79     STORE_FLAG(has_usage_count);
80     STORE_FLAG(has_edit_date);
81     STORE_FLAG(has_request_count);
82     STORE_FLAG(creates_join_request_);
83     STORE_FLAG(has_title);
84     END_STORE_FLAGS();
85     store(invite_link_, storer);
86     store(creator_user_id_, storer);
87     store(date_, storer);
88     if (has_expire_date) {
89       store(expire_date_, storer);
90     }
91     if (has_usage_limit) {
92       store(usage_limit_, storer);
93     }
94     if (has_usage_count) {
95       store(usage_count_, storer);
96     }
97     if (has_edit_date) {
98       store(edit_date_, storer);
99     }
100     if (has_request_count) {
101       store(request_count_, storer);
102     }
103     if (has_title) {
104       store(title_, storer);
105     }
106   }
107 
108   template <class ParserT>
parse(ParserT & parser)109   void parse(ParserT &parser) {
110     using td::parse;
111     bool has_expire_date;
112     bool has_usage_limit;
113     bool has_usage_count;
114     bool has_edit_date;
115     bool has_request_count;
116     bool has_title;
117     BEGIN_PARSE_FLAGS();
118     PARSE_FLAG(is_revoked_);
119     PARSE_FLAG(is_permanent_);
120     PARSE_FLAG(has_expire_date);
121     PARSE_FLAG(has_usage_limit);
122     PARSE_FLAG(has_usage_count);
123     PARSE_FLAG(has_edit_date);
124     PARSE_FLAG(has_request_count);
125     PARSE_FLAG(creates_join_request_);
126     PARSE_FLAG(has_title);
127     END_PARSE_FLAGS();
128     parse(invite_link_, parser);
129     parse(creator_user_id_, parser);
130     parse(date_, parser);
131     if (has_expire_date) {
132       parse(expire_date_, parser);
133     }
134     if (has_usage_limit) {
135       parse(usage_limit_, parser);
136     }
137     if (has_usage_count) {
138       parse(usage_count_, parser);
139     }
140     if (has_edit_date) {
141       parse(edit_date_, parser);
142     }
143     if (has_request_count) {
144       parse(request_count_, parser);
145     }
146     if (has_title) {
147       parse(title_, parser);
148     }
149     if (creates_join_request_) {
150       usage_limit_ = 0;
151     }
152   }
153 };
154 
155 bool operator==(const DialogInviteLink &lhs, const DialogInviteLink &rhs);
156 
157 bool operator!=(const DialogInviteLink &lhs, const DialogInviteLink &rhs);
158 
159 StringBuilder &operator<<(StringBuilder &string_builder, const DialogInviteLink &invite_link);
160 
161 }  // namespace td
162