grammers_session/storages/
memory.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
9use crate::defs::{ChannelState, DcOption, PeerId, PeerInfo, UpdateState, UpdatesState};
10use crate::{Session, SessionData};
11use std::sync::Mutex;
12
13/// In-memory session interface.
14///
15/// Does not actually offer direct ways to persist the state anywhere,
16/// so it should only be used in very few select cases.
17///
18/// Logging in has a very high cost in terms of flood wait errors,
19/// so the state really should be persisted by other means.
20#[derive(Default)]
21pub struct MemorySession(Mutex<SessionData>);
22
23impl From<SessionData> for MemorySession {
24    /// Constructs a memory session from the entirety of the session data,
25    /// unlike the blanket `From` implementation which cannot import all values
26    fn from(session_data: SessionData) -> Self {
27        Self(Mutex::new(session_data))
28    }
29}
30
31impl Session for MemorySession {
32    fn home_dc_id(&self) -> i32 {
33        self.0.lock().unwrap().home_dc
34    }
35
36    fn set_home_dc_id(&self, dc_id: i32) {
37        self.0.lock().unwrap().home_dc = dc_id;
38    }
39
40    fn dc_option(&self, dc_id: i32) -> Option<DcOption> {
41        self.0.lock().unwrap().dc_options.get(&dc_id).cloned()
42    }
43
44    fn set_dc_option(&self, dc_option: &DcOption) {
45        self.0
46            .lock()
47            .unwrap()
48            .dc_options
49            .insert(dc_option.id, dc_option.clone());
50    }
51
52    fn peer(&self, peer: PeerId) -> Option<PeerInfo> {
53        self.0.lock().unwrap().peer_infos.get(&peer).cloned()
54    }
55
56    fn cache_peer(&self, peer: &PeerInfo) {
57        self.0
58            .lock()
59            .unwrap()
60            .peer_infos
61            .insert(peer.id(), peer.clone());
62    }
63
64    fn updates_state(&self) -> UpdatesState {
65        self.0.lock().unwrap().updates_state.clone()
66    }
67
68    fn set_update_state(&self, update: UpdateState) {
69        let mut data = self.0.lock().unwrap();
70
71        match update {
72            UpdateState::All(updates_state) => {
73                data.updates_state = updates_state;
74            }
75            UpdateState::Primary { pts, date, seq } => {
76                data.updates_state.pts = pts;
77                data.updates_state.date = date;
78                data.updates_state.seq = seq;
79            }
80            UpdateState::Secondary { qts } => {
81                data.updates_state.qts = qts;
82            }
83            UpdateState::Channel { id, pts } => {
84                data.updates_state.channels.retain(|c| c.id != id);
85                data.updates_state.channels.push(ChannelState { id, pts });
86            }
87        }
88    }
89}