31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1 | use std::{sync::Arc, time::Duration}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 3 | use futures_util::StreamExt; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 4 | use matrix_sdk::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 5 | Client, Room, SessionChange, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | ruma::events::{AnyMessageLikeEventContent, room::message::MessageType}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 7 | send_queue::{LocalEchoContent, RoomSendQueueUpdate}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 8 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 9 | use matrix_sdk_ui::sync_service::{State as SyncState, SyncService}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 10 | use tokio::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 11 | sync::{Notify, broadcast::error::RecvError, watch}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 12 | task::JoinSet, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 13 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 15 | use crate::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 16 | ConnectionState, Event, encryption::encryption_state, error::classify_sdk_error, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 17 | transport::EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 18 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 19 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | const SYNC_RESTART_MIN: Duration = Duration::from_secs(1); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 21 | const SYNC_RESTART_MAX: Duration = Duration::from_secs(60); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 22 | const SEND_QUEUE_RETRY: Duration = Duration::from_secs(5); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 23 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 24 | pub(super) async fn session( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 25 | mut changes: tokio::sync::broadcast::Receiver<SessionChange>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 26 | events: EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 27 | stop: Arc<Notify>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | let event = match changes.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 31 | Ok(SessionChange::TokensRefreshed) => Event::SessionTokensChanged, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 32 | Ok(SessionChange::UnknownToken(data)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 33 | let _ = events |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 34 | .send(Event::SessionAccessRejected { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 35 | soft_logout: data.soft_logout, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 36 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 37 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | stop.notify_one(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | // a missed refresh costs one needless save, a missed rejection comes round again |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | Err(RecvError::Lagged(_)) => Event::SessionTokensChanged, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | Err(RecvError::Closed) => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | if events.send(event).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | /// `states` must be taken before sync starts, a late subscriber never hears the transition it missed |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | pub(super) async fn connection( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | mut states: impl futures_util::Stream<Item = SyncState> + Unpin, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | sync: Arc<SyncService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | events: EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | connection: watch::Sender<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 58 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | let mut backoff = SYNC_RESTART_MIN; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | while let Some(state) = states.next().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | let (state, restart) = match state { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | SyncState::Idle => (ConnectionState::Idle, false), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | SyncState::Running => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | backoff = SYNC_RESTART_MIN; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | // the sdk parks a room queue on any send error and never unparks it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | client.send_queue().set_enabled(true).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | (ConnectionState::Online, false) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | SyncState::Offline => (ConnectionState::Offline, false), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | SyncState::Terminated => (ConnectionState::Offline, true), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | SyncState::Error(error) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | tracing::warn!(%error, "sync service reported an error"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | (ConnectionState::Offline, true) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 76 | if *connection.borrow() != state { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | connection.send_replace(state); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | if events.send(Event::ConnectionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | if restart { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | tokio::time::sleep(backoff).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | backoff = (backoff * 2).min(SYNC_RESTART_MAX); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | sync.start().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | /// a recoverable failure while we're online is a hiccup, so give the room another go |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | pub(super) async fn send_queue_errors( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | mut errors: tokio::sync::broadcast::Receiver<matrix_sdk::send_queue::SendQueueRoomError>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | online: watch::Receiver<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | let mut retries = JoinSet::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | let error = tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | error = errors.recv() => error, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | Some(_) = retries.join_next(), if !retries.is_empty() => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | Ok(error) if error.is_recoverable => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | let client = client.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | let online = online.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | retries.spawn(async move { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | tokio::time::sleep(SEND_QUEUE_RETRY).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | if *online.borrow() == ConnectionState::Online |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | && let Some(room) = client.get_room(&error.room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | room.send_queue().set_enabled(true); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | Ok(_) | Err(RecvError::Lagged(_)) => {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | Err(RecvError::Closed) => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | /// upload progress only goes out on the room's own channel, never the client-wide one |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | pub(super) async fn room_uploads(room: Room, events: EventSender, ready: watch::Sender<bool>) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | let room_id = room.room_id().to_owned(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | let mut updates = match room.send_queue().subscribe().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | Ok((_, updates)) => updates, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | Err(error) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | tracing::warn!(%room_id, %error, "reading the media send queue failed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | ready.send_replace(true); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | let update = match updates.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | Ok(update) => update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | Err(RecvError::Lagged(skipped)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 136 | tracing::warn!(%room_id, skipped, "upload watcher lagged"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | continue; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 139 | Err(RecvError::Closed) => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 140 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 141 | let event = match update { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 142 | RoomSendQueueUpdate::MediaUpload { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 143 | related_to, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 144 | file, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 145 | progress, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 146 | .. |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 147 | } => Event::MediaUploadProgress { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 148 | room_id: room_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 149 | transaction_id: related_to, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 150 | uploaded_bytes: progress.current as u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 151 | total_bytes: progress.total as u64, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 152 | complete: file.is_some() && progress.current == progress.total, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 153 | }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 154 | RoomSendQueueUpdate::SendError { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 155 | transaction_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 156 | error, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 157 | is_recoverable, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 158 | } => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 159 | let is_attachment = room |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 160 | .send_queue() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 161 | .subscribe() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 162 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 163 | .is_ok_and(|(echoes, _)| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 164 | echoes.iter().any(|echo| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 165 | echo.transaction_id == transaction_id |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 166 | && is_attachment_echo(&echo.content) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 167 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 168 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 169 | if !is_attachment { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 170 | continue; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 171 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 172 | Event::MediaUploadFailed { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 173 | room_id: room_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 174 | transaction_id, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 175 | kind: classify_sdk_error(&error), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 176 | recoverable: is_recoverable, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 177 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 178 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 179 | _ => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 180 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 181 | if events.send(event).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 182 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 183 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 184 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 185 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 186 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 187 | fn is_attachment_echo(content: &LocalEchoContent) -> bool { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 188 | let LocalEchoContent::Event { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 189 | serialized_event, .. |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 190 | } = content |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 191 | else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 192 | return false; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 193 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 194 | matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 195 | serialized_event.deserialize(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 196 | Ok(AnyMessageLikeEventContent::RoomMessage(content)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 197 | if matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 198 | content.msgtype, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 199 | MessageType::Audio(_) | MessageType::File(_) | MessageType::Image(_) | MessageType::Video(_) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 200 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 201 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 202 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 203 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 204 | pub(super) async fn encryption(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 205 | let Some(device_id) = client.device_id().map(ToOwned::to_owned) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 206 | tracing::warn!("encryption watcher stopped because the client has no device id"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 207 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 208 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 209 | let recovery = client.encryption().recovery(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 210 | let backups = client.encryption().backups(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 211 | let mut recovery_stream = recovery.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 212 | let mut backup_stream = backups.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 213 | let mut verification_stream = client.encryption().verification_state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 214 | let mut recovery_state = recovery.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | let mut backup_state = backups.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 216 | let mut verification_state = verification_stream.get(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 217 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 218 | let state = encryption_state( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 219 | device_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 220 | recovery_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 221 | verification_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 222 | backup_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 223 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 224 | if events.send(Event::EncryptionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 225 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 226 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 227 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 228 | Some(update) = recovery_stream.next() => recovery_state = update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 229 | Some(update) = verification_stream.next() => verification_state = update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 230 | Some(update) = backup_stream.next() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 231 | backup_state = update.unwrap_or_else(|_| backups.state()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 232 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 233 | else => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 234 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 235 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 236 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 237 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 238 | pub(super) async fn rooms(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 239 | let (_, mut rooms) = client.rooms_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 240 | let mut info = client.room_info_notable_update_receiver(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 241 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 242 | if events.send(Event::RoomsChanged).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 243 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 244 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 245 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 246 | update = rooms.next() => if update.is_none() { return }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 247 | update = info.recv() => if matches!(update, Err(RecvError::Closed)) { return }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 248 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 249 | // a sync touching fifty rooms is one invalidation, not fifty |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 250 | while let Ok(_) | Err(tokio::sync::broadcast::error::TryRecvError::Lagged(_)) = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 251 | info.try_recv() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 252 | {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 253 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 254 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 255 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 256 | pub(super) async fn notification_settings(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 257 | let settings = client.notification_settings().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 258 | let mut changes = settings.subscribe_to_changes(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 259 | while let Ok(()) | Err(RecvError::Lagged(_)) = changes.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 260 | if events |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 261 | .send(Event::NotificationSettingsChanged) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 262 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 263 | .is_err() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 264 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 265 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 266 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 267 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 268 | } |