use std::time::Duration;
#[cfg(not(test))]
pub(super) use std::time::Instant;
#[cfg(test)]
pub(super) mod fake_clock {
use std::cell::RefCell;
use std::ops::Add;
use std::time::Duration;
thread_local! {
static NOW: RefCell<Duration> = const { RefCell::new(Duration::ZERO) };
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Instant(pub Duration);
impl Instant {
pub fn now() -> Self {
NOW.with_borrow(|d| Self(*d))
}
pub fn elapsed_secs(&self) -> u64 {
self.0.as_secs()
}
}
impl Add<Duration> for Instant {
type Output = Instant;
fn add(self, rhs: Duration) -> Instant {
Instant(self.0 + rhs)
}
}
pub fn reset_time() {
NOW.with_borrow_mut(|d| *d = Duration::ZERO);
}
pub fn advance_time_by(dur: Duration) {
NOW.with_borrow_mut(|d| *d += dur);
}
}
#[cfg(test)]
pub(super) use fake_clock::Instant;
#[cfg(test)]
impl From<fake_clock::Instant> for tokio::time::Instant {
fn from(i: fake_clock::Instant) -> Self {
tokio::time::Instant::now()
.checked_add(i.0)
.unwrap_or_else(tokio::time::Instant::now)
}
}
use ferogram_tl_types as tl;
pub(super) const NO_SEQ: i32 = 0;
pub(super) const NO_PTS: i32 = 0;
pub(super) const NO_DATE: i32 = 0;
pub(super) const POSSIBLE_GAP_TIMEOUT: Duration = Duration::from_millis(500);
pub(super) const NO_UPDATES_TIMEOUT: Duration = Duration::from_secs(15 * 60);
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) enum Key {
Common,
Secondary,
Channel(i64),
}
#[derive(Debug)]
pub(super) struct LiveEntry {
pub(super) key: Key,
pub(super) pts: i32,
pub(super) deadline: Instant,
pub(super) possible_gap: Option<PossibleGap>,
}
impl LiveEntry {
pub(super) fn effective_deadline(&self) -> Instant {
match &self.possible_gap {
Some(gap) => gap.deadline.min(self.deadline),
None => self.deadline,
}
}
}
#[derive(Debug)]
pub(super) struct PossibleGap {
pub(super) deadline: Instant,
pub(super) updates: Vec<tl::enums::Update>,
}
#[derive(Debug)]
pub struct MessageBoxes {
pub(super) entries: Vec<LiveEntry>,
pub(super) date: i32,
pub(super) seq: i32,
pub(super) getting_diff_for: Vec<Key>,
pub(super) next_deadline: Instant,
}
#[derive(Debug)]
pub(super) struct PtsInfo {
pub(super) key: Key,
pub(super) pts: i32,
pub(super) count: i32,
}
#[derive(Debug, PartialEq, Eq)]
pub struct Gap;
#[derive(Debug)]
pub enum UpdatesLike {
Updates(Box<tl::enums::Updates>),
ConnectionClosed,
MalformedUpdates,
AffectedMessages(tl::types::messages::AffectedMessages),
AffectedChannelMessages {
affected: tl::types::messages::AffectedMessages,
channel_id: i64,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ChannelState {
pub id: i64,
pub pts: i32,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct UpdatesStateSnap {
pub pts: i32,
pub qts: i32,
pub date: i32,
pub seq: i32,
pub channels: Vec<ChannelState>,
}
pub(super) type UpdateAndPeers = (
Vec<tl::enums::Update>,
Vec<tl::enums::User>,
Vec<tl::enums::Chat>,
);