rust_tdlib/types/
session.rs

1use crate::errors::Result;
2use crate::types::*;
3use uuid::Uuid;
4
5/// Contains information about one session in a Telegram application used by the current user. Sessions must be shown to the user in the returned order
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct Session {
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    /// Session identifier
14
15    #[serde(
16        deserialize_with = "super::_common::number_from_string",
17        serialize_with = "super::_common::string_to_number"
18    )]
19    #[serde(default)]
20    id: i64,
21    /// True, if this session is the current session
22
23    #[serde(default)]
24    is_current: bool,
25    /// True, if a password is needed to complete authorization of the session
26
27    #[serde(default)]
28    is_password_pending: bool,
29    /// True, if incoming secret chats can be accepted by the session
30
31    #[serde(default)]
32    can_accept_secret_chats: bool,
33    /// True, if incoming calls can be accepted by the session
34
35    #[serde(default)]
36    can_accept_calls: bool,
37    /// Telegram API identifier, as provided by the application
38
39    #[serde(default)]
40    api_id: i32,
41    /// Name of the application, as provided by the application
42
43    #[serde(default)]
44    application_name: String,
45    /// The version of the application, as provided by the application
46
47    #[serde(default)]
48    application_version: String,
49    /// True, if the application is an official application or uses the api_id of an official application
50
51    #[serde(default)]
52    is_official_application: bool,
53    /// Model of the device the application has been run or is running on, as provided by the application
54
55    #[serde(default)]
56    device_model: String,
57    /// Operating system the application has been run or is running on, as provided by the application
58
59    #[serde(default)]
60    platform: String,
61    /// Version of the operating system the application has been run or is running on, as provided by the application
62
63    #[serde(default)]
64    system_version: String,
65    /// Point in time (Unix timestamp) when the user has logged in
66
67    #[serde(default)]
68    log_in_date: i32,
69    /// Point in time (Unix timestamp) when the session was last used
70
71    #[serde(default)]
72    last_active_date: i32,
73    /// IP address from which the session was created, in human-readable format
74
75    #[serde(default)]
76    ip: String,
77    /// A two-letter country code for the country from which the session was created, based on the IP address
78
79    #[serde(default)]
80    country: String,
81    /// Region code from which the session was created, based on the IP address
82
83    #[serde(default)]
84    region: String,
85}
86
87impl RObject for Session {
88    #[doc(hidden)]
89    fn extra(&self) -> Option<&str> {
90        self.extra.as_deref()
91    }
92    #[doc(hidden)]
93    fn client_id(&self) -> Option<i32> {
94        self.client_id
95    }
96}
97
98impl Session {
99    pub fn from_json<S: AsRef<str>>(json: S) -> Result<Self> {
100        Ok(serde_json::from_str(json.as_ref())?)
101    }
102    pub fn builder() -> SessionBuilder {
103        let mut inner = Session::default();
104        inner.extra = Some(Uuid::new_v4().to_string());
105
106        SessionBuilder { inner }
107    }
108
109    pub fn id(&self) -> i64 {
110        self.id
111    }
112
113    pub fn is_current(&self) -> bool {
114        self.is_current
115    }
116
117    pub fn is_password_pending(&self) -> bool {
118        self.is_password_pending
119    }
120
121    pub fn can_accept_secret_chats(&self) -> bool {
122        self.can_accept_secret_chats
123    }
124
125    pub fn can_accept_calls(&self) -> bool {
126        self.can_accept_calls
127    }
128
129    pub fn api_id(&self) -> i32 {
130        self.api_id
131    }
132
133    pub fn application_name(&self) -> &String {
134        &self.application_name
135    }
136
137    pub fn application_version(&self) -> &String {
138        &self.application_version
139    }
140
141    pub fn is_official_application(&self) -> bool {
142        self.is_official_application
143    }
144
145    pub fn device_model(&self) -> &String {
146        &self.device_model
147    }
148
149    pub fn platform(&self) -> &String {
150        &self.platform
151    }
152
153    pub fn system_version(&self) -> &String {
154        &self.system_version
155    }
156
157    pub fn log_in_date(&self) -> i32 {
158        self.log_in_date
159    }
160
161    pub fn last_active_date(&self) -> i32 {
162        self.last_active_date
163    }
164
165    pub fn ip(&self) -> &String {
166        &self.ip
167    }
168
169    pub fn country(&self) -> &String {
170        &self.country
171    }
172
173    pub fn region(&self) -> &String {
174        &self.region
175    }
176}
177
178#[doc(hidden)]
179pub struct SessionBuilder {
180    inner: Session,
181}
182
183#[deprecated]
184pub type RTDSessionBuilder = SessionBuilder;
185
186impl SessionBuilder {
187    pub fn build(&self) -> Session {
188        self.inner.clone()
189    }
190
191    pub fn id(&mut self, id: i64) -> &mut Self {
192        self.inner.id = id;
193        self
194    }
195
196    pub fn is_current(&mut self, is_current: bool) -> &mut Self {
197        self.inner.is_current = is_current;
198        self
199    }
200
201    pub fn is_password_pending(&mut self, is_password_pending: bool) -> &mut Self {
202        self.inner.is_password_pending = is_password_pending;
203        self
204    }
205
206    pub fn can_accept_secret_chats(&mut self, can_accept_secret_chats: bool) -> &mut Self {
207        self.inner.can_accept_secret_chats = can_accept_secret_chats;
208        self
209    }
210
211    pub fn can_accept_calls(&mut self, can_accept_calls: bool) -> &mut Self {
212        self.inner.can_accept_calls = can_accept_calls;
213        self
214    }
215
216    pub fn api_id(&mut self, api_id: i32) -> &mut Self {
217        self.inner.api_id = api_id;
218        self
219    }
220
221    pub fn application_name<T: AsRef<str>>(&mut self, application_name: T) -> &mut Self {
222        self.inner.application_name = application_name.as_ref().to_string();
223        self
224    }
225
226    pub fn application_version<T: AsRef<str>>(&mut self, application_version: T) -> &mut Self {
227        self.inner.application_version = application_version.as_ref().to_string();
228        self
229    }
230
231    pub fn is_official_application(&mut self, is_official_application: bool) -> &mut Self {
232        self.inner.is_official_application = is_official_application;
233        self
234    }
235
236    pub fn device_model<T: AsRef<str>>(&mut self, device_model: T) -> &mut Self {
237        self.inner.device_model = device_model.as_ref().to_string();
238        self
239    }
240
241    pub fn platform<T: AsRef<str>>(&mut self, platform: T) -> &mut Self {
242        self.inner.platform = platform.as_ref().to_string();
243        self
244    }
245
246    pub fn system_version<T: AsRef<str>>(&mut self, system_version: T) -> &mut Self {
247        self.inner.system_version = system_version.as_ref().to_string();
248        self
249    }
250
251    pub fn log_in_date(&mut self, log_in_date: i32) -> &mut Self {
252        self.inner.log_in_date = log_in_date;
253        self
254    }
255
256    pub fn last_active_date(&mut self, last_active_date: i32) -> &mut Self {
257        self.inner.last_active_date = last_active_date;
258        self
259    }
260
261    pub fn ip<T: AsRef<str>>(&mut self, ip: T) -> &mut Self {
262        self.inner.ip = ip.as_ref().to_string();
263        self
264    }
265
266    pub fn country<T: AsRef<str>>(&mut self, country: T) -> &mut Self {
267        self.inner.country = country.as_ref().to_string();
268        self
269    }
270
271    pub fn region<T: AsRef<str>>(&mut self, region: T) -> &mut Self {
272        self.inner.region = region.as_ref().to_string();
273        self
274    }
275}
276
277impl AsRef<Session> for Session {
278    fn as_ref(&self) -> &Session {
279        self
280    }
281}
282
283impl AsRef<Session> for SessionBuilder {
284    fn as_ref(&self) -> &Session {
285        &self.inner
286    }
287}