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