rust_tdlib/types/
chat.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// A chat. (Can be a private chat, basic group, supergroup, or secret chat)
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct Chat {
8    #[doc(hidden)]
9    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
10    extra: Option<String>,
11    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
12    client_id: Option<i32>,
13    /// Chat unique identifier
14
15    #[serde(default)]
16    id: i64,
17    /// Type of the chat
18
19    #[serde(rename(serialize = "type", deserialize = "type"))]
20    #[serde(skip_serializing_if = "ChatType::_is_default")]
21    type_: ChatType,
22    /// Chat title
23
24    #[serde(default)]
25    title: String,
26    /// Chat photo; may be null
27    photo: Option<ChatPhotoInfo>,
28    /// Actions that non-administrator chat members are allowed to take in the chat
29    permissions: ChatPermissions,
30    /// Last message in the chat; may be null
31    last_message: Option<Message>,
32    /// Positions of the chat in chat lists
33
34    #[serde(default)]
35    positions: Option<Vec<ChatPosition>>,
36    /// Identifier of a user or chat that is selected to send messages in the chat; may be null if the user can't change message sender
37    message_sender_id: Option<MessageSender>,
38    /// True, if chat content can't be saved locally, forwarded, or copied
39
40    #[serde(default)]
41    has_protected_content: bool,
42    /// True, if the chat is marked as unread
43
44    #[serde(default)]
45    is_marked_as_unread: bool,
46    /// True, if the chat is blocked by the current user and private messages from the chat can't be received
47
48    #[serde(default)]
49    is_blocked: bool,
50    /// True, if the chat has scheduled messages
51
52    #[serde(default)]
53    has_scheduled_messages: bool,
54    /// True, if the chat messages can be deleted only for the current user while other users will continue to see the messages
55
56    #[serde(default)]
57    can_be_deleted_only_for_self: bool,
58    /// True, if the chat messages can be deleted for all users
59
60    #[serde(default)]
61    can_be_deleted_for_all_users: bool,
62    /// True, if the chat can be reported to Telegram moderators through reportChat or reportChatPhoto
63
64    #[serde(default)]
65    can_be_reported: bool,
66    /// Default value of the disable_notification parameter, used when a message is sent to the chat
67
68    #[serde(default)]
69    default_disable_notification: bool,
70    /// Number of unread messages in the chat
71
72    #[serde(default)]
73    unread_count: i32,
74    /// Identifier of the last read incoming message
75
76    #[serde(default)]
77    last_read_inbox_message_id: i64,
78    /// Identifier of the last read outgoing message
79
80    #[serde(default)]
81    last_read_outbox_message_id: i64,
82    /// Number of unread messages with a mention/reply in the chat
83
84    #[serde(default)]
85    unread_mention_count: i32,
86    /// Notification settings for this chat
87    notification_settings: ChatNotificationSettings,
88    /// Current message Time To Live setting (self-destruct timer) for the chat; 0 if not defined. TTL is counted from the time message or its content is viewed in secret chats and from the send date in other chats
89
90    #[serde(default)]
91    message_ttl: i32,
92    /// If non-empty, name of a theme, set for the chat
93
94    #[serde(default)]
95    theme_name: String,
96    /// Information about actions which must be possible to do through the chat action bar; may be null
97    action_bar: Option<ChatActionBar>,
98    /// Information about video chat of the chat
99    video_chat: VideoChat,
100    /// Information about pending join requests; may be null
101    pending_join_requests: Option<ChatJoinRequestsInfo>,
102    /// Identifier of the message from which reply markup needs to be used; 0 if there is no default custom reply markup in the chat
103
104    #[serde(default)]
105    reply_markup_message_id: i64,
106    /// A draft of a message in the chat; may be null
107    draft_message: Option<DraftMessage>,
108    /// Application-specific data associated with the chat. (For example, the chat scroll position or local chat notification settings can be stored here.) Persistent if the message database is used
109
110    #[serde(default)]
111    client_data: String,
112}
113
114impl RObject for Chat {
115    #[doc(hidden)]
116    fn extra(&self) -> Option<&str> {
117        self.extra.as_deref()
118    }
119    #[doc(hidden)]
120    fn client_id(&self) -> Option<i32> {
121        self.client_id
122    }
123}
124
125impl Chat {
126    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
127        Ok(serde_json::from_str(json.as_ref())?)
128    }
129    pub fn builder() -> ChatBuilder {
130        let mut inner = Chat::default();
131        inner.extra = Some(Uuid::new_v4().to_string());
132
133        ChatBuilder { inner }
134    }
135
136    pub fn id(&self) -> i64 {
137        self.id
138    }
139
140    pub fn type_(&self) -> &ChatType {
141        &self.type_
142    }
143
144    pub fn title(&self) -> &String {
145        &self.title
146    }
147
148    pub fn photo(&self) -> &Option<ChatPhotoInfo> {
149        &self.photo
150    }
151
152    pub fn permissions(&self) -> &ChatPermissions {
153        &self.permissions
154    }
155
156    pub fn last_message(&self) -> &Option<Message> {
157        &self.last_message
158    }
159
160    pub fn positions(&self) -> &Option<Vec<ChatPosition>> {
161        &self.positions
162    }
163
164    pub fn message_sender_id(&self) -> &Option<MessageSender> {
165        &self.message_sender_id
166    }
167
168    pub fn has_protected_content(&self) -> bool {
169        self.has_protected_content
170    }
171
172    pub fn is_marked_as_unread(&self) -> bool {
173        self.is_marked_as_unread
174    }
175
176    pub fn is_blocked(&self) -> bool {
177        self.is_blocked
178    }
179
180    pub fn has_scheduled_messages(&self) -> bool {
181        self.has_scheduled_messages
182    }
183
184    pub fn can_be_deleted_only_for_self(&self) -> bool {
185        self.can_be_deleted_only_for_self
186    }
187
188    pub fn can_be_deleted_for_all_users(&self) -> bool {
189        self.can_be_deleted_for_all_users
190    }
191
192    pub fn can_be_reported(&self) -> bool {
193        self.can_be_reported
194    }
195
196    pub fn default_disable_notification(&self) -> bool {
197        self.default_disable_notification
198    }
199
200    pub fn unread_count(&self) -> i32 {
201        self.unread_count
202    }
203
204    pub fn last_read_inbox_message_id(&self) -> i64 {
205        self.last_read_inbox_message_id
206    }
207
208    pub fn last_read_outbox_message_id(&self) -> i64 {
209        self.last_read_outbox_message_id
210    }
211
212    pub fn unread_mention_count(&self) -> i32 {
213        self.unread_mention_count
214    }
215
216    pub fn notification_settings(&self) -> &ChatNotificationSettings {
217        &self.notification_settings
218    }
219
220    pub fn message_ttl(&self) -> i32 {
221        self.message_ttl
222    }
223
224    pub fn theme_name(&self) -> &String {
225        &self.theme_name
226    }
227
228    pub fn action_bar(&self) -> &Option<ChatActionBar> {
229        &self.action_bar
230    }
231
232    pub fn video_chat(&self) -> &VideoChat {
233        &self.video_chat
234    }
235
236    pub fn pending_join_requests(&self) -> &Option<ChatJoinRequestsInfo> {
237        &self.pending_join_requests
238    }
239
240    pub fn reply_markup_message_id(&self) -> i64 {
241        self.reply_markup_message_id
242    }
243
244    pub fn draft_message(&self) -> &Option<DraftMessage> {
245        &self.draft_message
246    }
247
248    pub fn client_data(&self) -> &String {
249        &self.client_data
250    }
251}
252
253#[doc(hidden)]
254pub struct ChatBuilder {
255    inner: Chat,
256}
257
258#[deprecated]
259pub type RTDChatBuilder = ChatBuilder;
260
261impl ChatBuilder {
262    pub fn build(&self) -> Chat {
263        self.inner.clone()
264    }
265
266    pub fn id(&mut self, id: i64) -> &mut Self {
267        self.inner.id = id;
268        self
269    }
270
271    pub fn type_<T: AsRef<ChatType>>(&mut self, type_: T) -> &mut Self {
272        self.inner.type_ = type_.as_ref().clone();
273        self
274    }
275
276    pub fn title<T: AsRef<str>>(&mut self, title: T) -> &mut Self {
277        self.inner.title = title.as_ref().to_string();
278        self
279    }
280
281    pub fn photo<T: AsRef<ChatPhotoInfo>>(&mut self, photo: T) -> &mut Self {
282        self.inner.photo = Some(photo.as_ref().clone());
283        self
284    }
285
286    pub fn permissions<T: AsRef<ChatPermissions>>(&mut self, permissions: T) -> &mut Self {
287        self.inner.permissions = permissions.as_ref().clone();
288        self
289    }
290
291    pub fn last_message<T: AsRef<Message>>(&mut self, last_message: T) -> &mut Self {
292        self.inner.last_message = Some(last_message.as_ref().clone());
293        self
294    }
295
296    pub fn positions(&mut self, positions: Vec<ChatPosition>) -> &mut Self {
297        self.inner.positions = Some(positions);
298        self
299    }
300
301    pub fn message_sender_id<T: AsRef<MessageSender>>(
302        &mut self,
303        message_sender_id: T,
304    ) -> &mut Self {
305        self.inner.message_sender_id = Some(message_sender_id.as_ref().clone());
306        self
307    }
308
309    pub fn has_protected_content(&mut self, has_protected_content: bool) -> &mut Self {
310        self.inner.has_protected_content = has_protected_content;
311        self
312    }
313
314    pub fn is_marked_as_unread(&mut self, is_marked_as_unread: bool) -> &mut Self {
315        self.inner.is_marked_as_unread = is_marked_as_unread;
316        self
317    }
318
319    pub fn is_blocked(&mut self, is_blocked: bool) -> &mut Self {
320        self.inner.is_blocked = is_blocked;
321        self
322    }
323
324    pub fn has_scheduled_messages(&mut self, has_scheduled_messages: bool) -> &mut Self {
325        self.inner.has_scheduled_messages = has_scheduled_messages;
326        self
327    }
328
329    pub fn can_be_deleted_only_for_self(
330        &mut self,
331        can_be_deleted_only_for_self: bool,
332    ) -> &mut Self {
333        self.inner.can_be_deleted_only_for_self = can_be_deleted_only_for_self;
334        self
335    }
336
337    pub fn can_be_deleted_for_all_users(
338        &mut self,
339        can_be_deleted_for_all_users: bool,
340    ) -> &mut Self {
341        self.inner.can_be_deleted_for_all_users = can_be_deleted_for_all_users;
342        self
343    }
344
345    pub fn can_be_reported(&mut self, can_be_reported: bool) -> &mut Self {
346        self.inner.can_be_reported = can_be_reported;
347        self
348    }
349
350    pub fn default_disable_notification(
351        &mut self,
352        default_disable_notification: bool,
353    ) -> &mut Self {
354        self.inner.default_disable_notification = default_disable_notification;
355        self
356    }
357
358    pub fn unread_count(&mut self, unread_count: i32) -> &mut Self {
359        self.inner.unread_count = unread_count;
360        self
361    }
362
363    pub fn last_read_inbox_message_id(&mut self, last_read_inbox_message_id: i64) -> &mut Self {
364        self.inner.last_read_inbox_message_id = last_read_inbox_message_id;
365        self
366    }
367
368    pub fn last_read_outbox_message_id(&mut self, last_read_outbox_message_id: i64) -> &mut Self {
369        self.inner.last_read_outbox_message_id = last_read_outbox_message_id;
370        self
371    }
372
373    pub fn unread_mention_count(&mut self, unread_mention_count: i32) -> &mut Self {
374        self.inner.unread_mention_count = unread_mention_count;
375        self
376    }
377
378    pub fn notification_settings<T: AsRef<ChatNotificationSettings>>(
379        &mut self,
380        notification_settings: T,
381    ) -> &mut Self {
382        self.inner.notification_settings = notification_settings.as_ref().clone();
383        self
384    }
385
386    pub fn message_ttl(&mut self, message_ttl: i32) -> &mut Self {
387        self.inner.message_ttl = message_ttl;
388        self
389    }
390
391    pub fn theme_name<T: AsRef<str>>(&mut self, theme_name: T) -> &mut Self {
392        self.inner.theme_name = theme_name.as_ref().to_string();
393        self
394    }
395
396    pub fn action_bar<T: AsRef<ChatActionBar>>(&mut self, action_bar: T) -> &mut Self {
397        self.inner.action_bar = Some(action_bar.as_ref().clone());
398        self
399    }
400
401    pub fn video_chat<T: AsRef<VideoChat>>(&mut self, video_chat: T) -> &mut Self {
402        self.inner.video_chat = video_chat.as_ref().clone();
403        self
404    }
405
406    pub fn pending_join_requests<T: AsRef<ChatJoinRequestsInfo>>(
407        &mut self,
408        pending_join_requests: T,
409    ) -> &mut Self {
410        self.inner.pending_join_requests = Some(pending_join_requests.as_ref().clone());
411        self
412    }
413
414    pub fn reply_markup_message_id(&mut self, reply_markup_message_id: i64) -> &mut Self {
415        self.inner.reply_markup_message_id = reply_markup_message_id;
416        self
417    }
418
419    pub fn draft_message<T: AsRef<DraftMessage>>(&mut self, draft_message: T) -> &mut Self {
420        self.inner.draft_message = Some(draft_message.as_ref().clone());
421        self
422    }
423
424    pub fn client_data<T: AsRef<str>>(&mut self, client_data: T) -> &mut Self {
425        self.inner.client_data = client_data.as_ref().to_string();
426        self
427    }
428}
429
430impl AsRef<Chat> for Chat {
431    fn as_ref(&self) -> &Chat {
432        self
433    }
434}
435
436impl AsRef<Chat> for ChatBuilder {
437    fn as_ref(&self) -> &Chat {
438        &self.inner
439    }
440}