im-client: parse directory revoked events and member disabled flag
Browse files at this commit5efbd60963c4473264ed765afeed038d4273d791Parent: 08994cc1 changed file +85 -12
crates/im-client/src/directory.rsmodified+85 -12
| @@ -5,8 +5,8 @@ | |||
| 5 | //! `/directory` answers it; | 5 | //! `/directory` answers it; |
| 6 | //! * [`DirectoryClient::photo`] — one member's photo bytes, for an | 6 | //! * [`DirectoryClient::photo`] — one member's photo bytes, for an |
| 7 | //! `<img src>` served out of the app's own hands; | 7 | //! `<img src>` served out of the app's own hands; |
| 8 | -//! * [`DirectoryClient::open_stream`] — the `/directory/live` feed, one | 8 | +//! event per changed member and one per sign-out in motion, parsed by |
| 9 | -//! event per changed member, parsed by hand from the byte stream. | 9 | +//! hand from the byte stream. |
| 10 | //! | 10 | //! |
| 11 | //! [`spawn_sync`] is the whole loop most apps want: a full pass through | 11 | //! [`spawn_sync`] is the whole loop most apps want: a full pass through |
| 12 | //! [`DirectoryClient::directory`], then the stream through the same | 12 | //! [`DirectoryClient::directory`], then the stream through the same |
| @@ -50,6 +50,10 @@ pub struct DirectoryMember { | |||
| 50 | /// Defaulted: rows mirrored before im carried the field still read. | 50 | /// Defaulted: rows mirrored before im carried the field still read. |
| 51 | #[serde(default = "default_timezone")] | 51 | #[serde(default = "default_timezone")] |
| 52 | pub timezone: String, | 52 | pub timezone: String, |
| 53 | + /// Whether im has switched the member off. Defaulted: a row mirrored | ||
| 54 | + /// before im carried the field reads as present-and-enabled. | ||
| 55 | + #[serde(default)] | ||
| 56 | + pub disabled: bool, | ||
| 53 | } | 57 | } |
| 54 | 58 | ||
| 55 | /// The timezone im starts every account on, and the one this SDK assumes | 59 | /// The timezone im starts every account on, and the one this SDK assumes |
| @@ -58,14 +62,24 @@ fn default_timezone() -> String { | |||
| 58 | "UTC+03:00".to_string() | 62 | "UTC+03:00".to_string() |
| 59 | } | 63 | } |
| 60 | 64 | ||
| 61 | -/// One event off the directory stream. The only thing `/directory/live` | 65 | +/// One event off the directory stream: a member's row, or a sign-out in |
| 62 | -/// announces today is a changed member; the enum leaves room for the feed | 66 | +/// motion. The enum leaves room for the feed to grow without a breaking |
| 63 | -/// to grow without a breaking change to consumers. | 67 | +/// change to consumers — unknown frame kinds are skipped, not fatal. |
| 64 | #[derive(Debug, Clone, PartialEq)] | 68 | #[derive(Debug, Clone, PartialEq)] |
| 65 | pub enum DirectoryEvent { | 69 | pub enum DirectoryEvent { |
| 66 | /// The named member's row changed — upload, address, admin flag, any | 70 | /// The named member's row changed — upload, address, admin flag, any |
| 67 | /// of it. The payload is the row as it now stands. | 71 | /// of it. The payload is the row as it now stands. |
| 68 | Profile(DirectoryMember), | 72 | Profile(DirectoryMember), |
| 73 | + /// The named subject's sessions were revoked: im's broadcast on any | ||
| 74 | + /// logout-everywhere, disable, or delete. The app's cue to send that | ||
| 75 | + /// subject's signed-in tabs home. | ||
| 76 | + Revoked { sub: String }, | ||
| 77 | +} | ||
| 78 | + | ||
| 79 | +/// The body of a `revoked` frame: the subject whose sessions died. | ||
| 80 | +#[derive(Deserialize)] | ||
| 81 | +struct RevokedFrame { | ||
| 82 | + sub: String, | ||
| 69 | } | 83 | } |
| 70 | 84 | ||
| 71 | /// The directory client: issuer plus the same `client_id:client_secret` | 85 | /// The directory client: issuer plus the same `client_id:client_secret` |
| @@ -257,14 +271,17 @@ impl SseParser { | |||
| 257 | fn take_event(&mut self) -> Option<DirectoryEvent> { | 271 | fn take_event(&mut self) -> Option<DirectoryEvent> { |
| 258 | let data = std::mem::take(&mut self.data); | 272 | let data = std::mem::take(&mut self.data); |
| 259 | let event = self.event.take(); | 273 | let event = self.event.take(); |
| 260 | - if let Some(kind) = event | 274 | + match event.as_deref() { |
| 261 | - && kind != "profile" | 275 | + Some("revoked") => serde_json::from_str::<RevokedFrame>(&data) |
| 262 | - { | 276 | + .ok() |
| 263 | - return None; | 277 | + .map(|frame| DirectoryEvent::Revoked { sub: frame.sub }), |
| 278 | + // A nameless frame (the retry hint, a tick) carries no member. | ||
| 279 | + Some("profile") | None => serde_json::from_str::<DirectoryMember>(&data) | ||
| 280 | + .ok() | ||
| 281 | + .map(DirectoryEvent::Profile), | ||
| 282 | + // The feed may grow: an unknown kind is skipped, not fatal. | ||
| 283 | + Some(_) => None, | ||
| 264 | } | 284 | } |
| 265 | - serde_json::from_str::<DirectoryMember>(&data) | ||
| 266 | - .ok() | ||
| 267 | - .map(DirectoryEvent::Profile) | ||
| 268 | } | 285 | } |
| 269 | } | 286 | } |
| 270 | 287 | ||
| @@ -339,6 +356,12 @@ where | |||
| 339 | // at the short wait. | 356 | // at the short wait. |
| 340 | backoff = BACKOFF_FLOOR; | 357 | backoff = BACKOFF_FLOOR; |
| 341 | } | 358 | } |
| 359 | + // A sign-out in motion: this loop's door carries | ||
| 360 | + // members only. The frame is read and dropped — the | ||
| 361 | + // app that needs the event reads the stream itself. | ||
| 362 | + Ok(DirectoryEvent::Revoked { .. }) => { | ||
| 363 | + backoff = BACKOFF_FLOOR; | ||
| 364 | + } | ||
| 342 | Err(_) => break, | 365 | Err(_) => break, |
| 343 | } | 366 | } |
| 344 | } | 367 | } |
| @@ -382,6 +405,7 @@ mod tests { | |||
| 382 | name: "Ann".into(), | 405 | name: "Ann".into(), |
| 383 | admin: false, | 406 | admin: false, |
| 384 | photo_version: 3, | 407 | photo_version: 3, |
| 408 | + disabled: false, | ||
| 385 | })] | 409 | })] |
| 386 | ); | 410 | ); |
| 387 | } | 411 | } |
| @@ -404,6 +428,7 @@ mod tests { | |||
| 404 | name: "Ann".into(), | 428 | name: "Ann".into(), |
| 405 | admin: false, | 429 | admin: false, |
| 406 | photo_version: 7, | 430 | photo_version: 7, |
| 431 | + disabled: false, | ||
| 407 | })); | 432 | })); |
| 408 | parser = SseParser::default(); | 433 | parser = SseParser::default(); |
| 409 | } | 434 | } |
| @@ -421,6 +446,7 @@ mod tests { | |||
| 421 | name: "Ann".into(), | 446 | name: "Ann".into(), |
| 422 | admin: false, | 447 | admin: false, |
| 423 | photo_version: 1, | 448 | photo_version: 1, |
| 449 | + disabled: false, | ||
| 424 | })); | 450 | })); |
| 425 | } | 451 | } |
| 426 | 452 | ||
| @@ -448,6 +474,7 @@ mod tests { | |||
| 448 | name: "Ann".into(), | 474 | name: "Ann".into(), |
| 449 | admin: false, | 475 | admin: false, |
| 450 | photo_version: 2, | 476 | photo_version: 2, |
| 477 | + disabled: false, | ||
| 451 | }) | 478 | }) |
| 452 | ); | 479 | ); |
| 453 | } | 480 | } |
| @@ -464,4 +491,50 @@ mod tests { | |||
| 464 | let joined = "event: profile\ndata: {\"sub\":\"five\",\ndata: \"x\"}\n\n"; | 491 | let joined = "event: profile\ndata: {\"sub\":\"five\",\ndata: \"x\"}\n\n"; |
| 465 | assert!(parser.feed(joined.as_bytes()).is_empty(), "invalid JSON is skipped, not fatal"); | 492 | assert!(parser.feed(joined.as_bytes()).is_empty(), "invalid JSON is skipped, not fatal"); |
| 466 | } | 493 | } |
| 494 | + | ||
| 495 | + #[test] | ||
| 496 | + fn a_revoked_frame_names_the_subject() { | ||
| 497 | + let mut parser = SseParser::default(); | ||
| 498 | + let bytes = b"event: revoked\ndata: {\"sub\":\"seven\"}\n\n"; | ||
| 499 | + let events = parser.feed(bytes); | ||
| 500 | + assert_eq!( | ||
| 501 | + events, | ||
| 502 | + vec![DirectoryEvent::Revoked { sub: "seven".into() }] | ||
| 503 | + ); | ||
| 504 | + } | ||
| 505 | + | ||
| 506 | + #[test] | ||
| 507 | + fn a_member_carries_the_disabled_flag_and_defaults_it() { | ||
| 508 | + let mut parser = SseParser::default(); | ||
| 509 | + let bytes = b"event: profile\ndata: {\"sub\":\"gone\",\"email\":\"g\",\ | ||
| 510 | + \"name\":\"G\",\"admin\":false,\"photo_version\":0,\ | ||
| 511 | + \"disabled\":true}\n\n"; | ||
| 512 | + let events = parser.feed(bytes); | ||
| 513 | + assert_eq!(events.len(), 1); | ||
| 514 | + assert!( | ||
| 515 | + matches!(&events[0], DirectoryEvent::Profile(member) if member.disabled), | ||
| 516 | + "the flag the frame carries is the flag the member reads" | ||
| 517 | + ); | ||
| 518 | + | ||
| 519 | + // An older im's row predates the flag: it reads as enabled. | ||
| 520 | + let mut parser = SseParser::default(); | ||
| 521 | + let events = parser.feed(frame("back", 1).as_bytes()); | ||
| 522 | + assert!( | ||
| 523 | + matches!(&events[0], DirectoryEvent::Profile(member) if !member.disabled), | ||
| 524 | + "a row without the field defaults to present-and-enabled" | ||
| 525 | + ); | ||
| 526 | + } | ||
| 527 | + | ||
| 528 | + #[test] | ||
| 529 | + fn an_unknown_event_kind_is_skipped_and_the_stream_stays_alive() { | ||
| 530 | + let mut parser = SseParser::default(); | ||
| 531 | + let bytes = format!( | ||
| 532 | + "event: somethingnew\ndata: {{\"whatever\":1}}\n\n{}", | ||
| 533 | + frame("after", 9) | ||
| 534 | + ) | ||
| 535 | + .into_bytes(); | ||
| 536 | + let events = parser.feed(&bytes); | ||
| 537 | + assert_eq!(events.len(), 1, "the unknown frame is not fatal"); | ||
| 538 | + assert!(matches!(&events[0], DirectoryEvent::Profile(member) if member.sub == "after")); | ||
| 539 | + } | ||
| 467 | } | 540 | } |