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::{ |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 5 | Client, Room, |
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}; |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 10 | use ruma::{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 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 13 | use super::executor::TaskSet; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | use crate::{ |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 15 | ConnectionState, Event, UploadOutcome, encryption::encryption_state, error::classify_sdk_error, |
f12377bce9b9 | atagen | 2026-09-26 12:32 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 | /// `states` must be taken before sync starts, a late subscriber never hears the transition it missed |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 36 | pub(super) async fn connection( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 37 | mut states: impl futures_util::Stream<Item = SyncState> + Unpin, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 38 | sync: Arc<SyncService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 39 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | events: EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | connection: watch::Sender<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | let mut backoff = SYNC_RESTART_MIN; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 44 | let mut online_since: Option<Instant> = None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | while let Some(state) = states.next().await { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 46 | // only a run that lasted counts as recovered, or a server that answers /versions |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 47 | // but refuses sync would have us bounce between offline and online forever |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 48 | let stable = online_since |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 49 | .take() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 50 | .map(|since| since.elapsed() >= SYNC_STABLE); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 51 | if stable == Some(true) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 52 | backoff = SYNC_RESTART_MIN; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 53 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | let (state, restart) = match state { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 55 | // only ever us stopping it for a restart, nothing a client needs to hear |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 56 | SyncState::Idle => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | SyncState::Running => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 58 | online_since = Some(Instant::now()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | // the sdk parks a room queue on any send error and never unparks it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | client.send_queue().set_enabled(true).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | (ConnectionState::Online, false) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 63 | SyncState::Offline => (ConnectionState::Offline, stable == Some(false)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | SyncState::Terminated => (ConnectionState::Offline, true), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | SyncState::Error(error) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | tracing::warn!(%error, "sync service reported an error"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 67 | (ConnectionState::Offline, true) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | if *connection.borrow() != state { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | connection.send_replace(state); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 72 | if events.send(Event::ConnectionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | return; |
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 restart { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 77 | sync.stop().await; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 78 | matrix_sdk::sleep::sleep(backoff).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | backoff = (backoff * 2).min(SYNC_RESTART_MAX); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | sync.start().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | /// a recoverable failure while we're online is a hiccup, so give the room another go |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | pub(super) async fn send_queue_errors( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | mut errors: tokio::sync::broadcast::Receiver<matrix_sdk::send_queue::SendQueueRoomError>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | online: watch::Receiver<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | ) { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 91 | let mut retries = TaskSet::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | let error = tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | error = errors.recv() => error, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | Some(_) = retries.join_next(), if !retries.is_empty() => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | Ok(error) if error.is_recoverable => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | let client = client.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | let online = online.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | retries.spawn(async move { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 102 | matrix_sdk::sleep::sleep(SEND_QUEUE_RETRY).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | if *online.borrow() == ConnectionState::Online |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | && let Some(room) = client.get_room(&error.room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | room.send_queue().set_enabled(true); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | } |
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 | Ok(_) | Err(RecvError::Lagged(_)) => {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | Err(RecvError::Closed) => return, |
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 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 116 | /// upload progress only goes out on the room's own channel, never the client-wide one. |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 117 | /// the room's sends and cancels come along behind it, so an upload's end can't |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 118 | /// overtake its own progress |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 119 | pub(super) async fn room_uploads( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 120 | room: Room, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 121 | tracker: mpsc::Sender<RoomUpload>, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 122 | ready: watch::Sender<bool>, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 123 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | let room_id = room.room_id().to_owned(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | let mut updates = match room.send_queue().subscribe().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | Ok((_, updates)) => updates, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | Err(error) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | tracing::warn!(%room_id, %error, "reading the media send queue failed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 130 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 131 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 132 | ready.send_replace(true); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | loop { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 134 | let report = match updates.recv().await { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 135 | Ok(RoomSendQueueUpdate::MediaUpload { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 136 | related_to, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 137 | file, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 138 | progress, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 139 | .. |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 140 | }) => RoomUpload::Progress { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 141 | upload: (room_id.clone(), related_to), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 142 | uploaded_bytes: progress.current as u64, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 143 | total_bytes: progress.total as u64, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 144 | complete: file.is_some() && progress.current == progress.total, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 145 | }, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 146 | Ok(RoomSendQueueUpdate::SentEvent { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 147 | transaction_id, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 148 | event_id, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 149 | }) => RoomUpload::Ended( |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 150 | (room_id.clone(), transaction_id), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 151 | UploadOutcome::Sent { event_id }, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 152 | ), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 153 | Ok(RoomSendQueueUpdate::CancelledLocalEvent { transaction_id }) => { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 154 | RoomUpload::Ended((room_id.clone(), transaction_id), UploadOutcome::Cancelled) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 155 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 156 | Ok(_) => continue, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 157 | Err(RecvError::Lagged(_)) => RoomUpload::Lagged, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 158 | Err(RecvError::Closed) => return, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 159 | }; |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 160 | if tracker.send(report).await.is_err() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 161 | return; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 162 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 163 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 164 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 165 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 166 | pub(super) enum RoomUpload { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 167 | Progress { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 168 | upload: Upload, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 169 | uploaded_bytes: u64, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 170 | total_bytes: u64, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 171 | complete: bool, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 172 | }, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 173 | Ended(Upload, UploadOutcome), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 174 | /// an end may have been lost with the ticks |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 175 | Lagged, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 176 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 177 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 178 | /// the one place upload events leave from, so an upload is never reported after it |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 179 | /// ended. progress comes in from the room forwarders, the rest from the client-wide |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 180 | /// channel, which exists before any upload does |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 181 | pub(super) async fn uploads( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 182 | client: Client, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 183 | mut updates: tokio::sync::broadcast::Receiver<SendQueueUpdate>, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 184 | mut rooms_reports: mpsc::Receiver<RoomUpload>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 185 | events: EventSender, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 186 | ) { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 187 | let (_, mut rooms) = client.rooms_stream(); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 188 | let mut tracker = UploadTracker { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 189 | live: attachment_echoes(&client).await, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 190 | ended: HashSet::new(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 191 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 192 | loop { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 193 | let reports = tokio::select! { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 194 | update = updates.recv() => match update { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 195 | Ok(update) => tracker.update(update).into_iter().collect(), |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 196 | Err(RecvError::Lagged(skipped)) => { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 197 | tracing::warn!(skipped, "upload tracker lagged"); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 198 | tracker.resnapshot(attachment_echoes(&client).await) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 199 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 200 | Err(RecvError::Closed) => return, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 201 | }, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 202 | Some(report) = rooms_reports.recv() => match report { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 203 | RoomUpload::Lagged => tracker.resnapshot(attachment_echoes(&client).await), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 204 | report => tracker.room(report).into_iter().collect(), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 205 | }, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 206 | update = rooms.next() => { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 207 | if update.is_none() { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 208 | return; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 209 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 210 | tracker.finish_where( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 211 | |(room_id, _)| client.get_room(room_id).is_none(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 212 | &UploadOutcome::RoomForgotten, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 213 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 214 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 215 | }; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 216 | for event in reports { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 217 | if events.send(event).await.is_err() { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 218 | return; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 219 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 220 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 221 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 222 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 223 | |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 224 | type Upload = (OwnedRoomId, OwnedTransactionId); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 225 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 226 | /// everything reported gets exactly one `MediaUploadFinished`. an upload ended from |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 227 | /// anywhere but its room's forwarder goes in `ended` until the forwarder's own end for |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 228 | /// it arrives, since until then the forwarder can still be holding progress for it |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 229 | struct UploadTracker { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 230 | live: HashSet<Upload>, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 231 | ended: HashSet<Upload>, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 232 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 233 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 234 | impl UploadTracker { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 235 | fn update(&mut self, update: SendQueueUpdate) -> Option<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 236 | let room_id = update.room_id; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 237 | let (transaction_id, outcome) = match update.update { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 238 | RoomSendQueueUpdate::NewLocalEvent(echo) => { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 239 | let upload = (room_id, echo.transaction_id); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 240 | if is_attachment_echo(&echo.content) && !self.ended.contains(&upload) { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 241 | self.live.insert(upload); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 242 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 243 | return None; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 244 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 245 | RoomSendQueueUpdate::SendError { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 246 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 247 | error, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 248 | is_recoverable, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 249 | } => { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 250 | let upload = (room_id, transaction_id); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 251 | if !self.live.contains(&upload) { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 252 | return None; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 253 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 254 | let (room_id, transaction_id) = upload; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 255 | return Some(Event::MediaUploadFailed { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 256 | room_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 257 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 258 | kind: classify_sdk_error(&error), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 259 | will_retry: is_recoverable, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 260 | }); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 261 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 262 | RoomSendQueueUpdate::SentEvent { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 263 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 264 | event_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 265 | } => (transaction_id, UploadOutcome::Sent { event_id }), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 266 | RoomSendQueueUpdate::CancelledLocalEvent { transaction_id } => { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 267 | (transaction_id, UploadOutcome::Cancelled) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 268 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 269 | _ => return None, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 270 | }; |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 271 | self.finish((room_id, transaction_id), outcome, true) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 272 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 273 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 274 | fn room(&mut self, report: RoomUpload) -> Option<Event> { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 275 | match report { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 276 | RoomUpload::Progress { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 277 | upload, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 278 | uploaded_bytes, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 279 | total_bytes, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 280 | complete, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 281 | } => { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 282 | if self.ended.contains(&upload) { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 283 | return None; |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 284 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 285 | self.live.insert(upload.clone()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 286 | let (room_id, transaction_id) = upload; |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 287 | Some(Event::MediaUploadProgress { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 288 | room_id, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 289 | transaction_id, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 290 | uploaded_bytes, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 291 | total_bytes, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 292 | complete, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 293 | }) |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 294 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 295 | RoomUpload::Ended(upload, outcome) => { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 296 | self.ended.remove(&upload); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 297 | self.finish(upload, outcome, false) |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 298 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 299 | RoomUpload::Lagged => None, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 300 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 301 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 302 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 303 | fn resnapshot(&mut self, current: HashSet<Upload>) -> Vec<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 304 | let reports = self.finish_where(|upload| !current.contains(upload), &UploadOutcome::Missed); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 305 | let ended = &self.ended; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 306 | self.live |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 307 | .extend(current.into_iter().filter(|upload| !ended.contains(upload))); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 308 | reports |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 309 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 310 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 311 | fn finish_where( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 312 | &mut self, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 313 | ends: impl Fn(&Upload) -> bool, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 314 | outcome: &UploadOutcome, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 315 | ) -> Vec<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 316 | let ending = self |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 317 | .live |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 318 | .iter() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 319 | .filter(|upload| ends(upload)) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 320 | .cloned() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 321 | .collect::<Vec<_>>(); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 322 | ending |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 323 | .into_iter() |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 324 | .filter_map(|upload| self.finish(upload, outcome.clone(), true)) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 325 | .collect() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 326 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 327 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 328 | fn finish(&mut self, upload: Upload, outcome: UploadOutcome, remember: bool) -> Option<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 329 | if !self.live.remove(&upload) { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 330 | return None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 331 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 332 | if remember { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 333 | self.ended.insert(upload.clone()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 334 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 335 | let (room_id, transaction_id) = upload; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 336 | Some(Event::MediaUploadFinished { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 337 | room_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 338 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 339 | outcome, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 340 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 341 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 342 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 343 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 344 | async fn attachment_echoes(client: &Client) -> HashSet<Upload> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 345 | let Ok(echoes) = client.send_queue().local_echoes().await else { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 346 | return HashSet::new(); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 347 | }; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 348 | echoes |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 349 | .into_iter() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 350 | .flat_map(|(room_id, echoes)| { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 351 | echoes |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 352 | .into_iter() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 353 | .filter(|echo| is_attachment_echo(&echo.content)) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 354 | .map(move |echo| (room_id.clone(), echo.transaction_id)) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 355 | }) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 356 | .collect() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 357 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 358 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 359 | fn is_attachment_echo(content: &LocalEchoContent) -> bool { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 360 | let LocalEchoContent::Event { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 361 | serialized_event, .. |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 362 | } = content |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 363 | else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 364 | return false; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 365 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 366 | matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 367 | serialized_event.deserialize(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 368 | Ok(AnyMessageLikeEventContent::RoomMessage(content)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | if matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | content.msgtype, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | MessageType::Audio(_) | MessageType::File(_) | MessageType::Image(_) | MessageType::Video(_) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 374 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 376 | pub(super) async fn encryption(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 377 | let Some(device_id) = client.device_id().map(ToOwned::to_owned) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 378 | tracing::warn!("encryption watcher stopped because the client has no device id"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 379 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 380 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 381 | let recovery = client.encryption().recovery(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 382 | let backups = client.encryption().backups(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 383 | let mut recovery_stream = recovery.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 384 | let mut backup_stream = backups.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 385 | let mut verification_stream = client.encryption().verification_state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 386 | let mut recovery_state = recovery.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 387 | let mut backup_state = backups.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 388 | let mut verification_state = verification_stream.get(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 389 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 390 | let state = encryption_state( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 391 | device_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 392 | recovery_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 393 | verification_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 394 | backup_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 395 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 396 | if events.send(Event::EncryptionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 397 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 398 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 399 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 400 | Some(update) = recovery_stream.next() => recovery_state = update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 401 | Some(update) = verification_stream.next() => verification_state = update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 402 | Some(update) = backup_stream.next() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 403 | backup_state = update.unwrap_or_else(|_| backups.state()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 404 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 405 | else => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 406 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 407 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 408 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 409 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 410 | pub(super) async fn rooms(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 411 | let (_, mut rooms) = client.rooms_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 412 | let mut info = client.room_info_notable_update_receiver(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 413 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 414 | if events.send(Event::RoomsChanged).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 415 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 416 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 417 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 418 | update = rooms.next() => if update.is_none() { return }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 419 | update = info.recv() => if matches!(update, Err(RecvError::Closed)) { return }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 420 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 421 | // a sync touching fifty rooms is one invalidation, not fifty |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 422 | while let Ok(_) | Err(tokio::sync::broadcast::error::TryRecvError::Lagged(_)) = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 423 | info.try_recv() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 424 | {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 425 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 426 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 427 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 428 | pub(super) async fn notification_settings(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 429 | let settings = client.notification_settings().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 430 | let mut changes = settings.subscribe_to_changes(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 431 | while let Ok(()) | Err(RecvError::Lagged(_)) = changes.recv().await { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 432 | // room summaries carry the effective mode, and push rule changes never touch room info |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 433 | for event in [Event::NotificationSettingsChanged, Event::RoomsChanged] { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 434 | if events.send(event).await.is_err() { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 435 | return; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 436 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 437 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 438 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 439 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 440 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 441 | #[cfg(test)] |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 442 | mod tests { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 443 | use matrix_sdk::send_queue::{RoomSendQueueUpdate, SendQueueUpdate}; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 444 | use ruma::{OwnedTransactionId, event_id, owned_room_id}; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 445 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 446 | use super::{HashSet, RoomUpload, Upload, UploadTracker}; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 447 | use crate::{Event, UploadOutcome}; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 448 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 449 | fn upload() -> Upload { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 450 | ( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 451 | owned_room_id!("!room:localhost"), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 452 | OwnedTransactionId::from("upload"), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 453 | ) |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 454 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 455 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 456 | fn tick() -> RoomUpload { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 457 | RoomUpload::Progress { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 458 | upload: upload(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 459 | uploaded_bytes: 1, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 460 | total_bytes: 2, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 461 | complete: false, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 462 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 463 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 464 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 465 | fn sent() -> UploadOutcome { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 466 | UploadOutcome::Sent { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 467 | event_id: event_id!("$sent:localhost").to_owned(), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 468 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 469 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 470 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 471 | fn tracker() -> UploadTracker { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 472 | UploadTracker { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 473 | live: HashSet::new(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 474 | ended: HashSet::new(), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 475 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 476 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 477 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 478 | fn finished(event: Option<Event>) -> Option<UploadOutcome> { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 479 | match event? { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 480 | Event::MediaUploadFinished { outcome, .. } => Some(outcome), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 481 | _ => None, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 482 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 483 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 484 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 485 | #[test] |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 486 | fn a_stale_tick_after_a_missed_end_changes_nothing() { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 487 | let mut tracker = tracker(); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 488 | assert!(tracker.room(tick()).is_some()); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 489 | let missed = tracker.resnapshot(HashSet::new()); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 490 | assert!(matches!( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 491 | missed.as_slice(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 492 | [Event::MediaUploadFinished { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 493 | outcome: UploadOutcome::Missed, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 494 | .. |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 495 | }] |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 496 | )); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 497 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 498 | assert!(tracker.room(tick()).is_none()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 499 | let (room_id, transaction_id) = upload(); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 500 | let global = SendQueueUpdate { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 501 | room_id, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 502 | update: RoomSendQueueUpdate::SentEvent { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 503 | transaction_id, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 504 | event_id: event_id!("$sent:localhost").to_owned(), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 505 | }, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 506 | }; |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 507 | assert!(tracker.update(global).is_none()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 508 | assert!(tracker.room(RoomUpload::Ended(upload(), sent())).is_none()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 509 | assert!(tracker.live.is_empty() && tracker.ended.is_empty()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 510 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 511 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 512 | #[test] |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 513 | fn an_end_the_tracker_never_knew_about_still_follows_its_progress() { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 514 | let mut tracker = tracker(); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 515 | let (room_id, transaction_id) = upload(); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 516 | let global = SendQueueUpdate { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 517 | room_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 518 | update: RoomSendQueueUpdate::SentEvent { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 519 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 520 | event_id: event_id!("$sent:localhost").to_owned(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 521 | }, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 522 | }; |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 523 | assert!(tracker.update(global).is_none()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 524 | assert!(tracker.room(tick()).is_some()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 525 | assert_eq!( |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 526 | finished(tracker.room(RoomUpload::Ended(upload(), sent()))), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 527 | Some(sent()) |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 528 | ); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 529 | assert!(tracker.live.is_empty() && tracker.ended.is_empty()); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 530 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 531 | } |