rust_tdlib/types/
notification_type.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5use std::fmt::Debug;
6
7/// Contains detailed information about a notification
8pub trait TDNotificationType: Debug + RObject {}
9
10/// Contains detailed information about a notification
11#[derive(Debug, Clone, Deserialize, Serialize, Default)]
12#[serde(tag = "@type")]
13pub enum NotificationType {
14    #[doc(hidden)]
15    #[default]
16    _Default,
17    /// New call was received
18    #[serde(rename = "notificationTypeNewCall")]
19    NewCall(NotificationTypeNewCall),
20    /// New message was received
21    #[serde(rename = "notificationTypeNewMessage")]
22    NewMessage(Box<NotificationTypeNewMessage>),
23    /// New message was received through a push notification
24    #[serde(rename = "notificationTypeNewPushMessage")]
25    NewPushMessage(Box<NotificationTypeNewPushMessage>),
26    /// New secret chat was created
27    #[serde(rename = "notificationTypeNewSecretChat")]
28    NewSecretChat(NotificationTypeNewSecretChat),
29}
30
31impl RObject for NotificationType {
32    #[doc(hidden)]
33    fn extra(&self) -> Option<&str> {
34        match self {
35            NotificationType::NewCall(t) => t.extra(),
36            NotificationType::NewMessage(t) => t.extra(),
37            NotificationType::NewPushMessage(t) => t.extra(),
38            NotificationType::NewSecretChat(t) => t.extra(),
39
40            _ => None,
41        }
42    }
43    #[doc(hidden)]
44    fn client_id(&self) -> Option<i32> {
45        match self {
46            NotificationType::NewCall(t) => t.client_id(),
47            NotificationType::NewMessage(t) => t.client_id(),
48            NotificationType::NewPushMessage(t) => t.client_id(),
49            NotificationType::NewSecretChat(t) => t.client_id(),
50
51            _ => None,
52        }
53    }
54}
55
56impl NotificationType {
57    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
58        Ok(serde_json::from_str(json.as_ref())?)
59    }
60    #[doc(hidden)]
61    pub fn _is_default(&self) -> bool {
62        matches!(self, NotificationType::_Default)
63    }
64}
65
66impl AsRef<NotificationType> for NotificationType {
67    fn as_ref(&self) -> &NotificationType {
68        self
69    }
70}
71
72/// New call was received
73#[derive(Debug, Clone, Default, Serialize, Deserialize)]
74pub struct NotificationTypeNewCall {
75    #[doc(hidden)]
76    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
77    extra: Option<String>,
78    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
79    client_id: Option<i32>,
80    /// Call identifier
81
82    #[serde(default)]
83    call_id: i32,
84}
85
86impl RObject for NotificationTypeNewCall {
87    #[doc(hidden)]
88    fn extra(&self) -> Option<&str> {
89        self.extra.as_deref()
90    }
91    #[doc(hidden)]
92    fn client_id(&self) -> Option<i32> {
93        self.client_id
94    }
95}
96
97impl TDNotificationType for NotificationTypeNewCall {}
98
99impl NotificationTypeNewCall {
100    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
101        Ok(serde_json::from_str(json.as_ref())?)
102    }
103    pub fn builder() -> NotificationTypeNewCallBuilder {
104        let mut inner = NotificationTypeNewCall::default();
105        inner.extra = Some(Uuid::new_v4().to_string());
106
107        NotificationTypeNewCallBuilder { inner }
108    }
109
110    pub fn call_id(&self) -> i32 {
111        self.call_id
112    }
113}
114
115#[doc(hidden)]
116pub struct NotificationTypeNewCallBuilder {
117    inner: NotificationTypeNewCall,
118}
119
120#[deprecated]
121pub type RTDNotificationTypeNewCallBuilder = NotificationTypeNewCallBuilder;
122
123impl NotificationTypeNewCallBuilder {
124    pub fn build(&self) -> NotificationTypeNewCall {
125        self.inner.clone()
126    }
127
128    pub fn call_id(&mut self, call_id: i32) -> &mut Self {
129        self.inner.call_id = call_id;
130        self
131    }
132}
133
134impl AsRef<NotificationTypeNewCall> for NotificationTypeNewCall {
135    fn as_ref(&self) -> &NotificationTypeNewCall {
136        self
137    }
138}
139
140impl AsRef<NotificationTypeNewCall> for NotificationTypeNewCallBuilder {
141    fn as_ref(&self) -> &NotificationTypeNewCall {
142        &self.inner
143    }
144}
145
146/// New message was received
147#[derive(Debug, Clone, Default, Serialize, Deserialize)]
148pub struct NotificationTypeNewMessage {
149    #[doc(hidden)]
150    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
151    extra: Option<String>,
152    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
153    client_id: Option<i32>,
154    /// The message
155    message: Message,
156}
157
158impl RObject for NotificationTypeNewMessage {
159    #[doc(hidden)]
160    fn extra(&self) -> Option<&str> {
161        self.extra.as_deref()
162    }
163    #[doc(hidden)]
164    fn client_id(&self) -> Option<i32> {
165        self.client_id
166    }
167}
168
169impl TDNotificationType for NotificationTypeNewMessage {}
170
171impl NotificationTypeNewMessage {
172    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
173        Ok(serde_json::from_str(json.as_ref())?)
174    }
175    pub fn builder() -> NotificationTypeNewMessageBuilder {
176        let mut inner = NotificationTypeNewMessage::default();
177        inner.extra = Some(Uuid::new_v4().to_string());
178
179        NotificationTypeNewMessageBuilder { inner }
180    }
181
182    pub fn message(&self) -> &Message {
183        &self.message
184    }
185}
186
187#[doc(hidden)]
188pub struct NotificationTypeNewMessageBuilder {
189    inner: NotificationTypeNewMessage,
190}
191
192#[deprecated]
193pub type RTDNotificationTypeNewMessageBuilder = NotificationTypeNewMessageBuilder;
194
195impl NotificationTypeNewMessageBuilder {
196    pub fn build(&self) -> NotificationTypeNewMessage {
197        self.inner.clone()
198    }
199
200    pub fn message<T: AsRef<Message>>(&mut self, message: T) -> &mut Self {
201        self.inner.message = message.as_ref().clone();
202        self
203    }
204}
205
206impl AsRef<NotificationTypeNewMessage> for NotificationTypeNewMessage {
207    fn as_ref(&self) -> &NotificationTypeNewMessage {
208        self
209    }
210}
211
212impl AsRef<NotificationTypeNewMessage> for NotificationTypeNewMessageBuilder {
213    fn as_ref(&self) -> &NotificationTypeNewMessage {
214        &self.inner
215    }
216}
217
218/// New message was received through a push notification
219#[derive(Debug, Clone, Default, Serialize, Deserialize)]
220pub struct NotificationTypeNewPushMessage {
221    #[doc(hidden)]
222    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
223    extra: Option<String>,
224    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
225    client_id: Option<i32>,
226    /// The message identifier. The message will not be available in the chat history, but the ID can be used in viewMessages, or as reply_to_message_id
227
228    #[serde(default)]
229    message_id: i64,
230    /// Identifier of the sender of the message. Corresponding user or chat may be inaccessible
231
232    #[serde(skip_serializing_if = "MessageSender::_is_default")]
233    sender_id: MessageSender,
234    /// Name of the sender
235
236    #[serde(default)]
237    sender_name: String,
238    /// True, if the message is outgoing
239
240    #[serde(default)]
241    is_outgoing: bool,
242    /// Push message content
243
244    #[serde(skip_serializing_if = "PushMessageContent::_is_default")]
245    content: PushMessageContent,
246}
247
248impl RObject for NotificationTypeNewPushMessage {
249    #[doc(hidden)]
250    fn extra(&self) -> Option<&str> {
251        self.extra.as_deref()
252    }
253    #[doc(hidden)]
254    fn client_id(&self) -> Option<i32> {
255        self.client_id
256    }
257}
258
259impl TDNotificationType for NotificationTypeNewPushMessage {}
260
261impl NotificationTypeNewPushMessage {
262    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
263        Ok(serde_json::from_str(json.as_ref())?)
264    }
265    pub fn builder() -> NotificationTypeNewPushMessageBuilder {
266        let mut inner = NotificationTypeNewPushMessage::default();
267        inner.extra = Some(Uuid::new_v4().to_string());
268
269        NotificationTypeNewPushMessageBuilder { inner }
270    }
271
272    pub fn message_id(&self) -> i64 {
273        self.message_id
274    }
275
276    pub fn sender_id(&self) -> &MessageSender {
277        &self.sender_id
278    }
279
280    pub fn sender_name(&self) -> &String {
281        &self.sender_name
282    }
283
284    pub fn is_outgoing(&self) -> bool {
285        self.is_outgoing
286    }
287
288    pub fn content(&self) -> &PushMessageContent {
289        &self.content
290    }
291}
292
293#[doc(hidden)]
294pub struct NotificationTypeNewPushMessageBuilder {
295    inner: NotificationTypeNewPushMessage,
296}
297
298#[deprecated]
299pub type RTDNotificationTypeNewPushMessageBuilder = NotificationTypeNewPushMessageBuilder;
300
301impl NotificationTypeNewPushMessageBuilder {
302    pub fn build(&self) -> NotificationTypeNewPushMessage {
303        self.inner.clone()
304    }
305
306    pub fn message_id(&mut self, message_id: i64) -> &mut Self {
307        self.inner.message_id = message_id;
308        self
309    }
310
311    pub fn sender_id<T: AsRef<MessageSender>>(&mut self, sender_id: T) -> &mut Self {
312        self.inner.sender_id = sender_id.as_ref().clone();
313        self
314    }
315
316    pub fn sender_name<T: AsRef<str>>(&mut self, sender_name: T) -> &mut Self {
317        self.inner.sender_name = sender_name.as_ref().to_string();
318        self
319    }
320
321    pub fn is_outgoing(&mut self, is_outgoing: bool) -> &mut Self {
322        self.inner.is_outgoing = is_outgoing;
323        self
324    }
325
326    pub fn content<T: AsRef<PushMessageContent>>(&mut self, content: T) -> &mut Self {
327        self.inner.content = content.as_ref().clone();
328        self
329    }
330}
331
332impl AsRef<NotificationTypeNewPushMessage> for NotificationTypeNewPushMessage {
333    fn as_ref(&self) -> &NotificationTypeNewPushMessage {
334        self
335    }
336}
337
338impl AsRef<NotificationTypeNewPushMessage> for NotificationTypeNewPushMessageBuilder {
339    fn as_ref(&self) -> &NotificationTypeNewPushMessage {
340        &self.inner
341    }
342}
343
344/// New secret chat was created
345#[derive(Debug, Clone, Default, Serialize, Deserialize)]
346pub struct NotificationTypeNewSecretChat {
347    #[doc(hidden)]
348    #[serde(rename(serialize = "@extra", deserialize = "@extra"))]
349    extra: Option<String>,
350    #[serde(rename(serialize = "@client_id", deserialize = "@client_id"))]
351    client_id: Option<i32>,
352}
353
354impl RObject for NotificationTypeNewSecretChat {
355    #[doc(hidden)]
356    fn extra(&self) -> Option<&str> {
357        self.extra.as_deref()
358    }
359    #[doc(hidden)]
360    fn client_id(&self) -> Option<i32> {
361        self.client_id
362    }
363}
364
365impl TDNotificationType for NotificationTypeNewSecretChat {}
366
367impl NotificationTypeNewSecretChat {
368    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
369        Ok(serde_json::from_str(json.as_ref())?)
370    }
371    pub fn builder() -> NotificationTypeNewSecretChatBuilder {
372        let mut inner = NotificationTypeNewSecretChat::default();
373        inner.extra = Some(Uuid::new_v4().to_string());
374
375        NotificationTypeNewSecretChatBuilder { inner }
376    }
377}
378
379#[doc(hidden)]
380pub struct NotificationTypeNewSecretChatBuilder {
381    inner: NotificationTypeNewSecretChat,
382}
383
384#[deprecated]
385pub type RTDNotificationTypeNewSecretChatBuilder = NotificationTypeNewSecretChatBuilder;
386
387impl NotificationTypeNewSecretChatBuilder {
388    pub fn build(&self) -> NotificationTypeNewSecretChat {
389        self.inner.clone()
390    }
391}
392
393impl AsRef<NotificationTypeNewSecretChat> for NotificationTypeNewSecretChat {
394    fn as_ref(&self) -> &NotificationTypeNewSecretChat {
395        self
396    }
397}
398
399impl AsRef<NotificationTypeNewSecretChat> for NotificationTypeNewSecretChatBuilder {
400    fn as_ref(&self) -> &NotificationTypeNewSecretChat {
401        &self.inner
402    }
403}