Skip to main content

grammers_session/
types.rs

1// Copyright 2020 - developers of the `grammers` project.
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Session type definitions.
10
11use std::net::{SocketAddrV4, SocketAddrV6};
12
13pub use crate::peer::{ChannelKind, PeerAuth, PeerId, PeerInfo, PeerKind, PeerRef};
14
15/// A datacenter option.
16///
17/// This is very similar to Telegram's own `dcOption` type, except it also
18/// contains the permanent authentication key and serves as a stable interface.
19#[derive(Clone, Debug, PartialEq, Eq)]
20#[cfg_attr(feature = "serde", serde_with::serde_as)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22pub struct DcOption {
23    /// Datacenter identifier.
24    ///
25    /// The primary datacenters have IDs from 1 to 5 inclusive, and are known statically by the session.
26    /// ```
27    /// let data = grammers_session::SessionData::default();
28    /// assert_eq!(data.dc_options.len(), 5);
29    /// (1..=5).for_each(|dc_id| assert!(data.dc_options.contains_key(&dc_id)));
30    /// ```
31    pub id: i32,
32    /// IPv4 address corresponding to this datacenter.
33    pub ipv4: SocketAddrV4,
34    /// IPv6 address corresponding to this datacenter. May actually be embedding the [`Self::ipv4`] address.
35    pub ipv6: SocketAddrV6,
36    /// Permanent authentication key generated for encrypted communication with this datacenter.
37    ///
38    /// A logged-in user may or not be bound to this authentication key.
39    #[cfg(not(feature = "serde"))]
40    pub auth_key: Option<[u8; 256]>,
41
42    #[cfg(feature = "serde")]
43    #[serde_as(as = "Option<serde_with::hex::Hex>")]
44    pub auth_key: Option<[u8; 256]>,
45}
46
47/// Full update state needed to process updates in order without gaps.
48#[derive(Clone, Debug, Default, PartialEq, Eq)]
49#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
50pub struct UpdatesState {
51    /// Primary persistent timestamp value.
52    pub pts: i32,
53    /// Secondary persistent timestamp value.
54    pub qts: i32,
55    /// Auxiliary date value.
56    pub date: i32,
57    /// Auxiliary sequence value.
58    pub seq: i32,
59    /// Persistent timestamp of each known channel.
60    pub channels: Vec<ChannelState>,
61}
62
63/// Update state for a single channel.
64#[derive(Clone, Debug, PartialEq, Eq)]
65#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
66pub struct ChannelState {
67    /// The [`PeerId::bare_id`] of the channel.
68    pub id: i64,
69    /// Persistent timestamp value.
70    pub pts: i32,
71}
72
73/// Used in [`crate::Session::set_update_state`] to update parts of the overall [`UpdatesState`].
74#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
75pub enum UpdateState {
76    /// Updates the entirety of the state.
77    All(UpdatesState),
78    /// Updates only what's known as the "primary" state of the account.
79    Primary {
80        /// New [`UpdatesState::pts`] value.
81        pts: i32,
82        /// New [`UpdatesState::date`] value.
83        date: i32,
84        /// New [`UpdatesState::seq`] value.
85        seq: i32,
86    },
87    /// Updates only what's known as the "secondary" state of the account.
88    Secondary {
89        /// New [`UpdatesState::qts`] value.
90        qts: i32,
91    },
92    /// Updates the state of a single channel.
93    Channel {
94        /// The [`PeerId::bare_id`] of the channel.
95        id: i64,
96        /// New [`ChannelState::pts`] value.
97        pts: i32,
98    },
99}