ferogram 0.3.8

Production-grade async Telegram MTProto client: updates, bots, flood-wait, dialogs, messages
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
// Copyright (c) Ankit Chaubey <ankitchaubey.dev@gmail.com>
//
// ferogram: async Telegram MTProto client in Rust
// https://github.com/ankit-chaubey/ferogram
//
// Licensed under either the MIT License or the Apache License 2.0.
// See the LICENSE-MIT or LICENSE-APACHE file in this repository:
// https://github.com/ankit-chaubey/ferogram
//
// Feel free to use, modify, and share this code.
// Please keep this notice when redistributing.

// OVERVIEW
// This module implements the SINGLE UPDATE AUTHORITY for ferogram.
// Previously, ferogram scattered update-gap handling across:
//   - PtsState + PossibleGapBuffer (pts.rs)
//   - check_and_fill_gap / check_and_fill_channel_gap / check_and_fill_qts_gap (pts.rs)
//   - check_update_deadline (pts.rs)
//   - Hard-coded getDifference calls inside dispatch_updates (lib.rs)
// This caused overlapping recovery paths, races between concurrent diff calls,
// and PFS bind operations interfering with gap recovery.
//   - MessageBoxes is a PURE STATE MACHINE (no async, no RPC calls).
//   - ONE entry point: process_updates().
//   - The caller checks check_deadlines() to know when to force getDifference.
//   - The caller executes the RPC (get_difference / get_channel_difference)
//     and feeds the result back via apply_difference / apply_channel_difference.
//   - Gap buffering lives inside each LiveEntry (not in a separate global buffer).
//   - PFS / reconnect only interacts via UpdatesLike::ConnectionClosed.

mod adaptor;
pub mod defs;

#[cfg(test)]
mod tests;

use std::cmp::Ordering;
use std::time::Duration;

use defs::Instant; // real std::time::Instant under cfg(not(test)), fake_clock::Instant under cfg(test)

use defs::Key;
pub use defs::{ChannelState, Gap, MessageBoxes, UpdatesLike, UpdatesStateSnap};
use defs::{
    LiveEntry, NO_DATE, NO_PTS, NO_SEQ, POSSIBLE_GAP_TIMEOUT, PossibleGap, PtsInfo, UpdateAndPeers,
};
use ferogram_tl_types as tl;

// Helpers

fn next_updates_deadline() -> Instant {
    Instant::now() + defs::NO_UPDATES_TIMEOUT
}

fn update_sort_key(update: &tl::enums::Update) -> i32 {
    match PtsInfo::from_update(update) {
        Some(info) => info.pts - info.count,
        None => NO_PTS,
    }
}

// Creation and state management

impl Default for MessageBoxes {
    fn default() -> Self {
        Self::new()
    }
}

impl MessageBoxes {
    /// Create a new, empty [`MessageBoxes`] (no prior state).
    pub fn new() -> Self {
        tracing::trace!("[ferogram/msgbox] created new (no prior state)");
        Self {
            entries: Vec::new(),
            date: NO_DATE,
            seq: NO_SEQ,
            getting_diff_for: Vec::new(),
            next_deadline: next_updates_deadline(),
        }
    }

    /// Create a [`MessageBoxes`] from a previously-persisted snapshot.
    pub fn load(state: UpdatesStateSnap) -> Self {
        tracing::trace!("[ferogram/msgbox] loaded from state: {:?}", state);
        let mut entries = Vec::with_capacity(2 + state.channels.len());
        let mut getting_diff_for = Vec::with_capacity(2 + state.channels.len());
        let deadline = next_updates_deadline();

        if state.pts != NO_PTS {
            entries.push(LiveEntry {
                key: Key::Common,
                pts: state.pts,
                deadline,
                possible_gap: None,
            });
        }
        if state.qts != NO_PTS {
            entries.push(LiveEntry {
                key: Key::Secondary,
                pts: state.qts,
                deadline,
                possible_gap: None,
            });
        }
        entries.extend(state.channels.iter().map(|c| LiveEntry {
            key: Key::Channel(c.id),
            pts: c.pts,
            deadline,
            possible_gap: None,
        }));
        entries.sort_by_key(|e| e.key);

        // On load we need to reconcile; mark all entries as needing diff.
        getting_diff_for.extend(entries.iter().map(|e| e.key));

        Self {
            entries,
            date: state.date,
            seq: state.seq,
            getting_diff_for,
            next_deadline: deadline,
        }
    }

    fn entry(&self, key: Key) -> Option<&LiveEntry> {
        self.entries
            .binary_search_by_key(&key, |e| e.key)
            .map(|i| &self.entries[i])
            .ok()
    }

    fn update_entry(&mut self, key: Key, f: impl FnOnce(&mut LiveEntry)) -> bool {
        match self.entries.binary_search_by_key(&key, |e| e.key) {
            Ok(i) => {
                f(&mut self.entries[i]);
                true
            }
            Err(_) => false,
        }
    }

    fn force_update_entry(&mut self, mut entry: LiveEntry, f: impl FnOnce(&mut LiveEntry)) {
        match self.entries.binary_search_by_key(&entry.key, |e| e.key) {
            Ok(i) => f(&mut self.entries[i]),
            Err(i) => {
                f(&mut entry);
                self.entries.insert(i, entry);
            }
        }
    }

    fn set_entry(&mut self, entry: LiveEntry) {
        match self.entries.binary_search_by_key(&entry.key, |e| e.key) {
            Ok(i) => self.entries[i] = entry,
            Err(i) => self.entries.insert(i, entry),
        }
    }

    fn set_pts(&mut self, key: Key, pts: i32) {
        if !self.update_entry(key, |e| e.pts = pts) {
            self.set_entry(LiveEntry {
                key,
                pts,
                deadline: next_updates_deadline(),
                possible_gap: None,
            });
        }
    }

    fn pop_entry(&mut self, key: Key) -> Option<LiveEntry> {
        match self.entries.binary_search_by_key(&key, |e| e.key) {
            Ok(i) => Some(self.entries.remove(i)),
            Err(_) => None,
        }
    }

    fn reset_deadline(&mut self, key: Key, deadline: Instant) {
        let mut old_deadline = self.next_deadline;
        self.update_entry(key, |e| {
            old_deadline = e.deadline;
            e.deadline = deadline;
        });
        if self.next_deadline == old_deadline {
            self.next_deadline = self
                .entries
                .iter()
                .fold(deadline, |d, e| d.min(e.effective_deadline()));
        }
    }

    fn reset_timeout(&mut self, key: Key, timeout: Option<i32>) {
        self.reset_deadline(
            key,
            timeout
                .map(|t| Instant::now() + Duration::from_secs(t as _))
                .unwrap_or_else(next_updates_deadline),
        );
    }

    fn try_begin_get_diff(&mut self, key: Key) {
        if !self.getting_diff_for.contains(&key) {
            if self.update_entry(key, |e| e.possible_gap = None) {
                self.getting_diff_for.push(key);
            } else {
                tracing::info!(
                    "[ferogram/msgbox] tried begin_get_diff but no entry for {:?}",
                    key
                );
            }
        }
    }

    fn try_end_get_diff(&mut self, key: Key) {
        let i = match self.getting_diff_for.iter().position(|&k| k == key) {
            Some(i) => i,
            None => return,
        };
        self.getting_diff_for.remove(i);
        self.reset_deadline(key, next_updates_deadline());
        debug_assert!(
            self.entry(key).is_none_or(|e| e.possible_gap.is_none()),
            "gaps shouldn't be created while getting difference"
        );
    }

    /// Whether this box has any state yet.
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Set state right after login (must only call when [`is_empty`] is true).
    pub fn set_state(&mut self, state: tl::types::updates::State) {
        debug_assert!(self.is_empty());
        let deadline = next_updates_deadline();
        self.set_entry(LiveEntry {
            key: Key::Common,
            pts: state.pts,
            deadline,
            possible_gap: None,
        });
        self.set_entry(LiveEntry {
            key: Key::Secondary,
            pts: state.qts,
            deadline,
            possible_gap: None,
        });
        self.date = state.date;
        self.seq = state.seq;
        self.next_deadline = deadline;
    }

    /// Record channel pts from `GetDialogs` - does nothing if the channel is already tracked.
    pub fn try_set_channel_state(&mut self, id: i64, pts: i32) {
        if self.entry(Key::Channel(id)).is_none() {
            self.set_entry(LiveEntry {
                key: Key::Channel(id),
                pts,
                deadline: next_updates_deadline(),
                possible_gap: None,
            });
        }
    }

    /// Current snapshot suitable for session persistence.
    pub fn session_state(&self) -> UpdatesStateSnap {
        UpdatesStateSnap {
            pts: self.entry(Key::Common).map(|e| e.pts).unwrap_or(NO_PTS),
            qts: self.entry(Key::Secondary).map(|e| e.pts).unwrap_or(NO_PTS),
            date: self.date,
            seq: self.seq,
            channels: self
                .entries
                .iter()
                .filter_map(|e| match e.key {
                    Key::Channel(id) => Some(ChannelState { id, pts: e.pts }),
                    _ => None,
                })
                .collect(),
        }
    }

    /// Return the next deadline instant.
    ///
    /// Call this in the select! loop to know when to wake up and check for
    /// expired gaps or timeouts.  When entries need diff, returns `now` so the
    /// caller immediately runs the diff.
    pub fn check_deadlines(&mut self) -> Instant {
        let now = Instant::now();

        if !self.getting_diff_for.is_empty() {
            return now; // fire the deadline arm immediately when diff is pending
        }

        if now >= self.next_deadline {
            // Mark entries whose deadlines have elapsed.
            self.getting_diff_for
                .extend(self.entries.iter().filter_map(|e| {
                    if now >= e.effective_deadline() {
                        tracing::debug!(
                            "[ferogram/msgbox] deadline met for {:?}; forcing diff",
                            e.key
                        );
                        Some(e.key)
                    } else {
                        None
                    }
                }));

            // Clear possible-gaps for entries we're about to diff.
            for i in 0..self.getting_diff_for.len() {
                self.update_entry(self.getting_diff_for[i], |e| e.possible_gap = None);
            }

            if self.getting_diff_for.is_empty() {
                self.next_deadline = next_updates_deadline();
            }
        }

        self.next_deadline
    }

    /// Return the `GetDifference` request to execute, if any.
    ///
    /// The caller is responsible for executing the RPC and calling
    /// [`apply_difference`] with the result.
    pub fn get_difference(&self) -> Option<tl::functions::updates::GetDifference> {
        for key in [Key::Common, Key::Secondary] {
            if self.getting_diff_for.contains(&key) {
                let pts = self
                    .entry(Key::Common)
                    .map(|e| e.pts)
                    .expect("Common entry must exist when diffing it");

                return Some(tl::functions::updates::GetDifference {
                    pts,
                    pts_limit: None,
                    pts_total_limit: None,
                    date: self.date.max(1),
                    qts: self.entry(Key::Secondary).map(|e| e.pts).unwrap_or(NO_PTS),
                    qts_limit: None,
                });
            }
        }
        None
    }

    /// Return the skeleton of a `GetChannelDifference` request, if any channel needs it.
    ///
    /// The caller must fill in `access_hash` and `limit` before executing.
    /// Call [`apply_channel_difference`] or [`end_channel_difference`] with the result.
    pub fn get_channel_difference(
        &self,
    ) -> Option<(i64, tl::functions::updates::GetChannelDifference)> {
        let (key, channel_id) = self.getting_diff_for.iter().find_map(|&k| match k {
            Key::Channel(id) => Some((k, id)),
            _ => None,
        })?;

        let pts = self
            .entry(key)
            .map(|e| e.pts)
            .expect("Channel entry must exist when diffing it");

        Some((
            channel_id,
            tl::functions::updates::GetChannelDifference {
                force: false,
                channel: tl::enums::InputChannel::InputChannel(tl::types::InputChannel {
                    channel_id,
                    access_hash: 0, // caller must fill this in
                }),
                filter: tl::enums::ChannelMessagesFilter::Empty,
                pts,
                limit: 0, // caller must fill this in
            },
        ))
    }
}

// Normal update flow

impl MessageBoxes {
    /// Process an update batch.  Returns `Ok((updates, users, chats))` or
    /// `Err(Gap)` when a gap has been detected and `get_difference` must be called.
    ///
    /// This is the **single entry point** for all incoming updates.
    pub fn process_updates(&mut self, updates: UpdatesLike) -> Result<UpdateAndPeers, Gap> {
        let deadline = next_updates_deadline();

        let tl::types::UpdatesCombined {
            date,
            seq_start,
            seq,
            mut updates,
            users,
            chats,
        } = match adaptor::adapt(updates) {
            Ok(combined) => combined,
            Err(Gap) => {
                self.try_begin_get_diff(Key::Common);
                return Err(Gap);
            }
        };

        let new_date = if date == NO_DATE { self.date } else { date };
        let new_seq = if seq == NO_SEQ { self.seq } else { seq };

        // Check seq for Updates / UpdatesCombined containers.
        if seq_start != NO_SEQ {
            match (self.seq + 1).cmp(&seq_start) {
                Ordering::Equal => {} // apply
                Ordering::Greater => {
                    tracing::debug!(
                        "[ferogram/msgbox] duplicate seq (local={}, remote={}), skipping",
                        self.seq,
                        seq_start
                    );
                    return Ok((Vec::new(), users, chats));
                }
                Ordering::Less => {
                    tracing::debug!(
                        "[ferogram/msgbox] seq gap (local={}, remote={})",
                        self.seq,
                        seq_start
                    );
                    self.try_begin_get_diff(Key::Common);
                    return Err(Gap);
                }
            }
        }

        // Sort so out-of-order updates within a container are applied in pts order.
        updates.sort_by_key(update_sort_key);

        let mut result: Vec<tl::enums::Update> = Vec::with_capacity(updates.len());
        let mut have_unresolved_gaps = false;

        for update in updates {
            // ChannelTooLong is handled specially.
            if let tl::enums::Update::ChannelTooLong(ref u) = update {
                let key = Key::Channel(u.channel_id);
                if let Some(pts) = u.pts {
                    self.set_entry(LiveEntry {
                        key,
                        pts,
                        deadline,
                        possible_gap: None,
                    });
                }
                self.try_begin_get_diff(key);
                continue;
            }

            let info = match PtsInfo::from_update(&update) {
                Some(info) => info,
                None => {
                    // No pts: can be applied in any order.
                    result.push(update);
                    continue;
                }
            };

            // While getting diff for this key, ignore matching updates (they
            // will arrive again via apply_difference / apply_channel_difference).
            if self.getting_diff_for.contains(&info.key) {
                tracing::debug!(
                    "[ferogram/msgbox] ignoring update for {:?} (diff in flight)",
                    info.key
                );
                self.reset_deadline(info.key, next_updates_deadline());
                result.push(update);
                continue;
            }

            let mut gap_deadline = None;

            self.force_update_entry(
                LiveEntry {
                    key: info.key,
                    pts: info.pts - info.count,
                    deadline,
                    possible_gap: None,
                },
                |entry| {
                    match (entry.pts + info.count).cmp(&info.pts) {
                        Ordering::Equal => {
                            // Normal in-order update.
                            entry.pts = info.pts;
                            entry.deadline = deadline;
                            result.push(update);
                        }
                        Ordering::Greater => {
                            // Duplicate.
                            tracing::debug!(
                                "[ferogram/msgbox] duplicate update for {:?} \
                                 (local={}, count={}, remote={})",
                                info.key,
                                entry.pts,
                                info.count,
                                info.pts
                            );
                        }
                        Ordering::Less => {
                            // Gap: buffer and wait.
                            tracing::info!(
                                "[ferogram/msgbox] gap for {:?} \
                                 (local={}, count={}, remote={})",
                                info.key,
                                entry.pts,
                                info.count,
                                info.pts
                            );
                            entry
                                .possible_gap
                                .get_or_insert_with(|| PossibleGap {
                                    deadline: Instant::now() + POSSIBLE_GAP_TIMEOUT,
                                    updates: Vec::new(),
                                })
                                .updates
                                .push(update.clone());
                        }
                    }

                    // Try to drain the possible_gap buffer now that pts advanced.
                    if let Some(mut gap) = entry.possible_gap.take() {
                        gap.updates.sort_by_key(|u| -update_sort_key(u));
                        while let Some(gap_update) = gap.updates.pop() {
                            let gap_info = PtsInfo::from_update(&gap_update)
                                .expect("only updates with pts may be buffered as gaps");
                            match (entry.pts + gap_info.count).cmp(&gap_info.pts) {
                                Ordering::Equal => {
                                    entry.pts = gap_info.pts;
                                    result.push(gap_update);
                                }
                                Ordering::Greater => {}
                                Ordering::Less => {
                                    gap.updates.push(gap_update);
                                    break;
                                }
                            }
                        }
                        if !gap.updates.is_empty() {
                            gap_deadline = Some(gap.deadline);
                            entry.possible_gap = Some(gap);
                            have_unresolved_gaps = true;
                        }
                    }
                },
            );

            self.next_deadline = self.next_deadline.min(gap_deadline.unwrap_or(deadline));
        }

        if !result.is_empty() && !have_unresolved_gaps {
            self.date = new_date;
            self.seq = new_seq;
        }

        Ok((result, users, chats))
    }
}

// Applying getDifference results

impl MessageBoxes {
    /// Apply the result of a `GetDifference` RPC call.
    pub fn apply_difference(
        &mut self,
        difference: tl::enums::updates::Difference,
    ) -> UpdateAndPeers {
        tracing::trace!("[ferogram/msgbox] applying account difference");
        if !self.getting_diff_for.contains(&Key::Common)
            && !self.getting_diff_for.contains(&Key::Secondary)
        {
            tracing::warn!(
                "[ferogram/msgbox] apply_difference called but no diff was pending \
                 (concurrent call already completed?); ignoring"
            );
            return (Vec::new(), Vec::new(), Vec::new());
        }

        let finish: bool;
        let result = match difference {
            tl::enums::updates::Difference::Empty(e) => {
                tracing::debug!(
                    "[ferogram/msgbox] difference empty (date={}, seq={})",
                    e.date,
                    e.seq
                );
                finish = true;
                self.date = e.date;
                self.seq = e.seq;
                (Vec::new(), Vec::new(), Vec::new())
            }
            tl::enums::updates::Difference::Difference(d) => {
                tracing::debug!("[ferogram/msgbox] full difference");
                finish = true;
                self.apply_difference_type(d)
            }
            tl::enums::updates::Difference::Slice(tl::types::updates::DifferenceSlice {
                new_messages,
                new_encrypted_messages,
                other_updates,
                chats,
                users,
                intermediate_state: state,
            }) => {
                tracing::debug!("[ferogram/msgbox] slice difference");
                finish = false;
                self.apply_difference_type(tl::types::updates::Difference {
                    new_messages,
                    new_encrypted_messages,
                    other_updates,
                    chats,
                    users,
                    state,
                })
            }
            tl::enums::updates::Difference::TooLong(d) => {
                tracing::warn!("[ferogram/msgbox] difference TooLong (pts={})", d.pts);
                finish = true;
                self.set_pts(Key::Common, d.pts);
                (Vec::new(), Vec::new(), Vec::new())
            }
        };

        if finish {
            self.try_end_get_diff(Key::Common);
            self.try_end_get_diff(Key::Secondary);
        }

        result
    }

    fn apply_difference_type(
        &mut self,
        tl::types::updates::Difference {
            new_messages,
            new_encrypted_messages,
            other_updates: updates,
            chats,
            users,
            state: tl::enums::updates::State::State(state),
        }: tl::types::updates::Difference,
    ) -> UpdateAndPeers {
        self.date = state.date;
        self.seq = state.seq;
        self.set_pts(Key::Common, state.pts);
        self.set_pts(Key::Secondary, state.qts);

        // Process other_updates through the normal path (handles ChannelTooLong etc).
        let us = UpdatesLike::Updates(Box::new(tl::enums::Updates::Updates(tl::types::Updates {
            updates,
            users,
            chats,
            date: NO_DATE,
            seq: NO_SEQ,
        })));
        let (mut result_updates, users, chats) = self
            .process_updates(us)
            .expect("gap detected while applying difference - should not happen");

        // Prepend new_messages as UpdateNewMessage with NO_PTS so they bypass pts checks.
        let msgs: Vec<tl::enums::Update> = new_messages
            .into_iter()
            .map(|msg| {
                tl::enums::Update::NewMessage(tl::types::UpdateNewMessage {
                    message: msg,
                    pts: NO_PTS,
                    pts_count: 0,
                })
            })
            .chain(new_encrypted_messages.into_iter().map(|msg| {
                tl::enums::Update::NewEncryptedMessage(tl::types::UpdateNewEncryptedMessage {
                    message: msg,
                    qts: NO_PTS,
                })
            }))
            .collect();

        result_updates.splice(0..0, msgs);
        (result_updates, users, chats)
    }
}

// Applying getChannelDifference results

impl MessageBoxes {
    /// Apply the result of a `GetChannelDifference` RPC call.
    pub fn apply_channel_difference(
        &mut self,
        difference: tl::enums::updates::ChannelDifference,
    ) -> UpdateAndPeers {
        let (key, channel_id) = self
            .getting_diff_for
            .iter()
            .find_map(|&k| match k {
                Key::Channel(id) => Some((k, id)),
                _ => None,
            })
            .expect("apply_channel_difference: no channel in getting_diff_for");

        tracing::trace!(
            "[ferogram/msgbox] applying channel {} difference",
            channel_id
        );
        self.update_entry(key, |e| e.possible_gap = None);

        let tl::types::updates::ChannelDifference {
            r#final,
            pts,
            timeout,
            new_messages,
            other_updates: updates,
            chats,
            users,
        } = adaptor::adapt_channel_difference(difference);

        if r#final {
            tracing::debug!(
                "[ferogram/msgbox] channel {} diff final; no longer getting diff",
                channel_id
            );
            self.try_end_get_diff(key);
        } else {
            tracing::debug!("[ferogram/msgbox] channel {} diff slice", channel_id);
        }

        self.set_pts(key, pts);

        let us = UpdatesLike::Updates(Box::new(tl::enums::Updates::Updates(tl::types::Updates {
            updates,
            users,
            chats,
            date: NO_DATE,
            seq: NO_SEQ,
        })));
        let (mut result_updates, users, chats) = self
            .process_updates(us)
            .expect("gap detected while applying channel difference");

        // Prepend new_messages.
        let msgs: Vec<tl::enums::Update> = new_messages
            .into_iter()
            .map(|msg| {
                tl::enums::Update::NewChannelMessage(tl::types::UpdateNewChannelMessage {
                    message: msg,
                    pts: NO_PTS,
                    pts_count: 0,
                })
            })
            .collect();
        result_updates.splice(0..0, msgs);

        self.reset_timeout(key, timeout);

        (result_updates, users, chats)
    }

    /// Mark a channel diff as ended prematurely (access error or ban).
    /// Abort a pending global difference after a parse or RPC failure.
    ///
    /// Clears `Key::Common` and `Key::Secondary` from `getting_diff_for` so
    /// that `check_deadlines()` stops returning `Instant::now()` and the
    /// deadline arm stops spawning hundreds of no-op tasks while the previous
    /// attempt's backoff sleep is still running.
    ///
    /// The pts counters are left unchanged; the next real update gap will
    /// re-queue getDifference automatically.
    pub fn abort_difference(&mut self) {
        for key in [Key::Common, Key::Secondary] {
            self.update_entry(key, |e| e.possible_gap = None);
            self.try_end_get_diff(key);
        }
        tracing::debug!(
            "[ferogram/msgbox] abort_difference: cleared Common+Secondary diff pending"
        );
    }

    /// Forcibly advance Common + Secondary pts to the server-reported values.
    ///
    /// Called after a getDifference parse failure (unknown TL constructor from a
    /// newer Telegram layer) so the stale pts gap does not re-trigger getDifference
    /// in an infinite loop.  Channel entries are left unchanged.
    pub fn force_reset_common_pts(&mut self, pts: i32, qts: i32, date: i32, seq: i32) {
        self.set_pts(Key::Common, pts);
        self.set_pts(Key::Secondary, qts);
        self.date = date;
        self.seq = seq;
        tracing::debug!(
            "[ferogram/msgbox] force_reset_common_pts: pts={pts}, qts={qts}, seq={seq}"
        );
    }

    pub fn end_channel_difference(&mut self, reason: PrematureEndReason) {
        let Some((key, channel_id)) = self.getting_diff_for.iter().find_map(|&k| match k {
            Key::Channel(id) => Some((k, id)),
            _ => None,
        }) else {
            tracing::warn!(
                "[ferogram/msgbox] end_channel_difference called but no channel pending \
                 (already ended? duplicate error path)"
            );
            return;
        };

        tracing::trace!(
            "[ferogram/msgbox] ending channel {} diff: {:?}",
            channel_id,
            reason
        );

        match reason {
            PrematureEndReason::TemporaryServerIssues => {
                self.update_entry(key, |e| e.possible_gap = None);
                self.try_end_get_diff(key);
            }
            PrematureEndReason::Banned => {
                self.update_entry(key, |e| e.possible_gap = None);
                self.try_end_get_diff(key);
                self.pop_entry(key);
            }
        }
    }
}

/// Reason for calling [`MessageBoxes::end_channel_difference`].
#[derive(Debug)]
pub enum PrematureEndReason {
    /// Temporary failure; keep the entry and retry later.
    TemporaryServerIssues,
    /// The account has been banned; remove the entry permanently.
    Banned,
}