f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 1 | use std::sync::Arc; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 2 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 3 | use matrix_sdk::{BoxFuture, Client, SessionChange}; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 4 | use ruma::OwnedDeviceId; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 5 | use tokio::sync::{ |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 6 | broadcast::{self, error::RecvError}, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 7 | mpsc, watch, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 8 | }; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 9 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 10 | use super::Logouts; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 11 | use crate::{Error, Event, Termination, transport::EventSender}; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 12 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 13 | #[cfg(not(target_family = "wasm"))] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 14 | pub(crate) type SaveRecord = dyn Fn(Client) -> BoxFuture<'static, Result<(), Error>> + Send + Sync; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 15 | #[cfg(target_family = "wasm")] |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 16 | pub(crate) type SaveRecord = dyn Fn(Client) -> BoxFuture<'static, Result<(), Error>>; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 17 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 18 | /// how the engine keeps the saved record current, handed over by `Session::build` |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 19 | pub(crate) struct Persistence { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 20 | pub(crate) save: Box<SaveRecord>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 21 | /// what the sdk's own save callback made of each refresh, when the store could write |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 22 | /// there and then |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 23 | pub(crate) inline: mpsc::UnboundedReceiver<Result<(), Error>>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 24 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 25 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 26 | impl Persistence { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 27 | async fn persist(&mut self, client: &Client) -> Result<(), Error> { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 28 | let mut inline = None; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 29 | while let Ok(result) = self.inline.try_recv() { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 30 | inline = Some(result); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 31 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 32 | if let Some(Ok(())) = inline { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 33 | return Ok(()); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 34 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 35 | // an inline save for a newer refresh can land while this one writes, so go again |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 36 | // until what's on disk is what the client holds |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 37 | loop { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 38 | let tokens = client.session_tokens(); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 39 | (self.save)(client.clone()).await?; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 40 | if client.session_tokens() == tokens { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 41 | return Ok(()); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 42 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 43 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 44 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 45 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 46 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 47 | pub(super) enum Notice { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 48 | Event(Event), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 49 | Rejected(Option<OwnedDeviceId>), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 50 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 51 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 52 | /// saves and notices are split, so a full event stream never holds a save back |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 53 | pub(super) async fn session( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 54 | client: Client, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 55 | mut changes: broadcast::Receiver<SessionChange>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 56 | mut persistence: Option<Persistence>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 57 | notices: mpsc::UnboundedSender<Notice>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 58 | device_id: OwnedDeviceId, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 59 | logouts: Arc<watch::Sender<Logouts>>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 60 | ) { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 61 | loop { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 62 | let event = match changes.recv().await { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 63 | // a missed refresh costs one needless save, a missed rejection comes round again |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 64 | Ok(SessionChange::TokensRefreshed) | Err(RecvError::Lagged(_)) => { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 65 | match persistence.as_mut() { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 66 | None => Event::SessionTokensChanged, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 67 | Some(persistence) => match persistence.persist(&client).await { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 68 | Ok(()) => Event::SessionTokensChanged, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 69 | Err(error) => Event::SessionSaveFailed { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 70 | message: error.message, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 71 | }, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 72 | }, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 73 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 74 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 75 | Ok(SessionChange::UnknownToken(data)) => { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 76 | let soft_logout = data.soft_logout.then_some(device_id); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 77 | let mut held = false; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 78 | // our own logout revokes the token, so a rejection meanwhile is its doing |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 79 | logouts.send_if_modified(|logouts| { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 80 | held = logouts.in_flight > 0; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 81 | if held { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 82 | logouts.rejected = Some(soft_logout.clone()); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 83 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 84 | held |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 85 | }); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 86 | if !held { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 87 | let _ = notices.send(Notice::Rejected(soft_logout)); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 88 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 89 | return; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 90 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 91 | Err(RecvError::Closed) => return, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 92 | }; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 93 | if notices.send(Notice::Event(event)).is_err() { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 94 | return; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 95 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 96 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 97 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 98 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 99 | pub(super) async fn session_notices( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 100 | mut notices: mpsc::UnboundedReceiver<Notice>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 101 | events: EventSender, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 102 | stop: mpsc::UnboundedSender<Termination>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 103 | ) { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 104 | while let Some(notice) = notices.recv().await { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 105 | match notice { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 106 | Notice::Event(event) => { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 107 | if events.send(event).await.is_err() { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 108 | return; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 109 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 110 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 111 | Notice::Rejected(soft_logout) => { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 112 | return reject_session(&events, &stop, soft_logout).await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 113 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 114 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 115 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 116 | } |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 117 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 118 | pub(super) async fn reject_session( |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 119 | events: &EventSender, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 120 | stop: &mpsc::UnboundedSender<Termination>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 121 | soft_logout: Option<OwnedDeviceId>, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 122 | ) { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 123 | let _ = events |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 124 | .send(Event::SessionAccessRejected { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 125 | soft_logout: soft_logout.clone(), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 126 | }) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 127 | .await; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 128 | let _ = stop.send(match soft_logout { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 129 | Some(device_id) => Termination::SoftLoggedOut { device_id }, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 130 | None => Termination::AccessRevoked, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 131 | }); |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 132 | } |