1fe480c16a3c | atagen | 2026-09-26 08:44 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}; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 10 | use ruma::time::Instant; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 11 | use tokio::sync::{Notify, broadcast::error::RecvError, 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::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 15 | ConnectionState, Event, encryption::encryption_state, error::classify_sdk_error, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 16 | 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, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | stop: Arc<Notify>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | let event = match changes.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | Ok(SessionChange::TokensRefreshed) => Event::SessionTokensChanged, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | Ok(SessionChange::UnknownToken(data)) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | let _ = events |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | .send(Event::SessionAccessRejected { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | soft_logout: data.soft_logout, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | .await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | stop.notify_one(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | // a missed refresh costs one needless save, a missed rejection comes round again |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | Err(RecvError::Lagged(_)) => Event::SessionTokensChanged, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | Err(RecvError::Closed) => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | if events.send(event).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 58 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | /// `states` must be taken before sync starts, a late subscriber never hears the transition it missed |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | pub(super) async fn connection( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | mut states: impl futures_util::Stream<Item = SyncState> + Unpin, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | sync: Arc<SyncService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | events: EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | connection: watch::Sender<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | let mut backoff = SYNC_RESTART_MIN; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 71 | let mut online_since: Option<Instant> = None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | while let Some(state) = states.next().await { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 73 | // only a run that lasted counts as recovered, or a server that answers /versions |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 74 | // but refuses sync would have us bounce between offline and online forever |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 75 | let stable = online_since |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 76 | .take() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 77 | .map(|since| since.elapsed() >= SYNC_STABLE); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 78 | if stable == Some(true) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 79 | backoff = SYNC_RESTART_MIN; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 80 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | let (state, restart) = match state { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 82 | // only ever us stopping it for a restart, nothing a client needs to hear |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 83 | SyncState::Idle => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | SyncState::Running => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 85 | online_since = Some(Instant::now()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | // the sdk parks a room queue on any send error and never unparks it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | client.send_queue().set_enabled(true).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | (ConnectionState::Online, false) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 90 | SyncState::Offline => (ConnectionState::Offline, stable == Some(false)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | SyncState::Terminated => (ConnectionState::Offline, true), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | SyncState::Error(error) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | tracing::warn!(%error, "sync service reported an error"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | (ConnectionState::Offline, true) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | if *connection.borrow() != state { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | connection.send_replace(state); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | if events.send(Event::ConnectionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | if restart { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 104 | sync.stop().await; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 105 | matrix_sdk::sleep::sleep(backoff).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | backoff = (backoff * 2).min(SYNC_RESTART_MAX); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | sync.start().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | /// a recoverable failure while we're online is a hiccup, so give the room another go |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | pub(super) async fn send_queue_errors( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | mut errors: tokio::sync::broadcast::Receiver<matrix_sdk::send_queue::SendQueueRoomError>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | online: watch::Receiver<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | ) { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 118 | let mut retries = TaskSet::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | let error = tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | error = errors.recv() => error, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | Some(_) = retries.join_next(), if !retries.is_empty() => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | Ok(error) if error.is_recoverable => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | let client = client.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | let online = online.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | retries.spawn(async move { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 129 | matrix_sdk::sleep::sleep(SEND_QUEUE_RETRY).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | if *online.borrow() == ConnectionState::Online |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | && let Some(room) = client.get_room(&error.room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | room.send_queue().set_enabled(true); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 136 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | Ok(_) | Err(RecvError::Lagged(_)) => {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | Err(RecvError::Closed) => return, |
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 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 143 | /// upload progress only goes out on the room's own channel, never the client-wide one |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 144 | pub(super) async fn room_uploads(room: Room, events: EventSender, ready: watch::Sender<bool>) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 145 | let room_id = room.room_id().to_owned(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 146 | let mut updates = match room.send_queue().subscribe().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 147 | Ok((_, updates)) => updates, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 148 | Err(error) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 149 | tracing::warn!(%room_id, %error, "reading the media send queue failed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 150 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 151 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 152 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 153 | ready.send_replace(true); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 154 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 155 | let update = match updates.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 156 | Ok(update) => update, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 157 | // a missed tick is fine, the next one says where it's got to |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 158 | Err(RecvError::Lagged(_)) => continue, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 159 | Err(RecvError::Closed) => return, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 160 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 161 | let RoomSendQueueUpdate::MediaUpload { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 162 | related_to, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 163 | file, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 164 | progress, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 165 | .. |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 166 | } = update |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 167 | else { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 168 | continue; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 169 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 170 | let event = Event::MediaUploadProgress { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 171 | room_id: room_id.clone(), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 172 | transaction_id: related_to, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 173 | uploaded_bytes: progress.current as u64, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 174 | total_bytes: progress.total as u64, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 175 | complete: file.is_some() && progress.current == progress.total, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 176 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 177 | if events.send(event).await.is_err() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 178 | return; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 179 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 180 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 181 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 182 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 183 | /// failures do reach the client-wide channel, and this one exists before any upload does |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 184 | pub(super) async fn upload_failures( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 185 | client: Client, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 186 | mut updates: tokio::sync::broadcast::Receiver<matrix_sdk::send_queue::SendQueueUpdate>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 187 | events: EventSender, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 188 | ) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 189 | loop { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 190 | let update = match updates.recv().await { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 191 | Ok(update) => update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 192 | Err(RecvError::Lagged(skipped)) => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 193 | tracing::warn!(skipped, "upload failure watcher lagged"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 194 | continue; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 195 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 196 | Err(RecvError::Closed) => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 197 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 198 | let RoomSendQueueUpdate::SendError { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 199 | transaction_id, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 200 | error, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 201 | is_recoverable, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 202 | } = update.update |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 203 | else { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 204 | continue; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 205 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 206 | let Some(room) = client.get_room(&update.room_id) else { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 207 | continue; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 208 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 209 | let is_attachment = room |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 210 | .send_queue() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 211 | .subscribe() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 212 | .await |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 213 | .is_ok_and(|(echoes, _)| { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 214 | echoes.iter().any(|echo| { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 215 | echo.transaction_id == transaction_id && is_attachment_echo(&echo.content) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 216 | }) |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 217 | }); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 218 | if !is_attachment { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 219 | continue; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 220 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 221 | let event = Event::MediaUploadFailed { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 222 | room_id: update.room_id, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 223 | transaction_id, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 224 | kind: classify_sdk_error(&error), |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 225 | recoverable: is_recoverable, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 226 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 227 | if events.send(event).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 228 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 229 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 230 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 231 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 232 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 233 | fn is_attachment_echo(content: &LocalEchoContent) -> bool { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 234 | let LocalEchoContent::Event { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 235 | serialized_event, .. |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 236 | } = content |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 237 | else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 238 | return false; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 239 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 240 | matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 241 | serialized_event.deserialize(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 242 | Ok(AnyMessageLikeEventContent::RoomMessage(content)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 243 | if matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 244 | content.msgtype, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 245 | MessageType::Audio(_) | MessageType::File(_) | MessageType::Image(_) | MessageType::Video(_) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 246 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 247 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 248 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 249 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 250 | pub(super) async fn encryption(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 251 | let Some(device_id) = client.device_id().map(ToOwned::to_owned) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 252 | tracing::warn!("encryption watcher stopped because the client has no device id"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 253 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 254 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 255 | let recovery = client.encryption().recovery(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 256 | let backups = client.encryption().backups(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 257 | let mut recovery_stream = recovery.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 258 | let mut backup_stream = backups.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 259 | let mut verification_stream = client.encryption().verification_state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 260 | let mut recovery_state = recovery.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 261 | let mut backup_state = backups.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 262 | let mut verification_state = verification_stream.get(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 263 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 264 | let state = encryption_state( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 265 | device_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 266 | recovery_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 267 | verification_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 268 | backup_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 269 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 270 | if events.send(Event::EncryptionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 271 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 272 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 273 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 274 | Some(update) = recovery_stream.next() => recovery_state = update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 275 | Some(update) = verification_stream.next() => verification_state = update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 276 | Some(update) = backup_stream.next() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 277 | backup_state = update.unwrap_or_else(|_| backups.state()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 278 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 279 | else => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 280 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 281 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 282 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 283 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 284 | pub(super) async fn rooms(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 285 | let (_, mut rooms) = client.rooms_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 286 | let mut info = client.room_info_notable_update_receiver(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 287 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 288 | if events.send(Event::RoomsChanged).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 289 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 290 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 291 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 292 | update = rooms.next() => if update.is_none() { return }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 293 | update = info.recv() => if matches!(update, Err(RecvError::Closed)) { return }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 294 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 295 | // a sync touching fifty rooms is one invalidation, not fifty |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 296 | while let Ok(_) | Err(tokio::sync::broadcast::error::TryRecvError::Lagged(_)) = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 297 | info.try_recv() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 298 | {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 299 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 300 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 301 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 302 | pub(super) async fn notification_settings(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 303 | let settings = client.notification_settings().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 304 | let mut changes = settings.subscribe_to_changes(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 305 | while let Ok(()) | Err(RecvError::Lagged(_)) = changes.recv().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 306 | if events |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 307 | .send(Event::NotificationSettingsChanged) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 308 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 309 | .is_err() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 310 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 311 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 312 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 313 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 314 | } |