411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 1 | use std::{ |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 2 | collections::{HashMap, HashSet, VecDeque}, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 3 | sync::Arc, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 4 | time::Duration, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 5 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 7 | use futures_util::StreamExt; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 8 | use matrix_sdk::{ |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 9 | Client, Room, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 10 | event_handler::EventHandlerHandle, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 11 | ruma::events::{AnyMessageLikeEventContent, room::message::MessageType}, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 12 | send_queue::{LocalEchoContent, RoomSendQueueUpdate, SendQueueUpdate}, |
60454002af5f | atagen | 2026-10-25 11:13 UTC | 13 | serde_helpers::extract_thread_root, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | }; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 15 | use matrix_sdk_base::RoomInfoNotableUpdateReasons; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 16 | use matrix_sdk_ui::sync_service::{State as SyncState, SyncService}; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 17 | use ruma::{ |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 18 | OwnedEventId, OwnedRoomId, OwnedTransactionId, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 19 | events::{ |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 20 | AnyGlobalAccountDataEvent, AnyRoomAccountDataEvent, AnySyncStateEvent, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 21 | AnySyncTimelineEvent, receipt::SyncReceiptEvent, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 22 | }, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 23 | serde::Raw, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 24 | time::Instant, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 25 | }; |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 26 | use tokio::sync::{ |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 27 | Notify, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 28 | broadcast::error::{RecvError, TryRecvError}, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 29 | mpsc, watch, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 30 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 31 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 32 | use super::executor::TaskSet; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 33 | use crate::{ |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 34 | ConnectionState, Event, UploadOutcome, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 35 | encryption::encryption_state, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 36 | error::classify_sdk_error, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 37 | normalise::{ENABLED_ROOMS, ROOM_PACKS, USER_PACK, recount_thread_unreads}, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 38 | space_order, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 39 | transport::EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 40 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 41 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 42 | const SYNC_RESTART_MIN: Duration = Duration::from_secs(1); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | const SYNC_RESTART_MAX: Duration = Duration::from_secs(60); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | const SEND_QUEUE_RETRY: Duration = Duration::from_secs(5); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 45 | const SYNC_STABLE: Duration = Duration::from_secs(30); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 46 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 47 | pub(super) async fn room_subscriptions( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 48 | sync: Arc<SyncService>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 49 | mut rooms: watch::Receiver<Vec<ruma::OwnedRoomId>>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 50 | ) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 51 | while rooms.changed().await.is_ok() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 52 | let room_ids = rooms.borrow_and_update().clone(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 53 | let room_ids = room_ids.iter().map(AsRef::as_ref).collect::<Vec<_>>(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 54 | sync.room_list_service().subscribe_to_rooms(&room_ids).await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 55 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 56 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 58 | /// `states` must be taken before sync starts, a late subscriber never hears the transition it missed |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | pub(super) async fn connection( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | mut states: impl futures_util::Stream<Item = SyncState> + Unpin, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 61 | sync: Arc<SyncService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 62 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | events: EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 64 | connection: watch::Sender<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 65 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | let mut backoff = SYNC_RESTART_MIN; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 67 | let mut online_since: Option<Instant> = None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | while let Some(state) = states.next().await { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 69 | // only a run that lasted counts as recovered, or a server that answers /versions |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 70 | // but refuses sync would have us bounce between offline and online forever |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 71 | let stable = online_since |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 72 | .take() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 73 | .map(|since| since.elapsed() >= SYNC_STABLE); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 74 | if stable == Some(true) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 75 | backoff = SYNC_RESTART_MIN; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 76 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | let (state, restart) = match state { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 78 | // only ever us stopping it for a restart, nothing a client needs to hear |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 79 | SyncState::Idle => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | SyncState::Running => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 81 | online_since = Some(Instant::now()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | // the sdk parks a room queue on any send error and never unparks it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | client.send_queue().set_enabled(true).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | (ConnectionState::Online, false) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 86 | SyncState::Offline => (ConnectionState::Offline, stable == Some(false)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | SyncState::Terminated => (ConnectionState::Offline, true), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | SyncState::Error(error) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | tracing::warn!(%error, "sync service reported an error"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | (ConnectionState::Offline, true) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | if *connection.borrow() != state { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | connection.send_replace(state); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | if events.send(Event::ConnectionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | if restart { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 100 | sync.stop().await; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 101 | matrix_sdk::sleep::sleep(backoff).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | backoff = (backoff * 2).min(SYNC_RESTART_MAX); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | sync.start().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | } |
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 | /// a recoverable failure while we're online is a hiccup, so give the room another go |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | pub(super) async fn send_queue_errors( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | mut errors: tokio::sync::broadcast::Receiver<matrix_sdk::send_queue::SendQueueRoomError>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | online: watch::Receiver<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | ) { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 114 | let mut retries = TaskSet::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | let error = tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | error = errors.recv() => error, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | Some(_) = retries.join_next(), if !retries.is_empty() => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | Ok(error) if error.is_recoverable => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | let client = client.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | let online = online.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | retries.spawn(async move { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 125 | matrix_sdk::sleep::sleep(SEND_QUEUE_RETRY).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | if *online.borrow() == ConnectionState::Online |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | && let Some(room) = client.get_room(&error.room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | room.send_queue().set_enabled(true); |
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 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | Ok(_) | Err(RecvError::Lagged(_)) => {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | Err(RecvError::Closed) => return, |
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 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 139 | /// the one place upload events leave from, so an upload is never reported after it |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 140 | /// ended. the client-wide channel exists before any upload does. it holds 32 updates and |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 141 | /// progress ticks share it, so this keeps draining while the event stream is full |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 142 | pub(super) async fn uploads( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 143 | client: Client, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 144 | mut updates: tokio::sync::broadcast::Receiver<SendQueueUpdate>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 145 | events: EventSender, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 146 | ) { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 147 | let (_, mut rooms) = client.rooms_stream(); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 148 | let mut tracker = UploadTracker::new(attachment_echoes(&client).await); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 149 | let mut pending = VecDeque::new(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 150 | loop { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 151 | let reports: Vec<Event> = tokio::select! { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 152 | biased; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 153 | update = updates.recv() => match update { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 154 | Ok(update) => tracker.update(update).into_iter().collect(), |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 155 | Err(RecvError::Lagged(skipped)) => { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 156 | tracing::warn!(skipped, "upload tracker lagged"); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 157 | tracker.resnapshot(attachment_echoes(&client).await) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 158 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 159 | Err(RecvError::Closed) => return, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 160 | }, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 161 | update = rooms.next() => { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 162 | if update.is_none() { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 163 | return; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 164 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 165 | tracker.finish_where( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 166 | |(room_id, _)| client.get_room(room_id).is_none(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 167 | &UploadOutcome::RoomForgotten, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 168 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 169 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 170 | permit = events.reserve(), if !pending.is_empty() => { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 171 | let Ok(permit) = permit else { return }; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 172 | permit.send(pending.pop_front().expect("pending was checked")); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 173 | continue; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 174 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 175 | }; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 176 | for event in reports { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 177 | queue_upload_event(&mut pending, event); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 178 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 179 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 180 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 181 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 182 | /// a progress tick replaces a waiting one for the same upload, unless something else about |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 183 | /// that upload is queued after it |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 184 | fn queue_upload_event(pending: &mut VecDeque<Event>, event: Event) { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 185 | if matches!(event, Event::MediaUploadProgress { .. }) |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 186 | && let Some(waiting) = pending |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 187 | .iter_mut() |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 188 | .rev() |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 189 | .find(|waiting| upload_of(waiting) == upload_of(&event)) |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 190 | && matches!(waiting, Event::MediaUploadProgress { .. }) |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 191 | { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 192 | *waiting = event; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 193 | return; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 194 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 195 | pending.push_back(event); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 196 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 197 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 198 | fn upload_of(event: &Event) -> Option<(&OwnedRoomId, &OwnedTransactionId)> { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 199 | match event { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 200 | Event::MediaUploadProgress { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 201 | room_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 202 | transaction_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 203 | .. |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 204 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 205 | | Event::MediaUploadFailed { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 206 | room_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 207 | transaction_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 208 | .. |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 209 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 210 | | Event::MediaUploadFinished { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 211 | room_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 212 | transaction_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 213 | .. |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 214 | } => Some((room_id, transaction_id)), |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 215 | _ => None, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 216 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 217 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 218 | |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 219 | type Upload = (OwnedRoomId, OwnedTransactionId); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 220 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 221 | /// the sdk sends progress ticks from a detached task, so a tick can land after its |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 222 | /// upload's end. it trails by one task hop, so a short memory of ends covers it |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 223 | const REMEMBERED_ENDS: usize = 256; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 224 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 225 | /// everything reported gets exactly one `MediaUploadFinished` and nothing after it |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 226 | struct UploadTracker { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 227 | live: HashSet<Upload>, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 228 | ended: HashSet<Upload>, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 229 | ended_order: VecDeque<Upload>, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 230 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 231 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 232 | impl UploadTracker { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 233 | fn new(live: HashSet<Upload>) -> Self { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 234 | Self { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 235 | live, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 236 | ended: HashSet::new(), |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 237 | ended_order: VecDeque::new(), |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 238 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 239 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 240 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 241 | fn update(&mut self, update: SendQueueUpdate) -> Option<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 242 | let room_id = update.room_id; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 243 | let (transaction_id, outcome) = match update.update { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 244 | RoomSendQueueUpdate::NewLocalEvent(echo) => { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 245 | let upload = (room_id, echo.transaction_id); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 246 | if is_attachment_echo(&echo.content) && !self.ended.contains(&upload) { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 247 | self.live.insert(upload); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 248 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 249 | return None; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 250 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 251 | RoomSendQueueUpdate::MediaUpload { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 252 | related_to, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 253 | file, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 254 | progress, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 255 | .. |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 256 | } => { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 257 | let upload = (room_id, related_to); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 258 | if self.ended.contains(&upload) { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 259 | return None; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 260 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 261 | self.live.insert(upload.clone()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 262 | let (room_id, transaction_id) = upload; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 263 | return Some(Event::MediaUploadProgress { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 264 | room_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 265 | transaction_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 266 | uploaded_bytes: progress.current as u64, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 267 | total_bytes: progress.total as u64, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 268 | complete: file.is_some() && progress.current == progress.total, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 269 | }); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 270 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 271 | RoomSendQueueUpdate::SendError { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 272 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 273 | error, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 274 | is_recoverable, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 275 | } => { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 276 | let upload = (room_id, transaction_id); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 277 | if !self.live.contains(&upload) { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 278 | return None; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 279 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 280 | let (room_id, transaction_id) = upload; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 281 | return Some(Event::MediaUploadFailed { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 282 | room_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 283 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 284 | kind: classify_sdk_error(&error), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 285 | will_retry: is_recoverable, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 286 | }); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 287 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 288 | RoomSendQueueUpdate::SentEvent { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 289 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 290 | event_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 291 | } => (transaction_id, UploadOutcome::Sent { event_id }), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 292 | RoomSendQueueUpdate::CancelledLocalEvent { transaction_id } => { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 293 | (transaction_id, UploadOutcome::Cancelled) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 294 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 295 | _ => return None, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 296 | }; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 297 | self.finish((room_id, transaction_id), outcome) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 298 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 299 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 300 | fn resnapshot(&mut self, current: HashSet<Upload>) -> Vec<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 301 | let reports = self.finish_where(|upload| !current.contains(upload), &UploadOutcome::Missed); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 302 | let ended = &self.ended; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 303 | self.live |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 304 | .extend(current.into_iter().filter(|upload| !ended.contains(upload))); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 305 | reports |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 306 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 307 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 308 | fn finish_where( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 309 | &mut self, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 310 | ends: impl Fn(&Upload) -> bool, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 311 | outcome: &UploadOutcome, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 312 | ) -> Vec<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 313 | let ending = self |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 314 | .live |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 315 | .iter() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 316 | .filter(|upload| ends(upload)) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 317 | .cloned() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 318 | .collect::<Vec<_>>(); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 319 | ending |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 320 | .into_iter() |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 321 | .filter_map(|upload| self.finish(upload, outcome.clone())) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 322 | .collect() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 323 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 324 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 325 | /// an end for an upload we never saw is remembered too, its ticks may still be coming |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 326 | fn finish(&mut self, upload: Upload, outcome: UploadOutcome) -> Option<Event> { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 327 | if self.ended.insert(upload.clone()) { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 328 | self.ended_order.push_back(upload.clone()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 329 | if self.ended_order.len() > REMEMBERED_ENDS |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 330 | && let Some(oldest) = self.ended_order.pop_front() |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 331 | { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 332 | self.ended.remove(&oldest); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 333 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 334 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 335 | if !self.live.remove(&upload) { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 336 | return None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 337 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 338 | let (room_id, transaction_id) = upload; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 339 | Some(Event::MediaUploadFinished { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 340 | room_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 341 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 342 | outcome, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 343 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 344 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 345 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 346 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 347 | async fn attachment_echoes(client: &Client) -> HashSet<Upload> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 348 | let Ok(echoes) = client.send_queue().local_echoes().await else { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 349 | return HashSet::new(); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 350 | }; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 351 | echoes |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 352 | .into_iter() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 353 | .flat_map(|(room_id, echoes)| { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 354 | echoes |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 355 | .into_iter() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 356 | .filter(|echo| is_attachment_echo(&echo.content)) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 357 | .map(move |echo| (room_id.clone(), echo.transaction_id)) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 358 | }) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 359 | .collect() |
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 | fn is_attachment_echo(content: &LocalEchoContent) -> bool { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 363 | let LocalEchoContent::Event { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 364 | serialized_event, .. |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 365 | } = content |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 366 | else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 367 | return false; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 368 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | serialized_event.deserialize(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | Ok(AnyMessageLikeEventContent::RoomMessage(content)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | if matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | content.msgtype, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 374 | MessageType::Audio(_) | MessageType::File(_) | MessageType::Image(_) | MessageType::Video(_) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | ) |
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 | |
743123167e29 | atagen | 2026-10-26 04:02 UTC | 379 | pub(super) async fn encryption( |
743123167e29 | atagen | 2026-10-26 04:02 UTC | 380 | client: Client, |
743123167e29 | atagen | 2026-10-26 04:02 UTC | 381 | events: EventSender, |
743123167e29 | atagen | 2026-10-26 04:02 UTC | 382 | mut enabling: watch::Receiver<bool>, |
743123167e29 | atagen | 2026-10-26 04:02 UTC | 383 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 384 | let Some(device_id) = client.device_id().map(ToOwned::to_owned) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 385 | tracing::warn!("encryption watcher stopped because the client has no device id"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 386 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 387 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 388 | let recovery = client.encryption().recovery(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 389 | let backups = client.encryption().backups(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 390 | let mut recovery_stream = recovery.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 391 | let mut backup_stream = backups.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 392 | let mut verification_stream = client.encryption().verification_state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 393 | let mut recovery_state = recovery.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 394 | let mut backup_state = backups.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 395 | let mut verification_state = verification_stream.get(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 396 | loop { |
743123167e29 | atagen | 2026-10-26 04:02 UTC | 397 | let mut state = encryption_state( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 398 | device_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 399 | recovery_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 400 | verification_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 401 | backup_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 402 | ); |
743123167e29 | atagen | 2026-10-26 04:02 UTC | 403 | if *enabling.borrow_and_update() { |
743123167e29 | atagen | 2026-10-26 04:02 UTC | 404 | state.recovery = crate::RecoveryState::Enabling; |
743123167e29 | atagen | 2026-10-26 04:02 UTC | 405 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 406 | if events.send(Event::EncryptionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 407 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 408 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 409 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 410 | Some(update) = recovery_stream.next() => recovery_state = update, |
743123167e29 | atagen | 2026-10-26 04:02 UTC | 411 | Ok(()) = enabling.changed() => {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 412 | Some(update) = verification_stream.next() => verification_state = update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 413 | Some(update) = backup_stream.next() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 414 | backup_state = update.unwrap_or_else(|_| backups.state()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 415 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 416 | else => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 417 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 418 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 419 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 420 | |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 421 | /// space orders, pins and power levels all feed summaries without raising a notable room info update |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 422 | pub(super) async fn rooms(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 423 | let (_, mut rooms) = client.rooms_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 424 | let mut info = client.room_info_notable_update_receiver(); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 425 | let changed = Arc::new(Notify::new()); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 426 | let handlers = [ |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 427 | client.add_event_handler({ |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 428 | let changed = changed.clone(); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 429 | move |event: Raw<AnyRoomAccountDataEvent>| { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 430 | let changed = changed.clone(); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 431 | async move { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 432 | if event_type(&event).as_deref() == Some(space_order::EVENT_TYPE) { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 433 | changed.notify_one(); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 434 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 435 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 436 | } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 437 | }), |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 438 | client.add_event_handler({ |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 439 | let changed = changed.clone(); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 440 | move |event: Raw<AnySyncStateEvent>| { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 441 | let changed = changed.clone(); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 442 | async move { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 443 | if matches!( |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 444 | event_type(&event).as_deref(), |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 445 | Some("m.room.pinned_events" | "m.room.power_levels") |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 446 | ) { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 447 | changed.notify_one(); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 448 | } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 449 | } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 450 | } |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 451 | }), |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 452 | ]; |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 453 | let (touches, mut touched) = mpsc::unbounded_channel(); |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 454 | let thread_handlers = thread_handlers(&client, &touches); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 455 | let mut timelines = client.event_cache().subscribe_to_room_generic_updates(); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 456 | 'invalidate: loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 457 | if events.send(Event::RoomsChanged).await.is_err() { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 458 | break; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 459 | } |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 460 | // thread events and threaded receipts leave room info alone, so neither reaches `info` |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 461 | let mut pending = Touched::default(); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 462 | loop { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 463 | let mut invalidate = tokio::select! { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 464 | update = rooms.next() => if update.is_none() { break 'invalidate } else { true }, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 465 | update = info.recv() => { |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 466 | if matches!(update, Err(RecvError::Closed)) { break 'invalidate } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 467 | true |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 468 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 469 | () = changed.notified() => true, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 470 | Some((room_id, root)) = touched.recv() => { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 471 | pending.add(room_id, root); |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 472 | false |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 473 | } |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 474 | update = timelines.recv() => match update { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 475 | Ok(update) => { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 476 | pending.add(update.room_id, None); |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 477 | false |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 478 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 479 | Err(RecvError::Lagged(_)) => true, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 480 | Err(RecvError::Closed) => break 'invalidate, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 481 | }, |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 482 | }; |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 483 | // a sync's room info lands before the event cache has stored its events, so |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 484 | // give it a moment and take the whole sync as one invalidation |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 485 | matrix_sdk::sleep::sleep(SYNC_SETTLE).await; |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 486 | loop { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 487 | match info.try_recv() { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 488 | Ok(_) | Err(TryRecvError::Lagged(_)) => invalidate = true, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 489 | Err(_) => break, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 490 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 491 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 492 | while let Ok((room_id, root)) = touched.try_recv() { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 493 | pending.add(room_id, root); |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 494 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 495 | loop { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 496 | match timelines.try_recv() { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 497 | Ok(update) => pending.add(update.room_id, None), |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 498 | Err(TryRecvError::Lagged(_)) => invalidate = true, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 499 | Err(_) => break, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 500 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 501 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 502 | for (room_id, roots) in pending.0.drain() { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 503 | if let Some(room) = client.get_room(&room_id) |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 504 | && recount_thread_unreads(&room, roots).await.is_some() |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 505 | { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 506 | invalidate = true; |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 507 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 508 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 509 | if invalidate { |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 510 | break; |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 511 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 512 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 513 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 514 | for handler in handlers.into_iter().chain(thread_handlers) { |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 515 | client.remove_event_handler(handler); |
fa5b4e09943a | atagen | 2026-10-24 09:33 UTC | 516 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 517 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 518 | |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 519 | const SYNC_SETTLE: Duration = Duration::from_millis(100); |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 520 | |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 521 | /// rooms to recount, with the threads a sync brought news of |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 522 | #[derive(Default)] |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 523 | struct Touched(HashMap<OwnedRoomId, HashSet<OwnedEventId>>); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 524 | |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 525 | impl Touched { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 526 | fn add(&mut self, room_id: OwnedRoomId, root: Option<OwnedEventId>) { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 527 | let roots = self.0.entry(room_id).or_default(); |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 528 | roots.extend(root); |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 529 | } |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 530 | } |
e5c6427adb35 | atagen | 2026-10-25 10:23 UTC | 531 | |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 532 | /// a thread event names its thread before the event cache has it, and only the user's own |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 533 | /// receipts move their counts |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 534 | fn thread_handlers( |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 535 | client: &Client, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 536 | touches: &mpsc::UnboundedSender<(OwnedRoomId, Option<OwnedEventId>)>, |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 537 | ) -> [EventHandlerHandle; 2] { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 538 | let own_user_id = client.user_id().map(ToOwned::to_owned); |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 539 | [ |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 540 | client.add_event_handler({ |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 541 | let touches = touches.clone(); |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 542 | move |event: Raw<AnySyncTimelineEvent>, room: Room| { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 543 | if let Some(root) = extract_thread_root(&event) { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 544 | let _ = touches.send((room.room_id().to_owned(), Some(root))); |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 545 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 546 | async {} |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 547 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 548 | }), |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 549 | client.add_event_handler({ |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 550 | let touches = touches.clone(); |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 551 | move |event: SyncReceiptEvent, room: Room| { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 552 | let own = own_user_id.as_ref().is_some_and(|own_user_id| { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 553 | event |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 554 | .content |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 555 | .0 |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 556 | .values() |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 557 | .flat_map(|receipts| receipts.values()) |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 558 | .any(|users| users.contains_key(own_user_id)) |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 559 | }); |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 560 | if own { |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 561 | let _ = touches.send((room.room_id().to_owned(), None)); |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 562 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 563 | async {} |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 564 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 565 | }), |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 566 | ] |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 567 | } |
21b9227729a4 | atagen | 2026-10-25 11:03 UTC | 568 | |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 569 | /// packs live in room state and account data, and leaving a room takes its packs with it |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 570 | pub(super) async fn image_packs(client: Client, events: EventSender) { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 571 | let changed = Arc::new(Notify::new()); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 572 | let handlers = [ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 573 | client.add_event_handler({ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 574 | let changed = changed.clone(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 575 | move |event: Raw<AnySyncStateEvent>| { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 576 | let changed = changed.clone(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 577 | async move { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 578 | if event_type(&event).as_deref() == Some(ROOM_PACKS) { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 579 | changed.notify_one(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 580 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 581 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 582 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 583 | }), |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 584 | client.add_event_handler({ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 585 | let changed = changed.clone(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 586 | move |event: Raw<AnyGlobalAccountDataEvent>| { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 587 | let changed = changed.clone(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 588 | async move { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 589 | if matches!( |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 590 | event_type(&event).as_deref(), |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 591 | Some(USER_PACK | ENABLED_ROOMS) |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 592 | ) { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 593 | changed.notify_one(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 594 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 595 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 596 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 597 | }), |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 598 | ]; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 599 | let mut info = client.room_info_notable_update_receiver(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 600 | loop { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 601 | tokio::select! { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 602 | () = changed.notified() => {} |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 603 | update = info.recv() => match update { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 604 | Ok(update) if !update.reasons.contains(RoomInfoNotableUpdateReasons::MEMBERSHIP) => { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 605 | continue; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 606 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 607 | Ok(_) | Err(RecvError::Lagged(_)) => {} |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 608 | Err(RecvError::Closed) => break, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 609 | }, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 610 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 611 | if events.send(Event::ImagePacksChanged).await.is_err() { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 612 | break; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 613 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 614 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 615 | for handler in handlers { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 616 | client.remove_event_handler(handler); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 617 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 618 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 619 | |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 620 | fn event_type<T>(event: &Raw<T>) -> Option<String> { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 621 | event.get_field("type").ok().flatten() |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 622 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 623 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 624 | pub(super) async fn notification_settings(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 625 | let settings = client.notification_settings().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 626 | let mut changes = settings.subscribe_to_changes(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 627 | while let Ok(()) | Err(RecvError::Lagged(_)) = changes.recv().await { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 628 | // room summaries carry the effective mode, and push rule changes never touch room info |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 629 | for event in [Event::NotificationSettingsChanged, Event::RoomsChanged] { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 630 | if events.send(event).await.is_err() { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 631 | return; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 632 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 633 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 634 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 635 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 636 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 637 | #[cfg(test)] |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 638 | mod tests { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 639 | use matrix_sdk::send_queue::{AbstractProgress, RoomSendQueueUpdate, SendQueueUpdate}; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 640 | use ruma::{OwnedTransactionId, event_id, owned_room_id}; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 641 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 642 | use super::{HashSet, Upload, UploadTracker}; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 643 | use crate::{Event, UploadOutcome}; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 644 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 645 | fn upload() -> Upload { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 646 | ( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 647 | owned_room_id!("!room:localhost"), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 648 | OwnedTransactionId::from("upload"), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 649 | ) |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 650 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 651 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 652 | fn update(update: RoomSendQueueUpdate) -> SendQueueUpdate { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 653 | SendQueueUpdate { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 654 | room_id: upload().0, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 655 | update, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 656 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 657 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 658 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 659 | fn tick() -> SendQueueUpdate { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 660 | update(RoomSendQueueUpdate::MediaUpload { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 661 | related_to: upload().1, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 662 | file: None, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 663 | index: 0, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 664 | progress: AbstractProgress { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 665 | current: 1, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 666 | total: 2, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 667 | }, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 668 | }) |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 669 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 670 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 671 | fn sent() -> SendQueueUpdate { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 672 | update(RoomSendQueueUpdate::SentEvent { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 673 | transaction_id: upload().1, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 674 | event_id: event_id!("$sent:localhost").to_owned(), |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 675 | }) |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 676 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 677 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 678 | fn finished(event: Option<Event>) -> Option<UploadOutcome> { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 679 | match event? { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 680 | Event::MediaUploadFinished { outcome, .. } => Some(outcome), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 681 | _ => None, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 682 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 683 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 684 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 685 | #[test] |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 686 | fn a_tick_trailing_its_end_says_nothing() { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 687 | let mut tracker = UploadTracker::new(HashSet::new()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 688 | assert!(tracker.update(tick()).is_some()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 689 | assert!(matches!( |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 690 | finished(tracker.update(sent())), |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 691 | Some(UploadOutcome::Sent { .. }) |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 692 | )); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 693 | assert!(tracker.update(tick()).is_none()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 694 | assert!(tracker.live.is_empty()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 695 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 696 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 697 | #[test] |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 698 | fn a_stale_tick_after_a_missed_end_changes_nothing() { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 699 | let mut tracker = UploadTracker::new(HashSet::new()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 700 | assert!(tracker.update(tick()).is_some()); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 701 | let missed = tracker.resnapshot(HashSet::new()); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 702 | assert!(matches!( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 703 | missed.as_slice(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 704 | [Event::MediaUploadFinished { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 705 | outcome: UploadOutcome::Missed, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 706 | .. |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 707 | }] |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 708 | )); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 709 | assert!(tracker.update(tick()).is_none()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 710 | assert!(tracker.update(sent()).is_none()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 711 | assert!(tracker.live.is_empty()); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 712 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 713 | } |