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
//! QR pairing lifecycle: broadcast + replay, fresh-pairing restart and the
//! one-shot first-pair flag that gates the connection greeting.
//!
//! The QR channel is a plain broadcast with no replay, so the last code is
//! remembered and replayed to late subscribers (the "press Enter twice"
//! bug). Once connected the QR is locked so a stale code never reappears in
//! the onboarding UI.
use std::sync::atomic::Ordering;
use super::WhatsAppState;
impl WhatsAppState {
/// Broadcast a QR code to any subscribed onboarding UI, and remember it so
/// a subscriber that joins after this point can replay it immediately.
///
/// No-op once connected: after pairing succeeds the QR is locked, so a late
/// or stale QR event can never reappear in the onboarding UI.
pub fn broadcast_qr(&self, code: &str) {
if self.connected.load(Ordering::SeqCst) {
tracing::debug!("WhatsApp: suppressing QR broadcast — already connected");
return;
}
*self.last_qr.lock().unwrap_or_else(|e| e.into_inner()) = Some(code.to_string());
let _ = self.qr_tx.send(code.to_string());
}
/// The most recently broadcast QR, if any. Replayed to a new subscriber so
/// it does not have to wait for the next refresh (fixes the "Enter twice"
/// race). Cleared on [`Self::request_restart`] and on connect.
pub fn current_qr(&self) -> Option<String> {
self.last_qr
.lock()
.unwrap_or_else(|e| e.into_inner())
.clone()
}
/// Request a fresh pairing: the next `reconcile_whatsapp` aborts the live
/// agent and starts a new one against the wiped session. Clears the stored
/// QR so the stale one is never replayed, and clears the connected flag so
/// a new QR can be broadcast again for the re-pairing.
pub fn request_restart(&self) {
*self.last_qr.lock().unwrap_or_else(|e| e.into_inner()) = None;
self.connected.store(false, Ordering::SeqCst);
self.restart_requested.store(true, Ordering::SeqCst);
}
/// Consume the restart request (returns whether one was pending).
pub fn take_restart_request(&self) -> bool {
self.restart_requested.swap(false, Ordering::SeqCst)
}
/// Mark that a fresh pairing just succeeded. The next `Connected` event
/// will fire the one-time connection greeting.
pub fn set_first_pair_pending(&self) {
self.first_pair_pending.store(true, Ordering::SeqCst);
}
/// Consume the first-pair flag. Returns `true` exactly once per pairing
/// (first-time or re-pair after reset), then resets to `false` so a plain
/// app restart never re-triggers the greeting.
pub fn take_first_pair_pending(&self) -> bool {
self.first_pair_pending.swap(false, Ordering::SeqCst)
}
}