atagen

macltok

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