grammers_session/defs.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//!
11//! <div class="warning">This module will be renamed to "types" in a future release.</div>
12
13use std::net::{SocketAddrV4, SocketAddrV6};
14
15pub use crate::peer::{ChannelKind, PeerAuth, PeerId, PeerInfo, PeerKind, PeerRef};
16
17/// A datacenter option.
18///
19/// This is very similar to Telegram's own `dcOption` type, except it also
20/// contains the permanent authentication key and serves as a stable interface.
21#[derive(Clone, Debug, PartialEq, Eq)]
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 pub auth_key: Option<[u8; 256]>,
40}
41
42/// Full update state needed to process updates in order without gaps.
43#[derive(Clone, Debug, Default, PartialEq, Eq)]
44pub struct UpdatesState {
45 /// Primary persistent timestamp value.
46 pub pts: i32,
47 /// Secondary persistent timestamp value.
48 pub qts: i32,
49 /// Auxiliary date value.
50 pub date: i32,
51 /// Auxiliary sequence value.
52 pub seq: i32,
53 /// Persistent timestamp of each known channel.
54 pub channels: Vec<ChannelState>,
55}
56
57/// Update state for a single channel.
58#[derive(Clone, Debug, PartialEq, Eq)]
59pub struct ChannelState {
60 /// The [`PeerId::bare_id`] of the channel.
61 pub id: i64,
62 /// Persistent timestamp value.
63 pub pts: i32,
64}
65
66/// Used in [`crate::Session::set_update_state`] to update parts of the overall [`UpdatesState`].
67pub enum UpdateState {
68 /// Updates the entirety of the state.
69 All(UpdatesState),
70 /// Updates only what's known as the "primary" state of the account.
71 Primary {
72 /// New [`UpdatesState::pts`] value.
73 pts: i32,
74 /// New [`UpdatesState::date`] value.
75 date: i32,
76 /// New [`UpdatesState::seq`] value.
77 seq: i32,
78 },
79 /// Updates only what's known as the "secondary" state of the account.
80 Secondary {
81 /// New [`UpdatesState::qts`] value.
82 qts: i32,
83 },
84 /// Updates the state of a single channel.
85 Channel {
86 /// The [`PeerId::bare_id`] of the channel.
87 id: i64,
88 /// New [`ChannelState::pts`] value.
89 pts: i32,
90 },
91}