411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 1 | use std::{ |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 2 | collections::{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::{ |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 9 | Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 10 | ruma::events::{AnyMessageLikeEventContent, room::message::MessageType}, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 11 | send_queue::{LocalEchoContent, RoomSendQueueUpdate, SendQueueUpdate}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 12 | }; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 13 | use matrix_sdk_base::RoomInfoNotableUpdateReasons; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 14 | use matrix_sdk_ui::sync_service::{State as SyncState, SyncService}; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 15 | use ruma::{ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 16 | OwnedRoomId, OwnedTransactionId, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 17 | events::{AnyGlobalAccountDataEvent, AnyRoomAccountDataEvent, AnySyncStateEvent}, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 18 | serde::Raw, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 19 | time::Instant, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 20 | }; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 21 | use tokio::sync::{Notify, broadcast::error::RecvError, watch}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 22 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 23 | use super::executor::TaskSet; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 24 | use crate::{ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 25 | ConnectionState, Event, UploadOutcome, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 26 | encryption::encryption_state, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 27 | error::classify_sdk_error, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 28 | normalise::{ENABLED_ROOMS, ROOM_PACKS, USER_PACK}, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 29 | space_order, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 30 | transport::EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 31 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 32 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 33 | const SYNC_RESTART_MIN: Duration = Duration::from_secs(1); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 34 | const SYNC_RESTART_MAX: Duration = Duration::from_secs(60); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 35 | const SEND_QUEUE_RETRY: Duration = Duration::from_secs(5); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 36 | const SYNC_STABLE: Duration = Duration::from_secs(30); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 37 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 38 | pub(super) async fn room_subscriptions( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 39 | sync: Arc<SyncService>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 40 | mut rooms: watch::Receiver<Vec<ruma::OwnedRoomId>>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 41 | ) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 42 | while rooms.changed().await.is_ok() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 43 | let room_ids = rooms.borrow_and_update().clone(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 44 | let room_ids = room_ids.iter().map(AsRef::as_ref).collect::<Vec<_>>(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 45 | sync.room_list_service().subscribe_to_rooms(&room_ids).await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 46 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 47 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | /// `states` must be taken before sync starts, a late subscriber never hears the transition it missed |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | pub(super) async fn connection( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | mut states: impl futures_util::Stream<Item = SyncState> + Unpin, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | sync: Arc<SyncService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 53 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | events: EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | connection: watch::Sender<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 57 | let mut backoff = SYNC_RESTART_MIN; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 58 | let mut online_since: Option<Instant> = None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 59 | while let Some(state) = states.next().await { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 60 | // only a run that lasted counts as recovered, or a server that answers /versions |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 61 | // but refuses sync would have us bounce between offline and online forever |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 62 | let stable = online_since |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 63 | .take() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 64 | .map(|since| since.elapsed() >= SYNC_STABLE); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 65 | if stable == Some(true) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 66 | backoff = SYNC_RESTART_MIN; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 67 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | let (state, restart) = match state { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 69 | // only ever us stopping it for a restart, nothing a client needs to hear |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 70 | SyncState::Idle => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | SyncState::Running => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 72 | online_since = Some(Instant::now()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | // the sdk parks a room queue on any send error and never unparks it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | client.send_queue().set_enabled(true).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | (ConnectionState::Online, false) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 76 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 77 | SyncState::Offline => (ConnectionState::Offline, stable == Some(false)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | SyncState::Terminated => (ConnectionState::Offline, true), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | SyncState::Error(error) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | tracing::warn!(%error, "sync service reported an error"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | (ConnectionState::Offline, true) |
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 | if *connection.borrow() != state { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | connection.send_replace(state); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 86 | if events.send(Event::ConnectionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | if restart { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 91 | sync.stop().await; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 92 | matrix_sdk::sleep::sleep(backoff).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | backoff = (backoff * 2).min(SYNC_RESTART_MAX); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | sync.start().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | /// a recoverable failure while we're online is a hiccup, so give the room another go |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | pub(super) async fn send_queue_errors( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | mut errors: tokio::sync::broadcast::Receiver<matrix_sdk::send_queue::SendQueueRoomError>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | online: watch::Receiver<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | ) { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 105 | let mut retries = TaskSet::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | let error = tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | error = errors.recv() => error, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | Some(_) = retries.join_next(), if !retries.is_empty() => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 111 | match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | Ok(error) if error.is_recoverable => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | let client = client.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | let online = online.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | retries.spawn(async move { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 116 | matrix_sdk::sleep::sleep(SEND_QUEUE_RETRY).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | if *online.borrow() == ConnectionState::Online |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | && let Some(room) = client.get_room(&error.room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | room.send_queue().set_enabled(true); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 121 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 122 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 123 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 124 | Ok(_) | Err(RecvError::Lagged(_)) => {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 125 | Err(RecvError::Closed) => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 126 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 127 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 128 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 129 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 130 | /// the one place upload events leave from, so an upload is never reported after it |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 131 | /// ended. the client-wide channel exists before any upload does. it holds 32 updates and |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 132 | /// progress ticks share it, so this keeps draining while the event stream is full |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 133 | pub(super) async fn uploads( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 134 | client: Client, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 135 | mut updates: tokio::sync::broadcast::Receiver<SendQueueUpdate>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 136 | events: EventSender, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 137 | ) { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 138 | let (_, mut rooms) = client.rooms_stream(); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 139 | let mut tracker = UploadTracker::new(attachment_echoes(&client).await); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 140 | let mut pending = VecDeque::new(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 141 | loop { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 142 | let reports: Vec<Event> = tokio::select! { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 143 | biased; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 144 | update = updates.recv() => match update { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 145 | Ok(update) => tracker.update(update).into_iter().collect(), |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 146 | Err(RecvError::Lagged(skipped)) => { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 147 | tracing::warn!(skipped, "upload tracker lagged"); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 148 | tracker.resnapshot(attachment_echoes(&client).await) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 149 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 150 | Err(RecvError::Closed) => return, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 151 | }, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 152 | update = rooms.next() => { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 153 | if update.is_none() { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 154 | return; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 155 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 156 | tracker.finish_where( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 157 | |(room_id, _)| client.get_room(room_id).is_none(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 158 | &UploadOutcome::RoomForgotten, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 159 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 160 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 161 | permit = events.reserve(), if !pending.is_empty() => { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 162 | let Ok(permit) = permit else { return }; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 163 | permit.send(pending.pop_front().expect("pending was checked")); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 164 | continue; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 165 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 166 | }; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 167 | for event in reports { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 168 | queue_upload_event(&mut pending, event); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 169 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 170 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 171 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 172 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 173 | /// a progress tick replaces a waiting one for the same upload, unless something else about |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 174 | /// that upload is queued after it |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 175 | fn queue_upload_event(pending: &mut VecDeque<Event>, event: Event) { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 176 | if matches!(event, Event::MediaUploadProgress { .. }) |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 177 | && let Some(waiting) = pending |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 178 | .iter_mut() |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 179 | .rev() |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 180 | .find(|waiting| upload_of(waiting) == upload_of(&event)) |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 181 | && matches!(waiting, Event::MediaUploadProgress { .. }) |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 182 | { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 183 | *waiting = event; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 184 | return; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 185 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 186 | pending.push_back(event); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 187 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 188 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 189 | fn upload_of(event: &Event) -> Option<(&OwnedRoomId, &OwnedTransactionId)> { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 190 | match event { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 191 | Event::MediaUploadProgress { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 192 | room_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 193 | transaction_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 194 | .. |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 195 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 196 | | Event::MediaUploadFailed { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 197 | room_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 198 | transaction_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 199 | .. |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 200 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 201 | | Event::MediaUploadFinished { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 202 | room_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 203 | transaction_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 204 | .. |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 205 | } => Some((room_id, transaction_id)), |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 206 | _ => None, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 207 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 208 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 209 | |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 210 | type Upload = (OwnedRoomId, OwnedTransactionId); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 211 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 212 | /// the sdk sends progress ticks from a detached task, so a tick can land after its |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 213 | /// 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 | 214 | const REMEMBERED_ENDS: usize = 256; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 215 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 216 | /// everything reported gets exactly one `MediaUploadFinished` and nothing after it |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 217 | struct UploadTracker { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 218 | live: HashSet<Upload>, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 219 | ended: HashSet<Upload>, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 220 | ended_order: VecDeque<Upload>, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 221 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 222 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 223 | impl UploadTracker { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 224 | fn new(live: HashSet<Upload>) -> Self { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 225 | Self { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 226 | live, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 227 | ended: HashSet::new(), |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 228 | ended_order: VecDeque::new(), |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 229 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 230 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 231 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 232 | fn update(&mut self, update: SendQueueUpdate) -> Option<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 233 | let room_id = update.room_id; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 234 | let (transaction_id, outcome) = match update.update { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 235 | RoomSendQueueUpdate::NewLocalEvent(echo) => { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 236 | let upload = (room_id, echo.transaction_id); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 237 | if is_attachment_echo(&echo.content) && !self.ended.contains(&upload) { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 238 | self.live.insert(upload); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 239 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 240 | return None; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 241 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 242 | RoomSendQueueUpdate::MediaUpload { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 243 | related_to, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 244 | file, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 245 | progress, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 246 | .. |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 247 | } => { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 248 | let upload = (room_id, related_to); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 249 | if self.ended.contains(&upload) { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 250 | return None; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 251 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 252 | self.live.insert(upload.clone()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 253 | let (room_id, transaction_id) = upload; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 254 | return Some(Event::MediaUploadProgress { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 255 | room_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 256 | transaction_id, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 257 | uploaded_bytes: progress.current as u64, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 258 | total_bytes: progress.total as u64, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 259 | complete: file.is_some() && progress.current == progress.total, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 260 | }); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 261 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 262 | RoomSendQueueUpdate::SendError { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 263 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 264 | error, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 265 | is_recoverable, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 266 | } => { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 267 | let upload = (room_id, transaction_id); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 268 | if !self.live.contains(&upload) { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 269 | return None; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 270 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 271 | let (room_id, transaction_id) = upload; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 272 | return Some(Event::MediaUploadFailed { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 273 | room_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 274 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 275 | kind: classify_sdk_error(&error), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 276 | will_retry: is_recoverable, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 277 | }); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 278 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 279 | RoomSendQueueUpdate::SentEvent { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 280 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 281 | event_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 282 | } => (transaction_id, UploadOutcome::Sent { event_id }), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 283 | RoomSendQueueUpdate::CancelledLocalEvent { transaction_id } => { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 284 | (transaction_id, UploadOutcome::Cancelled) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 285 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 286 | _ => return None, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 287 | }; |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 288 | self.finish((room_id, transaction_id), outcome) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 289 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 290 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 291 | fn resnapshot(&mut self, current: HashSet<Upload>) -> Vec<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 292 | let reports = self.finish_where(|upload| !current.contains(upload), &UploadOutcome::Missed); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 293 | let ended = &self.ended; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 294 | self.live |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 295 | .extend(current.into_iter().filter(|upload| !ended.contains(upload))); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 296 | reports |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 297 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 298 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 299 | fn finish_where( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 300 | &mut self, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 301 | ends: impl Fn(&Upload) -> bool, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 302 | outcome: &UploadOutcome, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 303 | ) -> Vec<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 304 | let ending = self |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 305 | .live |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 306 | .iter() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 307 | .filter(|upload| ends(upload)) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 308 | .cloned() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 309 | .collect::<Vec<_>>(); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 310 | ending |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 311 | .into_iter() |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 312 | .filter_map(|upload| self.finish(upload, outcome.clone())) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 313 | .collect() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 314 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 315 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 316 | /// 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 | 317 | fn finish(&mut self, upload: Upload, outcome: UploadOutcome) -> Option<Event> { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 318 | if self.ended.insert(upload.clone()) { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 319 | self.ended_order.push_back(upload.clone()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 320 | if self.ended_order.len() > REMEMBERED_ENDS |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 321 | && let Some(oldest) = self.ended_order.pop_front() |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 322 | { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 323 | self.ended.remove(&oldest); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 324 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 325 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 326 | if !self.live.remove(&upload) { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 327 | return None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 328 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 329 | let (room_id, transaction_id) = upload; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 330 | Some(Event::MediaUploadFinished { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 331 | room_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 332 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 333 | outcome, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 334 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 335 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 336 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 337 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 338 | async fn attachment_echoes(client: &Client) -> HashSet<Upload> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 339 | let Ok(echoes) = client.send_queue().local_echoes().await else { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 340 | return HashSet::new(); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 341 | }; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 342 | echoes |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 343 | .into_iter() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 344 | .flat_map(|(room_id, echoes)| { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 345 | echoes |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 346 | .into_iter() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 347 | .filter(|echo| is_attachment_echo(&echo.content)) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 348 | .map(move |echo| (room_id.clone(), echo.transaction_id)) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 349 | }) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 350 | .collect() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 351 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 352 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 353 | fn is_attachment_echo(content: &LocalEchoContent) -> bool { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 354 | let LocalEchoContent::Event { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 355 | serialized_event, .. |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 356 | } = content |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 357 | else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 358 | return false; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 359 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 360 | matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 361 | serialized_event.deserialize(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 362 | Ok(AnyMessageLikeEventContent::RoomMessage(content)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 363 | if matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 364 | content.msgtype, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 365 | MessageType::Audio(_) | MessageType::File(_) | MessageType::Image(_) | MessageType::Video(_) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 366 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 367 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 368 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | pub(super) async fn encryption(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | let Some(device_id) = client.device_id().map(ToOwned::to_owned) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | tracing::warn!("encryption watcher stopped because the client has no device id"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 374 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | let recovery = client.encryption().recovery(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 376 | let backups = client.encryption().backups(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 377 | let mut recovery_stream = recovery.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 378 | let mut backup_stream = backups.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 379 | let mut verification_stream = client.encryption().verification_state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 380 | let mut recovery_state = recovery.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 381 | let mut backup_state = backups.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 382 | let mut verification_state = verification_stream.get(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 383 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 384 | let state = encryption_state( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 385 | device_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 386 | recovery_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 387 | verification_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 388 | backup_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 389 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 390 | if events.send(Event::EncryptionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 391 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 392 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 393 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 394 | Some(update) = recovery_stream.next() => recovery_state = update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 395 | Some(update) = verification_stream.next() => verification_state = update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 396 | Some(update) = backup_stream.next() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 397 | backup_state = update.unwrap_or_else(|_| backups.state()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 398 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 399 | else => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 400 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 401 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 402 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 403 | |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 404 | /// space orders are room account data, which never touches room info |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 405 | pub(super) async fn rooms(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 406 | let (_, mut rooms) = client.rooms_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 407 | let mut info = client.room_info_notable_update_receiver(); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 408 | let order_changed = Arc::new(Notify::new()); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 409 | let handler = client.add_event_handler({ |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 410 | let order_changed = order_changed.clone(); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 411 | move |event: Raw<AnyRoomAccountDataEvent>| { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 412 | let order_changed = order_changed.clone(); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 413 | async move { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 414 | if event_type(&event).as_deref() == Some(space_order::EVENT_TYPE) { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 415 | order_changed.notify_one(); |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 416 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 417 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 418 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 419 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 420 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 421 | if events.send(Event::RoomsChanged).await.is_err() { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 422 | break; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 423 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 424 | tokio::select! { |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 425 | update = rooms.next() => if update.is_none() { break }, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 426 | update = info.recv() => if matches!(update, Err(RecvError::Closed)) { break }, |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 427 | () = order_changed.notified() => {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 428 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 429 | // a sync touching fifty rooms is one invalidation, not fifty |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 430 | while let Ok(_) | Err(tokio::sync::broadcast::error::TryRecvError::Lagged(_)) = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 431 | info.try_recv() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 432 | {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 433 | } |
9bd34e25bb86 | atagen | 2026-10-24 07:23 UTC | 434 | client.remove_event_handler(handler); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 435 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 436 | |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 437 | /// 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 | 438 | pub(super) async fn image_packs(client: Client, events: EventSender) { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 439 | let changed = Arc::new(Notify::new()); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 440 | let handlers = [ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 441 | client.add_event_handler({ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 442 | let changed = changed.clone(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 443 | move |event: Raw<AnySyncStateEvent>| { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 444 | let changed = changed.clone(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 445 | async move { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 446 | if event_type(&event).as_deref() == Some(ROOM_PACKS) { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 447 | changed.notify_one(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 448 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 449 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 450 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 451 | }), |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 452 | client.add_event_handler({ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 453 | let changed = changed.clone(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 454 | move |event: Raw<AnyGlobalAccountDataEvent>| { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 455 | let changed = changed.clone(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 456 | async move { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 457 | if matches!( |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 458 | event_type(&event).as_deref(), |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 459 | Some(USER_PACK | ENABLED_ROOMS) |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 460 | ) { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 461 | changed.notify_one(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 462 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 463 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 464 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 465 | }), |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 466 | ]; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 467 | let mut info = client.room_info_notable_update_receiver(); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 468 | loop { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 469 | tokio::select! { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 470 | () = changed.notified() => {} |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 471 | update = info.recv() => match update { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 472 | Ok(update) if !update.reasons.contains(RoomInfoNotableUpdateReasons::MEMBERSHIP) => { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 473 | continue; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 474 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 475 | Ok(_) | Err(RecvError::Lagged(_)) => {} |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 476 | Err(RecvError::Closed) => break, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 477 | }, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 478 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 479 | if events.send(Event::ImagePacksChanged).await.is_err() { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 480 | break; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 481 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 482 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 483 | for handler in handlers { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 484 | client.remove_event_handler(handler); |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 485 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 486 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 487 | |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 488 | fn event_type<T>(event: &Raw<T>) -> Option<String> { |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 489 | event.get_field("type").ok().flatten() |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 490 | } |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 491 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 492 | pub(super) async fn notification_settings(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 493 | let settings = client.notification_settings().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 494 | let mut changes = settings.subscribe_to_changes(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 495 | while let Ok(()) | Err(RecvError::Lagged(_)) = changes.recv().await { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 496 | // room summaries carry the effective mode, and push rule changes never touch room info |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 497 | for event in [Event::NotificationSettingsChanged, Event::RoomsChanged] { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 498 | if events.send(event).await.is_err() { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 499 | return; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 500 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 501 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 502 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 503 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 504 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 505 | #[cfg(test)] |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 506 | mod tests { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 507 | use matrix_sdk::send_queue::{AbstractProgress, RoomSendQueueUpdate, SendQueueUpdate}; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 508 | use ruma::{OwnedTransactionId, event_id, owned_room_id}; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 509 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 510 | use super::{HashSet, Upload, UploadTracker}; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 511 | use crate::{Event, UploadOutcome}; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 512 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 513 | fn upload() -> Upload { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 514 | ( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 515 | owned_room_id!("!room:localhost"), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 516 | OwnedTransactionId::from("upload"), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 517 | ) |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 518 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 519 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 520 | fn update(update: RoomSendQueueUpdate) -> SendQueueUpdate { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 521 | SendQueueUpdate { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 522 | room_id: upload().0, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 523 | update, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 524 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 525 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 526 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 527 | fn tick() -> SendQueueUpdate { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 528 | update(RoomSendQueueUpdate::MediaUpload { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 529 | related_to: upload().1, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 530 | file: None, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 531 | index: 0, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 532 | progress: AbstractProgress { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 533 | current: 1, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 534 | total: 2, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 535 | }, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 536 | }) |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 537 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 538 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 539 | fn sent() -> SendQueueUpdate { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 540 | update(RoomSendQueueUpdate::SentEvent { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 541 | transaction_id: upload().1, |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 542 | event_id: event_id!("$sent:localhost").to_owned(), |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 543 | }) |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 544 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 545 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 546 | fn finished(event: Option<Event>) -> Option<UploadOutcome> { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 547 | match event? { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 548 | Event::MediaUploadFinished { outcome, .. } => Some(outcome), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 549 | _ => None, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 550 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 551 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 552 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 553 | #[test] |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 554 | fn a_tick_trailing_its_end_says_nothing() { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 555 | let mut tracker = UploadTracker::new(HashSet::new()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 556 | assert!(tracker.update(tick()).is_some()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 557 | assert!(matches!( |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 558 | finished(tracker.update(sent())), |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 559 | Some(UploadOutcome::Sent { .. }) |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 560 | )); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 561 | assert!(tracker.update(tick()).is_none()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 562 | assert!(tracker.live.is_empty()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 563 | } |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 564 | |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 565 | #[test] |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 566 | fn a_stale_tick_after_a_missed_end_changes_nothing() { |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 567 | let mut tracker = UploadTracker::new(HashSet::new()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 568 | assert!(tracker.update(tick()).is_some()); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 569 | let missed = tracker.resnapshot(HashSet::new()); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 570 | assert!(matches!( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 571 | missed.as_slice(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 572 | [Event::MediaUploadFinished { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 573 | outcome: UploadOutcome::Missed, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 574 | .. |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 575 | }] |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 576 | )); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 577 | assert!(tracker.update(tick()).is_none()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 578 | assert!(tracker.update(sent()).is_none()); |
411bc1c78b65 | atagen | 2026-10-22 07:39 UTC | 579 | assert!(tracker.live.is_empty()); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 580 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 581 | } |