0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 1 | use std::{collections::HashSet, sync::Arc, time::Duration}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 3 | use futures_util::StreamExt; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 4 | use matrix_sdk::{ |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 5 | Client, Room, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | ruma::events::{AnyMessageLikeEventContent, room::message::MessageType}, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 7 | send_queue::{LocalEchoContent, RoomSendQueueUpdate, SendQueueUpdate}, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 8 | }; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 9 | use matrix_sdk_base::RoomInfoNotableUpdateReasons; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 10 | use matrix_sdk_ui::sync_service::{State as SyncState, SyncService}; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 11 | use ruma::{ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 12 | OwnedRoomId, OwnedTransactionId, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 13 | events::{AnyGlobalAccountDataEvent, AnySyncStateEvent}, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 14 | serde::Raw, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 15 | time::Instant, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 16 | }; |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 17 | use tokio::sync::{Notify, broadcast::error::RecvError, mpsc, watch}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 18 | |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 19 | use super::executor::TaskSet; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 20 | use crate::{ |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 21 | ConnectionState, Event, UploadOutcome, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 22 | encryption::encryption_state, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 23 | error::classify_sdk_error, |
17e042fbc331 | atagen | 2026-10-08 12:39 UTC | 24 | normalise::{ENABLED_ROOMS, ROOM_PACKS, USER_PACK}, |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 25 | transport::EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 26 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 27 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 28 | const SYNC_RESTART_MIN: Duration = Duration::from_secs(1); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 29 | const SYNC_RESTART_MAX: Duration = Duration::from_secs(60); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 30 | const SEND_QUEUE_RETRY: Duration = Duration::from_secs(5); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 31 | const SYNC_STABLE: Duration = Duration::from_secs(30); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 32 | |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 33 | pub(super) async fn room_subscriptions( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 34 | sync: Arc<SyncService>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 35 | mut rooms: watch::Receiver<Vec<ruma::OwnedRoomId>>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 36 | ) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 37 | while rooms.changed().await.is_ok() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 38 | let room_ids = rooms.borrow_and_update().clone(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 39 | let room_ids = room_ids.iter().map(AsRef::as_ref).collect::<Vec<_>>(); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 40 | sync.room_list_service().subscribe_to_rooms(&room_ids).await; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 41 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 42 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 43 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 44 | /// `states` must be taken before sync starts, a late subscriber never hears the transition it missed |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 45 | pub(super) async fn connection( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 46 | mut states: impl futures_util::Stream<Item = SyncState> + Unpin, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 47 | sync: Arc<SyncService>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 48 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 49 | events: EventSender, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 50 | connection: watch::Sender<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 51 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 52 | let mut backoff = SYNC_RESTART_MIN; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 53 | let mut online_since: Option<Instant> = None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | while let Some(state) = states.next().await { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 55 | // only a run that lasted counts as recovered, or a server that answers /versions |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 56 | // but refuses sync would have us bounce between offline and online forever |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 57 | let stable = online_since |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 58 | .take() |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 59 | .map(|since| since.elapsed() >= SYNC_STABLE); |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 60 | if stable == Some(true) { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 61 | backoff = SYNC_RESTART_MIN; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 62 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 63 | let (state, restart) = match state { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 64 | // only ever us stopping it for a restart, nothing a client needs to hear |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 65 | SyncState::Idle => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 66 | SyncState::Running => { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 67 | online_since = Some(Instant::now()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 68 | // the sdk parks a room queue on any send error and never unparks it |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 69 | client.send_queue().set_enabled(true).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 70 | (ConnectionState::Online, false) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 71 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 72 | SyncState::Offline => (ConnectionState::Offline, stable == Some(false)), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 73 | SyncState::Terminated => (ConnectionState::Offline, true), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 74 | SyncState::Error(error) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 75 | tracing::warn!(%error, "sync service reported an error"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 76 | (ConnectionState::Offline, true) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 77 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 78 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 79 | if *connection.borrow() != state { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | connection.send_replace(state); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | if events.send(Event::ConnectionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | if restart { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 86 | sync.stop().await; |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 87 | matrix_sdk::sleep::sleep(backoff).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | backoff = (backoff * 2).min(SYNC_RESTART_MAX); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | sync.start().await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 91 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 92 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | /// a recoverable failure while we're online is a hiccup, so give the room another go |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 95 | pub(super) async fn send_queue_errors( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | client: Client, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | mut errors: tokio::sync::broadcast::Receiver<matrix_sdk::send_queue::SendQueueRoomError>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | online: watch::Receiver<ConnectionState>, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 99 | ) { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 100 | let mut retries = TaskSet::new(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 101 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | let error = tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | error = errors.recv() => error, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 104 | Some(_) = retries.join_next(), if !retries.is_empty() => continue, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | match error { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | Ok(error) if error.is_recoverable => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 108 | let client = client.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 109 | let online = online.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 110 | retries.spawn(async move { |
1fe480c16a3c | atagen | 2026-09-26 08:44 UTC | 111 | matrix_sdk::sleep::sleep(SEND_QUEUE_RETRY).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 112 | if *online.borrow() == ConnectionState::Online |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 113 | && let Some(room) = client.get_room(&error.room_id) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 114 | { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 115 | room.send_queue().set_enabled(true); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 116 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 117 | }); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 118 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 119 | Ok(_) | Err(RecvError::Lagged(_)) => {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 120 | Err(RecvError::Closed) => return, |
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 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 125 | /// upload progress only goes out on the room's own channel, never the client-wide one. |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 126 | /// the room's sends and cancels come along behind it, so an upload's end can't |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 127 | /// overtake its own progress |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 128 | pub(super) async fn room_uploads( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 129 | room: Room, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 130 | tracker: mpsc::Sender<RoomUpload>, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 131 | ready: watch::Sender<bool>, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 132 | ) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 133 | let room_id = room.room_id().to_owned(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 134 | let mut updates = match room.send_queue().subscribe().await { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 135 | Ok((_, updates)) => updates, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 136 | Err(error) => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 137 | tracing::warn!(%room_id, %error, "reading the media send queue failed"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 138 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 139 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 140 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 141 | ready.send_replace(true); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 142 | loop { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 143 | let report = match updates.recv().await { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 144 | Ok(RoomSendQueueUpdate::MediaUpload { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 145 | related_to, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 146 | file, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 147 | progress, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 148 | .. |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 149 | }) => RoomUpload::Progress { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 150 | upload: (room_id.clone(), related_to), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 151 | uploaded_bytes: progress.current as u64, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 152 | total_bytes: progress.total as u64, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 153 | complete: file.is_some() && progress.current == progress.total, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 154 | }, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 155 | Ok(RoomSendQueueUpdate::SentEvent { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 156 | transaction_id, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 157 | event_id, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 158 | }) => RoomUpload::Ended( |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 159 | (room_id.clone(), transaction_id), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 160 | UploadOutcome::Sent { event_id }, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 161 | ), |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 162 | Ok(RoomSendQueueUpdate::CancelledLocalEvent { transaction_id }) => { |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 163 | RoomUpload::Ended((room_id.clone(), transaction_id), UploadOutcome::Cancelled) |
f12377bce9b9 | atagen | 2026-09-26 12:32 UTC | 164 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 165 | Ok(_) => continue, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 166 | Err(RecvError::Lagged(_)) => RoomUpload::Lagged, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 167 | Err(RecvError::Closed) => return, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 168 | }; |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 169 | if tracker.send(report).await.is_err() { |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 170 | return; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 171 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 172 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 173 | } |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 174 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 175 | pub(super) enum RoomUpload { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 176 | Progress { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 177 | upload: Upload, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 178 | uploaded_bytes: u64, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 179 | total_bytes: u64, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 180 | complete: bool, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 181 | }, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 182 | Ended(Upload, UploadOutcome), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 183 | /// an end may have been lost with the ticks |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 184 | Lagged, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 185 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 186 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 187 | /// the one place upload events leave from, so an upload is never reported after it |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 188 | /// ended. progress comes in from the room forwarders, the rest from the client-wide |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 189 | /// channel, which exists before any upload does |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 190 | pub(super) async fn uploads( |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 191 | client: Client, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 192 | mut updates: tokio::sync::broadcast::Receiver<SendQueueUpdate>, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 193 | mut rooms_reports: mpsc::Receiver<RoomUpload>, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 194 | events: EventSender, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 195 | ) { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 196 | let (_, mut rooms) = client.rooms_stream(); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 197 | let mut tracker = UploadTracker { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 198 | live: attachment_echoes(&client).await, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 199 | ended: HashSet::new(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 200 | }; |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 201 | loop { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 202 | let reports = tokio::select! { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 203 | update = updates.recv() => match update { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 204 | Ok(update) => tracker.update(update).into_iter().collect(), |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 205 | Err(RecvError::Lagged(skipped)) => { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 206 | tracing::warn!(skipped, "upload tracker lagged"); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 207 | tracker.resnapshot(attachment_echoes(&client).await) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 208 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 209 | Err(RecvError::Closed) => return, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 210 | }, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 211 | Some(report) = rooms_reports.recv() => match report { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 212 | RoomUpload::Lagged => tracker.resnapshot(attachment_echoes(&client).await), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 213 | report => tracker.room(report).into_iter().collect(), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 214 | }, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 215 | update = rooms.next() => { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 216 | if update.is_none() { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 217 | return; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 218 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 219 | tracker.finish_where( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 220 | |(room_id, _)| client.get_room(room_id).is_none(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 221 | &UploadOutcome::RoomForgotten, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 222 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 223 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 224 | }; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 225 | for event in reports { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 226 | if events.send(event).await.is_err() { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 227 | return; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 228 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 229 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 230 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 231 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 232 | |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 233 | type Upload = (OwnedRoomId, OwnedTransactionId); |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 234 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 235 | /// everything reported gets exactly one `MediaUploadFinished`. an upload ended from |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 236 | /// anywhere but its room's forwarder goes in `ended` until the forwarder's own end for |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 237 | /// it arrives, since until then the forwarder can still be holding progress for it |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 238 | struct UploadTracker { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 239 | live: HashSet<Upload>, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 240 | ended: HashSet<Upload>, |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 241 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 242 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 243 | impl UploadTracker { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 244 | fn update(&mut self, update: SendQueueUpdate) -> Option<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 245 | let room_id = update.room_id; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 246 | let (transaction_id, outcome) = match update.update { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 247 | RoomSendQueueUpdate::NewLocalEvent(echo) => { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 248 | let upload = (room_id, echo.transaction_id); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 249 | if is_attachment_echo(&echo.content) && !self.ended.contains(&upload) { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 250 | self.live.insert(upload); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 251 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 252 | return None; |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 253 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 254 | RoomSendQueueUpdate::SendError { |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 255 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 256 | error, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 257 | is_recoverable, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 258 | } => { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 259 | let upload = (room_id, transaction_id); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 260 | if !self.live.contains(&upload) { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 261 | return None; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 262 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 263 | let (room_id, transaction_id) = upload; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 264 | return Some(Event::MediaUploadFailed { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 265 | room_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 266 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 267 | kind: classify_sdk_error(&error), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 268 | will_retry: is_recoverable, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 269 | }); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 270 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 271 | RoomSendQueueUpdate::SentEvent { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 272 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 273 | event_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 274 | } => (transaction_id, UploadOutcome::Sent { event_id }), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 275 | RoomSendQueueUpdate::CancelledLocalEvent { transaction_id } => { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 276 | (transaction_id, UploadOutcome::Cancelled) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 277 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 278 | _ => return None, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 279 | }; |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 280 | self.finish((room_id, transaction_id), outcome, true) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 281 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 282 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 283 | fn room(&mut self, report: RoomUpload) -> Option<Event> { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 284 | match report { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 285 | RoomUpload::Progress { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 286 | upload, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 287 | uploaded_bytes, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 288 | total_bytes, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 289 | complete, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 290 | } => { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 291 | if self.ended.contains(&upload) { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 292 | return None; |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 293 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 294 | self.live.insert(upload.clone()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 295 | let (room_id, transaction_id) = upload; |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 296 | Some(Event::MediaUploadProgress { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 297 | room_id, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 298 | transaction_id, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 299 | uploaded_bytes, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 300 | total_bytes, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 301 | complete, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 302 | }) |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 303 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 304 | RoomUpload::Ended(upload, outcome) => { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 305 | self.ended.remove(&upload); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 306 | self.finish(upload, outcome, false) |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 307 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 308 | RoomUpload::Lagged => None, |
5a52012c9b1d | atagen | 2026-09-24 23:45 UTC | 309 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 310 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 311 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 312 | fn resnapshot(&mut self, current: HashSet<Upload>) -> Vec<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 313 | let reports = self.finish_where(|upload| !current.contains(upload), &UploadOutcome::Missed); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 314 | let ended = &self.ended; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 315 | self.live |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 316 | .extend(current.into_iter().filter(|upload| !ended.contains(upload))); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 317 | reports |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 318 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 319 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 320 | fn finish_where( |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 321 | &mut self, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 322 | ends: impl Fn(&Upload) -> bool, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 323 | outcome: &UploadOutcome, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 324 | ) -> Vec<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 325 | let ending = self |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 326 | .live |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 327 | .iter() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 328 | .filter(|upload| ends(upload)) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 329 | .cloned() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 330 | .collect::<Vec<_>>(); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 331 | ending |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 332 | .into_iter() |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 333 | .filter_map(|upload| self.finish(upload, outcome.clone(), true)) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 334 | .collect() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 335 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 336 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 337 | fn finish(&mut self, upload: Upload, outcome: UploadOutcome, remember: bool) -> Option<Event> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 338 | if !self.live.remove(&upload) { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 339 | return None; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 340 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 341 | if remember { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 342 | self.ended.insert(upload.clone()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 343 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 344 | let (room_id, transaction_id) = upload; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 345 | Some(Event::MediaUploadFinished { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 346 | room_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 347 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 348 | outcome, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 349 | }) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 350 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 351 | } |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 352 | |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 353 | async fn attachment_echoes(client: &Client) -> HashSet<Upload> { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 354 | let Ok(echoes) = client.send_queue().local_echoes().await else { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 355 | return HashSet::new(); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 356 | }; |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 357 | echoes |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 358 | .into_iter() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 359 | .flat_map(|(room_id, echoes)| { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 360 | echoes |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 361 | .into_iter() |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 362 | .filter(|echo| is_attachment_echo(&echo.content)) |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 363 | .map(move |echo| (room_id.clone(), echo.transaction_id)) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 364 | }) |
0bfa3f46f0be | atagen | 2026-09-26 09:12 UTC | 365 | .collect() |
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 | fn is_attachment_echo(content: &LocalEchoContent) -> bool { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 369 | let LocalEchoContent::Event { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 370 | serialized_event, .. |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 371 | } = content |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 372 | else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 373 | return false; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 374 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 375 | matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 376 | serialized_event.deserialize(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 377 | Ok(AnyMessageLikeEventContent::RoomMessage(content)) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 378 | if matches!( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 379 | content.msgtype, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 380 | MessageType::Audio(_) | MessageType::File(_) | MessageType::Image(_) | MessageType::Video(_) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 381 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 382 | ) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 383 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 384 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 385 | pub(super) async fn encryption(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 386 | let Some(device_id) = client.device_id().map(ToOwned::to_owned) else { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 387 | tracing::warn!("encryption watcher stopped because the client has no device id"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 388 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 389 | }; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 390 | let recovery = client.encryption().recovery(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 391 | let backups = client.encryption().backups(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 392 | let mut recovery_stream = recovery.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 393 | let mut backup_stream = backups.state_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 394 | let mut verification_stream = client.encryption().verification_state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 395 | let mut recovery_state = recovery.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 396 | let mut backup_state = backups.state(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 397 | let mut verification_state = verification_stream.get(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 398 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 399 | let state = encryption_state( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 400 | device_id.clone(), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 401 | recovery_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 402 | verification_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 403 | backup_state, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 404 | ); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 405 | if events.send(Event::EncryptionChanged(state)).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 406 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 407 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 408 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 409 | Some(update) = recovery_stream.next() => recovery_state = update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 410 | Some(update) = verification_stream.next() => verification_state = update, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 411 | Some(update) = backup_stream.next() => { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 412 | backup_state = update.unwrap_or_else(|_| backups.state()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 413 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 414 | else => return, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 415 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 416 | } |
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 | pub(super) async fn rooms(client: Client, events: EventSender) { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 420 | let (_, mut rooms) = client.rooms_stream(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 421 | let mut info = client.room_info_notable_update_receiver(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 422 | loop { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 423 | if events.send(Event::RoomsChanged).await.is_err() { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 424 | return; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 425 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 426 | tokio::select! { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 427 | update = rooms.next() => if update.is_none() { return }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 428 | update = info.recv() => if matches!(update, Err(RecvError::Closed)) { return }, |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 429 | } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 430 | // a sync touching fifty rooms is one invalidation, not fifty |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 431 | while let Ok(_) | Err(tokio::sync::broadcast::error::TryRecvError::Lagged(_)) = |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 432 | info.try_recv() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 433 | {} |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 434 | } |
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 { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 507 | use matrix_sdk::send_queue::{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 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 510 | use super::{HashSet, RoomUpload, 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 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 520 | fn tick() -> RoomUpload { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 521 | RoomUpload::Progress { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 522 | upload: upload(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 523 | uploaded_bytes: 1, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 524 | total_bytes: 2, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 525 | complete: false, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 526 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 527 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 528 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 529 | fn sent() -> UploadOutcome { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 530 | UploadOutcome::Sent { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 531 | event_id: event_id!("$sent:localhost").to_owned(), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 532 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 533 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 534 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 535 | fn tracker() -> UploadTracker { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 536 | UploadTracker { |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 537 | live: HashSet::new(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 538 | ended: HashSet::new(), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 539 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 540 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 541 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 542 | fn finished(event: Option<Event>) -> Option<UploadOutcome> { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 543 | match event? { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 544 | Event::MediaUploadFinished { outcome, .. } => Some(outcome), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 545 | _ => None, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 546 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 547 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 548 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 549 | #[test] |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 550 | fn a_stale_tick_after_a_missed_end_changes_nothing() { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 551 | let mut tracker = tracker(); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 552 | assert!(tracker.room(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 | )); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 561 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 562 | assert!(tracker.room(tick()).is_none()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 563 | let (room_id, transaction_id) = upload(); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 564 | let global = SendQueueUpdate { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 565 | room_id, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 566 | update: RoomSendQueueUpdate::SentEvent { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 567 | transaction_id, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 568 | event_id: event_id!("$sent:localhost").to_owned(), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 569 | }, |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 570 | }; |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 571 | assert!(tracker.update(global).is_none()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 572 | assert!(tracker.room(RoomUpload::Ended(upload(), sent())).is_none()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 573 | assert!(tracker.live.is_empty() && tracker.ended.is_empty()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 574 | } |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 575 | |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 576 | #[test] |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 577 | fn an_end_the_tracker_never_knew_about_still_follows_its_progress() { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 578 | let mut tracker = tracker(); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 579 | let (room_id, transaction_id) = upload(); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 580 | let global = SendQueueUpdate { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 581 | room_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 582 | update: RoomSendQueueUpdate::SentEvent { |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 583 | transaction_id, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 584 | event_id: event_id!("$sent:localhost").to_owned(), |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 585 | }, |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 586 | }; |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 587 | assert!(tracker.update(global).is_none()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 588 | assert!(tracker.room(tick()).is_some()); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 589 | assert_eq!( |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 590 | finished(tracker.room(RoomUpload::Ended(upload(), sent()))), |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 591 | Some(sent()) |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 592 | ); |
7cf348521204 | atagen | 2026-09-26 12:28 UTC | 593 | assert!(tracker.live.is_empty() && tracker.ended.is_empty()); |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 594 | } |
7c26fad34822 | atagen | 2026-09-26 09:41 UTC | 595 | } |