atagen

macltok

public
Star0
Watch0
cb0668d30c14c14e0b1edddc9133ebf1e98bd5cb
3
ssh://brewery@pls.lobotomise.me:33/atagen/macltok.git

Blame: src/engine/watchers.rs

0bfa3f46f0beatagen2026-09-26 09:12 UTC1
use std::{collections::HashSet, sync::Arc, time::Duration};
31ce2453c82fatagen2026-09-24 13:07 UTC2
31ce2453c82fatagen2026-09-24 13:07 UTC3
use futures_util::StreamExt;
31ce2453c82fatagen2026-09-24 13:07 UTC4
use matrix_sdk::{
f12377bce9b9atagen2026-09-26 12:32 UTC5
    Client, Room,
31ce2453c82fatagen2026-09-24 13:07 UTC6
    ruma::events::{AnyMessageLikeEventContent, room::message::MessageType},
0bfa3f46f0beatagen2026-09-26 09:12 UTC7
    send_queue::{LocalEchoContent, RoomSendQueueUpdate, SendQueueUpdate},
31ce2453c82fatagen2026-09-24 13:07 UTC8
};
31ce2453c82fatagen2026-09-24 13:07 UTC9
use matrix_sdk_ui::sync_service::{State as SyncState, SyncService};
f12377bce9b9atagen2026-09-26 12:32 UTC10
use ruma::{OwnedRoomId, OwnedTransactionId, time::Instant};
fe4133dbcd47atagen2026-09-26 09:05 UTC11
use tokio::sync::{broadcast::error::RecvError, mpsc, watch};
31ce2453c82fatagen2026-09-24 13:07 UTC12
f12377bce9b9atagen2026-09-26 12:32 UTC13
use super::executor::TaskSet;
31ce2453c82fatagen2026-09-24 13:07 UTC14
use crate::{
f12377bce9b9atagen2026-09-26 12:32 UTC15
    ConnectionState, Event, UploadOutcome, encryption::encryption_state, error::classify_sdk_error,
f12377bce9b9atagen2026-09-26 12:32 UTC16
    transport::EventSender,
31ce2453c82fatagen2026-09-24 13:07 UTC17
};
31ce2453c82fatagen2026-09-24 13:07 UTC18
31ce2453c82fatagen2026-09-24 13:07 UTC19
const SYNC_RESTART_MIN: Duration = Duration::from_secs(1);
31ce2453c82fatagen2026-09-24 13:07 UTC20
const SYNC_RESTART_MAX: Duration = Duration::from_secs(60);
31ce2453c82fatagen2026-09-24 13:07 UTC21
const SEND_QUEUE_RETRY: Duration = Duration::from_secs(5);
5a52012c9b1datagen2026-09-24 23:45 UTC22
const SYNC_STABLE: Duration = Duration::from_secs(30);
5a52012c9b1datagen2026-09-24 23:45 UTC23
5a52012c9b1datagen2026-09-24 23:45 UTC24
pub(super) async fn room_subscriptions(
5a52012c9b1datagen2026-09-24 23:45 UTC25
    sync: Arc<SyncService>,
5a52012c9b1datagen2026-09-24 23:45 UTC26
    mut rooms: watch::Receiver<Vec<ruma::OwnedRoomId>>,
5a52012c9b1datagen2026-09-24 23:45 UTC27
) {
5a52012c9b1datagen2026-09-24 23:45 UTC28
    while rooms.changed().await.is_ok() {
5a52012c9b1datagen2026-09-24 23:45 UTC29
        let room_ids = rooms.borrow_and_update().clone();
5a52012c9b1datagen2026-09-24 23:45 UTC30
        let room_ids = room_ids.iter().map(AsRef::as_ref).collect::<Vec<_>>();
5a52012c9b1datagen2026-09-24 23:45 UTC31
        sync.room_list_service().subscribe_to_rooms(&room_ids).await;
5a52012c9b1datagen2026-09-24 23:45 UTC32
    }
5a52012c9b1datagen2026-09-24 23:45 UTC33
}
31ce2453c82fatagen2026-09-24 13:07 UTC34
31ce2453c82fatagen2026-09-24 13:07 UTC35
/// `states` must be taken before sync starts, a late subscriber never hears the transition it missed
31ce2453c82fatagen2026-09-24 13:07 UTC36
pub(super) async fn connection(
31ce2453c82fatagen2026-09-24 13:07 UTC37
    mut states: impl futures_util::Stream<Item = SyncState> + Unpin,
31ce2453c82fatagen2026-09-24 13:07 UTC38
    sync: Arc<SyncService>,
31ce2453c82fatagen2026-09-24 13:07 UTC39
    client: Client,
31ce2453c82fatagen2026-09-24 13:07 UTC40
    events: EventSender,
31ce2453c82fatagen2026-09-24 13:07 UTC41
    connection: watch::Sender<ConnectionState>,
31ce2453c82fatagen2026-09-24 13:07 UTC42
) {
31ce2453c82fatagen2026-09-24 13:07 UTC43
    let mut backoff = SYNC_RESTART_MIN;
5a52012c9b1datagen2026-09-24 23:45 UTC44
    let mut online_since: Option<Instant> = None;
31ce2453c82fatagen2026-09-24 13:07 UTC45
    while let Some(state) = states.next().await {
5a52012c9b1datagen2026-09-24 23:45 UTC46
        // only a run that lasted counts as recovered, or a server that answers /versions
5a52012c9b1datagen2026-09-24 23:45 UTC47
        // but refuses sync would have us bounce between offline and online forever
5a52012c9b1datagen2026-09-24 23:45 UTC48
        let stable = online_since
5a52012c9b1datagen2026-09-24 23:45 UTC49
            .take()
5a52012c9b1datagen2026-09-24 23:45 UTC50
            .map(|since| since.elapsed() >= SYNC_STABLE);
5a52012c9b1datagen2026-09-24 23:45 UTC51
        if stable == Some(true) {
5a52012c9b1datagen2026-09-24 23:45 UTC52
            backoff = SYNC_RESTART_MIN;
5a52012c9b1datagen2026-09-24 23:45 UTC53
        }
31ce2453c82fatagen2026-09-24 13:07 UTC54
        let (state, restart) = match state {
5a52012c9b1datagen2026-09-24 23:45 UTC55
            // only ever us stopping it for a restart, nothing a client needs to hear
5a52012c9b1datagen2026-09-24 23:45 UTC56
            SyncState::Idle => continue,
31ce2453c82fatagen2026-09-24 13:07 UTC57
            SyncState::Running => {
5a52012c9b1datagen2026-09-24 23:45 UTC58
                online_since = Some(Instant::now());
31ce2453c82fatagen2026-09-24 13:07 UTC59
                // the sdk parks a room queue on any send error and never unparks it
31ce2453c82fatagen2026-09-24 13:07 UTC60
                client.send_queue().set_enabled(true).await;
31ce2453c82fatagen2026-09-24 13:07 UTC61
                (ConnectionState::Online, false)
31ce2453c82fatagen2026-09-24 13:07 UTC62
            }
5a52012c9b1datagen2026-09-24 23:45 UTC63
            SyncState::Offline => (ConnectionState::Offline, stable == Some(false)),
31ce2453c82fatagen2026-09-24 13:07 UTC64
            SyncState::Terminated => (ConnectionState::Offline, true),
31ce2453c82fatagen2026-09-24 13:07 UTC65
            SyncState::Error(error) => {
31ce2453c82fatagen2026-09-24 13:07 UTC66
                tracing::warn!(%error, "sync service reported an error");
31ce2453c82fatagen2026-09-24 13:07 UTC67
                (ConnectionState::Offline, true)
31ce2453c82fatagen2026-09-24 13:07 UTC68
            }
31ce2453c82fatagen2026-09-24 13:07 UTC69
        };
31ce2453c82fatagen2026-09-24 13:07 UTC70
        if *connection.borrow() != state {
31ce2453c82fatagen2026-09-24 13:07 UTC71
            connection.send_replace(state);
31ce2453c82fatagen2026-09-24 13:07 UTC72
            if events.send(Event::ConnectionChanged(state)).await.is_err() {
31ce2453c82fatagen2026-09-24 13:07 UTC73
                return;
31ce2453c82fatagen2026-09-24 13:07 UTC74
            }
31ce2453c82fatagen2026-09-24 13:07 UTC75
        }
31ce2453c82fatagen2026-09-24 13:07 UTC76
        if restart {
5a52012c9b1datagen2026-09-24 23:45 UTC77
            sync.stop().await;
1fe480c16a3catagen2026-09-26 08:44 UTC78
            matrix_sdk::sleep::sleep(backoff).await;
31ce2453c82fatagen2026-09-24 13:07 UTC79
            backoff = (backoff * 2).min(SYNC_RESTART_MAX);
31ce2453c82fatagen2026-09-24 13:07 UTC80
            sync.start().await;
31ce2453c82fatagen2026-09-24 13:07 UTC81
        }
31ce2453c82fatagen2026-09-24 13:07 UTC82
    }
31ce2453c82fatagen2026-09-24 13:07 UTC83
}
31ce2453c82fatagen2026-09-24 13:07 UTC84
31ce2453c82fatagen2026-09-24 13:07 UTC85
/// a recoverable failure while we're online is a hiccup, so give the room another go
31ce2453c82fatagen2026-09-24 13:07 UTC86
pub(super) async fn send_queue_errors(
31ce2453c82fatagen2026-09-24 13:07 UTC87
    client: Client,
31ce2453c82fatagen2026-09-24 13:07 UTC88
    mut errors: tokio::sync::broadcast::Receiver<matrix_sdk::send_queue::SendQueueRoomError>,
31ce2453c82fatagen2026-09-24 13:07 UTC89
    online: watch::Receiver<ConnectionState>,
31ce2453c82fatagen2026-09-24 13:07 UTC90
) {
1fe480c16a3catagen2026-09-26 08:44 UTC91
    let mut retries = TaskSet::new();
31ce2453c82fatagen2026-09-24 13:07 UTC92
    loop {
31ce2453c82fatagen2026-09-24 13:07 UTC93
        let error = tokio::select! {
31ce2453c82fatagen2026-09-24 13:07 UTC94
            error = errors.recv() => error,
31ce2453c82fatagen2026-09-24 13:07 UTC95
            Some(_) = retries.join_next(), if !retries.is_empty() => continue,
31ce2453c82fatagen2026-09-24 13:07 UTC96
        };
31ce2453c82fatagen2026-09-24 13:07 UTC97
        match error {
31ce2453c82fatagen2026-09-24 13:07 UTC98
            Ok(error) if error.is_recoverable => {
31ce2453c82fatagen2026-09-24 13:07 UTC99
                let client = client.clone();
31ce2453c82fatagen2026-09-24 13:07 UTC100
                let online = online.clone();
31ce2453c82fatagen2026-09-24 13:07 UTC101
                retries.spawn(async move {
1fe480c16a3catagen2026-09-26 08:44 UTC102
                    matrix_sdk::sleep::sleep(SEND_QUEUE_RETRY).await;
31ce2453c82fatagen2026-09-24 13:07 UTC103
                    if *online.borrow() == ConnectionState::Online
31ce2453c82fatagen2026-09-24 13:07 UTC104
                        && let Some(room) = client.get_room(&error.room_id)
31ce2453c82fatagen2026-09-24 13:07 UTC105
                    {
31ce2453c82fatagen2026-09-24 13:07 UTC106
                        room.send_queue().set_enabled(true);
31ce2453c82fatagen2026-09-24 13:07 UTC107
                    }
31ce2453c82fatagen2026-09-24 13:07 UTC108
                });
31ce2453c82fatagen2026-09-24 13:07 UTC109
            }
31ce2453c82fatagen2026-09-24 13:07 UTC110
            Ok(_) | Err(RecvError::Lagged(_)) => {}
31ce2453c82fatagen2026-09-24 13:07 UTC111
            Err(RecvError::Closed) => return,
31ce2453c82fatagen2026-09-24 13:07 UTC112
        }
31ce2453c82fatagen2026-09-24 13:07 UTC113
    }
31ce2453c82fatagen2026-09-24 13:07 UTC114
}
31ce2453c82fatagen2026-09-24 13:07 UTC115
7cf348521204atagen2026-09-26 12:28 UTC116
/// upload progress only goes out on the room's own channel, never the client-wide one.
7cf348521204atagen2026-09-26 12:28 UTC117
/// the room's sends and cancels come along behind it, so an upload's end can't
7cf348521204atagen2026-09-26 12:28 UTC118
/// overtake its own progress
7c26fad34822atagen2026-09-26 09:41 UTC119
pub(super) async fn room_uploads(
7c26fad34822atagen2026-09-26 09:41 UTC120
    room: Room,
7cf348521204atagen2026-09-26 12:28 UTC121
    tracker: mpsc::Sender<RoomUpload>,
7c26fad34822atagen2026-09-26 09:41 UTC122
    ready: watch::Sender<bool>,
7c26fad34822atagen2026-09-26 09:41 UTC123
) {
31ce2453c82fatagen2026-09-24 13:07 UTC124
    let room_id = room.room_id().to_owned();
31ce2453c82fatagen2026-09-24 13:07 UTC125
    let mut updates = match room.send_queue().subscribe().await {
31ce2453c82fatagen2026-09-24 13:07 UTC126
        Ok((_, updates)) => updates,
31ce2453c82fatagen2026-09-24 13:07 UTC127
        Err(error) => {
31ce2453c82fatagen2026-09-24 13:07 UTC128
            tracing::warn!(%room_id, %error, "reading the media send queue failed");
31ce2453c82fatagen2026-09-24 13:07 UTC129
            return;
31ce2453c82fatagen2026-09-24 13:07 UTC130
        }
31ce2453c82fatagen2026-09-24 13:07 UTC131
    };
31ce2453c82fatagen2026-09-24 13:07 UTC132
    ready.send_replace(true);
31ce2453c82fatagen2026-09-24 13:07 UTC133
    loop {
7cf348521204atagen2026-09-26 12:28 UTC134
        let report = match updates.recv().await {
7cf348521204atagen2026-09-26 12:28 UTC135
            Ok(RoomSendQueueUpdate::MediaUpload {
7cf348521204atagen2026-09-26 12:28 UTC136
                related_to,
7cf348521204atagen2026-09-26 12:28 UTC137
                file,
7cf348521204atagen2026-09-26 12:28 UTC138
                progress,
7cf348521204atagen2026-09-26 12:28 UTC139
                ..
7cf348521204atagen2026-09-26 12:28 UTC140
            }) => RoomUpload::Progress {
7cf348521204atagen2026-09-26 12:28 UTC141
                upload: (room_id.clone(), related_to),
7cf348521204atagen2026-09-26 12:28 UTC142
                uploaded_bytes: progress.current as u64,
7cf348521204atagen2026-09-26 12:28 UTC143
                total_bytes: progress.total as u64,
7cf348521204atagen2026-09-26 12:28 UTC144
                complete: file.is_some() && progress.current == progress.total,
7cf348521204atagen2026-09-26 12:28 UTC145
            },
7cf348521204atagen2026-09-26 12:28 UTC146
            Ok(RoomSendQueueUpdate::SentEvent {
7cf348521204atagen2026-09-26 12:28 UTC147
                transaction_id,
7cf348521204atagen2026-09-26 12:28 UTC148
                event_id,
7cf348521204atagen2026-09-26 12:28 UTC149
            }) => RoomUpload::Ended(
7cf348521204atagen2026-09-26 12:28 UTC150
                (room_id.clone(), transaction_id),
7cf348521204atagen2026-09-26 12:28 UTC151
                UploadOutcome::Sent { event_id },
7cf348521204atagen2026-09-26 12:28 UTC152
            ),
f12377bce9b9atagen2026-09-26 12:32 UTC153
            Ok(RoomSendQueueUpdate::CancelledLocalEvent { transaction_id }) => {
f12377bce9b9atagen2026-09-26 12:32 UTC154
                RoomUpload::Ended((room_id.clone(), transaction_id), UploadOutcome::Cancelled)
f12377bce9b9atagen2026-09-26 12:32 UTC155
            }
7cf348521204atagen2026-09-26 12:28 UTC156
            Ok(_) => continue,
7cf348521204atagen2026-09-26 12:28 UTC157
            Err(RecvError::Lagged(_)) => RoomUpload::Lagged,
5a52012c9b1datagen2026-09-24 23:45 UTC158
            Err(RecvError::Closed) => return,
5a52012c9b1datagen2026-09-24 23:45 UTC159
        };
7cf348521204atagen2026-09-26 12:28 UTC160
        if tracker.send(report).await.is_err() {
5a52012c9b1datagen2026-09-24 23:45 UTC161
            return;
5a52012c9b1datagen2026-09-24 23:45 UTC162
        }
5a52012c9b1datagen2026-09-24 23:45 UTC163
    }
5a52012c9b1datagen2026-09-24 23:45 UTC164
}
5a52012c9b1datagen2026-09-24 23:45 UTC165
7cf348521204atagen2026-09-26 12:28 UTC166
pub(super) enum RoomUpload {
7cf348521204atagen2026-09-26 12:28 UTC167
    Progress {
7cf348521204atagen2026-09-26 12:28 UTC168
        upload: Upload,
7cf348521204atagen2026-09-26 12:28 UTC169
        uploaded_bytes: u64,
7cf348521204atagen2026-09-26 12:28 UTC170
        total_bytes: u64,
7cf348521204atagen2026-09-26 12:28 UTC171
        complete: bool,
7cf348521204atagen2026-09-26 12:28 UTC172
    },
7cf348521204atagen2026-09-26 12:28 UTC173
    Ended(Upload, UploadOutcome),
7cf348521204atagen2026-09-26 12:28 UTC174
    /// an end may have been lost with the ticks
7cf348521204atagen2026-09-26 12:28 UTC175
    Lagged,
7c26fad34822atagen2026-09-26 09:41 UTC176
}
7c26fad34822atagen2026-09-26 09:41 UTC177
7c26fad34822atagen2026-09-26 09:41 UTC178
/// the one place upload events leave from, so an upload is never reported after it
7c26fad34822atagen2026-09-26 09:41 UTC179
/// ended. progress comes in from the room forwarders, the rest from the client-wide
7c26fad34822atagen2026-09-26 09:41 UTC180
/// channel, which exists before any upload does
0bfa3f46f0beatagen2026-09-26 09:12 UTC181
pub(super) async fn uploads(
5a52012c9b1datagen2026-09-24 23:45 UTC182
    client: Client,
0bfa3f46f0beatagen2026-09-26 09:12 UTC183
    mut updates: tokio::sync::broadcast::Receiver<SendQueueUpdate>,
7cf348521204atagen2026-09-26 12:28 UTC184
    mut rooms_reports: mpsc::Receiver<RoomUpload>,
5a52012c9b1datagen2026-09-24 23:45 UTC185
    events: EventSender,
5a52012c9b1datagen2026-09-24 23:45 UTC186
) {
0bfa3f46f0beatagen2026-09-26 09:12 UTC187
    let (_, mut rooms) = client.rooms_stream();
7c26fad34822atagen2026-09-26 09:41 UTC188
    let mut tracker = UploadTracker {
7c26fad34822atagen2026-09-26 09:41 UTC189
        live: attachment_echoes(&client).await,
7c26fad34822atagen2026-09-26 09:41 UTC190
        ended: HashSet::new(),
7c26fad34822atagen2026-09-26 09:41 UTC191
    };
5a52012c9b1datagen2026-09-24 23:45 UTC192
    loop {
7c26fad34822atagen2026-09-26 09:41 UTC193
        let reports = tokio::select! {
0bfa3f46f0beatagen2026-09-26 09:12 UTC194
            update = updates.recv() => match update {
7c26fad34822atagen2026-09-26 09:41 UTC195
                Ok(update) => tracker.update(update).into_iter().collect(),
0bfa3f46f0beatagen2026-09-26 09:12 UTC196
                Err(RecvError::Lagged(skipped)) => {
7cf348521204atagen2026-09-26 12:28 UTC197
                    tracing::warn!(skipped, "upload tracker lagged");
7cf348521204atagen2026-09-26 12:28 UTC198
                    tracker.resnapshot(attachment_echoes(&client).await)
0bfa3f46f0beatagen2026-09-26 09:12 UTC199
                }
0bfa3f46f0beatagen2026-09-26 09:12 UTC200
                Err(RecvError::Closed) => return,
0bfa3f46f0beatagen2026-09-26 09:12 UTC201
            },
7cf348521204atagen2026-09-26 12:28 UTC202
            Some(report) = rooms_reports.recv() => match report {
7cf348521204atagen2026-09-26 12:28 UTC203
                RoomUpload::Lagged => tracker.resnapshot(attachment_echoes(&client).await),
7cf348521204atagen2026-09-26 12:28 UTC204
                report => tracker.room(report).into_iter().collect(),
7cf348521204atagen2026-09-26 12:28 UTC205
            },
0bfa3f46f0beatagen2026-09-26 09:12 UTC206
            update = rooms.next() => {
0bfa3f46f0beatagen2026-09-26 09:12 UTC207
                if update.is_none() {
0bfa3f46f0beatagen2026-09-26 09:12 UTC208
                    return;
0bfa3f46f0beatagen2026-09-26 09:12 UTC209
                }
7c26fad34822atagen2026-09-26 09:41 UTC210
                tracker.finish_where(
7c26fad34822atagen2026-09-26 09:41 UTC211
                    |(room_id, _)| client.get_room(room_id).is_none(),
7c26fad34822atagen2026-09-26 09:41 UTC212
                    &UploadOutcome::RoomForgotten,
7c26fad34822atagen2026-09-26 09:41 UTC213
                )
31ce2453c82fatagen2026-09-24 13:07 UTC214
            }
31ce2453c82fatagen2026-09-24 13:07 UTC215
        };
7c26fad34822atagen2026-09-26 09:41 UTC216
        for event in reports {
0bfa3f46f0beatagen2026-09-26 09:12 UTC217
            if events.send(event).await.is_err() {
0bfa3f46f0beatagen2026-09-26 09:12 UTC218
                return;
0bfa3f46f0beatagen2026-09-26 09:12 UTC219
            }
0bfa3f46f0beatagen2026-09-26 09:12 UTC220
        }
0bfa3f46f0beatagen2026-09-26 09:12 UTC221
    }
0bfa3f46f0beatagen2026-09-26 09:12 UTC222
}
0bfa3f46f0beatagen2026-09-26 09:12 UTC223
0bfa3f46f0beatagen2026-09-26 09:12 UTC224
type Upload = (OwnedRoomId, OwnedTransactionId);
0bfa3f46f0beatagen2026-09-26 09:12 UTC225
7cf348521204atagen2026-09-26 12:28 UTC226
/// everything reported gets exactly one `MediaUploadFinished`. an upload ended from
7cf348521204atagen2026-09-26 12:28 UTC227
/// anywhere but its room's forwarder goes in `ended` until the forwarder's own end for
7cf348521204atagen2026-09-26 12:28 UTC228
/// it arrives, since until then the forwarder can still be holding progress for it
7c26fad34822atagen2026-09-26 09:41 UTC229
struct UploadTracker {
7c26fad34822atagen2026-09-26 09:41 UTC230
    live: HashSet<Upload>,
7c26fad34822atagen2026-09-26 09:41 UTC231
    ended: HashSet<Upload>,
0bfa3f46f0beatagen2026-09-26 09:12 UTC232
}
0bfa3f46f0beatagen2026-09-26 09:12 UTC233
7c26fad34822atagen2026-09-26 09:41 UTC234
impl UploadTracker {
7c26fad34822atagen2026-09-26 09:41 UTC235
    fn update(&mut self, update: SendQueueUpdate) -> Option<Event> {
7c26fad34822atagen2026-09-26 09:41 UTC236
        let room_id = update.room_id;
7c26fad34822atagen2026-09-26 09:41 UTC237
        let (transaction_id, outcome) = match update.update {
7c26fad34822atagen2026-09-26 09:41 UTC238
            RoomSendQueueUpdate::NewLocalEvent(echo) => {
7c26fad34822atagen2026-09-26 09:41 UTC239
                let upload = (room_id, echo.transaction_id);
7c26fad34822atagen2026-09-26 09:41 UTC240
                if is_attachment_echo(&echo.content) && !self.ended.contains(&upload) {
7c26fad34822atagen2026-09-26 09:41 UTC241
                    self.live.insert(upload);
7c26fad34822atagen2026-09-26 09:41 UTC242
                }
0bfa3f46f0beatagen2026-09-26 09:12 UTC243
                return None;
0bfa3f46f0beatagen2026-09-26 09:12 UTC244
            }
7c26fad34822atagen2026-09-26 09:41 UTC245
            RoomSendQueueUpdate::SendError {
0bfa3f46f0beatagen2026-09-26 09:12 UTC246
                transaction_id,
7c26fad34822atagen2026-09-26 09:41 UTC247
                error,
7c26fad34822atagen2026-09-26 09:41 UTC248
                is_recoverable,
7c26fad34822atagen2026-09-26 09:41 UTC249
            } => {
7c26fad34822atagen2026-09-26 09:41 UTC250
                let upload = (room_id, transaction_id);
7c26fad34822atagen2026-09-26 09:41 UTC251
                if !self.live.contains(&upload) {
7c26fad34822atagen2026-09-26 09:41 UTC252
                    return None;
7c26fad34822atagen2026-09-26 09:41 UTC253
                }
7c26fad34822atagen2026-09-26 09:41 UTC254
                let (room_id, transaction_id) = upload;
7c26fad34822atagen2026-09-26 09:41 UTC255
                return Some(Event::MediaUploadFailed {
7c26fad34822atagen2026-09-26 09:41 UTC256
                    room_id,
7c26fad34822atagen2026-09-26 09:41 UTC257
                    transaction_id,
7c26fad34822atagen2026-09-26 09:41 UTC258
                    kind: classify_sdk_error(&error),
7c26fad34822atagen2026-09-26 09:41 UTC259
                    will_retry: is_recoverable,
7c26fad34822atagen2026-09-26 09:41 UTC260
                });
7c26fad34822atagen2026-09-26 09:41 UTC261
            }
7c26fad34822atagen2026-09-26 09:41 UTC262
            RoomSendQueueUpdate::SentEvent {
7c26fad34822atagen2026-09-26 09:41 UTC263
                transaction_id,
7c26fad34822atagen2026-09-26 09:41 UTC264
                event_id,
7c26fad34822atagen2026-09-26 09:41 UTC265
            } => (transaction_id, UploadOutcome::Sent { event_id }),
7c26fad34822atagen2026-09-26 09:41 UTC266
            RoomSendQueueUpdate::CancelledLocalEvent { transaction_id } => {
7c26fad34822atagen2026-09-26 09:41 UTC267
                (transaction_id, UploadOutcome::Cancelled)
7c26fad34822atagen2026-09-26 09:41 UTC268
            }
7c26fad34822atagen2026-09-26 09:41 UTC269
            _ => return None,
7c26fad34822atagen2026-09-26 09:41 UTC270
        };
7cf348521204atagen2026-09-26 12:28 UTC271
        self.finish((room_id, transaction_id), outcome, true)
7c26fad34822atagen2026-09-26 09:41 UTC272
    }
7c26fad34822atagen2026-09-26 09:41 UTC273
7cf348521204atagen2026-09-26 12:28 UTC274
    fn room(&mut self, report: RoomUpload) -> Option<Event> {
7cf348521204atagen2026-09-26 12:28 UTC275
        match report {
7cf348521204atagen2026-09-26 12:28 UTC276
            RoomUpload::Progress {
7cf348521204atagen2026-09-26 12:28 UTC277
                upload,
7cf348521204atagen2026-09-26 12:28 UTC278
                uploaded_bytes,
7cf348521204atagen2026-09-26 12:28 UTC279
                total_bytes,
7cf348521204atagen2026-09-26 12:28 UTC280
                complete,
7cf348521204atagen2026-09-26 12:28 UTC281
            } => {
7cf348521204atagen2026-09-26 12:28 UTC282
                if self.ended.contains(&upload) {
7cf348521204atagen2026-09-26 12:28 UTC283
                    return None;
7cf348521204atagen2026-09-26 12:28 UTC284
                }
7cf348521204atagen2026-09-26 12:28 UTC285
                self.live.insert(upload.clone());
7cf348521204atagen2026-09-26 12:28 UTC286
                let (room_id, transaction_id) = upload;
7cf348521204atagen2026-09-26 12:28 UTC287
                Some(Event::MediaUploadProgress {
7cf348521204atagen2026-09-26 12:28 UTC288
                    room_id,
7cf348521204atagen2026-09-26 12:28 UTC289
                    transaction_id,
7cf348521204atagen2026-09-26 12:28 UTC290
                    uploaded_bytes,
7cf348521204atagen2026-09-26 12:28 UTC291
                    total_bytes,
7cf348521204atagen2026-09-26 12:28 UTC292
                    complete,
7cf348521204atagen2026-09-26 12:28 UTC293
                })
7cf348521204atagen2026-09-26 12:28 UTC294
            }
7cf348521204atagen2026-09-26 12:28 UTC295
            RoomUpload::Ended(upload, outcome) => {
7cf348521204atagen2026-09-26 12:28 UTC296
                self.ended.remove(&upload);
7cf348521204atagen2026-09-26 12:28 UTC297
                self.finish(upload, outcome, false)
7cf348521204atagen2026-09-26 12:28 UTC298
            }
7cf348521204atagen2026-09-26 12:28 UTC299
            RoomUpload::Lagged => None,
5a52012c9b1datagen2026-09-24 23:45 UTC300
        }
7c26fad34822atagen2026-09-26 09:41 UTC301
    }
7c26fad34822atagen2026-09-26 09:41 UTC302
7c26fad34822atagen2026-09-26 09:41 UTC303
    fn resnapshot(&mut self, current: HashSet<Upload>) -> Vec<Event> {
7c26fad34822atagen2026-09-26 09:41 UTC304
        let reports = self.finish_where(|upload| !current.contains(upload), &UploadOutcome::Missed);
7c26fad34822atagen2026-09-26 09:41 UTC305
        let ended = &self.ended;
7c26fad34822atagen2026-09-26 09:41 UTC306
        self.live
7c26fad34822atagen2026-09-26 09:41 UTC307
            .extend(current.into_iter().filter(|upload| !ended.contains(upload)));
7c26fad34822atagen2026-09-26 09:41 UTC308
        reports
7c26fad34822atagen2026-09-26 09:41 UTC309
    }
7c26fad34822atagen2026-09-26 09:41 UTC310
7c26fad34822atagen2026-09-26 09:41 UTC311
    fn finish_where(
7c26fad34822atagen2026-09-26 09:41 UTC312
        &mut self,
7c26fad34822atagen2026-09-26 09:41 UTC313
        ends: impl Fn(&Upload) -> bool,
7c26fad34822atagen2026-09-26 09:41 UTC314
        outcome: &UploadOutcome,
7c26fad34822atagen2026-09-26 09:41 UTC315
    ) -> Vec<Event> {
7c26fad34822atagen2026-09-26 09:41 UTC316
        let ending = self
7c26fad34822atagen2026-09-26 09:41 UTC317
            .live
7c26fad34822atagen2026-09-26 09:41 UTC318
            .iter()
7c26fad34822atagen2026-09-26 09:41 UTC319
            .filter(|upload| ends(upload))
7c26fad34822atagen2026-09-26 09:41 UTC320
            .cloned()
7c26fad34822atagen2026-09-26 09:41 UTC321
            .collect::<Vec<_>>();
7c26fad34822atagen2026-09-26 09:41 UTC322
        ending
7c26fad34822atagen2026-09-26 09:41 UTC323
            .into_iter()
7cf348521204atagen2026-09-26 12:28 UTC324
            .filter_map(|upload| self.finish(upload, outcome.clone(), true))
7c26fad34822atagen2026-09-26 09:41 UTC325
            .collect()
7c26fad34822atagen2026-09-26 09:41 UTC326
    }
7c26fad34822atagen2026-09-26 09:41 UTC327
7cf348521204atagen2026-09-26 12:28 UTC328
    fn finish(&mut self, upload: Upload, outcome: UploadOutcome, remember: bool) -> Option<Event> {
7c26fad34822atagen2026-09-26 09:41 UTC329
        if !self.live.remove(&upload) {
7c26fad34822atagen2026-09-26 09:41 UTC330
            return None;
31ce2453c82fatagen2026-09-24 13:07 UTC331
        }
7cf348521204atagen2026-09-26 12:28 UTC332
        if remember {
7cf348521204atagen2026-09-26 12:28 UTC333
            self.ended.insert(upload.clone());
7cf348521204atagen2026-09-26 12:28 UTC334
        }
7c26fad34822atagen2026-09-26 09:41 UTC335
        let (room_id, transaction_id) = upload;
7c26fad34822atagen2026-09-26 09:41 UTC336
        Some(Event::MediaUploadFinished {
7c26fad34822atagen2026-09-26 09:41 UTC337
            room_id,
7c26fad34822atagen2026-09-26 09:41 UTC338
            transaction_id,
7c26fad34822atagen2026-09-26 09:41 UTC339
            outcome,
7c26fad34822atagen2026-09-26 09:41 UTC340
        })
31ce2453c82fatagen2026-09-24 13:07 UTC341
    }
0bfa3f46f0beatagen2026-09-26 09:12 UTC342
}
0bfa3f46f0beatagen2026-09-26 09:12 UTC343
7c26fad34822atagen2026-09-26 09:41 UTC344
async fn attachment_echoes(client: &Client) -> HashSet<Upload> {
7c26fad34822atagen2026-09-26 09:41 UTC345
    let Ok(echoes) = client.send_queue().local_echoes().await else {
7c26fad34822atagen2026-09-26 09:41 UTC346
        return HashSet::new();
7c26fad34822atagen2026-09-26 09:41 UTC347
    };
7c26fad34822atagen2026-09-26 09:41 UTC348
    echoes
0bfa3f46f0beatagen2026-09-26 09:12 UTC349
        .into_iter()
7c26fad34822atagen2026-09-26 09:41 UTC350
        .flat_map(|(room_id, echoes)| {
7c26fad34822atagen2026-09-26 09:41 UTC351
            echoes
7c26fad34822atagen2026-09-26 09:41 UTC352
                .into_iter()
7c26fad34822atagen2026-09-26 09:41 UTC353
                .filter(|echo| is_attachment_echo(&echo.content))
7c26fad34822atagen2026-09-26 09:41 UTC354
                .map(move |echo| (room_id.clone(), echo.transaction_id))
0bfa3f46f0beatagen2026-09-26 09:12 UTC355
        })
0bfa3f46f0beatagen2026-09-26 09:12 UTC356
        .collect()
31ce2453c82fatagen2026-09-24 13:07 UTC357
}
31ce2453c82fatagen2026-09-24 13:07 UTC358
31ce2453c82fatagen2026-09-24 13:07 UTC359
fn is_attachment_echo(content: &LocalEchoContent) -> bool {
31ce2453c82fatagen2026-09-24 13:07 UTC360
    let LocalEchoContent::Event {
31ce2453c82fatagen2026-09-24 13:07 UTC361
        serialized_event, ..
31ce2453c82fatagen2026-09-24 13:07 UTC362
    } = content
31ce2453c82fatagen2026-09-24 13:07 UTC363
    else {
31ce2453c82fatagen2026-09-24 13:07 UTC364
        return false;
31ce2453c82fatagen2026-09-24 13:07 UTC365
    };
31ce2453c82fatagen2026-09-24 13:07 UTC366
    matches!(
31ce2453c82fatagen2026-09-24 13:07 UTC367
        serialized_event.deserialize(),
31ce2453c82fatagen2026-09-24 13:07 UTC368
        Ok(AnyMessageLikeEventContent::RoomMessage(content))
31ce2453c82fatagen2026-09-24 13:07 UTC369
            if matches!(
31ce2453c82fatagen2026-09-24 13:07 UTC370
                content.msgtype,
31ce2453c82fatagen2026-09-24 13:07 UTC371
                MessageType::Audio(_) | MessageType::File(_) | MessageType::Image(_) | MessageType::Video(_)
31ce2453c82fatagen2026-09-24 13:07 UTC372
            )
31ce2453c82fatagen2026-09-24 13:07 UTC373
    )
31ce2453c82fatagen2026-09-24 13:07 UTC374
}
31ce2453c82fatagen2026-09-24 13:07 UTC375
31ce2453c82fatagen2026-09-24 13:07 UTC376
pub(super) async fn encryption(client: Client, events: EventSender) {
31ce2453c82fatagen2026-09-24 13:07 UTC377
    let Some(device_id) = client.device_id().map(ToOwned::to_owned) else {
31ce2453c82fatagen2026-09-24 13:07 UTC378
        tracing::warn!("encryption watcher stopped because the client has no device id");
31ce2453c82fatagen2026-09-24 13:07 UTC379
        return;
31ce2453c82fatagen2026-09-24 13:07 UTC380
    };
31ce2453c82fatagen2026-09-24 13:07 UTC381
    let recovery = client.encryption().recovery();
31ce2453c82fatagen2026-09-24 13:07 UTC382
    let backups = client.encryption().backups();
31ce2453c82fatagen2026-09-24 13:07 UTC383
    let mut recovery_stream = recovery.state_stream();
31ce2453c82fatagen2026-09-24 13:07 UTC384
    let mut backup_stream = backups.state_stream();
31ce2453c82fatagen2026-09-24 13:07 UTC385
    let mut verification_stream = client.encryption().verification_state();
31ce2453c82fatagen2026-09-24 13:07 UTC386
    let mut recovery_state = recovery.state();
31ce2453c82fatagen2026-09-24 13:07 UTC387
    let mut backup_state = backups.state();
31ce2453c82fatagen2026-09-24 13:07 UTC388
    let mut verification_state = verification_stream.get();
31ce2453c82fatagen2026-09-24 13:07 UTC389
    loop {
31ce2453c82fatagen2026-09-24 13:07 UTC390
        let state = encryption_state(
31ce2453c82fatagen2026-09-24 13:07 UTC391
            device_id.clone(),
31ce2453c82fatagen2026-09-24 13:07 UTC392
            recovery_state,
31ce2453c82fatagen2026-09-24 13:07 UTC393
            verification_state,
31ce2453c82fatagen2026-09-24 13:07 UTC394
            backup_state,
31ce2453c82fatagen2026-09-24 13:07 UTC395
        );
31ce2453c82fatagen2026-09-24 13:07 UTC396
        if events.send(Event::EncryptionChanged(state)).await.is_err() {
31ce2453c82fatagen2026-09-24 13:07 UTC397
            return;
31ce2453c82fatagen2026-09-24 13:07 UTC398
        }
31ce2453c82fatagen2026-09-24 13:07 UTC399
        tokio::select! {
31ce2453c82fatagen2026-09-24 13:07 UTC400
            Some(update) = recovery_stream.next() => recovery_state = update,
31ce2453c82fatagen2026-09-24 13:07 UTC401
            Some(update) = verification_stream.next() => verification_state = update,
31ce2453c82fatagen2026-09-24 13:07 UTC402
            Some(update) = backup_stream.next() => {
31ce2453c82fatagen2026-09-24 13:07 UTC403
                backup_state = update.unwrap_or_else(|_| backups.state());
31ce2453c82fatagen2026-09-24 13:07 UTC404
            }
31ce2453c82fatagen2026-09-24 13:07 UTC405
            else => return,
31ce2453c82fatagen2026-09-24 13:07 UTC406
        }
31ce2453c82fatagen2026-09-24 13:07 UTC407
    }
31ce2453c82fatagen2026-09-24 13:07 UTC408
}
31ce2453c82fatagen2026-09-24 13:07 UTC409
31ce2453c82fatagen2026-09-24 13:07 UTC410
pub(super) async fn rooms(client: Client, events: EventSender) {
31ce2453c82fatagen2026-09-24 13:07 UTC411
    let (_, mut rooms) = client.rooms_stream();
31ce2453c82fatagen2026-09-24 13:07 UTC412
    let mut info = client.room_info_notable_update_receiver();
31ce2453c82fatagen2026-09-24 13:07 UTC413
    loop {
31ce2453c82fatagen2026-09-24 13:07 UTC414
        if events.send(Event::RoomsChanged).await.is_err() {
31ce2453c82fatagen2026-09-24 13:07 UTC415
            return;
31ce2453c82fatagen2026-09-24 13:07 UTC416
        }
31ce2453c82fatagen2026-09-24 13:07 UTC417
        tokio::select! {
31ce2453c82fatagen2026-09-24 13:07 UTC418
            update = rooms.next() => if update.is_none() { return },
31ce2453c82fatagen2026-09-24 13:07 UTC419
            update = info.recv() => if matches!(update, Err(RecvError::Closed)) { return },
31ce2453c82fatagen2026-09-24 13:07 UTC420
        }
31ce2453c82fatagen2026-09-24 13:07 UTC421
        // a sync touching fifty rooms is one invalidation, not fifty
31ce2453c82fatagen2026-09-24 13:07 UTC422
        while let Ok(_) | Err(tokio::sync::broadcast::error::TryRecvError::Lagged(_)) =
31ce2453c82fatagen2026-09-24 13:07 UTC423
            info.try_recv()
31ce2453c82fatagen2026-09-24 13:07 UTC424
        {}
31ce2453c82fatagen2026-09-24 13:07 UTC425
    }
31ce2453c82fatagen2026-09-24 13:07 UTC426
}
31ce2453c82fatagen2026-09-24 13:07 UTC427
31ce2453c82fatagen2026-09-24 13:07 UTC428
pub(super) async fn notification_settings(client: Client, events: EventSender) {
31ce2453c82fatagen2026-09-24 13:07 UTC429
    let settings = client.notification_settings().await;
31ce2453c82fatagen2026-09-24 13:07 UTC430
    let mut changes = settings.subscribe_to_changes();
31ce2453c82fatagen2026-09-24 13:07 UTC431
    while let Ok(()) | Err(RecvError::Lagged(_)) = changes.recv().await {
0bfa3f46f0beatagen2026-09-26 09:12 UTC432
        // room summaries carry the effective mode, and push rule changes never touch room info
0bfa3f46f0beatagen2026-09-26 09:12 UTC433
        for event in [Event::NotificationSettingsChanged, Event::RoomsChanged] {
0bfa3f46f0beatagen2026-09-26 09:12 UTC434
            if events.send(event).await.is_err() {
0bfa3f46f0beatagen2026-09-26 09:12 UTC435
                return;
0bfa3f46f0beatagen2026-09-26 09:12 UTC436
            }
31ce2453c82fatagen2026-09-24 13:07 UTC437
        }
31ce2453c82fatagen2026-09-24 13:07 UTC438
    }
31ce2453c82fatagen2026-09-24 13:07 UTC439
}
7c26fad34822atagen2026-09-26 09:41 UTC440
7c26fad34822atagen2026-09-26 09:41 UTC441
#[cfg(test)]
7c26fad34822atagen2026-09-26 09:41 UTC442
mod tests {
7c26fad34822atagen2026-09-26 09:41 UTC443
    use matrix_sdk::send_queue::{RoomSendQueueUpdate, SendQueueUpdate};
7c26fad34822atagen2026-09-26 09:41 UTC444
    use ruma::{OwnedTransactionId, event_id, owned_room_id};
7c26fad34822atagen2026-09-26 09:41 UTC445
7cf348521204atagen2026-09-26 12:28 UTC446
    use super::{HashSet, RoomUpload, Upload, UploadTracker};
7c26fad34822atagen2026-09-26 09:41 UTC447
    use crate::{Event, UploadOutcome};
7c26fad34822atagen2026-09-26 09:41 UTC448
7cf348521204atagen2026-09-26 12:28 UTC449
    fn upload() -> Upload {
7cf348521204atagen2026-09-26 12:28 UTC450
        (
7c26fad34822atagen2026-09-26 09:41 UTC451
            owned_room_id!("!room:localhost"),
7c26fad34822atagen2026-09-26 09:41 UTC452
            OwnedTransactionId::from("upload"),
7cf348521204atagen2026-09-26 12:28 UTC453
        )
7cf348521204atagen2026-09-26 12:28 UTC454
    }
7cf348521204atagen2026-09-26 12:28 UTC455
7cf348521204atagen2026-09-26 12:28 UTC456
    fn tick() -> RoomUpload {
7cf348521204atagen2026-09-26 12:28 UTC457
        RoomUpload::Progress {
7cf348521204atagen2026-09-26 12:28 UTC458
            upload: upload(),
7c26fad34822atagen2026-09-26 09:41 UTC459
            uploaded_bytes: 1,
7c26fad34822atagen2026-09-26 09:41 UTC460
            total_bytes: 2,
7c26fad34822atagen2026-09-26 09:41 UTC461
            complete: false,
7cf348521204atagen2026-09-26 12:28 UTC462
        }
7cf348521204atagen2026-09-26 12:28 UTC463
    }
7cf348521204atagen2026-09-26 12:28 UTC464
7cf348521204atagen2026-09-26 12:28 UTC465
    fn sent() -> UploadOutcome {
7cf348521204atagen2026-09-26 12:28 UTC466
        UploadOutcome::Sent {
7cf348521204atagen2026-09-26 12:28 UTC467
            event_id: event_id!("$sent:localhost").to_owned(),
7cf348521204atagen2026-09-26 12:28 UTC468
        }
7cf348521204atagen2026-09-26 12:28 UTC469
    }
7cf348521204atagen2026-09-26 12:28 UTC470
7cf348521204atagen2026-09-26 12:28 UTC471
    fn tracker() -> UploadTracker {
7cf348521204atagen2026-09-26 12:28 UTC472
        UploadTracker {
7c26fad34822atagen2026-09-26 09:41 UTC473
            live: HashSet::new(),
7c26fad34822atagen2026-09-26 09:41 UTC474
            ended: HashSet::new(),
7cf348521204atagen2026-09-26 12:28 UTC475
        }
7cf348521204atagen2026-09-26 12:28 UTC476
    }
7cf348521204atagen2026-09-26 12:28 UTC477
7cf348521204atagen2026-09-26 12:28 UTC478
    fn finished(event: Option<Event>) -> Option<UploadOutcome> {
7cf348521204atagen2026-09-26 12:28 UTC479
        match event? {
7cf348521204atagen2026-09-26 12:28 UTC480
            Event::MediaUploadFinished { outcome, .. } => Some(outcome),
7cf348521204atagen2026-09-26 12:28 UTC481
            _ => None,
7cf348521204atagen2026-09-26 12:28 UTC482
        }
7cf348521204atagen2026-09-26 12:28 UTC483
    }
7c26fad34822atagen2026-09-26 09:41 UTC484
7cf348521204atagen2026-09-26 12:28 UTC485
    #[test]
7cf348521204atagen2026-09-26 12:28 UTC486
    fn a_stale_tick_after_a_missed_end_changes_nothing() {
7cf348521204atagen2026-09-26 12:28 UTC487
        let mut tracker = tracker();
7cf348521204atagen2026-09-26 12:28 UTC488
        assert!(tracker.room(tick()).is_some());
7c26fad34822atagen2026-09-26 09:41 UTC489
        let missed = tracker.resnapshot(HashSet::new());
7c26fad34822atagen2026-09-26 09:41 UTC490
        assert!(matches!(
7c26fad34822atagen2026-09-26 09:41 UTC491
            missed.as_slice(),
7c26fad34822atagen2026-09-26 09:41 UTC492
            [Event::MediaUploadFinished {
7c26fad34822atagen2026-09-26 09:41 UTC493
                outcome: UploadOutcome::Missed,
7c26fad34822atagen2026-09-26 09:41 UTC494
                ..
7c26fad34822atagen2026-09-26 09:41 UTC495
            }]
7c26fad34822atagen2026-09-26 09:41 UTC496
        ));
7c26fad34822atagen2026-09-26 09:41 UTC497
7cf348521204atagen2026-09-26 12:28 UTC498
        assert!(tracker.room(tick()).is_none());
7cf348521204atagen2026-09-26 12:28 UTC499
        let (room_id, transaction_id) = upload();
7cf348521204atagen2026-09-26 12:28 UTC500
        let global = SendQueueUpdate {
7cf348521204atagen2026-09-26 12:28 UTC501
            room_id,
7cf348521204atagen2026-09-26 12:28 UTC502
            update: RoomSendQueueUpdate::SentEvent {
7cf348521204atagen2026-09-26 12:28 UTC503
                transaction_id,
7cf348521204atagen2026-09-26 12:28 UTC504
                event_id: event_id!("$sent:localhost").to_owned(),
7cf348521204atagen2026-09-26 12:28 UTC505
            },
7cf348521204atagen2026-09-26 12:28 UTC506
        };
7cf348521204atagen2026-09-26 12:28 UTC507
        assert!(tracker.update(global).is_none());
7cf348521204atagen2026-09-26 12:28 UTC508
        assert!(tracker.room(RoomUpload::Ended(upload(), sent())).is_none());
7cf348521204atagen2026-09-26 12:28 UTC509
        assert!(tracker.live.is_empty() && tracker.ended.is_empty());
7cf348521204atagen2026-09-26 12:28 UTC510
    }
7cf348521204atagen2026-09-26 12:28 UTC511
7cf348521204atagen2026-09-26 12:28 UTC512
    #[test]
7cf348521204atagen2026-09-26 12:28 UTC513
    fn an_end_the_tracker_never_knew_about_still_follows_its_progress() {
7cf348521204atagen2026-09-26 12:28 UTC514
        let mut tracker = tracker();
7cf348521204atagen2026-09-26 12:28 UTC515
        let (room_id, transaction_id) = upload();
7cf348521204atagen2026-09-26 12:28 UTC516
        let global = SendQueueUpdate {
7cf348521204atagen2026-09-26 12:28 UTC517
            room_id,
7c26fad34822atagen2026-09-26 09:41 UTC518
            update: RoomSendQueueUpdate::SentEvent {
7cf348521204atagen2026-09-26 12:28 UTC519
                transaction_id,
7c26fad34822atagen2026-09-26 09:41 UTC520
                event_id: event_id!("$sent:localhost").to_owned(),
7c26fad34822atagen2026-09-26 09:41 UTC521
            },
7c26fad34822atagen2026-09-26 09:41 UTC522
        };
7cf348521204atagen2026-09-26 12:28 UTC523
        assert!(tracker.update(global).is_none());
7cf348521204atagen2026-09-26 12:28 UTC524
        assert!(tracker.room(tick()).is_some());
7cf348521204atagen2026-09-26 12:28 UTC525
        assert_eq!(
7cf348521204atagen2026-09-26 12:28 UTC526
            finished(tracker.room(RoomUpload::Ended(upload(), sent()))),
7cf348521204atagen2026-09-26 12:28 UTC527
            Some(sent())
7cf348521204atagen2026-09-26 12:28 UTC528
        );
7cf348521204atagen2026-09-26 12:28 UTC529
        assert!(tracker.live.is_empty() && tracker.ended.is_empty());
7c26fad34822atagen2026-09-26 09:41 UTC530
    }
7c26fad34822atagen2026-09-26 09:41 UTC531
}