1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
use serde::{Deserialize, Serialize};

use crate::{
    api::{Method, Payload},
    types::{ChatId, Integer, User},
};

#[cfg(test)]
mod tests;

/// Represents an invite link for a chat.
#[derive(Clone, Debug, Deserialize, PartialEq, PartialOrd, Serialize)]
pub struct ChatInviteLink {
    /// Indicates whether users joining the chat via the link
    /// need to be approved by chat administrators.
    pub creates_join_request: bool,
    /// The creator of the link.
    pub creator: User,
    /// The invite link.
    ///
    /// If the link was created by another chat administrator,
    /// then the second part of the link will be replaced with “…”.
    pub invite_link: String,
    /// Indicates whether the link is primary.
    pub is_primary: bool,
    /// Indicates whether the link is revoked.
    pub is_revoked: bool,
    /// The point in time (Unix timestamp) when the link will expire or has been expired.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expire_date: Option<Integer>,
    /// The maximum number of users that can be members
    /// of the chat simultaneously after joining
    /// the chat via this invite link; 1-99999.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub member_limit: Option<Integer>,
    /// The name of the invite link.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// The number of pending join requests created using this link.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pending_join_request_count: Option<Integer>,
}

impl ChatInviteLink {
    /// Creates a new `ChatInviteLink`.
    ///
    /// # Arguments
    ///
    /// * `invite_link` - Invite link.
    /// * `creator` - Creator of the link.
    pub fn new<T>(invite_link: T, creator: User) -> Self
    where
        T: Into<String>,
    {
        Self {
            invite_link: invite_link.into(),
            creator,
            creates_join_request: false,
            is_primary: false,
            is_revoked: false,
            name: None,
            expire_date: None,
            member_limit: None,
            pending_join_request_count: None,
        }
    }

    /// Sets a new value for a `creates_join_request` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether users joining the chat via the link need
    ///             to be approved by chat administrators.
    pub fn with_creates_join_request(mut self, value: bool) -> Self {
        self.creates_join_request = value;
        self
    }

    /// Sets a new expiration date.
    ///
    /// # Arguments
    ///
    /// * `value` - The point in time (Unix timestamp) when the link will expire or has been expired.
    pub fn with_expire_date(mut self, value: Integer) -> Self {
        self.expire_date = Some(value);
        self
    }

    /// Sets a new value for an `is_primary` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the link is primary.
    pub fn with_is_primary(mut self, value: bool) -> Self {
        self.is_primary = value;
        self
    }

    /// Sets a new value for an `is_revoked` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether the link is revoked.
    pub fn with_is_revoked(mut self, value: bool) -> Self {
        self.is_revoked = value;
        self
    }

    /// Sets a new member limit
    ///
    /// # Arguments
    ///
    /// * `value` - The maximum number of users that can be members
    ///             of the chat simultaneously after joining
    ///             the chat via this invite link; 1-99999.
    pub fn with_member_limit(mut self, value: Integer) -> Self {
        self.member_limit = Some(value);
        self
    }

    /// Sets a new name of the invite link.
    ///
    /// # Arguments
    ///
    /// * `value` - The name of the invite link.
    pub fn with_name<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.name = Some(value.into());
        self
    }

    /// Sets a new pending join requests count.
    ///
    /// # Arguments
    ///
    /// * `value` - The number of pending join requests created using this link.
    pub fn with_pending_join_request_count(mut self, value: Integer) -> Self {
        self.pending_join_request_count = Some(value);
        self
    }
}

/// Creates an additional invite link for a chat.
///
/// The bot must be an administrator in the chat for this to work
/// and must have the appropriate admin rights.
/// The link can be revoked using the method [`RevokeChatInviteLink`].
/// Returns the new invite link as [`ChatInviteLink`] object.
#[derive(Clone, Debug, Serialize)]
pub struct CreateChatInviteLink {
    chat_id: ChatId,
    #[serde(skip_serializing_if = "Option::is_none")]
    creates_join_request: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    expire_date: Option<Integer>,
    #[serde(skip_serializing_if = "Option::is_none")]
    member_limit: Option<Integer>,
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
}

impl CreateChatInviteLink {
    /// Creates a new `CreateChatInviteLink`.
    ///
    /// # Arguments
    ///
    /// * `chat_id` - Unique identifier of the target chat.
    pub fn new<T>(chat_id: T) -> Self
    where
        T: Into<ChatId>,
    {
        Self {
            chat_id: chat_id.into(),
            creates_join_request: None,
            expire_date: None,
            member_limit: None,
            name: None,
        }
    }

    /// Sets a new value for a `creates_join_request` flag.
    ///
    /// * `value` - Indicates whether users joining the chat via the link need
    ///             to be approved by chat administrators;
    ///             if `true`, member_limit can't be specified.
    pub fn with_creates_join_request(mut self, value: bool) -> Self {
        self.creates_join_request = Some(value);
        self
    }

    /// Sets a new expiration date.
    ///
    /// # Arguments
    ///
    /// * `value` - The point in time (Unix timestamp) when the link will expire.
    pub fn with_expire_date(mut self, value: Integer) -> Self {
        self.expire_date = Some(value);
        self
    }

    /// Sets a new member limit.
    ///
    /// # Arguments
    ///
    /// * `value` - The maximum number of users that can be members of the chat simultaneously
    ///             after joining the chat via this invite link; 1-99999.
    pub fn with_member_limit(mut self, value: Integer) -> Self {
        self.member_limit = Some(value);
        self
    }

    /// Sets a new invite link name.
    ///
    /// # Arguments
    ///
    /// * `value` - The name of the invite link; 0-32 characters.
    pub fn with_name<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.name = Some(value.into());
        self
    }
}

impl Method for CreateChatInviteLink {
    type Response = ChatInviteLink;

    fn into_payload(self) -> Payload {
        Payload::json("createChatInviteLink", self)
    }
}

/// Changes a non-primary invite link created by a bot.
///
/// The bot must be an administrator in the chat for this to work
/// and must have the appropriate admin rights.
/// Returns the edited invite link as a [`ChatInviteLink`] object.
#[derive(Clone, Debug, Serialize)]
pub struct EditChatInviteLink {
    chat_id: ChatId,
    invite_link: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    creates_join_request: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    expire_date: Option<Integer>,
    #[serde(skip_serializing_if = "Option::is_none")]
    member_limit: Option<Integer>,
    #[serde(skip_serializing_if = "Option::is_none")]
    name: Option<String>,
}

impl EditChatInviteLink {
    /// Creates a new `EditChatInviteLink`.
    ///
    /// # Arguments
    ///
    /// * `chat_id` - Unique identifier of the target chat.
    /// * `invite_link` - The invite link to edit.
    pub fn new<A, B>(chat_id: A, invite_link: B) -> Self
    where
        A: Into<ChatId>,
        B: Into<String>,
    {
        Self {
            chat_id: chat_id.into(),
            invite_link: invite_link.into(),
            creates_join_request: None,
            expire_date: None,
            member_limit: None,
            name: None,
        }
    }

    /// Sets a new value for a `creates_join_request` flag.
    ///
    /// # Arguments
    ///
    /// * `value` - Indicates whether users joining the chat via the link need
    ///             to be approved by chat administrators;
    ///             if `true`, `member_limit` can't be specified.
    pub fn with_creates_join_request(mut self, value: bool) -> Self {
        self.creates_join_request = Some(value);
        self
    }

    /// Sets a new expiration date.
    ///
    /// # Arguments
    ///
    /// * `value` - The point in time (Unix timestamp) when the link will expire.
    pub fn with_expire_date(mut self, value: Integer) -> Self {
        self.expire_date = Some(value);
        self
    }

    /// Sets a new member limit.
    ///
    /// # Arguments
    ///
    /// * `value` - The maximum number of users that can be members of the chat simultaneously
    ///             after joining the chat via this invite link; 1-99999.
    pub fn with_member_limit(mut self, value: Integer) -> Self {
        self.member_limit = Some(value);
        self
    }

    /// Sets a new name of the invite link.
    ///
    /// # Arguments
    ///
    /// * `value` - The name of the invite link; 0-32 characters.
    pub fn with_name<T>(mut self, value: T) -> Self
    where
        T: Into<String>,
    {
        self.name = Some(value.into());
        self
    }
}

impl Method for EditChatInviteLink {
    type Response = ChatInviteLink;

    fn into_payload(self) -> Payload {
        Payload::json("editChatInviteLink", self)
    }
}

/// Generates a new invite link for a chat.
///
/// Any previously generated link is revoked.
/// The bot must be an administrator in the chat for this to work
/// and must have the appropriate admin rights.
/// Returns the new invite link as String on success.
///
/// # Notes
///
/// Each administrator in a chat generates their own invite links.
/// Bots can't use invite links generated by other administrators.
/// If you want your bot to work with invite links, it will need to generate
/// its own link using [`ExportChatInviteLink`] or by calling the [`crate::types::GetChat`] method.
/// If your bot needs to generate a new primary invite link replacing its previous one,
/// use [`ExportChatInviteLink`] again.
#[derive(Clone, Debug, Serialize)]
pub struct ExportChatInviteLink {
    chat_id: ChatId,
}

impl ExportChatInviteLink {
    /// Creates a new `ExportChatInviteLink`.
    ///
    /// # Arguments
    ///
    /// * `chat_id` - Unique identifier of the target chat.
    pub fn new<T>(chat_id: T) -> Self
    where
        T: Into<ChatId>,
    {
        Self {
            chat_id: chat_id.into(),
        }
    }
}

impl Method for ExportChatInviteLink {
    type Response = String;

    fn into_payload(self) -> Payload {
        Payload::json("exportChatInviteLink", self)
    }
}

/// Revokes an invite link created by a bot.
///
/// If the primary link is revoked, a new link is automatically generated.
/// The bot must be an administrator in the chat for this to work and
/// must have the appropriate admin rights.
/// Returns the revoked invite link as [`ChatInviteLink`] object.
#[derive(Clone, Debug, Serialize)]
pub struct RevokeChatInviteLink {
    chat_id: ChatId,
    invite_link: String,
}

impl RevokeChatInviteLink {
    /// Creates a new `RevokeChatInviteLink`.
    ///
    /// # Arguments
    ///
    /// * `chat_id` - Unique identifier of the target chat.
    /// * `invite_link` - The invite link to revoke.
    pub fn new<A, B>(chat_id: A, invite_link: B) -> Self
    where
        A: Into<ChatId>,
        B: Into<String>,
    {
        Self {
            chat_id: chat_id.into(),
            invite_link: invite_link.into(),
        }
    }
}

impl Method for RevokeChatInviteLink {
    type Response = ChatInviteLink;

    fn into_payload(self) -> Payload {
        Payload::json("revokeChatInviteLink", self)
    }
}