0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 1 | use std::{collections::HashSet, 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}, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 7 | send_queue::{LocalEchoContent, RoomSendQueueUpdate, SendQueueUpdate}, |
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}; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 10 | use ruma::{OwnedDeviceId, OwnedRoomId, OwnedTransactionId, time::Instant}; |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 11 | use tokio::sync::{broadcast::error::RecvError, mpsc, watch}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 12 | |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 13 | use super::executor::TaskSet; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | use crate::{ |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 15 | ConnectionState, Event, Termination, UploadOutcome, encryption::encryption_state, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 16 | error::classify_sdk_error, transport::EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 17 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 18 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 19 | const SYNC_RESTART_MIN: Duration = Duration::from_secs(1); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | const SYNC_RESTART_MAX: Duration = Duration::from_secs(60); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 21 | const SEND_QUEUE_RETRY: Duration = Duration::from_secs(5); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 22 | const SYNC_STABLE: Duration = Duration::from_secs(30); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 23 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 24 | pub(super) async fn room_subscriptions( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 25 | sync: Arc<SyncService>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 26 | mut rooms: watch::Receiver<Vec<ruma::OwnedRoomId>>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 27 | ) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 28 | while rooms.changed().await.is_ok() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 29 | let room_ids = rooms.borrow_and_update().clone(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 30 | let room_ids = room_ids.iter().map(AsRef::as_ref).collect::<Vec<_>>(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 31 | sync.room_list_service().subscribe_to_rooms(&room_ids).await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 32 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 33 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 34 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 35 | pub(super) async fn session( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 36 | mut changes: tokio::sync::broadcast::Receiver<SessionChange>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 37 | events: EventSender, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 38 | device_id: OwnedDeviceId, |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 39 | stop: mpsc::UnboundedSender<Termination>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | let event = match changes.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | Ok(SessionChange::TokensRefreshed) => Event::SessionTokensChanged, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | Ok(SessionChange::UnknownToken(data)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | let _ = events |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | .send(Event::SessionAccessRejected { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | soft_logout: data.soft_logout, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | .await; |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 50 | let _ = stop.send(if data.soft_logout { |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 51 | Termination::SoftLoggedOut { device_id } |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 52 | } else { |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 53 | Termination::AccessRevoked |
fe4133dbcd47 | atagen | 2026-09-26 09:05 UTC | 54 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | // a missed refresh costs one needless save, a missed rejection comes round again |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 58 | Err(RecvError::Lagged(_)) => Event::SessionTokensChanged, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | Err(RecvError::Closed) => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | if events.send(event).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | /// `states` must be taken before sync starts, a late subscriber never hears the transition it missed |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | pub(super) async fn connection( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | mut states: impl futures_util::Stream<Item = SyncState> + Unpin, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | sync: Arc<SyncService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | events: EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | connection: watch::Sender<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | let mut backoff = SYNC_RESTART_MIN; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 76 | let mut online_since: Option<Instant> = None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | while let Some(state) = states.next().await { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 78 | // only a run that lasted counts as recovered, or a server that answers /versions |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 79 | // but refuses sync would have us bounce between offline and online forever |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 80 | let stable = online_since |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 81 | .take() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 82 | .map(|since| since.elapsed() >= SYNC_STABLE); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 83 | if stable == Some(true) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 84 | backoff = SYNC_RESTART_MIN; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 85 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | let (state, restart) = match state { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 87 | // only ever us stopping it for a restart, nothing a client needs to hear |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 88 | SyncState::Idle => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | SyncState::Running => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 90 | online_since = Some(Instant::now()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | // the sdk parks a room queue on any send error and never unparks it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | client.send_queue().set_enabled(true).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | (ConnectionState::Online, false) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 95 | SyncState::Offline => (ConnectionState::Offline, stable == Some(false)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | SyncState::Terminated => (ConnectionState::Offline, true), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | SyncState::Error(error) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | tracing::warn!(%error, "sync service reported an error"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | (ConnectionState::Offline, true) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | if *connection.borrow() != state { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | connection.send_replace(state); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | if events.send(Event::ConnectionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | if restart { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 109 | sync.stop().await; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 110 | matrix_sdk::sleep::sleep(backoff).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | backoff = (backoff * 2).min(SYNC_RESTART_MAX); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | sync.start().await; |
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 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | /// a recoverable failure while we're online is a hiccup, so give the room another go |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | pub(super) async fn send_queue_errors( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | mut errors: tokio::sync::broadcast::Receiver<matrix_sdk::send_queue::SendQueueRoomError>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | online: watch::Receiver<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | ) { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 123 | let mut retries = TaskSet::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | let error = tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | error = errors.recv() => error, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | Some(_) = retries.join_next(), if !retries.is_empty() => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | Ok(error) if error.is_recoverable => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | let client = client.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | let online = online.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | retries.spawn(async move { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 134 | matrix_sdk::sleep::sleep(SEND_QUEUE_RETRY).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | if *online.borrow() == ConnectionState::Online |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 136 | && let Some(room) = client.get_room(&error.room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | room.send_queue().set_enabled(true); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 139 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 140 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 141 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 142 | Ok(_) | Err(RecvError::Lagged(_)) => {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 143 | Err(RecvError::Closed) => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 144 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 145 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 146 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 147 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 148 | /// upload progress only goes out on the room's own channel, never the client-wide one |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 149 | pub(super) async fn room_uploads(room: Room, events: EventSender, ready: watch::Sender<bool>) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 150 | let room_id = room.room_id().to_owned(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 151 | let mut updates = match room.send_queue().subscribe().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 152 | Ok((_, updates)) => updates, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 153 | Err(error) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 154 | tracing::warn!(%room_id, %error, "reading the media send queue failed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 155 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 156 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 157 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 158 | ready.send_replace(true); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 159 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 160 | let update = match updates.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 161 | Ok(update) => update, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 162 | // a missed tick is fine, the next one says where it's got to |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 163 | Err(RecvError::Lagged(_)) => continue, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 164 | Err(RecvError::Closed) => return, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 165 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 166 | let RoomSendQueueUpdate::MediaUpload { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 167 | related_to, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 168 | file, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 169 | progress, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 170 | .. |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 171 | } = update |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 172 | else { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 173 | continue; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 174 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 175 | let event = Event::MediaUploadProgress { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 176 | room_id: room_id.clone(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 177 | transaction_id: related_to, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 178 | uploaded_bytes: progress.current as u64, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 179 | total_bytes: progress.total as u64, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 180 | complete: file.is_some() && progress.current == progress.total, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 181 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 182 | if events.send(event).await.is_err() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 183 | return; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 184 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 185 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 186 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 187 | |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 188 | /// progress only goes out on each room's own channel, everything else reaches this |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 189 | /// one, which exists before any upload does |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 190 | pub(super) async fn uploads( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 191 | client: Client, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 192 | mut updates: tokio::sync::broadcast::Receiver<SendQueueUpdate>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 193 | events: EventSender, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 194 | ) { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 195 | let (_, mut rooms) = client.rooms_stream(); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 196 | let mut uploads = attachment_echoes(&client).await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 197 | loop { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 198 | let finished = tokio::select! { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 199 | update = updates.recv() => match update { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 200 | Ok(update) => match upload_update(&mut uploads, update) { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 201 | Some(event) => vec![event], |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 202 | None => continue, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 203 | }, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 204 | Err(RecvError::Lagged(skipped)) => { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 205 | tracing::warn!(skipped, "upload watcher lagged"); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 206 | let current = attachment_echoes(&client).await; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 207 | let missed = uploads.difference(¤t).cloned().collect::<Vec<_>>(); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 208 | uploads = current; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 209 | finished_uploads(missed, UploadOutcome::Missed) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 210 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 211 | Err(RecvError::Closed) => return, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 212 | }, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 213 | update = rooms.next() => { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 214 | if update.is_none() { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 215 | return; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 216 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 217 | let forgotten = uploads |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 218 | .iter() |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 219 | .filter(|(room_id, _)| client.get_room(room_id).is_none()) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 220 | .cloned() |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 221 | .collect::<Vec<_>>(); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 222 | for upload in &forgotten { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 223 | uploads.remove(upload); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 224 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 225 | finished_uploads(forgotten, UploadOutcome::RoomForgotten) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 226 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 227 | }; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 228 | for event in finished { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 229 | if events.send(event).await.is_err() { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 230 | return; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 231 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 232 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 233 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 234 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 235 | |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 236 | type Upload = (OwnedRoomId, OwnedTransactionId); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 237 | |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 238 | async fn attachment_echoes(client: &Client) -> HashSet<Upload> { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 239 | let Ok(echoes) = client.send_queue().local_echoes().await else { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 240 | return HashSet::new(); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 241 | }; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 242 | echoes |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 243 | .into_iter() |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 244 | .flat_map(|(room_id, echoes)| { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 245 | echoes |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 246 | .into_iter() |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 247 | .filter(|echo| is_attachment_echo(&echo.content)) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 248 | .map(move |echo| (room_id.clone(), echo.transaction_id)) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 249 | }) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 250 | .collect() |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 251 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 252 | |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 253 | fn upload_update(uploads: &mut HashSet<Upload>, update: SendQueueUpdate) -> Option<Event> { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 254 | let room_id = update.room_id; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 255 | let (transaction_id, outcome) = match update.update { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 256 | RoomSendQueueUpdate::NewLocalEvent(echo) => { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 257 | if is_attachment_echo(&echo.content) { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 258 | uploads.insert((room_id, echo.transaction_id)); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 259 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 260 | return None; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 261 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 262 | RoomSendQueueUpdate::SendError { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 263 | transaction_id, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 264 | error, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 265 | is_recoverable, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 266 | } => { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 267 | let upload = (room_id, transaction_id); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 268 | if !uploads.contains(&upload) { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 269 | return None; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 270 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 271 | let (room_id, transaction_id) = upload; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 272 | return Some(Event::MediaUploadFailed { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 273 | room_id, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 274 | transaction_id, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 275 | kind: classify_sdk_error(&error), |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 276 | will_retry: is_recoverable, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 277 | }); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 278 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 279 | RoomSendQueueUpdate::SentEvent { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 280 | transaction_id, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 281 | event_id, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 282 | } => (transaction_id, UploadOutcome::Sent { event_id }), |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 283 | RoomSendQueueUpdate::CancelledLocalEvent { transaction_id } => { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 284 | (transaction_id, UploadOutcome::Cancelled) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 285 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 286 | _ => return None, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 287 | }; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 288 | let upload = (room_id, transaction_id); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 289 | if !uploads.remove(&upload) { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 290 | return None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 291 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 292 | let (room_id, transaction_id) = upload; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 293 | Some(Event::MediaUploadFinished { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 294 | room_id, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 295 | transaction_id, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 296 | outcome, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 297 | }) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 298 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 299 | |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 300 | fn finished_uploads(uploads: Vec<Upload>, outcome: UploadOutcome) -> Vec<Event> { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 301 | uploads |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 302 | .into_iter() |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 303 | .map(|(room_id, transaction_id)| Event::MediaUploadFinished { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 304 | room_id, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 305 | transaction_id, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 306 | outcome: outcome.clone(), |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 307 | }) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 308 | .collect() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 309 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 310 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | fn is_attachment_echo(content: &LocalEchoContent) -> bool { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 312 | let LocalEchoContent::Event { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 313 | serialized_event, .. |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 314 | } = content |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 315 | else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 316 | return false; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 317 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 318 | matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 319 | serialized_event.deserialize(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 320 | Ok(AnyMessageLikeEventContent::RoomMessage(content)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 321 | if matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 322 | content.msgtype, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 323 | MessageType::Audio(_) | MessageType::File(_) | MessageType::Image(_) | MessageType::Video(_) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 324 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 325 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 326 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 327 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 328 | pub(super) async fn encryption(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 329 | let Some(device_id) = client.device_id().map(ToOwned::to_owned) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 330 | tracing::warn!("encryption watcher stopped because the client has no device id"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 331 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 332 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 333 | let recovery = client.encryption().recovery(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 334 | let backups = client.encryption().backups(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 335 | let mut recovery_stream = recovery.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 336 | let mut backup_stream = backups.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 337 | let mut verification_stream = client.encryption().verification_state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 338 | let mut recovery_state = recovery.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 339 | let mut backup_state = backups.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 340 | let mut verification_state = verification_stream.get(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 341 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 342 | let state = encryption_state( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 343 | device_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 344 | recovery_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 345 | verification_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 346 | backup_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 347 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 348 | if events.send(Event::EncryptionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 349 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 350 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 351 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 352 | Some(update) = recovery_stream.next() => recovery_state = update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 353 | Some(update) = verification_stream.next() => verification_state = update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 354 | Some(update) = backup_stream.next() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 355 | backup_state = update.unwrap_or_else(|_| backups.state()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 356 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 357 | else => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 358 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 359 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 360 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 361 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 362 | pub(super) async fn rooms(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 363 | let (_, mut rooms) = client.rooms_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 364 | let mut info = client.room_info_notable_update_receiver(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 365 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 366 | if events.send(Event::RoomsChanged).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 367 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 368 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | update = rooms.next() => if update.is_none() { return }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | update = info.recv() => if matches!(update, Err(RecvError::Closed)) { return }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | // a sync touching fifty rooms is one invalidation, not fifty |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 374 | while let Ok(_) | Err(tokio::sync::broadcast::error::TryRecvError::Lagged(_)) = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | info.try_recv() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 376 | {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 377 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 378 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 379 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 380 | pub(super) async fn notification_settings(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 381 | let settings = client.notification_settings().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 382 | let mut changes = settings.subscribe_to_changes(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 383 | while let Ok(()) | Err(RecvError::Lagged(_)) = changes.recv().await { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 384 | // room summaries carry the effective mode, and push rule changes never touch room info |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 385 | for event in [Event::NotificationSettingsChanged, Event::RoomsChanged] { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 386 | if events.send(event).await.is_err() { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 387 | return; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 388 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 389 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 390 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 391 | } |