sew-call-client 0.9.6

Sew's native voice engine: LiveKit-protocol signalling and reconnect against Sew's media server, str0m transport with TURN over UDP, TCP and TLS (relay-only when asked), MLS call keys with the mandatory E2EE checks over the call credential and the call socket, the device store, and the kessel-audio bridge.
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
//! [`MlsKeys`]: the engine's real E2EE provider, and the default one (`CallClient::with_mls`).
//! It fills the engine's E2EE interface ([`E2eeProvider`] / [`KeySession`]) with the key session:
//!
//! - **start**: the call socket with the call credential (READY: `session_id`, the install id and
//!   the trust anchors, TOFU-pinned), the device (loaded or enrolled), a KeyPackage for
//!   `call:<channel>` (uploaded), then the deferred key join with the credential's `join_id`. A
//!   host that holds its own Sew session instead of a call credential prepares the key join before
//!   its voice join ([`MlsKeys::prepare_session_join`], carried inline in the voice join) and the
//!   answer's `e2ee` starts the session.
//! - **frames**: [`KeySession::encrypt`] is the [`SendGate`] (sealed with
//!   sew-lk-compat's LkGcm under the active slot, or dropped), [`KeySession::decrypt`] is the
//!   [`RecvGate`] (verified, subscribed senders only), [`KeySession::may_subscribe`] is the
//!   [`SubscribeGuard`], and the publication counts only once the server echoed GCM
//!   ([`PublishGuard`], [`KeySession::published`]).
//! - **events** to the engine: `Active{epoch}` when a key epoch is active and the local sender
//!   has its key, `Members` from the verified roster, `VoiceStates`, `ServerUpdated` and
//!   `Recording` from the call socket, `Warning` (`key_storage_file`, `pins_reset`), `Refused`
//!   and `CredentialLost`.
//! - **recording**: the engine's [`KeySession::recording`] reaches the key session
//!   ([`MlsSession::set_recording`]) in order, through the bridge.
//! - **renewal**: the engine renews the credential; each new token reaches the key routes and
//!   the call socket's REAUTH here.

use std::collections::BTreeSet;
use std::fmt;
use std::sync::{Arc, Mutex};
use std::time::Duration;

use sew_e2ee::{JoinId, LeftReason, LkIdentity};
use sew_lkproto::livekit::{TrackInfo, TrackType};
use sew_proto::e2ee::{ConsentState, E2eeJoin, RemoveReason};
use sew_proto::id::Id;
use sew_proto::voice::VoiceStateUpdated;
use tokio::sync::{Notify, mpsc};

use super::credential::Bearer;
use super::guard::{
    PublishGuard, RecvGate, RecvRefused, SendGate, SubscribeDecision, SubscribeGuard,
};
use super::keys::{MediaKeys, lock};
use super::lkgcm::LkGcm;
use super::session::{KeyEnd, KeyError, KeySessionConfig, KeySessionEvent, MlsSession};
use super::{
    CredentialHandle, CryptoError, E2eeContext, E2eeError, E2eeEvent, E2eeEvents, E2eeProvider,
    KeySession, RemoteTrack, Subscribe, VideoFrameCodec,
};
use crate::Clock;
use crate::config::ClientConfig;
use crate::device::{DevicePlatform, StoreChoice, VaultConfig, default_platform, platform_name};
use crate::provider::BoxFuture;

/// How often the bridge re-checks the key state when nothing else happened.
const BRIDGE_TICK: Duration = Duration::from_millis(100);

fn refused(code: &str, detail: impl Into<String>) -> E2eeError {
    E2eeError {
        code: code.to_owned(),
        detail: detail.into(),
    }
}

fn from_key_error(e: &KeyError) -> E2eeError {
    refused(e.code(), e.to_string())
}

/// What a host with its own Sew session (not a call credential) gives
/// [`MlsKeys::prepare_session_join`].
#[derive(Clone)]
pub struct SessionJoin {
    /// Sew's client API (the host's door).
    pub api_base: url::Url,
    /// The host's Sew session token (`sews_…`); held in Rust memory only.
    pub bearer: String,
    /// A stable 32-hex scope for this login (the stored device is keyed on it).
    pub device_scope: String,
    /// The voice channel (or DM call channel) being joined.
    pub channel: Id,
}

impl fmt::Debug for SessionJoin {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SessionJoin")
            .field("api_base", &self.api_base.as_str())
            .field("bearer", &"<redacted>")
            .field("device_scope", &self.device_scope)
            .field("channel", &self.channel)
            .finish()
    }
}

struct Pending {
    session: MlsSession<LkGcm>,
    events: mpsc::UnboundedReceiver<KeySessionEvent>,
}

/// Sew's MLS call keys for the engine (the default [`E2eeProvider`]).
pub struct MlsKeys {
    store: StoreChoice,
    label: String,
    platform: DevicePlatform,
    clock: Clock,
    pending: Mutex<Option<Pending>>,
}

impl fmt::Debug for MlsKeys {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MlsKeys")
            .field("store", &self.store)
            .field("label", &self.label)
            .finish_non_exhaustive()
    }
}

impl Default for MlsKeys {
    fn default() -> MlsKeys {
        MlsKeys::new(StoreChoice::Auto)
    }
}

impl MlsKeys {
    /// Keys with the device key in `store` (`Auto`: a 0600 file with a `key_storage_file`
    /// warning, or with feature `keyring-store` the OS credential store when it answers;
    /// `Host`: the host's own [`crate::device::DeviceKeyStore`]).
    pub fn new(store: StoreChoice) -> MlsKeys {
        MlsKeys {
            store,
            label: format!("Sew voice on {}", platform_name()),
            platform: default_platform(),
            clock: crate::system_clock(),
            pending: Mutex::new(None),
        }
    }

    /// The device label Sew shows ("K2 on macOS").
    pub fn with_label(mut self, label: &str) -> MlsKeys {
        label.clone_into(&mut self.label);
        self
    }

    /// The platform the device enrols as (default from `target_os`: iOS, Android, otherwise
    /// desktop).
    pub fn with_platform(mut self, platform: DevicePlatform) -> MlsKeys {
        self.platform = platform;
        self
    }

    /// The wall clock the key session reads (tests run a server on a clock of their own).
    pub fn with_clock(mut self, clock: Clock) -> MlsKeys {
        self.clock = clock;
        self
    }

    fn vault(&self, app_id: &str, data_dir: &std::path::Path) -> VaultConfig {
        VaultConfig {
            data_dir: data_dir.to_path_buf(),
            app_id: app_id.to_owned(),
            store: self.store.clone(),
            label: self.label.clone(),
            platform: self.platform,
        }
    }

    /// For a host that joins with its own Sew session rather than a call credential (a
    /// desktop that holds a `sews_` session; the key join inline): opens the call socket, loads or
    /// enrols the device, uploads a KeyPackage for `join.channel` and answers the voice join's
    /// `e2ee` object. The host sends its join with it; the engine's next [`E2eeProvider::start`]
    /// with an answer that has `e2ee` and no `call_credential` takes this session over. One
    /// prepared join is held at a time (one call per app); a new one replaces it.
    pub async fn prepare_session_join(
        &self,
        cfg: &ClientConfig,
        join: SessionJoin,
    ) -> Result<E2eeJoin, E2eeError> {
        let tls = cfg
            .tls()
            .map_err(|e| refused("network", format!("tls: {e}")))?;
        let kcfg = KeySessionConfig {
            api_base: join.api_base.clone(),
            bearer: Bearer::new(join.bearer.clone()),
            credential_expires_at_ms: None,
            device_scope: join.device_scope.clone(),
            vault: self.vault(&cfg.app_id, &cfg.data_dir),
            install_id: None,
            clock: self.clock.clone(),
            tls: Some(tls),
        };
        let (session, events) = MlsSession::connect(kcfg, MediaKeys::new(LkGcm))
            .await
            .map_err(|e| from_key_error(&e))?;
        let e2ee = session
            .prepare_join(join.channel)
            .await
            .map_err(|e| from_key_error(&e))?;
        *lock(&self.pending) = Some(Pending { session, events });
        Ok(e2ee)
    }

    async fn start_keys(
        &self,
        ctx: E2eeContext,
        events: E2eeEvents,
    ) -> Result<Arc<dyn KeySession>, E2eeError> {
        let (session, rx) = match ctx.credential.current() {
            Some(cred) => {
                let join_id = JoinId::parse(&cred.join_id).map_err(|_| {
                    refused("e2ee_refused", "the call credential's join_id is malformed")
                })?;
                let kcfg = KeySessionConfig {
                    api_base: ctx.api_base.clone(),
                    bearer: Bearer::new(cred.token.clone()),
                    // The engine renews the credential; the bridge passes each new token on.
                    credential_expires_at_ms: None,
                    device_scope: cred.device_scope.clone(),
                    vault: self.vault(&ctx.app_id, &ctx.data_dir),
                    install_id: None,
                    clock: self.clock.clone(),
                    tls: Some(ctx.tls.clone()),
                };
                let (session, rx) = MlsSession::connect(kcfg, MediaKeys::new(LkGcm))
                    .await
                    .map_err(|e| from_key_error(&e))?;
                // The deferred key join: a KeyPackage for the call's channel, then the key join.
                session
                    .key_join(ctx.join.state.channel_id, join_id)
                    .await
                    .map_err(|e| from_key_error(&e))?;
                (session, rx)
            }
            None => {
                let pending = lock(&self.pending).take().ok_or_else(|| {
                    refused(
                        "e2ee_refused",
                        "the join answer has no call_credential and no session join was prepared",
                    )
                })?;
                let init = ctx.join.e2ee.clone().ok_or_else(|| {
                    refused(
                        "e2ee_refused",
                        "the join answer has neither call_credential nor e2ee",
                    )
                })?;
                pending
                    .session
                    .begin(init)
                    .await
                    .map_err(|e| from_key_error(&e))?;
                (pending.session, pending.events)
            }
        };
        let session = Arc::new(session);
        let keys = session.media_keys();
        let stop = Arc::new(Notify::new());
        let clock = self.clock.clone();
        let (recording_tx, recording_rx) = mpsc::unbounded_channel();
        let ks = Arc::new(MlsKeySession {
            session: session.clone(),
            recording: recording_tx,
            send: SendGate::new(&keys),
            recv: RecvGate::new(&keys, clock),
            subscribe: SubscribeGuard::new(&keys),
            publish: PublishGuard::new(&keys),
            keys,
            roster: Mutex::new(BTreeSet::new()),
            stop: stop.clone(),
        });
        let me = ctx.join.state.member_id;
        tokio::spawn(bridge(
            session,
            rx,
            events,
            ctx.credential.clone(),
            stop,
            me,
            recording_rx,
        ));
        Ok(ks)
    }
}

impl E2eeProvider for MlsKeys {
    fn start(
        &self,
        ctx: E2eeContext,
        events: E2eeEvents,
    ) -> BoxFuture<'_, Result<Arc<dyn KeySession>, E2eeError>> {
        Box::pin(self.start_keys(ctx, events))
    }
}

/// One join's keys as the engine's media path sees them.
struct MlsKeySession {
    session: Arc<MlsSession<LkGcm>>,
    /// The engine's recording state, to the bridge (applied in order).
    recording: mpsc::UnboundedSender<(Option<Id>, ConsentState)>,
    keys: MediaKeys<LkGcm>,
    send: SendGate<LkGcm>,
    recv: RecvGate<LkGcm>,
    subscribe: SubscribeGuard<LkGcm>,
    publish: PublishGuard<LkGcm>,
    roster: Mutex<BTreeSet<String>>,
    stop: Arc<Notify>,
}

impl fmt::Debug for MlsKeySession {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("MlsKeySession")
            .field("keys", &self.keys)
            .finish_non_exhaustive()
    }
}

impl KeySession for MlsKeySession {
    fn encrypt_video(
        &self,
        codec: VideoFrameCodec,
        frame: &[u8],
        ssrc: u32,
        rtp_ts: u32,
        out: &mut Vec<u8>,
    ) -> Result<(), CryptoError> {
        match self.send.seal_video(codec, ssrc, rtp_ts, frame) {
            Some(sealed) => {
                out.extend_from_slice(&sealed);
                Ok(())
            }
            None if self.keys.local_slot().is_none() => Err(CryptoError::NoSendKey),
            None => Err(CryptoError::BadFrame),
        }
    }

    fn encrypt(
        &self,
        frame: &[u8],
        ssrc: u32,
        rtp_ts: u32,
        out: &mut Vec<u8>,
    ) -> Result<(), CryptoError> {
        match self.send.seal(ssrc, rtp_ts, frame) {
            Some(sealed) => {
                out.extend_from_slice(&sealed);
                Ok(())
            }
            None if self.keys.local_slot().is_none() => Err(CryptoError::NoSendKey),
            None => Err(CryptoError::BadFrame),
        }
    }

    fn decrypt(&self, sender: &str, frame: &[u8], out: &mut Vec<u8>) -> Result<(), CryptoError> {
        let Ok(id) = LkIdentity::parse(sender) else {
            return Err(CryptoError::BadFrame);
        };
        match self.recv.open(&id, frame) {
            Ok(opus) => {
                out.extend_from_slice(&opus);
                Ok(())
            }
            Err(RecvRefused::Unverified) => Err(CryptoError::NoKey),
            Err(RecvRefused::KeyPending(_)) => Err(CryptoError::KeyPending),
            Err(RecvRefused::Undecryptable(_)) => Err(CryptoError::BadFrame),
        }
    }

    fn decrypt_video(
        &self,
        sender: &str,
        codec: crate::video::VideoCodec,
        frame: &[u8],
        out: &mut Vec<u8>,
    ) -> Result<(), CryptoError> {
        let Ok(id) = LkIdentity::parse(sender) else {
            return Err(CryptoError::BadFrame);
        };
        match self.recv.open_video(&id, codec, frame) {
            Ok(plain) => {
                out.extend_from_slice(&plain);
                Ok(())
            }
            Err(RecvRefused::Unverified) => Err(CryptoError::NoKey),
            Err(RecvRefused::KeyPending(_)) => Err(CryptoError::KeyPending),
            Err(RecvRefused::Undecryptable(_)) => Err(CryptoError::BadFrame),
        }
    }

    fn may_subscribe(&self, publisher: &str, track: &RemoteTrack) -> Subscribe {
        let info = TrackInfo {
            sid: track.sid.clone(),
            r#type: if track.audio {
                TrackType::Audio as i32
            } else {
                TrackType::Video as i32
            },
            encryption: track.encryption,
            mime_type: track.mime.clone(),
            ..TrackInfo::default()
        };
        match self.subscribe.check(publisher, &info) {
            SubscribeDecision::Subscribe => Subscribe::Yes,
            SubscribeDecision::Wait => Subscribe::NotYet,
            SubscribeDecision::Refuse(r) => Subscribe::No {
                reason: format!("{r:?}"),
            },
        }
    }

    fn published(&self, track: &TrackInfo) -> bool {
        // Every publication (the first, a full reconnect's, a move's) is confirmed afresh; a
        // refusal holds for the rest of the call.
        self.publish.on_republish();
        match self.publish.on_published(track) {
            Ok(()) => true,
            Err(e) => {
                tracing::warn!(target: "sew_call_client::keys", error = %e, "the publication is refused");
                false
            }
        }
    }

    fn roster(&self, identities: &[String]) {
        let now: BTreeSet<String> = identities.iter().cloned().collect();
        let gone: Vec<String> = {
            let mut known = lock(&self.roster);
            let gone = known.difference(&now).cloned().collect();
            *known = now;
            gone
        };
        for id in gone {
            self.subscribe.unsubscribed(&id);
        }
    }

    fn media_reconnected(&self, full: bool) {
        // The sender slot is re-asserted by the key session itself (its stuck-sender check);
        // the new publication is confirmed through `published`, and until then the engine has
        // no send path.
        let _ = full;
    }

    fn recording(&self, recording_id: Option<Id>, my_consent: ConsentState) {
        let _ = self.recording.send((recording_id, my_consent));
    }

    fn network_changed(&self) {
        self.session.network_changed();
    }

    fn device_id(&self) -> Option<String> {
        Some(self.session.device_id().to_string())
    }

    fn close(&self) -> BoxFuture<'_, ()> {
        Box::pin(async move {
            self.stop.notify_one();
            self.session.leave().await;
        })
    }
}

impl Drop for MlsKeySession {
    fn drop(&mut self) {
        self.stop.notify_one();
    }
}

/// Turns the key session's events into the engine's, passes the engine's renewed credential to
/// the key routes and the call socket, and reports `Active` and `Members` as they change.
async fn bridge(
    session: Arc<MlsSession<LkGcm>>,
    mut rx: mpsc::UnboundedReceiver<KeySessionEvent>,
    events: E2eeEvents,
    mut credential: CredentialHandle,
    stop: Arc<Notify>,
    me: Id,
    mut recording: mpsc::UnboundedReceiver<(Option<Id>, ConsentState)>,
) {
    let mut last_epoch: Option<u64> = None;
    let mut last_members: Vec<(String, String)> = Vec::new();
    let mut last_security: Option<crate::call::CallSecurity> = None;
    // Only renewals of this join's credential: a re-join publishes another credential, which
    // belongs to the next key session.
    let join_id = credential.current().map(|c| c.join_id);
    let mut renewals = join_id.is_some();
    loop {
        let ev = tokio::select! {
            () = stop.notified() => return,
            e = rx.recv() => match e {
                Some(e) => Some(e),
                None => return,
            },
            Some((rid, consent)) = recording.recv() => {
                session.set_recording(rid, consent).await;
                None
            }
            r = credential.changed(), if renewals => {
                match r {
                    Ok(()) => match credential.current() {
                        Some(c) if Some(&c.join_id) == join_id.as_ref() => {
                            session.renewed(Bearer::new(c.token));
                        }
                        _ => renewals = false,
                    },
                    Err(_) => renewals = false,
                }
                None
            }
            () = tokio::time::sleep(BRIDGE_TICK) => None,
        };
        let mut out = Vec::new();
        if let Some(ev) = ev {
            match ev {
                KeySessionEvent::Status(_) | KeySessionEvent::Roster => {}
                KeySessionEvent::VoiceServerUpdated(u) => out.push(E2eeEvent::ServerUpdated(*u)),
                KeySessionEvent::Recording(r) => out.push(E2eeEvent::Recording(r)),
                KeySessionEvent::RecordingControls {
                    channel_id,
                    controls,
                } => out.push(E2eeEvent::RecordingControls {
                    channel_id,
                    controls,
                }),
                KeySessionEvent::MediaPolicy(m) => out.push(E2eeEvent::MediaPolicy(m)),
                KeySessionEvent::VoiceStateUpdated(u) => {
                    if let Some(reason) = self_left(&u, me) {
                        out.push(E2eeEvent::Ended { reason });
                    } else if let Some(st) = u.state {
                        out.push(E2eeEvent::VoiceStates(vec![st]));
                    }
                }
                KeySessionEvent::Warning(w) => out.push(E2eeEvent::Warning {
                    code: w.code().to_owned(),
                    detail: String::new(),
                }),
                KeySessionEvent::KeyJoinFailed(e) => out.push(E2eeEvent::Refused {
                    code: e.code().to_owned(),
                    detail: format!("the move's key join failed: {e}"),
                }),
                KeySessionEvent::Ended(KeyEnd::CredentialLost { .. }) => {
                    out.push(E2eeEvent::CredentialLost);
                }
                KeySessionEvent::Ended(KeyEnd::Ended { reason }) => {
                    out.push(E2eeEvent::Ended { reason });
                }
                KeySessionEvent::Ended(KeyEnd::Socket(why)) => out.push(E2eeEvent::Refused {
                    code: "network".to_owned(),
                    detail: format!("the call socket ended: {why:?}"),
                }),
                // The key group removed this device and said why (Sew's voice leave reason,
                // or `disconnected` for one the key protocol cannot name, so the call socket's
                // reason still wins; a move re-keys instead of ending the call).
                KeySessionEvent::Ended(KeyEnd::Left(LeftReason::Removed(r)))
                    if r != RemoveReason::Moved =>
                {
                    out.push(E2eeEvent::KeysRemoved {
                        reason: r.as_str().to_owned(),
                    });
                }
                KeySessionEvent::Ended(KeyEnd::Left(why)) => out.push(E2eeEvent::Refused {
                    code: "e2ee_refused".to_owned(),
                    detail: format!("the key session left: {why:?}"),
                }),
                KeySessionEvent::Ended(KeyEnd::Device(e)) => out.push(E2eeEvent::Refused {
                    code: "e2ee_refused".to_owned(),
                    detail: format!("the device key could not sign: {e}"),
                }),
            }
        }
        let members = session.members();
        if members != last_members {
            last_members.clone_from(&members);
            out.push(E2eeEvent::Members(members));
        }
        if session.is_active() {
            let epoch = session.active_epoch();
            if epoch != last_epoch {
                last_epoch = epoch;
                out.push(E2eeEvent::Active {
                    epoch: epoch.unwrap_or(0),
                });
            }
        }
        if let Some(security) = session.security()
            && last_security.as_ref() != Some(&security)
        {
            last_security = Some(security.clone());
            out.push(E2eeEvent::Security(security));
        }
        for e in out {
            if !events.send(e) {
                return;
            }
        }
    }
}

/// The reason of the self copy of `voice_state_updated` that takes this device's member out of
/// voice (`state` absent, `reason` given: Sew sends a reason to the member only). A move keeps
/// the member in voice (`state` present) and reaches the engine as `voice_server_updated`.
fn self_left(u: &VoiceStateUpdated, me: Id) -> Option<String> {
    if u.member_id != me || u.state.is_some() {
        return None;
    }
    u.reason.map(|r| r.as_str().to_owned())
}

#[cfg(test)]
mod tests {
    use super::*;
    use sew_proto::voice::VoiceLeaveReason;

    fn left(member: u64, reason: Option<VoiceLeaveReason>) -> VoiceStateUpdated {
        VoiceStateUpdated {
            member_id: Id::new_const(member),
            space_id: None,
            channel_id: None,
            previous_channel_id: Some(Id::new_const(900)),
            state: None,
            reason,
        }
    }

    #[test]
    fn the_self_copy_of_a_leave_keeps_its_reason() {
        let me = Id::new_const(7);
        for r in [
            VoiceLeaveReason::PermsLost,
            VoiceLeaveReason::RemovedFromSpace,
            VoiceLeaveReason::TimedOut,
            VoiceLeaveReason::ChannelArchived,
            VoiceLeaveReason::AccountRemoved,
            VoiceLeaveReason::RemovedByModerator,
            VoiceLeaveReason::SessionRevoked,
            VoiceLeaveReason::DeviceRevoked,
        ] {
            assert_eq!(
                self_left(&left(7, Some(r)), me).as_deref(),
                Some(r.as_str())
            );
        }
        // Someone else's leave, a leave without a reason, and a state that stays in voice are
        // not this device's end.
        assert_eq!(
            self_left(&left(8, Some(VoiceLeaveReason::PermsLost)), me),
            None
        );
        assert_eq!(self_left(&left(7, None), me), None);
    }
}